Add some random locks in pager operations
[pscnv.git] / pscnv / nouveau_bsddrv.c
blobf887fbb403277c5608cf9880ec02adfdf3dc9a47
1 /*
2 * Copyright 2005 Stephane Marchesin.
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 #include "nouveau_drv.h"
26 #include "drm.h"
27 #include "drm_crtc_helper.h"
28 #include "nouveau_pm.h"
29 #include "pscnv_gem.h"
30 #include "pscnv_vm.h"
31 #include "pscnv_chan.h"
32 #if 0
33 #include "nouveau_hw.h"
34 #include "nouveau_fb.h"
35 #include "nouveau_fbcon.h"
36 #include "nv50_display.h"
37 #endif
39 #include "drm_pciids.h"
40 #include "pscnv_ioctl.h"
41 #include "pscnv_kapi.h"
42 #include "device_if.h"
44 #define MODULE_PARM_DESC(a, b)
45 #define module_param_named(name, where, type, mode) TUNABLE_INT("drm.pscnv." #name, &where)
46 #define module_param_str(name, where) TUNABLE_STR("drm.pscnv." #name , where, sizeof(where))
48 MODULE_PARM_DESC(agpmode, "AGP mode (0 to disable AGP)");
49 int nouveau_agpmode = -1;
50 module_param_named(agpmode, nouveau_agpmode, int, 0400);
52 MODULE_PARM_DESC(modeset, "Enable kernel modesetting");
53 static int nouveau_modeset = 1; /* kms */
54 module_param_named(modeset, nouveau_modeset, int, 0400);
56 MODULE_PARM_DESC(vbios, "Override default VBIOS location");
57 char nouveau_vbios_array[32];
58 char *nouveau_vbios = nouveau_vbios_array;
59 module_param_str(vbios, nouveau_vbios_array);
61 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (>=GeForce 8)");
62 int nouveau_duallink = 1;
63 module_param_named(duallink, nouveau_duallink, int, 0400);
65 MODULE_PARM_DESC(uscript_lvds, "LVDS output script table ID (>=GeForce 8)");
66 int nouveau_uscript_lvds = -1;
67 module_param_named(uscript_lvds, nouveau_uscript_lvds, int, 0400);
69 MODULE_PARM_DESC(uscript_tmds, "TMDS output script table ID (>=GeForce 8)");
70 int nouveau_uscript_tmds = -1;
71 module_param_named(uscript_tmds, nouveau_uscript_tmds, int, 0400);
73 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
74 int nouveau_ignorelid = 0;
75 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
77 MODULE_PARM_DESC(noaccel, "Disable all acceleration");
78 int nouveau_noaccel = 0;
79 module_param_named(noaccel, nouveau_noaccel, int, 0400);
81 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
82 int nouveau_nofbaccel = 0;
83 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
85 MODULE_PARM_DESC(force_post, "Force POST");
86 int nouveau_force_post = 0;
87 module_param_named(force_post, nouveau_force_post, int, 0400);
89 MODULE_PARM_DESC(override_conntype, "Ignore DCB connector type");
90 int nouveau_override_conntype = 0;
91 module_param_named(override_conntype, nouveau_override_conntype, int, 0400);
93 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection\n");
94 int nouveau_tv_disable = 0;
95 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
97 MODULE_PARM_DESC(reg_debug, "Register access debug bitmask:\n"
98 "\t\t0x1 mc, 0x2 video, 0x4 fb, 0x8 extdev,\n"
99 "\t\t0x10 crtc, 0x20 ramdac, 0x40 vgacrtc, 0x80 rmvio,\n"
100 "\t\t0x100 vgaattr, 0x200 EVO (G80+). ");
101 int nouveau_reg_debug;
102 module_param_named(reg_debug, nouveau_reg_debug, int, 0600);
104 MODULE_PARM_DESC(perflvl, "Performance level (default: boot)\n");
105 static char nouveau_perflvl_array[32];
106 char *nouveau_perflvl = nouveau_perflvl_array;
107 module_param_str(perflvl, nouveau_perflvl_array);
109 MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)\n");
110 int nouveau_perflvl_wr;
111 module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
113 MODULE_PARM_DESC(mm_debug, "mm debug level: 0-2.");
114 int pscnv_mm_debug = 0;
115 module_param_named(mm_debug, pscnv_mm_debug, int, 0400);
117 MODULE_PARM_DESC(mem_debug, "memory debug level: 0-1.");
118 int pscnv_mem_debug = 1;
119 module_param_named(mem_debug, pscnv_mem_debug, int, 0400);
121 MODULE_PARM_DESC(vm_debug, "VM debug level: 0-2.");
122 int pscnv_vm_debug = 1;
123 module_param_named(vm_debug, pscnv_vm_debug, int, 0400);
125 MODULE_PARM_DESC(ramht_debug, "RAMHT debug level: 0-2.");
126 int pscnv_ramht_debug = 0;
127 module_param_named(ramht_debug, pscnv_ramht_debug, int, 0400);
129 MODULE_PARM_DESC(gem_debug, "GEM debug level: 0-1.");
130 int pscnv_gem_debug = 1;
131 module_param_named(gem_debug, pscnv_gem_debug, int, 0400);
133 int nouveau_fbpercrtc;
134 #if 0
135 module_param_named(fbpercrtc, nouveau_fbpercrtc, int, 0400);
136 #endif
138 static drm_pci_id_list_t pciidlist[] = {
140 /*PCI_VENDOR_ID_NVIDIA*/ 0x10de, 0U, 0, "nVidia graphics card"
143 /*PCI_VENDOR_ID_NVIDIA_SGS*/ 0x12d2, 0U, 0, "nVidia SGS graphics card"
148 static struct drm_driver_info driver;
150 static int
151 pscnv_attach(device_t kdev)
153 struct drm_device *dev;
154 dev = device_get_softc(kdev);
155 if (nouveau_modeset == 1)
156 driver.driver_features |= DRIVER_MODESET;
157 dev->driver = &driver;
158 /* drm_sleep_locking_init(dev); XXX PLHK */
159 return (drm_attach(kdev, pciidlist));
163 static int
164 pscnv_probe(device_t kdev)
166 return drm_probe(kdev, pciidlist);
169 static int
170 nouveau_suspend(device_t kdev)
172 return (ENXIO);
175 static int
176 nouveau_resume(device_t kdev)
178 return (ENXIO);
181 static device_method_t pscnv_methods[] = {
182 /* Device interface */
183 DEVMETHOD(device_probe, pscnv_probe),
184 DEVMETHOD(device_attach, pscnv_attach),
185 #ifdef DEVICE_AFTER_ATTACH
186 DEVMETHOD(device_after_attach, drm_after_attach),
187 #endif
188 DEVMETHOD(device_suspend, nouveau_suspend),
189 DEVMETHOD(device_resume, nouveau_resume),
190 DEVMETHOD(device_detach, drm_detach),
191 DEVMETHOD_END
194 static driver_t pscnv_driver = {
195 "drm",
196 pscnv_methods,
197 sizeof(struct drm_device)
200 extern devclass_t drm_devclass;
201 DRIVER_MODULE_ORDERED(pscnv, vgapci, pscnv_driver, drm_devclass, 0, 0,
202 SI_ORDER_ANY);
203 MODULE_DEPEND(pscnv, drmn, 1, 1, 1);
204 MODULE_DEPEND(pscnv, agp, 1, 1, 1);
205 MODULE_DEPEND(pscnv, iicbus, 1, 1, 1);
206 MODULE_DEPEND(pscnv, iic, 1, 1, 1);
207 MODULE_DEPEND(pscnv, iicbb, 1, 1, 1);
209 struct drm_ioctl_desc nouveau_ioctls[] = {
210 DRM_IOCTL_DEF(DRM_PSCNV_GETPARAM, pscnv_ioctl_getparam, DRM_UNLOCKED),
211 DRM_IOCTL_DEF(DRM_PSCNV_GEM_NEW, pscnv_ioctl_gem_new, DRM_UNLOCKED),
212 DRM_IOCTL_DEF(DRM_PSCNV_GEM_INFO, pscnv_ioctl_gem_info, DRM_UNLOCKED),
213 DRM_IOCTL_DEF(DRM_PSCNV_VSPACE_NEW, pscnv_ioctl_vspace_new, DRM_UNLOCKED),
214 DRM_IOCTL_DEF(DRM_PSCNV_VSPACE_FREE, pscnv_ioctl_vspace_free, DRM_UNLOCKED),
215 DRM_IOCTL_DEF(DRM_PSCNV_VSPACE_MAP, pscnv_ioctl_vspace_map, DRM_UNLOCKED),
216 DRM_IOCTL_DEF(DRM_PSCNV_VSPACE_UNMAP, pscnv_ioctl_vspace_unmap, DRM_UNLOCKED),
217 DRM_IOCTL_DEF(DRM_PSCNV_CHAN_NEW, pscnv_ioctl_chan_new, DRM_UNLOCKED),
218 DRM_IOCTL_DEF(DRM_PSCNV_CHAN_FREE, pscnv_ioctl_chan_free, DRM_UNLOCKED),
219 DRM_IOCTL_DEF(DRM_PSCNV_OBJ_VDMA_NEW, pscnv_ioctl_obj_vdma_new, DRM_UNLOCKED),
220 DRM_IOCTL_DEF(DRM_PSCNV_FIFO_INIT, pscnv_ioctl_fifo_init, DRM_UNLOCKED),
221 DRM_IOCTL_DEF(DRM_PSCNV_OBJ_ENG_NEW, pscnv_ioctl_obj_eng_new, DRM_UNLOCKED),
222 DRM_IOCTL_DEF(DRM_PSCNV_FIFO_INIT_IB, pscnv_ioctl_fifo_init_ib, DRM_UNLOCKED),
225 static int
226 pscnv_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
227 vm_ooffset_t foff, struct ucred *cred, u_short *color)
229 struct drm_gem_object *gem_obj = handle;
230 struct drm_device *dev = gem_obj->dev;
231 struct drm_nouveau_private *dev_priv = dev->dev_private;
232 struct pscnv_bo *bo = gem_obj->driver_private;
233 int ret;
234 *color = 0; /* ...? */
236 if (!bo->chan && !bo->dmapages && (ret = -dev_priv->vm->map_user(bo)))
237 return (ret);
239 if (bo->chan)
240 pscnv_chan_ref(bo->chan);
241 /* else */
242 drm_gem_object_reference(gem_obj);
244 NV_WARN(dev, "Mapping bo %p, handle %p, chan %p\n", bo, handle, bo->chan);
245 return (0);
248 static int
249 pscnv_gem_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
250 vm_page_t *mres)
252 struct drm_gem_object *gem_obj = vm_obj->handle;
253 struct pscnv_bo *bo = gem_obj->driver_private;
254 struct drm_device *dev = gem_obj->dev;
255 struct drm_nouveau_private *dev_priv = dev->dev_private;
256 vm_page_t m = NULL;
257 vm_page_t oldm;
258 vm_memattr_t mattr;
259 vm_paddr_t paddr;
260 const char *what;
262 if (bo->chan) {
263 paddr = dev_priv->fb_phys + offset +
264 nvc0_fifo_ctrl_offs(dev, bo->chan->cid);
265 mattr = VM_MEMATTR_UNCACHEABLE;
266 what = "fifo";
267 } else switch (bo->flags & PSCNV_GEM_MEMTYPE_MASK) {
268 case PSCNV_GEM_VRAM_SMALL:
269 case PSCNV_GEM_VRAM_LARGE:
270 paddr = dev_priv->fb_phys + bo->map1->start + offset;
271 mattr = VM_MEMATTR_WRITE_COMBINING;
272 what = "vram";
273 break;
274 case PSCNV_GEM_SYSRAM_SNOOP:
275 case PSCNV_GEM_SYSRAM_NOSNOOP:
276 paddr = bo->dmapages[OFF_TO_IDX(offset)];
277 mattr = VM_MEMATTR_WRITE_BACK;
278 what = "sysram";
279 break;
280 default: return (EINVAL);
283 if (offset >= bo->size) {
284 if (pscnv_mem_debug > 0)
285 NV_WARN(dev, "Reading %p + %08llx (%s) is past max size %08llx\n",
286 bo, offset, what, bo->size);
287 return (VM_PAGER_ERROR);
289 DRM_LOCK(dev);
290 if (pscnv_mem_debug > 0)
291 NV_WARN(dev, "Connecting %p+%08llx (%s) at phys %010llx\n",
292 bo, offset, what, paddr);
293 vm_object_pip_add(vm_obj, 1);
295 if (*mres != NULL) {
296 oldm = *mres;
297 vm_page_lock(oldm);
298 vm_page_remove(oldm);
299 vm_page_unlock(oldm);
300 *mres = NULL;
301 } else
302 oldm = NULL;
303 //VM_OBJECT_LOCK(vm_obj);
304 m = vm_phys_fictitious_to_vm_page(paddr);
305 if (m == NULL) {
306 DRM_UNLOCK(dev);
307 return -EFAULT;
309 KASSERT((m->flags & PG_FICTITIOUS) != 0,
310 ("not fictitious %p", m));
311 KASSERT(m->wire_count == 1, ("wire_count not 1 %p", m));
313 if ((m->flags & VPO_BUSY) != 0) {
314 DRM_UNLOCK(dev);
315 return -EFAULT;
317 pmap_page_set_memattr(m, mattr);
318 m->valid = VM_PAGE_BITS_ALL;
319 *mres = m;
320 vm_page_lock(m);
321 vm_page_insert(m, vm_obj, OFF_TO_IDX(offset));
322 vm_page_unlock(m);
323 vm_page_busy(m);
325 printf("fault %p %jx %x phys %x", gem_obj, offset, prot,
326 m->phys_addr);
327 DRM_UNLOCK(dev);
328 if (oldm != NULL) {
329 vm_page_lock(oldm);
330 vm_page_free(oldm);
331 vm_page_unlock(oldm);
333 vm_object_pip_wakeup(vm_obj);
334 return (VM_PAGER_OK);
337 static void
338 pscnv_gem_pager_dtor(void *handle)
340 struct drm_gem_object *gem_obj = handle;
341 struct pscnv_bo *bo = gem_obj->driver_private;
342 struct drm_device *dev = gem_obj->dev;
343 vm_object_t devobj;
345 DRM_LOCK(dev);
346 devobj = cdev_pager_lookup(handle);
348 if (devobj != NULL) {
349 vm_size_t page_count = OFF_TO_IDX(bo->size);
350 vm_page_t m;
351 int i;
352 VM_OBJECT_LOCK(devobj);
353 for (i = 0; i < page_count; i++) {
354 m = vm_page_lookup(devobj, i);
355 if (!m)
356 continue;
357 if (pscnv_mem_debug > 0)
358 NV_WARN(dev, "Freeing %010llx + %08llx (%p\n", bo->start, i * PAGE_SIZE, m);
359 cdev_pager_free_page(devobj, m);
361 VM_OBJECT_UNLOCK(devobj);
362 vm_object_deallocate(devobj);
364 else {
365 DRM_UNLOCK(dev);
366 NV_ERROR(dev, "Could not find handle %p bo %p\n", handle, bo);
367 return;
369 if (pscnv_mem_debug > 0)
370 NV_WARN(dev, "Freed %010llx (%p)\n", bo->start, bo);
371 //kfree(bo->fake_pages);
373 if (bo->chan)
374 pscnv_chan_unref(bo->chan);
375 else
376 drm_gem_object_unreference_unlocked(gem_obj);
377 DRM_UNLOCK(dev);
380 static struct cdev_pager_ops pscnv_gem_pager_ops = {
381 .cdev_pg_fault = pscnv_gem_pager_fault,
382 .cdev_pg_ctor = pscnv_gem_pager_ctor,
383 .cdev_pg_dtor = pscnv_gem_pager_dtor
386 static struct drm_driver_info driver = {
387 .driver_features =
388 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG | DRIVER_USE_MTRR |
389 DRIVER_HAVE_IRQ | DRIVER_LOCKLESS_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
390 .load = nouveau_load,
391 .firstopen = nouveau_firstopen,
392 .lastclose = nouveau_lastclose,
393 .unload = nouveau_unload,
394 .preclose = nouveau_preclose,
395 .irq_preinstall = nouveau_irq_preinstall,
396 .irq_postinstall = nouveau_irq_postinstall,
397 .irq_uninstall = nouveau_irq_uninstall,
398 .irq_handler = nouveau_irq_handler,
399 .ioctls = nouveau_ioctls,
400 .max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls),
402 // .gem_init_object ?
403 // .dumb_* ?
404 // sysctl init/cleanup ?
405 .gem_free_object = pscnv_gem_free_object,
406 .gem_pager_ops = &pscnv_gem_pager_ops,
408 .name = DRIVER_NAME,
409 .desc = DRIVER_DESC,
410 #ifdef GIT_REVISION
411 .date = GIT_REVISION,
412 #else
413 .date = DRIVER_DATE,
414 #endif
415 .major = DRIVER_MAJOR,
416 .minor = DRIVER_MINOR,
417 .patchlevel = DRIVER_PATCHLEVEL,