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_time(
147 BOOL NV_API_CALL os_is_administrator(PHWINFO pDev)
149 return suser(CURTHREAD) ? FALSE : TRUE;
152 U008 NV_API_CALL os_io_read_byte(
157 /* XXX Fix me? (bus_space access) */
161 void NV_API_CALL os_io_write_byte(
167 /* XXX Fix me? (bus_space access) */
168 outb(address, value);
171 U016 NV_API_CALL os_io_read_word(
176 /* XXX Fix me? (bus_space access) */
180 void NV_API_CALL os_io_write_word(
186 /* XXX Fix me? (bus_space access) */
187 return outw(address, value);
190 U032 NV_API_CALL os_io_read_dword(
195 /* XXX Fix me? (bus_space access) */
199 void NV_API_CALL os_io_write_dword(
205 /* XXX Fix me? (bus_space access) */
206 outl(address, value);
209 void* NV_API_CALL os_map_kernel_space(
217 #if defined(NVCPU_X86) && !defined(PAE)
218 if (start > 0xffffffff)
222 if (start & PAGE_MASK)
225 size = NV_ALIGN_UP(size, PAGE_SIZE);
228 case NV_MEMORY_CACHED:
229 map_mode = PAT_WRITE_BACK;
231 case NV_MEMORY_WRITECOMBINED:
232 map_mode = PAT_WRITE_COMBINING;
234 case NV_MEMORY_UNCACHED:
235 case NV_MEMORY_DEFAULT:
236 map_mode = PAT_UNCACHEABLE;
239 nv_printf(NV_DBG_ERRORS,
240 "NVRM: unknown mode in os_map_kernel_space()\n");
244 return pmap_mapdev_attr(start, size, map_mode);
247 void NV_API_CALL os_unmap_kernel_space(
252 pmap_unmapdev_attr((vm_offset_t)address, size);
255 void* NV_API_CALL os_map_kernel_space_high(
261 if (!(pfn & ~0xfffff)) {
262 start = pfn << PAGE_SHIFT;
263 return os_map_kernel_space(start, size, NV_MEMORY_CACHED);
268 void NV_API_CALL os_unmap_kernel_space_high(
274 os_unmap_kernel_space(addr, size);
277 RM_STATUS NV_API_CALL os_set_mem_range(
284 struct mem_range_desc mrd;
288 mrd.mr_flags = MDF_WRITECOMBINE;
290 strcpy(mrd.mr_owner, "NVIDIA");
291 arg = MEMRANGE_SET_UPDATE;
293 if (mem_range_attr_set(&mrd, &arg))
299 RM_STATUS NV_API_CALL os_unset_mem_range(
305 struct mem_range_desc mrd;
309 arg = MEMRANGE_SET_REMOVE;
311 if (mem_range_attr_set(&mrd, &arg))
319 * The current debug level is used to determine if certain debug messages
320 * are printed to the system console/log files or not. It defaults to the
321 * highest debug level, i.e. the lowest debug output.
324 U032 cur_debuglevel = 0xffffffff;
326 void NV_API_CALL os_dbg_init(void)
331 NV_UMA_ZONE_ALLOC_STACK(sp);
335 if (rm_read_registry_dword(sp, NULL, "NVreg", "ResmanDebugLevel",
336 &new_debuglevel) == RM_OK) {
337 if (new_debuglevel != 0xffffffff)
338 cur_debuglevel = new_debuglevel;
341 NV_UMA_ZONE_FREE_STACK(sp);
344 RM_STATUS NV_API_CALL os_schedule(void)
346 return RM_ERR_NOT_SUPPORTED;
349 void NV_API_CALL os_dbg_set_level(U032 new_debuglevel)
351 cur_debuglevel = new_debuglevel;
354 void NV_API_CALL os_dbg_breakpoint(void)
357 /* FreeBSD 5.x kdb */
358 kdb_enter("os_dbg_breakpoint()");
362 void NV_API_CALL out_string(const char *message)
364 #if defined(DEBUG) || defined(QA_BUILD)
365 printf("%s", message);
369 static char nv_error_string[MAX_ERROR_STRING];
371 int NV_API_CALL nv_printf(
377 char *message = nv_error_string;
379 int chars_written = 0;
381 if (debuglevel >= ((cur_debuglevel >> 4) & 3)) {
382 va_start(arglist, format);
383 chars_written = vsprintf(message, format, arglist);
385 printf("%s", message);
388 return chars_written;
391 int NV_API_CALL nv_snprintf(
401 va_start(arglist, fmt);
402 chars_written = vsnprintf(buf, size, fmt, arglist);
405 return chars_written;
408 void NV_API_CALL nv_os_log(
415 sprintf(nv_error_string, "NVRM: ");
416 l = strlen(nv_error_string);
417 vsnprintf(nv_error_string + l, MAX_ERROR_STRING - l, fmt, ap);
418 printf("%s", nv_error_string);
421 S032 NV_API_CALL os_mem_cmp(
427 return memcmp(buf0, buf1, length);
430 U008* NV_API_CALL os_mem_copy(
436 #if defined(NVCPU_X86_64)
438 for (i = 0; i < length; i++) dst[i] = src[i];
441 return memcpy(dst, src, length);
445 RM_STATUS NV_API_CALL os_memcpy_from_user(
451 if (src < (void *) VM_MAXUSER_ADDRESS)
452 return copyin(src, dst, length) ? RM_ERR_INVALID_POINTER : RM_OK;
454 return os_mem_copy(dst, src, length) ? RM_ERR_INVALID_POINTER : RM_OK;
457 RM_STATUS NV_API_CALL os_memcpy_to_user(
463 if (dst < (void *) VM_MAXUSER_ADDRESS)
464 return copyout(src, dst, length) ? RM_ERR_INVALID_POINTER : RM_OK;
466 return os_mem_copy(dst, src, length) ? RM_ERR_INVALID_POINTER : RM_OK;
469 void* NV_API_CALL os_mem_set(
475 return memset(b, c, length);
478 S032 NV_API_CALL os_string_compare(
483 return strcmp(s1, s2);
486 char* NV_API_CALL os_string_copy(
491 return strcpy(dst, src);
494 U032 NV_API_CALL os_string_length(const char* s)
499 RM_STATUS NV_API_CALL os_strncpy_from_user(
505 return copyinstr(src, dst, n, NULL) ? RM_ERR_INVALID_POINTER : RM_OK;
508 U032 NV_API_CALL os_get_page_size(void)
513 NvU64 NV_API_CALL os_get_page_mask(void)
516 * On FreeBSD, PAGE_MASK means (PAGE_SIZE - 1); on Linux it means the
517 * opposite, ~(PAGE_SIZE - 1); that is what this function is expected
523 NvU64 NV_API_CALL os_get_system_memory_size(void)
525 return ((NvU64)physmem * PAGE_SIZE) / RM_PAGE_SIZE;
528 U032 NV_API_CALL os_get_cpu_count(void)
533 RM_STATUS NV_API_CALL os_flush_cpu_cache(void)
536 * XXX This will do for now, but this may need to be extended
537 * to make IPI calls (flushing all caches).
539 __asm__ __volatile__("wbinvd": : :"memory");
543 void NV_API_CALL os_flush_cpu_write_combine_buffer(void)
545 __asm__ __volatile__("sfence": : :"memory");
548 RM_STATUS NV_API_CALL os_raise_smp_barrier(void)
550 return RM_ERR_NOT_SUPPORTED;
553 RM_STATUS NV_API_CALL os_clear_smp_barrier(void)
555 return RM_ERR_NOT_SUPPORTED;
560 struct mtx mutex_mtx;
565 RM_STATUS NV_API_CALL os_alloc_sema(void **semaphore)
568 struct os_mutex *mtx;
571 NV_UMA_ZONE_ALLOC_STACK(sp);
573 return RM_ERR_NO_FREE_MEM;
575 status = os_alloc_mem((void **)&mtx, sizeof(struct os_mutex));
576 if (status != RM_OK) {
577 NV_UMA_ZONE_FREE_STACK(sp);
581 mtx_init(&mtx->mutex_mtx, "os.mutex_mtx", NULL, MTX_DEF | MTX_RECURSE);
582 cv_init(&mtx->mutex_cv, "os.mutex_cv");
587 *semaphore = (void *) mtx;
592 RM_STATUS NV_API_CALL os_free_sema(void *semaphore)
594 struct os_mutex *mtx = semaphore;
598 NV_UMA_ZONE_FREE_STACK(sp);
600 mtx_destroy(&mtx->mutex_mtx);
601 cv_destroy(&mtx->mutex_cv);
603 os_free_mem(semaphore);
608 RM_STATUS NV_API_CALL os_acquire_sema(void *semaphore)
610 struct os_mutex *mtx = semaphore;
612 mtx_lock(&mtx->mutex_mtx);
614 rm_disable_interrupts(mtx->sp);
617 cv_wait(&mtx->mutex_cv, &mtx->mutex_mtx);
618 mtx_unlock(&mtx->mutex_mtx);
623 BOOL NV_API_CALL os_cond_acquire_sema(void *semaphore)
625 struct os_mutex *mtx = semaphore;
627 mtx_lock(&mtx->mutex_mtx);
628 if (mtx->refcnt < 1) {
629 mtx_unlock(&mtx->mutex_mtx);
632 rm_disable_interrupts(mtx->sp);
634 mtx_unlock(&mtx->mutex_mtx);
640 RM_STATUS NV_API_CALL os_release_sema(void *semaphore)
642 struct os_mutex *mtx = semaphore;
644 mtx_lock(&mtx->mutex_mtx);
646 cv_signal(&mtx->mutex_cv);
648 rm_enable_interrupts(mtx->sp);
650 mtx_unlock(&mtx->mutex_mtx);
655 BOOL NV_API_CALL os_is_acquired_sema(void *semaphore)
657 struct os_mutex *mtx = semaphore;
658 return (mtx->refcnt < 1);
661 BOOL NV_API_CALL os_pat_supported(void)
664 * FreeBSD has no native PAT support and there's no good
665 * way to implement it privately as we do on Linux.
670 RM_STATUS NV_API_CALL os_set_mlock_capability()
675 S032 NV_API_CALL os_mlock_user_memory(
683 S032 NV_API_CALL os_munlock_user_memory(
691 RM_STATUS NV_API_CALL os_check_process_map_limit(
692 NvU64 proc_max_map_count
698 void NV_API_CALL os_register_compatible_ioctl(
705 void NV_API_CALL os_unregister_compatible_ioctl(
712 RM_STATUS NV_API_CALL os_disable_console_access(void)
717 RM_STATUS NV_API_CALL os_enable_console_access(void)