adapt for devfs
[nvidia.git] / src / nvidia_subr.c
1 /* _NVRM_COPYRIGHT_BEGIN_
2  *
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.
7  *
8  * _NVRM_COPYRIGHT_END_
9  */
10
11 #include "nv-misc.h"
12 #include "os-interface.h"
13 #include "nv.h"
14 #include "nv-freebsd.h"
15
16 #if defined(NVCPU_X86) && defined(NV_USE_OS_VM86_INT10CALL)
17 #include <machine/vm86.h>
18 #endif
19
20 MALLOC_DEFINE(M_NV_STACK, "nvstack", "NVidia stack");
21 static nv_stack_t *__nvidia_init_sp = NULL;
22
23 devclass_t nvidia_devclass;
24 nv_state_t nvidia_ctl_state;
25
26 int nvidia_attach(device_t dev)
27 {
28     int status;
29     U032 i;
30     struct nvidia_softc *sc;
31     nv_state_t *nv;
32
33     sc = device_get_softc(dev);
34     nv = sc->nv_state;
35
36     nv->os_state         = sc;
37     nv->flags            = 0;
38     nv->bus              = pci_get_bus(dev);
39     nv->slot             = pci_get_slot(dev);
40     nv->vendor_id        = pci_get_vendor(dev);
41     nv->device_id        = pci_get_device(dev);
42     nv->interrupt_line   = pci_get_irq(dev);
43     nv->handle           = dev;
44     callout_init(&sc->timer_ch);
45
46     for (i = 0; i < NV_GPU_NUM_BARS; i++) {
47         if (sc->BAR_recs[i] != NULL) {
48             nv->bars[i].address = rman_get_start(sc->BAR_recs[i]);
49             nv->bars[i].size = rman_get_size(sc->BAR_recs[i]);
50         }
51     }
52
53     nv->fb   = &nv->bars[NV_GPU_BAR_INDEX_FB];
54     nv->regs = &nv->bars[NV_GPU_BAR_INDEX_REGS];
55
56     pci_enable_io(dev, SYS_RES_MEMORY);
57
58     if ((status = rm_is_supported_device(sc->attach_sp, nv)) != RM_OK) {
59         nv_printf(NV_DBG_ERRORS,
60             "NVRM: The NVIDIA GPU %02x:%02x (PCI ID: %04x:%04x) installed\n"
61             "NVRM: in this system is not supported by the %s NVIDIA FreeBSD\n"
62             "NVRM: graphics driver release.  Please see 'Appendix A -\n"
63             "NVRM: Supported NVIDIA GPU Products' in this release's README,\n"
64             "NVRM: available on the FreeBSD graphics driver download page at\n"
65             "NVRM: www.nvidia.com.\n",
66             nv->bus, nv->slot, nv->vendor_id, nv->device_id, NV_VERSION_STRING);
67         return ENXIO;
68     }
69
70     if ((status = nvidia_dev_attach(sc)) != 0)
71         return status;
72
73     if ((status = nvidia_ctl_attach()) != 0)
74         return status;
75
76     nv_sysctl_init(nv);
77     return 0;
78 }
79
80 int nvidia_detach(device_t dev)
81 {
82     int status;
83     struct nvidia_softc *sc;
84
85     sc = device_get_softc(dev);
86     nv_sysctl_exit(sc->nv_state);
87
88     status = nvidia_dev_detach(sc);
89     if (status) {
90         device_printf(dev, "NVRM: NVIDIA driver DEV detach failed.\n");
91         goto fail;
92     }
93
94     status = nvidia_ctl_detach();
95     if (status) {
96         device_printf(dev, "NVRM: NVIDIA driver CTL detach failed.\n");
97         goto fail;
98     }
99
100 fail:
101     /* XXX Fix me? (state) */
102     return status;
103 }
104
105
106 #ifdef NV_SUPPORT_ACPI_PM
107 int nvidia_suspend(device_t dev)
108 {
109     nv_stack_t *sp;
110     struct nvidia_softc *sc;
111     nv_state_t *nv;
112     int status = RM_ERROR;
113
114     /* Only if ACPI is running */
115     if (devclass_get_softc(devclass_find("ACPI"), 0) == NULL)
116         return ENODEV;
117
118     NV_UMA_ZONE_ALLOC_STACK(sp);
119     if (sp == NULL)
120         return ENOMEM;
121
122     sc = device_get_softc(dev);
123     nv = sc->nv_state;
124
125     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
126     status = rm_power_management(sp, nv, 0, NV_PM_ACPI_STANDBY);
127
128     NV_UMA_ZONE_FREE_STACK(sp);
129
130     return status;
131 }
132
133 int nvidia_resume(device_t dev)
134 {
135     nv_stack_t *sp;
136     struct nvidia_softc *sc;
137     nv_state_t *nv;
138     int status = RM_ERROR;
139
140     NV_UMA_ZONE_ALLOC_STACK(sp);
141     if (sp == NULL)
142         return ENOMEM;
143
144     sc = device_get_softc(dev);
145     nv = sc->nv_state;
146
147     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
148     status = rm_power_management(sp, nv, 0, NV_PM_ACPI_RESUME);
149
150     NV_UMA_ZONE_FREE_STACK(sp);
151
152     return status;
153 }
154 #endif /* NV_SUPPORT_ACPI_PM */
155
156
157 int nvidia_alloc_hardware(device_t dev)
158 {
159     int error = 0;
160     struct nvidia_softc *sc;
161     U032 flags, i;
162
163     sc = device_get_softc(dev);
164     sc->dev = dev;
165
166     flags = 0; /* not RF_ACTIVE */
167     for (i = 0; i < NV_GPU_NUM_BARS && sc->BAR_rids[i] != 0; i++) {
168         struct resource *res;
169         res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->BAR_rids[i], flags);
170         if (res == NULL) {
171             /*
172              * The most likely reason for this failure is that the SBIOS failed
173              * to assign a valid address range to this BAR; FreeBSD is unable to
174              * correct the problem and fails this BUS resource allocation. We
175              * trust the kernel with BAR validation at this point, but later try
176              * to catch cases where the X server "corrects" "invalid" BAR's.
177              *
178              * Please see to nvidia_pci_check_config_space() in nvidia_pci.c for
179              * additional information.
180              */
181             device_printf(dev,
182                 "NVRM: NVIDIA MEM resource alloc failed, BAR%d @ 0x%02x.\n",
183                 i, sc->nv_state->bars[i].offset);
184             error = ENXIO;
185             goto fail;
186         }
187         sc->BAR_recs[i] = res;
188     }
189
190     flags = RF_SHAREABLE | RF_ACTIVE;
191     sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, flags);
192     if (sc->irq == NULL) {
193         device_printf(dev, "NVRM: NVIDIA IRQ resource alloc failed.\n");
194         error = ENXIO;
195         goto fail;
196     }
197
198 fail:
199     return (error);
200 }
201
202 void nvidia_free_hardware(device_t dev)
203 {
204     struct nvidia_softc *sc;
205     U032 i;
206
207     sc = device_get_softc(dev);
208
209     for (i = 0; i < NV_GPU_NUM_BARS && sc->BAR_recs[i] != NULL; i++)
210         bus_release_resource(dev, SYS_RES_MEMORY, sc->BAR_rids[i], sc->BAR_recs[i]);
211     if (sc->irq != NULL)
212         bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
213     if (sc->iop != NULL)
214         bus_release_resource(dev, SYS_RES_IOPORT, sc->iop_rid, sc->iop);
215 }
216
217 void nvidia_intr(void *xsc)
218 {
219     struct nvidia_softc *sc;
220     nv_state_t *nv;
221     U032 run_bottom_half = 0;
222     nv_stack_t *sp;
223
224     sc = (struct nvidia_softc *) xsc;
225     nv = sc->nv_state;
226
227     sp = sc->isr_sp;
228
229     if (sp == NULL)
230         return;
231
232     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, FALSE);
233     rm_isr(sp, nv, &run_bottom_half);
234
235     if (run_bottom_half) {
236         /* We're not executing in an HW ISR context */
237         rm_isr_bh(sp, nv);
238     }
239 }
240
241 int nvidia_get_card_info(struct nv_ioctl_card_info *ci)
242 {
243     unsigned int i;
244     struct nvidia_softc *sc;
245     nv_state_t *nv;
246
247     /*
248      * Clients supporting versioning will pass version magic in the first
249      * card information field.
250      */
251     struct nv_ioctl_rm_api_old_version *av = (void *) ci;
252     int status = 0;
253
254     switch (av->magic) {
255         case NV_RM_API_OLD_VERSION_MAGIC_OVERRIDE_REQ:
256         case NV_RM_API_OLD_VERSION_MAGIC_LAX_REQ:
257         case NV_RM_API_OLD_VERSION_MAGIC_REQ:
258             /*
259              * the client is using the old major-minor-patch API
260              * version check; reject it.
261              */
262             nv_printf(NV_DBG_ERRORS,
263                       "NVRM: API mismatch: the client has the version %d.%d-%d, but\n"
264                       "NVRM: this kernel module has the version %s.  Please\n"
265                       "NVRM: make sure that this kernel module and all NVIDIA driver\n"
266                       "NVRM: components have the same version.\n",
267                       av->major, av->minor, av->patch,
268                       NV_VERSION_STRING);
269             status = -EINVAL;
270             break;
271
272         case NV_RM_API_OLD_VERSION_MAGIC_IGNORE:
273             /*
274              * the client is telling us to ignore the old version
275              * scheme; it will do a version check via
276              * NV_ESC_CHECK_VERSION_STR
277              */
278             break;
279         default:
280             status = -EINVAL;
281             break;
282     }
283
284     /* clear card information structure */
285     memset(ci, 0, sizeof(ci));
286
287     for (i = 0; i < NV_MAX_DEVICES; i++) {
288         sc = devclass_get_softc(nvidia_devclass, i);
289         if (!sc)
290             continue;
291         nv = sc->nv_state;
292
293         ci[i].flags          = (NV_IOCTL_CARD_INFO_FLAG_PRESENT |
294                                 NV_IOCTL_CARD_INFO_FLAG_NEED_MSYNC);
295         ci[i].bus            = nv->bus;
296         ci[i].slot           = nv->slot;
297         ci[i].vendor_id      = nv->vendor_id;
298         ci[i].device_id      = nv->device_id;
299         ci[i].interrupt_line = nv->interrupt_line;
300         ci[i].fb_address     = nv->fb->address;
301         ci[i].fb_size        = nv->fb->size;
302         ci[i].reg_address    = nv->regs->address;
303         ci[i].reg_size       = nv->regs->size;
304     }
305
306     return status;
307 }
308
309 int nvidia_handle_ioctl(
310     struct cdev *dev,
311     u_long cmd,
312     caddr_t data,
313     int fflag,
314     d_thread_t *td
315 )
316 {
317     struct nvidia_softc *sc;
318     nv_state_t *nv;
319     int unit = minor(dev);
320     nv_stack_t *sp;
321     void *args;
322     nv_ioctl_xfer_t *xfer = NULL;
323     int status;
324     int nr, size;
325
326     if (unit == CDEV_CTL_MINOR) {
327         nv = &nvidia_ctl_state;
328         sc = nv->os_state;
329     } else {
330         sc = devclass_get_softc(nvidia_devclass, unit);
331         if (!sc)
332             return ENXIO;
333         nv = sc->nv_state;
334     }
335
336     sp = sc->api_sp;
337
338     size = __NV_IOC_SIZE(cmd);
339     nr = __NV_IOC_NR(cmd);
340
341     args = (void *)data;
342
343     if (nr == NV_ESC_IOCTL_XFER_CMD) {
344         if (__NV_IOC_SIZE(cmd) != sizeof(nv_ioctl_xfer_t))
345             return EINVAL;
346
347         xfer = args;
348         size = xfer->size;
349
350         if (size > NV_ABSOLUTE_MAX_IOCTL_SIZE)
351             return EINVAL;
352
353         args = malloc(size, M_NVIDIA, M_WAITOK);
354         if (args == NULL)
355             return ENOMEM;
356
357         if (copyin(NvP64_VALUE(xfer->ptr), args, size) != 0) {
358             free(args, M_NVIDIA);
359             return EFAULT;
360         }
361
362         nr = xfer->cmd;
363     }
364
365     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
366
367     switch (nr) {
368         case NV_ESC_CHECK_VERSION_STR:
369             status = (rm_perform_version_check(sp, args) == RM_OK)
370                 ? 0 : EINVAL;
371             break;
372
373         case NV_ESC_CARD_INFO:
374             status = nvidia_get_card_info(args);
375             break;
376
377         default:
378             status = rm_ioctl(sp, nv, __TD_FDT(td), nr, args)
379                 ? 0 : EINVAL;
380             break;
381     }
382
383     if (args != (void *)data) {
384         if (copyout(args, NvP64_VALUE(xfer->ptr), size) != 0)
385             status = EFAULT;
386         free(args, M_NVIDIA);
387     }
388
389     return status;
390 }
391
392 int nvidia_open_ctl(
393     struct cdev *dev,
394     d_thread_t *td
395 )
396 {
397     struct nvidia_softc *sc;
398     nv_state_t *nv = &nvidia_ctl_state;
399     BOOL new_filep = FALSE;
400     struct nvidia_filep *filep;
401
402     sc = nv->os_state;
403
404     STAILQ_FOREACH(filep, &sc->filep_queue, queue) {
405         if (filep->fd_table == __TD_FDT(td)) 
406             break;
407     }
408
409     if (filep == NULL) {
410         filep = kmalloc(sizeof(nvidia_filep_t), M_NVIDIA, M_NOWAIT | M_ZERO | M_NULLOK);
411         if (filep == NULL)
412             return ENOMEM;
413         filep->fd_table = __TD_FDT(td);
414         filep->fd_refcnt = 0;
415         STAILQ_INSERT_HEAD(&sc->filep_queue, filep, queue);
416         new_filep = TRUE;
417     }
418
419     filep->fd_refcnt++;
420
421     if (sc->refcnt == 0) {
422         NV_UMA_ZONE_ALLOC_STACK(sc->api_sp);
423         if (sc->api_sp == NULL) {
424             if (new_filep)
425                 free(filep, M_NVIDIA);
426             return ENOMEM;
427         }
428
429         STAILQ_INIT(&sc->event_queue);
430         nv->flags |= (NV_FLAG_OPEN | NV_FLAG_CONTROL);
431     }
432
433     sc->refcnt++;
434
435     return 0;
436 }
437
438 int nvidia_close_ctl(
439     struct cdev *dev,
440     d_thread_t *td
441 )
442 {
443     struct nvidia_softc *sc;
444     nv_state_t *nv = &nvidia_ctl_state;
445     struct nvidia_event *et;
446     struct nvidia_filep *filep;
447     nv_stack_t *sp;
448
449     sc = nv->os_state;
450
451     STAILQ_FOREACH(filep, &sc->filep_queue, queue) {
452         if (filep->fd_table == __TD_FDT(td)) 
453             break;
454     }
455
456     if (filep == NULL)
457         return EINVAL;
458
459     sp = sc->api_sp;
460
461     if (--filep->fd_refcnt == 0) {
462         rm_free_unused_clients(sp, nv, __TD_FDT(td));
463         STAILQ_REMOVE(&sc->filep_queue, filep, nvidia_filep, queue);
464         free(filep, M_NVIDIA);
465     }
466
467     if (--sc->refcnt == 0) {
468         while ((et = STAILQ_FIRST(&sc->event_queue))) {
469             STAILQ_REMOVE(&sc->event_queue, et, nvidia_event, queue);
470             free(et, M_NVIDIA);
471         }
472
473         NV_UMA_ZONE_FREE_STACK(sp);
474
475         nv->flags &= ~NV_FLAG_OPEN;
476     }
477
478     return 0;
479 }
480
481 int nvidia_open_dev(
482     struct nvidia_softc *sc,
483     struct cdev *dev,
484     d_thread_t *td
485 )
486 {
487     int status = ENOMEM;
488     nv_state_t *nv = sc->nv_state;
489     BOOL new_filep = FALSE;
490     struct nvidia_filep *filep;
491     nv_stack_t *sp = NULL;
492
493     STAILQ_FOREACH(filep, &sc->filep_queue, queue) {
494         if (filep->fd_table == __TD_FDT(td)) 
495             break;
496     }
497
498     if (filep == NULL) {
499         filep = kmalloc(sizeof(nvidia_filep_t), M_NVIDIA, M_NOWAIT | M_ZERO | M_NULLOK);
500         if (filep == NULL)
501             return ENOMEM;
502         filep->fd_table = __TD_FDT(td);
503         filep->fd_refcnt = 0;
504         new_filep = TRUE;
505     }
506
507     if (sc->refcnt == 0) {
508         NV_UMA_ZONE_ALLOC_STACK(sc->api_sp);
509         if (sc->api_sp == NULL)
510             goto failed;
511
512         NV_UMA_ZONE_ALLOC_STACK(sc->pci_cfgchk_sp);
513         if (sc->pci_cfgchk_sp == NULL)
514             goto failed;
515
516         NV_UMA_ZONE_ALLOC_STACK(sc->isr_sp);
517         if (sc->isr_sp == NULL)
518             goto failed;
519
520         NV_UMA_ZONE_ALLOC_STACK(sc->timer_sp);
521         if (sc->timer_sp == NULL)
522             goto failed;
523     }
524
525     sp = sc->api_sp;
526     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
527
528     if (sc->refcnt == 0) {
529         if (!rm_init_adapter(sp, nv)) {
530             device_printf(sc->dev, "NVRM: rm_init_adapter() failed!\n");
531             status = EIO;
532             goto failed;
533         }
534
535         STAILQ_INIT(&sc->event_queue);
536         nv->flags |= NV_FLAG_OPEN;
537     }
538
539     filep->fd_refcnt++;
540     if (new_filep)
541         STAILQ_INSERT_HEAD(&sc->filep_queue, filep, queue);
542
543     sc->refcnt++;
544
545     return 0;
546
547 failed:
548     if (sc->refcnt == 0) {
549         if (sc->timer_sp != NULL)
550             NV_UMA_ZONE_FREE_STACK(sc->timer_sp);
551         if (sc->isr_sp != NULL)
552             NV_UMA_ZONE_FREE_STACK(sc->isr_sp);
553         if (sc->pci_cfgchk_sp != NULL)
554             NV_UMA_ZONE_FREE_STACK(sc->pci_cfgchk_sp);
555         if (sc->api_sp != NULL)
556             NV_UMA_ZONE_FREE_STACK(sc->api_sp);
557     }
558
559     if (new_filep)
560         free(filep, M_NVIDIA);
561
562     return status;
563 }
564
565 int nvidia_close_dev(
566     struct nvidia_softc *sc,
567     struct cdev *dev,
568     d_thread_t *td
569 )
570 {
571     nv_state_t *nv = sc->nv_state;
572     struct nvidia_event *et;
573     struct nvidia_filep *filep;
574     nv_stack_t *sp;
575
576     STAILQ_FOREACH(filep, &sc->filep_queue, queue) {
577         if (filep->fd_table == __TD_FDT(td)) 
578             break;
579     }
580
581     if (filep == NULL)
582         return EINVAL;
583
584     sp = sc->api_sp;
585
586     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
587
588     if (--filep->fd_refcnt == 0) {
589         rm_free_unused_clients(sp, nv, __TD_FDT(td));
590         STAILQ_REMOVE(&sc->filep_queue, filep, nvidia_filep, queue);
591         free(filep, M_NVIDIA);
592     }
593
594     if (--sc->refcnt == 0) {
595         rm_disable_adapter(sp, nv);
596         rm_shutdown_adapter(sp, nv);
597
598         while ((et = STAILQ_FIRST(&sc->event_queue))) {
599             STAILQ_REMOVE(&sc->event_queue, et, nvidia_event, queue);
600             free(et, M_NVIDIA);
601         }
602
603         NV_UMA_ZONE_FREE_STACK(sc->timer_sp);
604         NV_UMA_ZONE_FREE_STACK(sc->isr_sp);
605         NV_UMA_ZONE_FREE_STACK(sc->pci_cfgchk_sp);
606         NV_UMA_ZONE_FREE_STACK(sc->api_sp);
607
608         nv->flags &= ~NV_FLAG_OPEN;
609     }
610
611     return 0;
612 }
613
614
615 int nvidia_modevent(
616     module_t mod,
617     int what,
618     void *arg
619 )
620 {
621     nv_state_t *nv;
622     struct nvidia_softc *sc;
623     nv_stack_t *sp;
624
625     switch (what) {
626         case MOD_LOAD:
627             /*
628              * The module load event. Our KLD has just been loaded and is
629              * ready to initialize. We setup the core resource manager in
630              * this routine, further initialization takes place at attach
631              * time.
632              */
633             sc = &nvidia_ctl_sc;
634
635             NV_UMA_ZONE_ALLOC_STACK(sp);
636             if (sp == NULL) {
637                 return ENOMEM;
638             }
639
640             bzero(sc, sizeof(nvidia_softc_t));
641             STAILQ_INIT(&sc->filep_queue);
642
643             if (!rm_init_rm(sp)) {
644                 kprintf("NVRM: rm_init_rm() failed!\n");
645                 NV_UMA_ZONE_FREE_STACK(sp);
646                 return EIO;
647             }
648
649             __nvidia_init_sp = sp;
650
651             spin_init(&sc->rm_lock);
652             lockinit(&sc->api_lock, "nvapi", 0, LK_CANRECURSE);
653
654             nvidia_ctl_state.os_state = sc;
655             sc->nv_state = (void *)&nvidia_ctl_state;
656
657             nvidia_sysctl_init();
658             nvidia_linux_init();
659
660             break;
661
662         case MOD_UNLOAD:
663             /*
664              * Check if the control device is still open and reject the
665              * unload request if it is. This event can occur even when the
666              * module usage count is non-zero!
667              */
668             nv = &nvidia_ctl_state;
669             sc = nv->os_state;
670
671             nv_lock_api(nv);
672
673             if (sc->refcnt != 0) { /* XXX Fix me? (refcnt) */
674                 nv_unlock_api(nv);
675                 return EBUSY;
676             }
677
678             nv_unlock_api(nv);
679
680             spin_uninit(&sc->rm_lock);
681             lockuninit(&sc->api_lock);
682
683             sp = __nvidia_init_sp;
684             rm_shutdown_rm(sp);
685
686             NV_UMA_ZONE_FREE_STACK(sp);
687
688             nvidia_sysctl_exit();
689             nvidia_linux_exit();
690
691             break;
692
693         default:
694             break;
695     }
696
697     return 0;
698 }
699
700
701 #ifdef NV_SUPPORT_OS_AGP
702 S032 nv_os_agp_init(
703     nv_stack_t *sp,
704     nv_state_t *nv,
705     void **base,
706     U032 *limit
707 )
708 {
709     void *bitmap;
710     struct nvidia_softc *sc = nv->os_state;
711     struct agp_info ai;
712
713     U032 mode = 0;
714     U032 fw   = 0;
715     U032 sba  = 0;
716     U032 rate = (8 | 4 | 2 | 1);
717     U032 size = 0;
718
719     sc->agp_dev = agp_find_device();
720     if (!sc->agp_dev) {
721         kprintf("NVRM: agp_find_device failed, chipset unsupported?\n");
722         return -ENODEV;
723     }
724
725     if (agp_acquire(sc->agp_dev) != 0)
726         return -EBUSY;
727
728     agp_get_info(sc->agp_dev, &ai);
729     mode = ai.ai_mode;
730
731     if (os_set_mem_range(ai.ai_aperture_base, ai.ai_aperture_size,
732                 NV_MEMORY_WRITECOMBINED) != RM_OK) {
733         /*
734          * Failure to set a write-combining range for the AGP aperture is
735          * not necessarily a fatal error condition; we don't know at this
736          * point, however, and abort to prevent performance and stability
737          * problems that may be hard to track down otherwise.
738          */
739         agp_release(sc->agp_dev);
740         return -EIO;
741     }
742
743     rm_read_registry_dword(sp, NULL, "NVreg", "ReqAGPRate", &rate);
744     rm_read_registry_dword(sp, NULL, "NVreg", "EnableAGPFW", &fw);
745     rm_read_registry_dword(sp, NULL, "NVreg", "EnableAGPSBA", &sba);
746
747     if (AGP_MODE_GET_MODE_3(mode))
748         rate = (rate >> 2) & 3;
749     mode = AGP_MODE_SET_RATE(mode, AGP_MODE_GET_RATE(mode) & rate);
750     mode |= 1 /* avoid 0x mode request */;
751
752     if (AGP_MODE_GET_RATE(mode) & 2)
753         mode = AGP_MODE_SET_RATE(mode, AGP_MODE_GET_RATE(mode) & ~1);
754     if (AGP_MODE_GET_RATE(mode) & 4)
755         mode = AGP_MODE_SET_RATE(mode, AGP_MODE_GET_RATE(mode) & ~2);
756
757     mode = AGP_MODE_SET_FW(mode, fw);
758     mode = AGP_MODE_SET_SBA(mode, sba);
759
760     if (agp_enable(sc->agp_dev, mode) != 0) {
761         agp_release(sc->agp_dev);
762         os_unset_mem_range(ai.ai_aperture_base, ai.ai_aperture_size);
763         return -EIO;
764     }
765
766     size = ai.ai_aperture_size / RM_PAGE_SIZE / 8;
767
768     if (os_alloc_mem((void **)&bitmap, size) != RM_OK) {
769         agp_release(sc->agp_dev);
770         os_unset_mem_range(ai.ai_aperture_base, ai.ai_aperture_size);
771         return -EIO;
772     }
773
774     os_mem_set(bitmap, 0xff, size);
775
776     if (rm_set_agp_bitmap(sp, nv, bitmap) != RM_OK) {
777         agp_release(sc->agp_dev);
778         os_free_mem(bitmap);
779         os_unset_mem_range(ai.ai_aperture_base, ai.ai_aperture_size);
780         return -EIO;
781     }
782
783     *base   = (void *) ai.ai_aperture_base;
784     *limit  = (U032)   ai.ai_aperture_size - 1;
785
786     return 0;
787 }
788
789 S032 nv_os_agp_teardown(
790     nv_stack_t *sp,
791     nv_state_t *nv
792 )
793 {
794     struct nvidia_softc *sc = nv->os_state;
795     void *bitmap;
796
797     if (agp_release(sc->agp_dev) != 0)
798         return -EBUSY;
799
800     rm_clear_agp_bitmap(sp, nv, &bitmap);
801     os_free_mem(bitmap);
802
803     os_unset_mem_range(nv->agp.address, nv->agp.size);
804
805     return 0;
806 }
807 #endif /* NV_SUPPORT_OS_AGP */
808
809 RM_STATUS NV_API_CALL nv_agp_init(
810     nv_state_t *nv,
811     void **base,
812     void *limit,
813     U032 config
814 )
815 {
816     RM_STATUS status = RM_ERROR;
817     nv_stack_t *sp;
818
819     if (NV_AGP_ENABLED(nv))
820         return RM_ERR_STATE_IN_USE;
821
822     if (config == NVOS_AGP_CONFIG_DISABLE_AGP) {
823         /*
824          * Match the behavior on Linux, don't consider the attempt
825          * to initialize AGP as 'disabled' an error.
826          */
827         nv->agp_config = NVOS_AGP_CONFIG_DISABLE_AGP;
828         nv->agp_status = NV_AGP_STATUS_DISABLED;
829         return RM_OK;
830     }
831
832     NV_UMA_ZONE_ALLOC_STACK(sp);
833     if (sp == NULL)
834         return RM_ERR_NO_FREE_MEM;
835
836 #ifdef NV_SUPPORT_OS_AGP
837     if ((config & NVOS_AGP_CONFIG_OSAGP) != 0) {
838         if (nv_os_agp_init(sp, nv, base, limit) == 0) {
839             /*
840              * If the operating system AGP GART driver successfully
841              * configured its backend, apply chipset overrides.
842              */
843             rm_update_agp_config(sp, nv);
844             NV_UMA_ZONE_FREE_STACK(sp);
845
846             nv->agp_config = NVOS_AGP_CONFIG_OSAGP;
847             nv->agp_status = NV_AGP_STATUS_ENABLED;
848
849             return RM_OK;
850         }
851     }
852 #endif /* NV_SUPPORT_OS_AGP */
853
854     if ((config & NVOS_AGP_CONFIG_NVAGP) == 0) {
855         status = RM_ERR_NOT_SUPPORTED;
856         goto failed;
857     }
858
859     if (devclass_get_softc(devclass_find("agp"), 0) != NULL) {
860         /*
861          * Make sure we don't try to use the internal GART driver when
862          * the OS AGPGART driver (agp.ko) is attached. While that may
863          * be perfectly fine on most systems, but is known to break on
864          * some.
865          * -------------------------------------------------------------
866          * DON'T REDISTRIBUTE THE DRIVER WITH THIS SANITY CHECK REMOVED!
867          * -------------------------------------------------------------
868          */
869         kprintf("NVRM: detected agp.ko, aborting NVIDIA AGP setup!\n");
870         goto failed;
871     }
872
873     status = rm_init_agp(sp, nv);
874     if (status == RM_OK) {
875         NV_UMA_ZONE_FREE_STACK(sp);
876
877         nv->agp_config = NVOS_AGP_CONFIG_NVAGP;
878         nv->agp_status = NV_AGP_STATUS_ENABLED;
879
880         return status;
881     }
882
883 failed:
884     NV_UMA_ZONE_FREE_STACK(sp);
885
886     nv->agp_config = NVOS_AGP_CONFIG_DISABLE_AGP;
887     nv->agp_status = NV_AGP_STATUS_FAILED;
888
889     return status;
890 }
891
892 RM_STATUS NV_API_CALL nv_agp_teardown(nv_state_t *nv)
893 {
894     RM_STATUS status = RM_ERR_NOT_SUPPORTED;
895     nv_stack_t *sp;
896
897     if (!NV_AGP_ENABLED(nv))
898         return status;
899
900     NV_UMA_ZONE_ALLOC_STACK(sp);
901     if (sp == NULL)
902         return RM_ERR_NO_FREE_MEM;
903
904 #ifdef NV_SUPPORT_OS_AGP
905     if (NV_OSAGP_ENABLED(nv))
906         status = (nv_os_agp_teardown(sp, nv) == 0)
907             ? RM_OK : RM_ERROR;
908 #endif
909     if (NV_NVAGP_ENABLED(nv))
910         status = rm_teardown_agp(sp, nv);
911
912     NV_UMA_ZONE_FREE_STACK(sp);
913
914     nv->agp_config = NVOS_AGP_CONFIG_DISABLE_AGP;
915     nv->agp_status = NV_AGP_STATUS_DISABLED;
916
917     return status;
918 }
919
920 S032 NV_API_CALL nv_no_incoherent_mappings(void)
921 {
922 #if !defined(NVCPU_X86_64) && !defined(NV_SUPPORT_OS_AGP)
923     return 1;
924 #else
925     /*
926      * XXX We can't modify FreeBSD/amd64's cached direct mapping
927      * and are thus can't provide coherent mappings. The driver
928      * will attempt to work around this problem, but AGP support
929      * may be unavailable on some newer systems.
930      *
931      * The FreeBSD AGP GART driver also doesn't currently update
932      * the kernel mappings of system memory mapped into the AGP
933      * aperture.
934      */
935     return 0;
936 #endif
937 }
938
939 void NV_API_CALL nv_lock_rm(nv_state_t *nv)
940 {
941     /*
942      * With SMPng, the "giant" kernel lock is gone. That means that we're
943      * in a more complex enviroment locking-wise, but since the necessary
944      * locking primitives are available to us, we can handle it.
945      *
946      * With mtx_lock_spin we acquire a spin mutex and locally disable all
947      * interrupts on the current processor.
948      */
949     struct nvidia_softc *sc = nv->os_state;
950     spin_lock_wr(&sc->rm_lock);
951 }
952
953 void NV_API_CALL nv_unlock_rm(nv_state_t *nv)
954 {
955     struct nvidia_softc *sc = nv->os_state;
956     spin_unlock_wr(&sc->rm_lock);
957 }
958
959 void nv_lock_api(nv_state_t *nv)
960 {
961     struct nvidia_softc *sc = nv->os_state;
962     lockmgr(&sc->api_lock, LK_EXCLUSIVE|LK_CANRECURSE);
963 }
964
965 void nv_unlock_api(nv_state_t *nv)
966 {
967     struct nvidia_softc *sc = nv->os_state;
968     lockmgr(&sc->api_lock, LK_RELEASE);
969 }
970
971
972 void NV_API_CALL nv_post_event(
973     nv_state_t *nv,
974     nv_event_t *event,
975     U032 hObject,
976     U032 index
977 )
978 {
979     struct nvidia_softc *sc;
980     struct nvidia_event *et; 
981
982     et = kmalloc(sizeof(nvidia_event_t), M_NVIDIA, M_NOWAIT | M_ZERO | M_NULLOK);
983     if (et == NULL)
984         return;
985
986     et->event = *event;
987     et->event.hObject = hObject;
988     et->event.index = index;
989
990     nv_lock_rm(nv);
991
992     sc = nv->os_state;
993     STAILQ_INSERT_TAIL(&sc->event_queue, et, queue);
994
995     nv_unlock_rm(nv);
996
997     selwakeup(&sc->rsel);
998 }
999
1000 S032 NV_API_CALL nv_get_event(
1001     nv_state_t *nv,
1002     void *file,
1003     nv_event_t *event,
1004     U032 *pending
1005 )
1006 {
1007     struct nvidia_softc *sc = nv->os_state;
1008     struct nvidia_event *et, *_et;
1009
1010     nv_lock_rm(nv);
1011
1012     STAILQ_FOREACH(et, &sc->event_queue, queue) {
1013         if (et->event.file == file)
1014             break;
1015     }
1016
1017     if (et != NULL) {
1018         *event = et->event;
1019
1020         STAILQ_REMOVE(&sc->event_queue, et, nvidia_event, queue);
1021
1022         STAILQ_FOREACH(_et, &sc->event_queue, queue) {
1023             if (_et->event.file == file)
1024                 break;
1025         }
1026
1027         *pending = (_et != NULL);
1028
1029         nv_unlock_rm(nv);
1030
1031         /* will attempt to acquire a blockable sleep lock */
1032         free(et, M_NVIDIA);
1033
1034         return RM_OK;
1035     }
1036
1037     nv_unlock_rm(nv);
1038     return RM_ERROR; /* RM polling? */
1039 }
1040
1041 void* NV_API_CALL nv_alloc_kernel_mapping(
1042     nv_state_t *nv,
1043     NvU64 address,
1044     U032 size,
1045     void **private
1046 )
1047 {
1048     struct nvidia_alloc *at;
1049     struct nvidia_softc *sc = nv->os_state;
1050     vm_offset_t offset, linear;
1051
1052     offset = (vm_offset_t) address & PAGE_MASK;
1053     address &= ~PAGE_MASK;
1054
1055     SLIST_FOREACH(at, &sc->alloc_list, list) {
1056         linear = at->address;
1057         do {
1058             if (vtophys(linear) == (vm_offset_t) address)
1059                 return (void *)(linear + offset);
1060             linear += PAGE_SIZE;
1061         } while (linear < (at->address + at->size));
1062     }
1063
1064     return NULL;
1065 }
1066
1067 S032 NV_API_CALL nv_free_kernel_mapping(
1068     nv_state_t *nv,
1069     void *address,
1070     void *private
1071 )
1072 {
1073     /* There's nothing to be done here. */
1074     return RM_OK;
1075 }
1076
1077 S032 nv_alloc_contig_pages(
1078     nv_state_t *nv,
1079     U032 count,
1080     U032 cache_type,
1081     NvU64 *pte_array,
1082     void **private
1083 )
1084 {
1085     struct nvidia_alloc *at;
1086     struct nvidia_softc *sc = nv->os_state;
1087     void *address;
1088     U032 size = count * PAGE_SIZE;
1089     int status;
1090
1091     if (os_alloc_contig_pages(&address, size) != RM_OK)
1092         return -ENOMEM;
1093
1094     at = kmalloc(sizeof(struct nvidia_alloc), M_NVIDIA, M_WAITOK | M_ZERO);
1095     if (!at) {
1096         os_free_contig_pages(address, size);
1097         return -ENOMEM;
1098     }
1099
1100     if (cache_type != NV_MEMORY_CACHED) {
1101         status = pmap_change_attr((vm_offset_t)address, size, PAT_UNCACHEABLE);
1102         if (status != 0) {
1103             free(at, M_NVIDIA);
1104             os_free_contig_pages(address, size);
1105             return status;
1106         }
1107     }
1108
1109     at->alloc_type_contiguous = 1;
1110
1111     at->cache_type = cache_type;
1112     at->size = size;
1113     at->address = (vm_offset_t)address;
1114     at->pte_array = pte_array;
1115
1116     pte_array[0] = vtophys(at->address);
1117
1118     *private = at;
1119     SLIST_INSERT_HEAD(&sc->alloc_list, at, list);
1120
1121     return 0;
1122 }
1123
1124 S032 nv_free_contig_pages(
1125     nv_state_t *nv,
1126     void *private
1127 )
1128 {
1129     struct nvidia_alloc *at = private;
1130     struct nvidia_softc *sc = nv->os_state;
1131
1132     SLIST_REMOVE(&sc->alloc_list, at, nvidia_alloc, list);
1133
1134     if (at->cache_type != NV_MEMORY_CACHED)
1135         pmap_change_attr(at->address, at->size, PAT_WRITE_BACK);
1136
1137     os_free_contig_pages((void *)at->address, at->size);
1138     free(at, M_NVIDIA);
1139
1140     return 0;
1141 }
1142
1143 S032 nv_alloc_system_pages(
1144     nv_state_t  *nv,
1145     U032 count,
1146     U032 cache_type,
1147     NvU64 *pte_array,
1148     void **private
1149 )
1150 {
1151     struct nvidia_alloc *at;
1152     struct nvidia_softc *sc = nv->os_state;
1153     void *address;
1154     u_int32_t i, size;
1155     int status;
1156
1157     size = count * PAGE_SIZE;
1158     at = kmalloc(sizeof(struct nvidia_alloc), M_NVIDIA, M_WAITOK | M_ZERO);
1159     if (!at) {
1160         return -ENOMEM;
1161     }
1162
1163     address = kmalloc(size, M_NVIDIA, M_WAITOK | M_ZERO);
1164     if (!address) {
1165         free(at, M_NVIDIA);
1166         return -ENOMEM;
1167     }
1168
1169     if (cache_type != NV_MEMORY_CACHED) {
1170         status = pmap_change_attr((vm_offset_t)address, size, PAT_UNCACHEABLE);
1171         if (status != 0) {
1172             free(at, M_NVIDIA);
1173             free(address, M_NVIDIA);
1174             return status;
1175         }
1176     }
1177
1178     at->alloc_type_contiguous = 0;
1179
1180     at->cache_type = cache_type;
1181     at->size = size;
1182     at->address = (vm_offset_t)address;
1183     at->pte_array = pte_array;
1184
1185     for (i = 0; i < count; i++) {
1186         pte_array[i] = (NvU64)vtophys(at->address + (i * PAGE_SIZE));
1187         vm_page_wire(PHYS_TO_VM_PAGE(pte_array[i]));
1188     }
1189
1190     *private = at;
1191     SLIST_INSERT_HEAD(&sc->alloc_list, at, list);
1192
1193     return 0;
1194 }
1195
1196 S032 nv_free_system_pages(
1197     nv_state_t *nv,
1198     void *private
1199 )
1200 {
1201     struct nvidia_alloc *at = private;
1202     struct nvidia_softc *sc = nv->os_state;
1203     u_int32_t i, count;
1204
1205     count = at->size / PAGE_SIZE;
1206     SLIST_REMOVE(&sc->alloc_list, at, nvidia_alloc, list);
1207
1208     for (i = 0; i < count; i++) {
1209         vm_page_unwire(PHYS_TO_VM_PAGE(at->pte_array[i]), 0);
1210     }
1211
1212     if (at->cache_type != NV_MEMORY_CACHED)
1213         pmap_change_attr(at->address, at->size, PAT_WRITE_BACK);
1214
1215     free((void *)at->address, M_NVIDIA);
1216     free(at, M_NVIDIA);
1217
1218     return 0;
1219 }
1220
1221 #ifdef NV_SUPPORT_OS_AGP
1222 S032 nv_alloc_agp_pages(
1223     nv_state_t *nv,
1224     U032 count,
1225     U032 offset,
1226     void **private
1227 )
1228 {
1229     void *handle;
1230     struct nvidia_softc *sc = nv->os_state;
1231
1232     handle = agp_alloc_memory(sc->agp_dev, 0, count << PAGE_SHIFT);
1233     if (!handle) {
1234         /*
1235          * This is very unlikely to happen, the system's memory resources
1236          * would have to be nearly exhausted.
1237          */
1238         return -ENOMEM;
1239     }
1240
1241     if (agp_bind_memory(sc->agp_dev, handle, offset) != 0) {
1242         /*
1243          * This shouldn't happen, we claimed the AGP backend and are thus
1244          * using it exclusively; the resource manager manages AGP offsets
1245          * internally, we wouldn't have been called had we run out of AGP
1246          * aperture space.
1247          */
1248         os_dbg_breakpoint();
1249
1250         agp_free_memory(sc->agp_dev, handle);
1251         return -ENOMEM;
1252     }
1253
1254     *private = handle;
1255     return 0;
1256 }
1257
1258 S032 nv_free_agp_pages(
1259     nv_state_t *nv,
1260     U032 count,
1261     void *private,
1262     U032 *offset
1263 )
1264 {
1265     void *handle = private;
1266     struct nvidia_softc *sc = nv->os_state;
1267     struct agp_memory_info info;
1268
1269     agp_memory_info(sc->agp_dev, handle, &info);
1270     *offset = info.ami_offset;
1271
1272     if (agp_unbind_memory(sc->agp_dev, handle) != 0) {
1273         /*
1274          * This is the only place where previously bound AGP memory would
1275          * be freed. If we fail to unbind this memory now, something very
1276          * wrong must have happened.
1277          */
1278         os_dbg_breakpoint();
1279     }
1280
1281     agp_free_memory(sc->agp_dev, handle);
1282     return 0;
1283 }
1284 #endif /* NV_SUPPORT_OS_AGP */
1285
1286
1287 RM_STATUS NV_API_CALL nv_alloc_pages(
1288     nv_state_t *nv,
1289     U032 count,
1290     U032 alloc_type_agp,
1291     U032 alloc_type_contiguous,
1292     U032 cache_type,
1293     NvU64 *pte_array,
1294     void **private
1295 )
1296 {
1297     U032 offset;
1298     RM_STATUS status = RM_ERR_NO_FREE_MEM;
1299     nv_stack_t *sp = NULL;
1300
1301     if (alloc_type_agp) {
1302         if (!NV_AGP_ENABLED(nv))
1303             return RM_ERR_NOT_SUPPORTED;
1304
1305         NV_UMA_ZONE_ALLOC_STACK(sp);
1306         if (sp == NULL)
1307             return RM_ERR_NO_FREE_MEM;
1308
1309 #ifdef NV_SUPPORT_OS_AGP
1310         if (NV_OSAGP_ENABLED(nv)) {
1311             status = rm_alloc_agp_bitmap(sp, nv, count, &offset);
1312             if (status != RM_OK)
1313                 goto failed;
1314
1315             if (nv_alloc_agp_pages(nv, count, (offset << PAGE_SHIFT),
1316                     private) != 0) {
1317                 rm_free_agp_bitmap(sp, nv, count, offset);
1318                 goto failed;
1319             }
1320
1321             NV_UMA_ZONE_FREE_STACK(sp);
1322
1323             pte_array[0] = (nv->agp.address + (offset << PAGE_SHIFT));
1324             return RM_OK;
1325         }
1326 #endif /* NV_SUPPORT_OS_AGP */
1327
1328         if (NV_NVAGP_ENABLED(nv)) {
1329             status = rm_alloc_agp_pages(sp, nv, count, private, &offset);
1330             if (status != RM_OK)
1331                 goto failed;
1332
1333             NV_UMA_ZONE_FREE_STACK(sp);
1334
1335             pte_array[0] = (nv->agp.address + (offset << PAGE_SHIFT));
1336             return RM_OK;
1337         }
1338     } else {
1339         /* XXX Fix me! (PAT) */
1340         if (cache_type == NV_MEMORY_WRITECOMBINED) {
1341             status = RM_ERR_NOT_SUPPORTED;
1342             goto failed;
1343         }
1344
1345         if (!alloc_type_contiguous) {
1346             if (nv_alloc_system_pages(nv, count, cache_type, pte_array, private))
1347                 goto failed;
1348         } else {
1349             if (nv_alloc_contig_pages(nv, count, cache_type, pte_array, private))
1350                 goto failed;
1351         }
1352
1353         return RM_OK;
1354     }
1355
1356 failed:
1357     if (sp != NULL)
1358         NV_UMA_ZONE_FREE_STACK(sp);
1359
1360     return status;
1361 }
1362
1363 RM_STATUS NV_API_CALL nv_free_pages(
1364     nv_state_t *nv,
1365     U032 count,
1366     U032 alloc_type_agp,
1367     U032 alloc_type_contiguous,
1368     U032 cache_type,
1369     void *private
1370 )
1371 {
1372     RM_STATUS status = RM_ERROR;
1373     nv_stack_t *sp = NULL;
1374
1375     if (alloc_type_agp) {
1376         if (!NV_AGP_ENABLED(nv))
1377             return RM_ERR_NOT_SUPPORTED;
1378
1379         NV_UMA_ZONE_ALLOC_STACK(sp);
1380         if (sp == NULL)
1381             return RM_ERR_NO_FREE_MEM;
1382
1383 #ifdef NV_SUPPORT_OS_AGP
1384         if (NV_OSAGP_ENABLED(nv)) {
1385             U032 offset;
1386
1387             if (nv_free_agp_pages(nv, count, private, &offset) != 0)
1388                 goto failed;
1389
1390             rm_free_agp_bitmap(sp, nv, count, (offset >> PAGE_SHIFT));
1391             NV_UMA_ZONE_FREE_STACK(sp);
1392
1393             return RM_OK;
1394         }
1395 #endif /* NV_SUPPORT_OS_AGP */
1396
1397         if (NV_NVAGP_ENABLED(nv)) {
1398             if (rm_free_agp_pages(sp, nv, private) != RM_OK)
1399                 goto failed;
1400         }
1401
1402         NV_UMA_ZONE_FREE_STACK(sp);
1403     } else {
1404         if (!alloc_type_contiguous) {
1405             if (nv_free_system_pages(nv, private))
1406                 goto failed;
1407         } else  {
1408             if (nv_free_contig_pages(nv, private))
1409                 goto failed;
1410         }
1411     }
1412
1413     return RM_OK;
1414
1415 failed:
1416     if (sp != NULL)
1417         NV_UMA_ZONE_FREE_STACK(sp);
1418
1419     return status;
1420 }
1421
1422 NvU64 NV_API_CALL nv_dma_to_mmap_token(
1423     nv_state_t *nv,
1424     NvU64 address
1425 )
1426 {
1427     struct nvidia_alloc *at;
1428     struct nvidia_softc *sc = nv->os_state;
1429     vm_offset_t offset, linear;
1430     uint32_t i;
1431
1432     offset = (vm_offset_t)address & PAGE_MASK;
1433     address &= ~PAGE_MASK;
1434
1435     /*
1436      * XXX FreeBSD doesn't currently allow the use of physical
1437      * addresses as mmap(2) tokens, a linear address range
1438      * derived from the allocation's contiguous kernel mapping
1439      * is used, instead.
1440      */
1441     SLIST_FOREACH(at, &sc->alloc_list, list) {
1442         for (i = 0; i < (at->size / PAGE_SIZE); i++) {
1443             if ((!at->alloc_type_contiguous &&
1444                         (address == (NvU64)(NvUPtr)at->pte_array[i]))
1445                   || (address == (NvU64)(NvUPtr)at->pte_array[0] + (i * PAGE_SIZE))) {
1446                 linear = at->address + (i * PAGE_SIZE);
1447                 return NV_VM_TO_MMAP_OFFSET(linear + offset);
1448             }
1449         }
1450     }
1451
1452     return 0;
1453 }
1454
1455
1456 NvU64 NV_API_CALL nv_get_kern_phys_address(NvU64 address)
1457 {
1458     vm_offset_t va = (vm_offset_t) address;
1459
1460 #if defined(NVCPU_X86_64)
1461     if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
1462         return DMAP_TO_PHYS(va);
1463 #endif
1464
1465     if (va < KERNBASE) {
1466         os_dbg_breakpoint();
1467         return 0;
1468     }
1469
1470     return vtophys(va);
1471 }
1472
1473 NvU64 NV_API_CALL nv_get_user_phys_address(NvU64 address)
1474 {
1475     struct vmspace *vm;
1476     vm_offset_t va = (vm_offset_t) address;
1477
1478     if (va >= KERNBASE) {
1479         os_dbg_breakpoint();
1480         return 0;
1481     }
1482
1483     /* if (vm_fault_quick((caddr_t) va, VM_PROT_WRITE))
1484         return 0; */
1485
1486     vm = curproc->p_vmspace;
1487     return pmap_extract(vmspace_pmap(vm), va);
1488 }
1489
1490
1491 int nvidia_mmap_dev(
1492     struct nvidia_softc *sc,
1493     vm_offset_t offset,
1494     vm_offset_t *physical
1495 )
1496 {
1497     struct nvidia_alloc *at;
1498     nv_state_t *nv = sc->nv_state;
1499     nv_stack_t *sp;
1500
1501     sp = sc->api_sp;
1502
1503     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
1504
1505     /*
1506      * Offsets that fall into the frame buffer, registry or AGP
1507      * apertures are physical addresses and mapped into userspace
1508      * directly.
1509      */
1510     if (IS_FB_OFFSET(nv, offset, PAGE_SIZE) ||
1511             IS_BC_OFFSET(nv, offset, PAGE_SIZE)) {
1512         *physical = offset;
1513         return 0;
1514     }
1515
1516     if (IS_REG_OFFSET(nv, offset, PAGE_SIZE)) {
1517         *physical = offset;
1518         return 0;
1519     }
1520
1521     if (IS_AGP_OFFSET(nv, offset, PAGE_SIZE)) {
1522         *physical = offset;
1523         return 0;
1524     }
1525
1526     offset = NV_MMAP_TO_VM_OFFSET(offset);
1527
1528     SLIST_FOREACH(at, &sc->alloc_list, list) {
1529         if (offset >= at->address &&
1530                 offset < at->address + at->size) {
1531             *physical = vtophys(offset);
1532             return 0;
1533         }
1534     }
1535
1536     return -1;
1537 }
1538
1539 void nvidia_rc_timer(void *data)
1540 {
1541     nv_state_t *nv = data;
1542     struct nvidia_softc *sc = nv->os_state;
1543     nv_stack_t *sp;
1544
1545     sp = sc->timer_sp;
1546
1547     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, FALSE);
1548
1549     /*
1550      * We need this timer to trigger again one second from
1551      * now, reset the timeout.
1552      */
1553     rm_run_rc_callback(sp, nv);
1554
1555     callout_reset(&sc->timer_ch, hz, nvidia_rc_timer, (void *) nv);
1556 }
1557
1558 int NV_API_CALL nv_start_rc_timer(
1559     nv_state_t *nv
1560 )
1561 {
1562     struct nvidia_softc *sc = nv->os_state;
1563
1564     if (nv->rc_timer_enabled != 0)
1565         return -EIO;
1566
1567     callout_reset(&sc->timer_ch, hz, nvidia_rc_timer, (void *) nv);
1568     nv->rc_timer_enabled = 1;
1569
1570     return 0;
1571 }
1572
1573 int NV_API_CALL nv_stop_rc_timer(
1574     nv_state_t *nv
1575 )
1576 {
1577     struct nvidia_softc *sc = nv->os_state;
1578
1579     if (nv->rc_timer_enabled == 0)
1580         return -EIO;
1581
1582     callout_stop(&sc->timer_ch);
1583     nv->rc_timer_enabled = 0;
1584
1585     return 0;
1586 }
1587
1588 void NV_API_CALL nv_set_dma_address_size(
1589     nv_state_t *nv,
1590     U032 phys_addr_bits
1591 )
1592 {
1593 }
1594
1595 void* NV_API_CALL nv_get_adapter_state(
1596     U016 bus,
1597     U016 slot
1598 )
1599 {
1600     unsigned int i;
1601     struct nvidia_softc *sc;
1602     nv_state_t *nv;
1603
1604     for (i = 0; i < NV_MAX_DEVICES; i++) {
1605         sc = devclass_get_softc(nvidia_devclass, i);
1606         if (!sc)
1607             continue;
1608         nv = sc->nv_state;
1609
1610         if (nv->bus == bus && nv->slot == slot)
1611             return (void *) nv;
1612     }
1613
1614     if (bus == 255 && slot == 255) {
1615         nv = &nvidia_ctl_state;
1616         return (void *) nv;
1617     }
1618
1619     return NULL;
1620 }
1621
1622 void NV_API_CALL nv_verify_pci_config(
1623     nv_state_t *nv,
1624     BOOL check_the_bars
1625 )
1626 {
1627     nv_stack_t *sp;
1628     struct nvidia_softc *sc = nv->os_state;
1629
1630     sp = sc->pci_cfgchk_sp;
1631
1632     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, check_the_bars, FALSE, FALSE);
1633 }
1634
1635 void NV_API_CALL nv_acpi_methods_init(U032 *handlesPresent)
1636 {
1637     *handlesPresent = 0;
1638 }
1639
1640 void NV_API_CALL nv_acpi_methods_uninit(void)
1641 {
1642     return;
1643 }
1644
1645 RM_STATUS NV_API_CALL nv_acpi_method(
1646     U032 acpi_method,
1647     U032 function,
1648     U032 subFunction,
1649     void *inParams,
1650     U016 inParamSize,
1651     U032 *outStatus,
1652     void *outData,
1653     U016 *outDataSize
1654 )
1655 {
1656     return RM_ERR_NOT_SUPPORTED;
1657 }