Merge branch 'upstream'
[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     BOOL guest_mapping
1072 )
1073 {
1074     /* There's nothing to be done here. */
1075     return RM_OK;
1076 }
1077
1078 S032 nv_alloc_contig_pages(
1079     nv_state_t *nv,
1080     U032 count,
1081     U032 cache_type,
1082     NvU64 *pte_array,
1083     void **private
1084 )
1085 {
1086     struct nvidia_alloc *at;
1087     struct nvidia_softc *sc = nv->os_state;
1088     void *address;
1089     U032 size = count * PAGE_SIZE;
1090     int status;
1091
1092     if (os_alloc_contig_pages(&address, size) != RM_OK)
1093         return -ENOMEM;
1094
1095     at = kmalloc(sizeof(struct nvidia_alloc), M_NVIDIA, M_WAITOK | M_ZERO);
1096     if (!at) {
1097         os_free_contig_pages(address, size);
1098         return -ENOMEM;
1099     }
1100
1101     if (cache_type != NV_MEMORY_CACHED) {
1102         status = pmap_change_attr((vm_offset_t)address, size, PAT_UNCACHEABLE);
1103         if (status != 0) {
1104             free(at, M_NVIDIA);
1105             os_free_contig_pages(address, size);
1106             return status;
1107         }
1108     }
1109
1110     at->alloc_type_contiguous = 1;
1111
1112     at->cache_type = cache_type;
1113     at->size = size;
1114     at->address = (vm_offset_t)address;
1115     at->pte_array = pte_array;
1116
1117     pte_array[0] = vtophys(at->address);
1118
1119     *private = at;
1120     SLIST_INSERT_HEAD(&sc->alloc_list, at, list);
1121
1122     return 0;
1123 }
1124
1125 S032 nv_free_contig_pages(
1126     nv_state_t *nv,
1127     void *private
1128 )
1129 {
1130     struct nvidia_alloc *at = private;
1131     struct nvidia_softc *sc = nv->os_state;
1132
1133     SLIST_REMOVE(&sc->alloc_list, at, nvidia_alloc, list);
1134
1135     if (at->cache_type != NV_MEMORY_CACHED)
1136         pmap_change_attr(at->address, at->size, PAT_WRITE_BACK);
1137
1138     os_free_contig_pages((void *)at->address, at->size);
1139     free(at, M_NVIDIA);
1140
1141     return 0;
1142 }
1143
1144 S032 nv_alloc_system_pages(
1145     nv_state_t  *nv,
1146     U032 count,
1147     U032 cache_type,
1148     NvU64 *pte_array,
1149     void **private
1150 )
1151 {
1152     struct nvidia_alloc *at;
1153     struct nvidia_softc *sc = nv->os_state;
1154     void *address;
1155     u_int32_t i, size;
1156     int status;
1157
1158     size = count * PAGE_SIZE;
1159     at = kmalloc(sizeof(struct nvidia_alloc), M_NVIDIA, M_WAITOK | M_ZERO);
1160     if (!at) {
1161         return -ENOMEM;
1162     }
1163
1164     address = kmalloc(size, M_NVIDIA, M_WAITOK | M_ZERO);
1165     if (!address) {
1166         free(at, M_NVIDIA);
1167         return -ENOMEM;
1168     }
1169
1170     if (cache_type != NV_MEMORY_CACHED) {
1171         status = pmap_change_attr((vm_offset_t)address, size, PAT_UNCACHEABLE);
1172         if (status != 0) {
1173             free(at, M_NVIDIA);
1174             free(address, M_NVIDIA);
1175             return status;
1176         }
1177     }
1178
1179     at->alloc_type_contiguous = 0;
1180
1181     at->cache_type = cache_type;
1182     at->size = size;
1183     at->address = (vm_offset_t)address;
1184     at->pte_array = pte_array;
1185
1186     for (i = 0; i < count; i++) {
1187         pte_array[i] = (NvU64)vtophys(at->address + (i * PAGE_SIZE));
1188         vm_page_wire(PHYS_TO_VM_PAGE(pte_array[i]));
1189     }
1190
1191     *private = at;
1192     SLIST_INSERT_HEAD(&sc->alloc_list, at, list);
1193
1194     return 0;
1195 }
1196
1197 S032 nv_free_system_pages(
1198     nv_state_t *nv,
1199     void *private
1200 )
1201 {
1202     struct nvidia_alloc *at = private;
1203     struct nvidia_softc *sc = nv->os_state;
1204     u_int32_t i, count;
1205
1206     count = at->size / PAGE_SIZE;
1207     SLIST_REMOVE(&sc->alloc_list, at, nvidia_alloc, list);
1208
1209     for (i = 0; i < count; i++) {
1210         vm_page_unwire(PHYS_TO_VM_PAGE(at->pte_array[i]), 0);
1211     }
1212
1213     if (at->cache_type != NV_MEMORY_CACHED)
1214         pmap_change_attr(at->address, at->size, PAT_WRITE_BACK);
1215
1216     free((void *)at->address, M_NVIDIA);
1217     free(at, M_NVIDIA);
1218
1219     return 0;
1220 }
1221
1222 #ifdef NV_SUPPORT_OS_AGP
1223 S032 nv_alloc_agp_pages(
1224     nv_state_t *nv,
1225     U032 count,
1226     U032 offset,
1227     void **private
1228 )
1229 {
1230     void *handle;
1231     struct nvidia_softc *sc = nv->os_state;
1232
1233     handle = agp_alloc_memory(sc->agp_dev, 0, count << PAGE_SHIFT);
1234     if (!handle) {
1235         /*
1236          * This is very unlikely to happen, the system's memory resources
1237          * would have to be nearly exhausted.
1238          */
1239         return -ENOMEM;
1240     }
1241
1242     if (agp_bind_memory(sc->agp_dev, handle, offset) != 0) {
1243         /*
1244          * This shouldn't happen, we claimed the AGP backend and are thus
1245          * using it exclusively; the resource manager manages AGP offsets
1246          * internally, we wouldn't have been called had we run out of AGP
1247          * aperture space.
1248          */
1249         os_dbg_breakpoint();
1250
1251         agp_free_memory(sc->agp_dev, handle);
1252         return -ENOMEM;
1253     }
1254
1255     *private = handle;
1256     return 0;
1257 }
1258
1259 S032 nv_free_agp_pages(
1260     nv_state_t *nv,
1261     U032 count,
1262     void *private,
1263     U032 *offset
1264 )
1265 {
1266     void *handle = private;
1267     struct nvidia_softc *sc = nv->os_state;
1268     struct agp_memory_info info;
1269
1270     agp_memory_info(sc->agp_dev, handle, &info);
1271     *offset = info.ami_offset;
1272
1273     if (agp_unbind_memory(sc->agp_dev, handle) != 0) {
1274         /*
1275          * This is the only place where previously bound AGP memory would
1276          * be freed. If we fail to unbind this memory now, something very
1277          * wrong must have happened.
1278          */
1279         os_dbg_breakpoint();
1280     }
1281
1282     agp_free_memory(sc->agp_dev, handle);
1283     return 0;
1284 }
1285 #endif /* NV_SUPPORT_OS_AGP */
1286
1287 RM_STATUS NV_API_CALL nv_alias_pages(
1288     nv_state_t *nv,
1289     U032 page_cnt,
1290     U032 contiguous,
1291     U032 cache_type,
1292     NvU64 guest_id,
1293     NvU64 *pte_array,
1294     void **priv_data
1295 )
1296 {
1297     return RM_ERR_NOT_SUPPORTED;
1298 }
1299
1300 RM_STATUS NV_API_CALL nv_guest_pfn_list(
1301     nv_state_t  *nv,
1302     unsigned int key,
1303     unsigned int pfn_count,
1304     unsigned int offset_index,
1305     unsigned int *user_pfn_list
1306 )
1307 {
1308     return RM_ERR_NOT_SUPPORTED;
1309 }
1310
1311 RM_STATUS NV_API_CALL nv_alloc_pages(
1312     nv_state_t *nv,
1313     U032 count,
1314     U032 alloc_type_agp,
1315     U032 alloc_type_contiguous,
1316     U032 cache_type,
1317     NvU64 *pte_array,
1318     void **private
1319 )
1320 {
1321     U032 offset;
1322     RM_STATUS status = RM_ERR_NO_FREE_MEM;
1323     nv_stack_t *sp = NULL;
1324
1325     if (alloc_type_agp) {
1326         if (!NV_AGP_ENABLED(nv))
1327             return RM_ERR_NOT_SUPPORTED;
1328
1329         NV_UMA_ZONE_ALLOC_STACK(sp);
1330         if (sp == NULL)
1331             return RM_ERR_NO_FREE_MEM;
1332
1333 #ifdef NV_SUPPORT_OS_AGP
1334         if (NV_OSAGP_ENABLED(nv)) {
1335             status = rm_alloc_agp_bitmap(sp, nv, count, &offset);
1336             if (status != RM_OK)
1337                 goto failed;
1338
1339             if (nv_alloc_agp_pages(nv, count, (offset << PAGE_SHIFT),
1340                     private) != 0) {
1341                 rm_free_agp_bitmap(sp, nv, count, offset);
1342                 goto failed;
1343             }
1344
1345             NV_UMA_ZONE_FREE_STACK(sp);
1346
1347             pte_array[0] = (nv->agp.address + (offset << PAGE_SHIFT));
1348             return RM_OK;
1349         }
1350 #endif /* NV_SUPPORT_OS_AGP */
1351
1352         if (NV_NVAGP_ENABLED(nv)) {
1353             status = rm_alloc_agp_pages(sp, nv, count, private, &offset);
1354             if (status != RM_OK)
1355                 goto failed;
1356
1357             NV_UMA_ZONE_FREE_STACK(sp);
1358
1359             pte_array[0] = (nv->agp.address + (offset << PAGE_SHIFT));
1360             return RM_OK;
1361         }
1362     } else {
1363         /* XXX Fix me! (PAT) */
1364         if (cache_type == NV_MEMORY_WRITECOMBINED) {
1365             status = RM_ERR_NOT_SUPPORTED;
1366             goto failed;
1367         }
1368
1369         if (!alloc_type_contiguous) {
1370             if (nv_alloc_system_pages(nv, count, cache_type, pte_array, private))
1371                 goto failed;
1372         } else {
1373             if (nv_alloc_contig_pages(nv, count, cache_type, pte_array, private))
1374                 goto failed;
1375         }
1376
1377         return RM_OK;
1378     }
1379
1380 failed:
1381     if (sp != NULL)
1382         NV_UMA_ZONE_FREE_STACK(sp);
1383
1384     return status;
1385 }
1386
1387 RM_STATUS NV_API_CALL nv_free_pages(
1388     nv_state_t *nv,
1389     U032 count,
1390     U032 alloc_type_agp,
1391     U032 alloc_type_contiguous,
1392     U032 cache_type,
1393     void *private
1394 )
1395 {
1396     RM_STATUS status = RM_ERROR;
1397     nv_stack_t *sp = NULL;
1398
1399     if (alloc_type_agp) {
1400         if (!NV_AGP_ENABLED(nv))
1401             return RM_ERR_NOT_SUPPORTED;
1402
1403         NV_UMA_ZONE_ALLOC_STACK(sp);
1404         if (sp == NULL)
1405             return RM_ERR_NO_FREE_MEM;
1406
1407 #ifdef NV_SUPPORT_OS_AGP
1408         if (NV_OSAGP_ENABLED(nv)) {
1409             U032 offset;
1410
1411             if (nv_free_agp_pages(nv, count, private, &offset) != 0)
1412                 goto failed;
1413
1414             rm_free_agp_bitmap(sp, nv, count, (offset >> PAGE_SHIFT));
1415             NV_UMA_ZONE_FREE_STACK(sp);
1416
1417             return RM_OK;
1418         }
1419 #endif /* NV_SUPPORT_OS_AGP */
1420
1421         if (NV_NVAGP_ENABLED(nv)) {
1422             if (rm_free_agp_pages(sp, nv, private) != RM_OK)
1423                 goto failed;
1424         }
1425
1426         NV_UMA_ZONE_FREE_STACK(sp);
1427     } else {
1428         if (!alloc_type_contiguous) {
1429             if (nv_free_system_pages(nv, private))
1430                 goto failed;
1431         } else  {
1432             if (nv_free_contig_pages(nv, private))
1433                 goto failed;
1434         }
1435     }
1436
1437     return RM_OK;
1438
1439 failed:
1440     if (sp != NULL)
1441         NV_UMA_ZONE_FREE_STACK(sp);
1442
1443     return status;
1444 }
1445
1446 NvU64 NV_API_CALL nv_dma_to_mmap_token(
1447     nv_state_t *nv,
1448     NvU64 address
1449 )
1450 {
1451     struct nvidia_alloc *at;
1452     struct nvidia_softc *sc = nv->os_state;
1453     vm_offset_t offset, linear;
1454     uint32_t i;
1455
1456     offset = (vm_offset_t)address & PAGE_MASK;
1457     address &= ~PAGE_MASK;
1458
1459     /*
1460      * XXX FreeBSD doesn't currently allow the use of physical
1461      * addresses as mmap(2) tokens, a linear address range
1462      * derived from the allocation's contiguous kernel mapping
1463      * is used, instead.
1464      */
1465     SLIST_FOREACH(at, &sc->alloc_list, list) {
1466         for (i = 0; i < (at->size / PAGE_SIZE); i++) {
1467             if ((!at->alloc_type_contiguous &&
1468                         (address == (NvU64)(NvUPtr)at->pte_array[i]))
1469                   || (address == (NvU64)(NvUPtr)at->pte_array[0] + (i * PAGE_SIZE))) {
1470                 linear = at->address + (i * PAGE_SIZE);
1471                 return NV_VM_TO_MMAP_OFFSET(linear + offset);
1472             }
1473         }
1474     }
1475
1476     return 0;
1477 }
1478
1479
1480 NvU64 NV_API_CALL nv_get_kern_phys_address(NvU64 address)
1481 {
1482     vm_offset_t va = (vm_offset_t) address;
1483
1484 #if defined(NVCPU_X86_64)
1485     if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
1486         return DMAP_TO_PHYS(va);
1487 #endif
1488
1489     if (va < KERNBASE) {
1490         os_dbg_breakpoint();
1491         return 0;
1492     }
1493
1494     return vtophys(va);
1495 }
1496
1497 NvU64 NV_API_CALL nv_get_user_phys_address(NvU64 address)
1498 {
1499     struct vmspace *vm;
1500     vm_offset_t va = (vm_offset_t) address;
1501
1502     if (va >= KERNBASE) {
1503         os_dbg_breakpoint();
1504         return 0;
1505     }
1506
1507     /* if (vm_fault_quick((caddr_t) va, VM_PROT_WRITE))
1508         return 0; */
1509
1510     vm = curproc->p_vmspace;
1511     return pmap_extract(vmspace_pmap(vm), va);
1512 }
1513
1514
1515 int nvidia_mmap_dev(
1516     struct nvidia_softc *sc,
1517     vm_offset_t offset,
1518     vm_offset_t *physical
1519 )
1520 {
1521     struct nvidia_alloc *at;
1522     nv_state_t *nv = sc->nv_state;
1523     nv_stack_t *sp;
1524
1525     sp = sc->api_sp;
1526
1527     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, TRUE);
1528
1529     /*
1530      * Offsets that fall into the frame buffer, registry or AGP
1531      * apertures are physical addresses and mapped into userspace
1532      * directly.
1533      */
1534     if (IS_FB_OFFSET(nv, offset, PAGE_SIZE) ||
1535             IS_BC_OFFSET(nv, offset, PAGE_SIZE)) {
1536         *physical = offset;
1537         return 0;
1538     }
1539
1540     if (IS_REG_OFFSET(nv, offset, PAGE_SIZE)) {
1541         *physical = offset;
1542         return 0;
1543     }
1544
1545     if (IS_AGP_OFFSET(nv, offset, PAGE_SIZE)) {
1546         *physical = offset;
1547         return 0;
1548     }
1549
1550     offset = NV_MMAP_TO_VM_OFFSET(offset);
1551
1552     SLIST_FOREACH(at, &sc->alloc_list, list) {
1553         if (offset >= at->address &&
1554                 offset < at->address + at->size) {
1555             *physical = vtophys(offset);
1556             return 0;
1557         }
1558     }
1559
1560     return -1;
1561 }
1562
1563 void nvidia_rc_timer(void *data)
1564 {
1565     nv_state_t *nv = data;
1566     struct nvidia_softc *sc = nv->os_state;
1567     nv_stack_t *sp;
1568
1569     sp = sc->timer_sp;
1570
1571     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, TRUE, TRUE, FALSE);
1572
1573     /*
1574      * We need this timer to trigger again one second from
1575      * now, reset the timeout.
1576      */
1577     rm_run_rc_callback(sp, nv);
1578
1579     callout_reset(&sc->timer_ch, hz, nvidia_rc_timer, (void *) nv);
1580 }
1581
1582 int NV_API_CALL nv_start_rc_timer(
1583     nv_state_t *nv
1584 )
1585 {
1586     struct nvidia_softc *sc = nv->os_state;
1587
1588     if (nv->rc_timer_enabled != 0)
1589         return -EIO;
1590
1591     callout_reset(&sc->timer_ch, hz, nvidia_rc_timer, (void *) nv);
1592     nv->rc_timer_enabled = 1;
1593
1594     return 0;
1595 }
1596
1597 int NV_API_CALL nv_stop_rc_timer(
1598     nv_state_t *nv
1599 )
1600 {
1601     struct nvidia_softc *sc = nv->os_state;
1602
1603     if (nv->rc_timer_enabled == 0)
1604         return -EIO;
1605
1606     callout_stop(&sc->timer_ch);
1607     nv->rc_timer_enabled = 0;
1608
1609     return 0;
1610 }
1611
1612 void NV_API_CALL nv_set_dma_address_size(
1613     nv_state_t *nv,
1614     U032 phys_addr_bits
1615 )
1616 {
1617 }
1618
1619 void* NV_API_CALL nv_get_adapter_state(
1620     U016 bus,
1621     U016 slot
1622 )
1623 {
1624     unsigned int i;
1625     struct nvidia_softc *sc;
1626     nv_state_t *nv;
1627
1628     for (i = 0; i < NV_MAX_DEVICES; i++) {
1629         sc = devclass_get_softc(nvidia_devclass, i);
1630         if (!sc)
1631             continue;
1632         nv = sc->nv_state;
1633
1634         if (nv->bus == bus && nv->slot == slot)
1635             return (void *) nv;
1636     }
1637
1638     if (bus == 255 && slot == 255) {
1639         nv = &nvidia_ctl_state;
1640         return (void *) nv;
1641     }
1642
1643     return NULL;
1644 }
1645
1646 void NV_API_CALL nv_verify_pci_config(
1647     nv_state_t *nv,
1648     BOOL check_the_bars
1649 )
1650 {
1651     nv_stack_t *sp;
1652     struct nvidia_softc *sc = nv->os_state;
1653
1654     sp = sc->pci_cfgchk_sp;
1655
1656     NV_PCI_CHECK_CONFIG_SPACE(sp, nv, check_the_bars, FALSE, FALSE);
1657 }
1658
1659 void NV_API_CALL nv_acpi_methods_init(U032 *handlesPresent)
1660 {
1661     *handlesPresent = 0;
1662 }
1663
1664 void NV_API_CALL nv_acpi_methods_uninit(void)
1665 {
1666     return;
1667 }
1668
1669 RM_STATUS NV_API_CALL nv_acpi_method(
1670     U032 acpi_method,
1671     U032 function,
1672     U032 subFunction,
1673     void *inParams,
1674     U016 inParamSize,
1675     U032 *outStatus,
1676     void *outData,
1677     U016 *outDataSize
1678 )
1679 {
1680     return RM_ERR_NOT_SUPPORTED;
1681 }
1682
1683 void* NV_API_CALL nv_get_smu_state(void)
1684 {
1685     return NULL;
1686 }
1687