1 /* _NVRM_COPYRIGHT_BEGIN_
3 * Copyright 2001-2002 by NVIDIA Corporation. All rights reserved. All
4 * information contained herein is proprietary and confidential to NVIDIA
5 * Corporation. Any use, reproduction, or disclosure without the written
6 * permission of NVIDIA Corporation is prohibited.
12 #include "os-interface.h"
14 #include "nv-freebsd.h"
18 * The NVIDIA kernel module's malloc identifier, needed for both tracking
19 * and actual allocation/freeing purposes. M_NVIDIA is declared elsewhere
20 * to make it known to other parts of the kernel module (nv-freebsd.h).
23 MALLOC_DEFINE(M_NVIDIA, "nvidia", "NVIDIA memory allocations");
26 #define MAX_ERROR_STRING 256
28 RM_STATUS NV_API_CALL os_alloc_contig_pages(
33 *address = contigmalloc(size, M_NVIDIA, 0, 0, ~0, PAGE_SIZE, 0);
34 return *address ? RM_OK : RM_ERROR;
37 void NV_API_CALL os_free_contig_pages(
42 contigfree(address, size, M_NVIDIA);
46 * The core resource manager's favorite source of memory, this routine is
47 * called from different contexts, including ISRs. This means that it can
48 * not be allowed to sleep when memory is low.
51 RM_STATUS NV_API_CALL os_alloc_mem(
56 /* XXX Fix me? (malloc flags) */
57 *address = malloc(size, M_NVIDIA, M_NOWAIT | M_ZERO);
58 return *address ? RM_OK : RM_ERROR;
61 void NV_API_CALL os_free_mem(void *address)
63 free(address, M_NVIDIA);
66 #define NV_MSECS_PER_TICK (1000 / hz)
67 #define NV_MSECS_TO_TICKS(ms) ((ms) * hz / 1000)
68 #define NV_USECS_PER_TICK (1000000 / hz)
69 #define NV_USECS_TO_TICKS(us) ((us) * hz / 1000000)
71 RM_STATUS NV_API_CALL os_delay(U032 MilliSeconds)
73 unsigned long MicroSeconds;
75 struct timeval tv_end, tv_aux;
77 getmicrotime(&tv_aux);
79 if (__NV_ITHREAD() && (MilliSeconds > NV_MAX_ISR_DELAY_MS))
83 DELAY(MilliSeconds * 1000);
87 MicroSeconds = MilliSeconds * 1000;
88 tv_end.tv_usec = MicroSeconds;
90 /* tv_end = tv_aux + tv_end */
91 NV_TIMERADD(&tv_aux, &tv_end, &tv_end);
93 ticks = NV_USECS_TO_TICKS(MicroSeconds);
97 tsleep((void *)os_delay, PUSER | PCATCH, "delay", ticks);
98 getmicrotime(&tv_aux);
99 if (NV_TIMERCMP(&tv_aux, &tv_end, <)) {
100 /* tv_aux = tv_end - tv_aux */
101 NV_TIMERSUB(&tv_end, &tv_aux, &tv_aux);
102 MicroSeconds = tv_aux.tv_usec + (tv_aux.tv_sec * 1000000);
105 } while ((ticks = NV_USECS_TO_TICKS(MicroSeconds)) > 0);
108 if (MicroSeconds > 0)
114 RM_STATUS NV_API_CALL os_delay_us(U032 MicroSeconds)
116 if (__NV_ITHREAD() && (MicroSeconds > NV_MAX_ISR_DELAY_US))
122 U032 NV_API_CALL os_get_cpu_frequency(void)
124 return ((tsc_freq + 4999) / 1000000);
127 U032 NV_API_CALL os_get_current_process(void)
129 return curproc->p_pid;
132 RM_STATUS NV_API_CALL os_get_current_thread(NvU64 *threadId)
137 *threadId = (NvU64) CURTHREAD->td_tid;
142 RM_STATUS NV_API_CALL os_get_current_time(
157 BOOL NV_API_CALL os_is_administrator(PHWINFO pDev)
159 return suser(CURTHREAD) ? FALSE : TRUE;
162 U008 NV_API_CALL os_io_read_byte(
166 /* XXX Fix me? (bus_space access) */
170 void NV_API_CALL os_io_write_byte(
175 /* XXX Fix me? (bus_space access) */
176 outb(address, value);
179 U016 NV_API_CALL os_io_read_word(
183 /* XXX Fix me? (bus_space access) */
187 void NV_API_CALL os_io_write_word(
192 /* XXX Fix me? (bus_space access) */
193 return outw(address, value);
196 U032 NV_API_CALL os_io_read_dword(
200 /* XXX Fix me? (bus_space access) */
204 void NV_API_CALL os_io_write_dword(
209 /* XXX Fix me? (bus_space access) */
210 outl(address, value);
213 void* NV_API_CALL os_map_kernel_space(
221 #if defined(NVCPU_X86) && !defined(PAE)
222 if (start > 0xffffffff)
226 if (start & PAGE_MASK)
229 size = NV_ALIGN_UP(size, PAGE_SIZE);
232 case NV_MEMORY_CACHED:
233 map_mode = PAT_WRITE_BACK;
235 case NV_MEMORY_WRITECOMBINED:
236 map_mode = PAT_WRITE_COMBINING;
238 case NV_MEMORY_UNCACHED:
239 case NV_MEMORY_DEFAULT:
240 map_mode = PAT_UNCACHEABLE;
243 nv_printf(NV_DBG_ERRORS,
244 "NVRM: unknown mode in os_map_kernel_space()\n");
248 return pmap_mapdev_attr(start, size, map_mode);
251 void NV_API_CALL os_unmap_kernel_space(
256 pmap_unmapdev_attr((vm_offset_t)address, size);
259 RM_STATUS NV_API_CALL os_set_mem_range(
266 struct mem_range_desc mrd;
270 mrd.mr_flags = MDF_WRITECOMBINE;
272 strcpy(mrd.mr_owner, "NVIDIA");
273 arg = MEMRANGE_SET_UPDATE;
275 if (mem_range_attr_set(&mrd, &arg))
281 RM_STATUS NV_API_CALL os_unset_mem_range(
287 struct mem_range_desc mrd;
291 arg = MEMRANGE_SET_REMOVE;
293 if (mem_range_attr_set(&mrd, &arg))
301 * The current debug level is used to determine if certain debug messages
302 * are printed to the system console/log files or not. It defaults to the
303 * highest debug level, i.e. the lowest debug output.
306 U032 cur_debuglevel = 0xffffffff;
308 void NV_API_CALL os_dbg_init(void)
313 NV_UMA_ZONE_ALLOC_STACK(sp);
317 if (rm_read_registry_dword(sp, NULL, "NVreg", "ResmanDebugLevel",
318 &new_debuglevel) == RM_OK) {
319 if (new_debuglevel != 0xffffffff)
320 cur_debuglevel = new_debuglevel;
323 NV_UMA_ZONE_FREE_STACK(sp);
326 RM_STATUS NV_API_CALL os_schedule(void)
328 return RM_ERR_NOT_SUPPORTED;
331 void NV_API_CALL os_dbg_set_level(U032 new_debuglevel)
333 cur_debuglevel = new_debuglevel;
336 void NV_API_CALL os_dbg_breakpoint(void)
339 /* FreeBSD 5.x kdb */
340 kdb_enter("os_dbg_breakpoint()");
344 void NV_API_CALL out_string(const char *message)
346 #if defined(DEBUG) || defined(QA_BUILD)
347 printf("%s", message);
351 static char nv_error_string[MAX_ERROR_STRING];
353 int NV_API_CALL nv_printf(
359 char *message = nv_error_string;
361 int chars_written = 0;
363 if (debuglevel >= ((cur_debuglevel >> 4) & 3)) {
364 va_start(arglist, format);
365 chars_written = vsprintf(message, format, arglist);
367 printf("%s", message);
370 return chars_written;
373 int NV_API_CALL nv_snprintf(
383 va_start(arglist, fmt);
384 chars_written = vsnprintf(buf, size, fmt, arglist);
387 return chars_written;
390 void NV_API_CALL nv_os_log(
397 sprintf(nv_error_string, "NVRM: ");
398 l = strlen(nv_error_string);
399 vsnprintf(nv_error_string + l, MAX_ERROR_STRING - l, fmt, ap);
400 printf("%s", nv_error_string);
403 S032 NV_API_CALL os_mem_cmp(
409 return memcmp(buf0, buf1, length);
412 U008* NV_API_CALL os_mem_copy(
418 #if defined(NVCPU_X86_64)
420 for (i = 0; i < length; i++) dst[i] = src[i];
423 return memcpy(dst, src, length);
427 RM_STATUS NV_API_CALL os_memcpy_from_user(
433 if (src < (void *) VM_MAXUSER_ADDRESS)
434 return copyin(src, dst, length) ? RM_ERR_INVALID_POINTER : RM_OK;
436 return os_mem_copy(dst, src, length) ? RM_ERR_INVALID_POINTER : RM_OK;
439 RM_STATUS NV_API_CALL os_memcpy_to_user(
445 if (dst < (void *) VM_MAXUSER_ADDRESS)
446 return copyout(src, dst, length) ? RM_ERR_INVALID_POINTER : RM_OK;
448 return os_mem_copy(dst, src, length) ? RM_ERR_INVALID_POINTER : RM_OK;
451 void* NV_API_CALL os_mem_set(
457 return memset(b, c, length);
460 S032 NV_API_CALL os_string_compare(
465 return strcmp(s1, s2);
468 char* NV_API_CALL os_string_copy(
473 return strcpy(dst, src);
476 U032 NV_API_CALL os_string_length(const char* s)
481 RM_STATUS NV_API_CALL os_strncpy_from_user(
487 return copyinstr(src, dst, n, NULL) ? RM_ERR_INVALID_POINTER : RM_OK;
490 U032 NV_API_CALL os_get_page_size(void)
495 NvU64 NV_API_CALL os_get_page_mask(void)
498 * On FreeBSD, PAGE_MASK means (PAGE_SIZE - 1); on Linux it means the
499 * opposite, ~(PAGE_SIZE - 1); that is what this function is expected
505 NvU64 NV_API_CALL os_get_system_memory_size(void)
507 return ((NvU64)physmem * PAGE_SIZE) / RM_PAGE_SIZE;
510 U032 NV_API_CALL os_get_cpu_count(void)
515 RM_STATUS NV_API_CALL os_flush_cpu_cache(void)
518 * XXX This will do for now, but this may need to be extended
519 * to make IPI calls (flushing all caches).
521 __asm__ __volatile__("wbinvd": : :"memory");
525 void NV_API_CALL os_flush_cpu_write_combine_buffer(void)
527 __asm__ __volatile__("sfence": : :"memory");
530 RM_STATUS NV_API_CALL os_raise_smp_barrier(void)
532 return RM_ERR_NOT_SUPPORTED;
535 RM_STATUS NV_API_CALL os_clear_smp_barrier(void)
537 return RM_ERR_NOT_SUPPORTED;
542 struct mtx mutex_mtx;
547 RM_STATUS NV_API_CALL os_alloc_sema(void **semaphore)
550 struct os_mutex *mtx;
553 NV_UMA_ZONE_ALLOC_STACK(sp);
555 return RM_ERR_NO_FREE_MEM;
557 status = os_alloc_mem((void **)&mtx, sizeof(struct os_mutex));
558 if (status != RM_OK) {
559 NV_UMA_ZONE_FREE_STACK(sp);
563 mtx_init(&mtx->mutex_mtx, "os.mutex_mtx", NULL, MTX_DEF | MTX_RECURSE);
564 cv_init(&mtx->mutex_cv, "os.mutex_cv");
569 *semaphore = (void *) mtx;
574 RM_STATUS NV_API_CALL os_free_sema(void *semaphore)
576 struct os_mutex *mtx = semaphore;
580 NV_UMA_ZONE_FREE_STACK(sp);
582 mtx_destroy(&mtx->mutex_mtx);
583 cv_destroy(&mtx->mutex_cv);
585 os_free_mem(semaphore);
590 RM_STATUS NV_API_CALL os_acquire_sema(void *semaphore)
592 struct os_mutex *mtx = semaphore;
594 mtx_lock(&mtx->mutex_mtx);
596 rm_disable_interrupts(mtx->sp);
599 cv_wait(&mtx->mutex_cv, &mtx->mutex_mtx);
600 mtx_unlock(&mtx->mutex_mtx);
605 RM_STATUS NV_API_CALL os_cond_acquire_sema(void *semaphore)
607 struct os_mutex *mtx = semaphore;
609 mtx_lock(&mtx->mutex_mtx);
610 if (mtx->refcnt < 1) {
611 mtx_unlock(&mtx->mutex_mtx);
614 rm_disable_interrupts(mtx->sp);
616 mtx_unlock(&mtx->mutex_mtx);
622 RM_STATUS NV_API_CALL os_release_sema(void *semaphore)
624 struct os_mutex *mtx = semaphore;
626 mtx_lock(&mtx->mutex_mtx);
628 cv_signal(&mtx->mutex_cv);
630 rm_enable_interrupts(mtx->sp);
632 mtx_unlock(&mtx->mutex_mtx);
637 BOOL NV_API_CALL os_is_acquired_sema(void *semaphore)
639 struct os_mutex *mtx = semaphore;
640 return (mtx->refcnt < 1);
643 BOOL NV_API_CALL os_pat_supported(void)
646 * FreeBSD has no native PAT support and there's no good
647 * way to implement it privately as we do on Linux.
652 void NV_API_CALL os_register_compatible_ioctl(
659 void NV_API_CALL os_unregister_compatible_ioctl(
666 RM_STATUS NV_API_CALL os_disable_console_access(void)
671 RM_STATUS NV_API_CALL os_enable_console_access(void)
675 NvU64 NV_API_CALL os_acquire_spinlock(void *pSema)
677 struct os_mutex *mtx = pSema;
679 mtx_lock(&mtx->mutex_mtx);
684 void NV_API_CALL os_release_spinlock(void *pSema, NvU64 oldIrql)
686 struct os_mutex *mtx = pSema;
688 mtx_unlock(&mtx->mutex_mtx);
691 RM_STATUS NV_API_CALL os_get_address_space_info(
692 NvU64 *userStartAddress,
693 NvU64 *userEndAddress,
694 NvU64 *kernelStartAddress,
695 NvU64 *kernelEndAddress
698 return RM_ERR_NOT_SUPPORTED;