2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
32 * The catch-all file for DRM device support, including module setup/teardown,
33 * open/close, and ioctl dispatch.
37 #include <sys/limits.h>
40 #include "drm_sarea.h"
42 #ifdef DRM_DEBUG_DEFAULT_ON
43 int drm_debug_flag = 1;
45 int drm_debug_flag = 0;
48 static int drm_load(drm_device_t *dev);
49 static void drm_unload(drm_device_t *dev);
50 static drm_pci_id_list_t *drm_find_description(int vendor, int device,
51 drm_pci_id_list_t *idlist);
54 #define DRIVER_SOFTC(unit) \
55 ((drm_device_t *)devclass_get_softc(drm_devclass, unit))
57 MODULE_VERSION(drm, 1);
58 MODULE_DEPEND(drm, agp, 1, 1, 1);
59 MODULE_DEPEND(drm, pci, 1, 1, 1);
60 #if __FreeBSD_version > 502127
61 MODULE_DEPEND(drm, mem, 1, 1, 1);
63 #endif /* __FreeBSD__ */
65 #if defined(__NetBSD__) || defined(__OpenBSD__)
66 #define DRIVER_SOFTC(unit) \
67 ((drm_device_t *)device_lookup(&drm_cd, unit))
68 #endif /* __NetBSD__ || __OpenBSD__ */
70 static drm_ioctl_desc_t drm_ioctls[256] = {
71 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0),
72 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
73 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
74 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
75 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0),
76 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
77 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
78 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER|DRM_ROOT_ONLY),
80 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
81 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
82 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
83 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
85 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
86 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
88 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
89 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
91 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
92 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
93 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
94 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
95 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
96 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
97 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
99 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
100 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
102 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
103 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
104 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
106 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
107 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER),
108 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
109 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
110 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
111 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma, DRM_AUTH),
113 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
115 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
116 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
117 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
118 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
119 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
120 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
121 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
122 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
124 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
125 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
127 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, 0),
128 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_update_draw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
132 static struct cdevsw drm_cdevsw = {
133 #if __FreeBSD_version >= 502103
134 .d_version = D_VERSION,
137 .d_close = drm_close,
139 .d_ioctl = drm_ioctl,
143 #if __FreeBSD_version >= 502103
144 .d_flags = D_TRACKCLOSE | D_NEEDGIANT,
147 .d_flags = D_TRACKCLOSE,
149 #if __FreeBSD_version < 500000
154 int drm_probe(device_t dev, drm_pci_id_list_t *idlist)
156 drm_pci_id_list_t *id_entry;
158 #if __FreeBSD_version < 700010
161 if (!strcmp(device_get_name(dev), "drmsub"))
162 realdev = device_get_parent(dev);
165 vendor = pci_get_vendor(realdev);
166 device = pci_get_device(realdev);
168 vendor = pci_get_vendor(dev);
169 device = pci_get_device(dev);
172 id_entry = drm_find_description(vendor, device, idlist);
173 if (id_entry != NULL) {
174 device_set_desc(dev, id_entry->name);
181 int drm_attach(device_t nbdev, drm_pci_id_list_t *idlist)
184 drm_pci_id_list_t *id_entry;
187 unit = device_get_unit(nbdev);
188 dev = device_get_softc(nbdev);
190 #if __FreeBSD_version < 700010
191 if (!strcmp(device_get_name(nbdev), "drmsub"))
192 dev->device = device_get_parent(nbdev);
198 dev->devnode = make_dev(&drm_cdevsw,
204 #if __FreeBSD_version >= 500000
205 mtx_init(&dev->dev_lock, "drm device", NULL, MTX_DEF);
206 mtx_init(&dev->drw_lock, "drmdrw", NULL, MTX_DEF);
209 id_entry = drm_find_description(pci_get_vendor(dev->device),
210 pci_get_device(dev->device), idlist);
211 dev->id_entry = id_entry;
213 return drm_load(dev);
216 int drm_detach(device_t dev)
218 drm_unload(device_get_softc(dev));
223 #define DRM_DEV_NAME "drm"
226 devclass_t drm_devclass;
228 #elif defined(__NetBSD__) || defined(__OpenBSD__)
230 static struct cdevsw drm_cdevsw = {
246 #if defined(__NetBSD__) && __NetBSD_Version__ >= 106080000
247 MOD_DEV("drm", DRIVER_NAME, NULL, -1, &drm_cdevsw, CDEV_MAJOR);
249 MOD_DEV("drm", LM_DT_CHAR, CDEV_MAJOR, &drm_cdevsw);
252 int drm_lkmentry(struct lkm_table *lkmtp, int cmd, int ver);
253 static int drm_lkmhandle(struct lkm_table *lkmtp, int cmd);
255 int drm_modprobe(void);
256 int drm_probe(struct pci_attach_args *pa);
257 void drm_attach(struct pci_attach_args *pa, dev_t kdev);
259 int drm_lkmentry(struct lkm_table *lkmtp, int cmd, int ver) {
260 DISPATCH(lkmtp, cmd, ver, drm_lkmhandle, drm_lkmhandle, drm_lkmhandle);
263 static int drm_lkmhandle(struct lkm_table *lkmtp, int cmd)
269 if (lkmexists(lkmtp))
292 int drm_modprobe(void)
294 struct pci_attach_args pa;
297 error = pci_find_device(&pa, drm_probe, idlist);
304 int drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t idlist)
307 drm_pci_id_list_t *id_entry;
309 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
310 PCI_PRODUCT(pa->pa_id), idlist);
311 if (id_entry != NULL) {
318 void drm_attach(struct pci_attach_args *pa, dev_t kdev,
319 drm_pci_id_list_t *idlist)
323 drm_pci_id_list_t *id_entry;
325 config_makeroom(kdev, &drm_cd);
326 drm_cd.cd_devs[(kdev)] = malloc(sizeof(drm_device_t), M_DRM, M_WAITOK);
327 dev = DRIVER_SOFTC(kdev);
329 memset(dev, 0, sizeof(drm_device_t));
330 memcpy(&dev->pa, pa, sizeof(dev->pa));
332 dev->irq = pa->pa_intrline;
334 dev->pci_bus = pa->pa_bus;
335 dev->pci_slot = pa->pa_device;
336 dev->pci_func = pa->pa_function;
337 dev->dma_tag = pa->pa_dmat;
339 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
340 PCI_PRODUCT(pa->pa_id), idlist);
341 dev->driver.pci_id_entry = id_entry;
343 DRM_INFO("%s", id_entry->name);
347 int drm_detach(struct device *self, int flags)
349 drm_unload((drm_device_t *)self);
353 int drm_activate(struct device *self, enum devact act)
360 case DVACT_DEACTIVATE:
366 #endif /* __NetBSD__ || __OpenBSD__ */
368 drm_pci_id_list_t *drm_find_description(int vendor, int device,
369 drm_pci_id_list_t *idlist)
373 for (i = 0; idlist[i].vendor != 0; i++) {
374 if ((idlist[i].vendor == vendor) &&
375 (idlist[i].device == device)) {
382 static int drm_firstopen(drm_device_t *dev)
384 drm_local_map_t *map;
387 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
389 /* prebuild the SAREA */
390 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
391 _DRM_CONTAINS_LOCK, &map);
395 if (dev->driver.firstopen)
396 dev->driver.firstopen(dev);
400 if (dev->driver.use_dma) {
401 i = drm_dma_setup(dev);
406 for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
407 dev->magiclist[i].head = NULL;
408 dev->magiclist[i].tail = NULL;
411 dev->lock.lock_queue = 0;
412 dev->irq_enabled = 0;
413 dev->context_flag = 0;
414 dev->last_context = 0;
418 dev->buf_sigio = NULL;
419 #elif defined(__NetBSD__) || defined(__OpenBSD__)
428 static int drm_lastclose(drm_device_t *dev)
430 drm_magic_entry_t *pt, *next;
431 drm_local_map_t *map, *mapsave;
434 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
438 if (dev->driver.lastclose != NULL)
439 dev->driver.lastclose(dev);
441 if (dev->irq_enabled)
442 drm_irq_uninstall(dev);
445 free(dev->unique, M_DRM);
450 for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
451 for ( pt = dev->magiclist[i].head ; pt ; pt = next ) {
455 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
458 /* Clear AGP information */
460 drm_agp_mem_t *entry;
461 drm_agp_mem_t *nexte;
463 /* Remove AGP resources, but leave dev->agp intact until
464 * drm_unload is called.
466 for ( entry = dev->agp->memory ; entry ; entry = nexte ) {
469 drm_agp_unbind_memory(entry->handle);
470 drm_agp_free_memory(entry->handle);
473 dev->agp->memory = NULL;
475 if (dev->agp->acquired)
476 drm_agp_release(dev);
478 dev->agp->acquired = 0;
479 dev->agp->enabled = 0;
481 if (dev->sg != NULL) {
482 drm_sg_cleanup(dev->sg);
486 TAILQ_FOREACH_SAFE(map, &dev->maplist, link, mapsave) {
491 drm_dma_takedown(dev);
492 if ( dev->lock.hw_lock ) {
493 dev->lock.hw_lock = NULL; /* SHM removed */
494 dev->lock.file_priv = NULL;
495 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
501 static int drm_load(drm_device_t *dev)
507 dev->irq = pci_get_irq(dev->device);
508 #if defined(__FreeBSD__) && __FreeBSD_version >= 700053
509 dev->pci_domain = pci_get_domain(dev->device);
513 dev->pci_bus = pci_get_bus(dev->device);
514 dev->pci_slot = pci_get_slot(dev->device);
515 dev->pci_func = pci_get_function(dev->device);
517 dev->pci_vendor = pci_get_vendor(dev->device);
518 dev->pci_device = pci_get_device(dev->device);
520 TAILQ_INIT(&dev->maplist);
524 drm_sysctl_init(dev);
526 TAILQ_INIT(&dev->files);
529 dev->types[0] = _DRM_STAT_LOCK;
530 dev->types[1] = _DRM_STAT_OPENS;
531 dev->types[2] = _DRM_STAT_CLOSES;
532 dev->types[3] = _DRM_STAT_IOCTLS;
533 dev->types[4] = _DRM_STAT_LOCKS;
534 dev->types[5] = _DRM_STAT_UNLOCKS;
536 for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
537 atomic_set( &dev->counts[i], 0 );
539 if (dev->driver.load != NULL) {
541 /* Shared code returns -errno. */
542 retcode = -dev->driver.load(dev,
543 dev->id_entry->driver_private);
549 if (dev->driver.use_agp) {
550 if (drm_device_is_agp(dev))
551 dev->agp = drm_agp_init();
552 if (dev->driver.require_agp && dev->agp == NULL) {
553 DRM_ERROR("Card isn't AGP, or couldn't initialize "
558 if (dev->agp != NULL) {
559 if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
560 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
565 retcode = drm_ctxbitmap_init(dev);
567 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
571 dev->drw_unrhdr = new_unrhdr(1, INT_MAX, NULL);
572 if (dev->drw_unrhdr == NULL) {
573 DRM_ERROR("Couldn't allocate drawable number allocator\n");
577 DRM_INFO("Initialized %s %d.%d.%d %s\n",
581 dev->driver.patchlevel,
588 drm_sysctl_cleanup(dev);
594 destroy_dev(dev->devnode);
595 #if __FreeBSD_version >= 500000
596 mtx_destroy(&dev->dev_lock);
602 static void drm_unload(drm_device_t *dev)
609 drm_sysctl_cleanup(dev);
610 destroy_dev(dev->devnode);
613 drm_ctxbitmap_cleanup(dev);
615 if (dev->agp && dev->agp->mtrr) {
616 int __unused retcode;
618 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base,
619 dev->agp->info.ai_aperture_size, DRM_MTRR_WC);
620 DRM_DEBUG("mtrr_del = %d", retcode);
627 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
628 * worried about resource consumption while the DRM is inactive (between
629 * lastclose and firstopen or unload) because these aren't actually
630 * taking up KVA, just keeping the PCI resource allocated.
632 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
633 if (dev->pcir[i] == NULL)
635 bus_release_resource(dev->device, SYS_RES_MEMORY,
636 dev->pcirid[i], dev->pcir[i]);
641 free(dev->agp, M_DRM);
645 if (dev->driver.unload != NULL)
646 dev->driver.unload(dev);
648 delete_unrhdr(dev->drw_unrhdr);
651 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
652 mtx_destroy(&dev->dev_lock);
657 int drm_version(drm_device_t *dev, void *data, struct drm_file *file_priv)
659 drm_version_t *version = data;
662 #define DRM_COPY( name, value ) \
663 len = strlen( value ); \
664 if ( len > name##_len ) len = name##_len; \
665 name##_len = strlen( value ); \
666 if ( len && name ) { \
667 if ( DRM_COPY_TO_USER( name, value, len ) ) \
671 version->version_major = dev->driver.major;
672 version->version_minor = dev->driver.minor;
673 version->version_patchlevel = dev->driver.patchlevel;
675 DRM_COPY(version->name, dev->driver.name);
676 DRM_COPY(version->date, dev->driver.date);
677 DRM_COPY(version->desc, dev->driver.desc);
682 int drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
684 drm_device_t *dev = NULL;
687 dev = DRIVER_SOFTC(minor(kdev));
689 DRM_DEBUG( "open_count = %d\n", dev->open_count );
691 retcode = drm_open_helper(kdev, flags, fmt, p, dev);
694 atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
697 device_busy(dev->device);
699 if ( !dev->open_count++ )
700 retcode = drm_firstopen(dev);
707 int drm_close(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
709 drm_device_t *dev = drm_get_device_from_kdev(kdev);
710 drm_file_t *file_priv;
713 DRM_DEBUG( "open_count = %d\n", dev->open_count );
717 file_priv = drm_find_file_by_proc(dev, p);
720 DRM_ERROR("can't find authenticator\n");
724 if (--file_priv->refs != 0)
727 if (dev->driver.preclose != NULL)
728 dev->driver.preclose(dev, file_priv);
730 /* ========================================================
731 * Begin inline drm_release
735 DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
736 DRM_CURRENTPID, (long)dev->device, dev->open_count );
737 #elif defined(__NetBSD__) || defined(__OpenBSD__)
738 DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
739 DRM_CURRENTPID, (long)&dev->device, dev->open_count);
742 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
743 && dev->lock.file_priv == file_priv) {
744 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
746 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
747 if (dev->driver.reclaim_buffers_locked != NULL)
748 dev->driver.reclaim_buffers_locked(dev, file_priv);
750 drm_lock_free(dev, &dev->lock.hw_lock->lock,
751 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
753 /* FIXME: may require heavy-handed reset of
754 hardware at this point, possibly
755 processed via a callback to the X
757 } else if (dev->driver.reclaim_buffers_locked != NULL &&
758 dev->lock.hw_lock != NULL) {
759 /* The lock is required to reclaim buffers */
761 if ( !dev->lock.hw_lock ) {
762 /* Device has been unregistered */
766 if (drm_lock_take(&dev->lock.hw_lock->lock,
767 DRM_KERNEL_CONTEXT)) {
768 dev->lock.file_priv = file_priv;
769 dev->lock.lock_time = jiffies;
770 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
771 break; /* Got lock */
774 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
775 retcode = msleep((void *)&dev->lock.lock_queue,
776 &dev->dev_lock, PZERO | PCATCH, "drmlk2", 0);
778 retcode = tsleep((void *)&dev->lock.lock_queue,
779 PZERO | PCATCH, "drmlk2", 0);
785 dev->driver.reclaim_buffers_locked(dev, file_priv);
786 drm_lock_free(dev, &dev->lock.hw_lock->lock,
791 if (dev->driver.use_dma && !dev->driver.reclaim_buffers_locked)
792 drm_reclaim_buffers(dev, file_priv);
794 #if defined (__FreeBSD__) && (__FreeBSD_version >= 500000)
795 funsetown(&dev->buf_sigio);
796 #elif defined(__FreeBSD__)
797 funsetown(dev->buf_sigio);
798 #elif defined(__NetBSD__) || defined(__OpenBSD__)
800 #endif /* __NetBSD__ || __OpenBSD__ */
802 if (dev->driver.postclose != NULL)
803 dev->driver.postclose(dev, file_priv);
804 TAILQ_REMOVE(&dev->files, file_priv, link);
805 free(file_priv, M_DRM);
807 /* ========================================================
808 * End inline drm_release
812 atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
814 device_unbusy(dev->device);
816 if (--dev->open_count == 0) {
817 retcode = drm_lastclose(dev);
825 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm.
827 int drm_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int flags,
830 drm_device_t *dev = drm_get_device_from_kdev(kdev);
832 drm_ioctl_desc_t *ioctl;
833 int (*func)(drm_device_t *dev, void *data, struct drm_file *file_priv);
834 int nr = DRM_IOCTL_NR(cmd);
835 int is_driver_ioctl = 0;
836 drm_file_t *file_priv;
839 file_priv = drm_find_file_by_proc(dev, p);
841 if (file_priv == NULL) {
842 DRM_ERROR("can't find authenticator\n");
846 atomic_inc( &dev->counts[_DRM_STAT_IOCTLS] );
847 ++file_priv->ioctl_count;
850 DRM_DEBUG( "pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
851 DRM_CURRENTPID, cmd, nr, (long)dev->device,
852 file_priv->authenticated );
853 #elif defined(__NetBSD__) || defined(__OpenBSD__)
854 DRM_DEBUG( "pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
855 DRM_CURRENTPID, cmd, nr, (long)&dev->device,
856 file_priv->authenticated );
866 return fsetown(*(int *)data, &dev->buf_sigio);
869 #if (__FreeBSD_version >= 500000)
870 *(int *) data = fgetown(&dev->buf_sigio);
872 *(int *) data = fgetown(dev->buf_sigio);
875 #endif /* __FreeBSD__ */
876 #if defined(__NetBSD__) || defined(__OpenBSD__)
878 dev->buf_pgid = *(int *)data;
882 *(int *)data = dev->buf_pgid;
884 #endif /* __NetBSD__ */
887 if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
888 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
892 ioctl = &drm_ioctls[nr];
893 /* It's not a core DRM ioctl, try driver-specific. */
894 if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
895 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
896 nr -= DRM_COMMAND_BASE;
897 if (nr > dev->driver.max_ioctl) {
898 DRM_DEBUG("Bad driver ioctl number, 0x%x (of 0x%x)\n",
899 nr, dev->driver.max_ioctl);
902 ioctl = &dev->driver.ioctls[nr];
908 DRM_DEBUG( "no function\n" );
912 if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
913 ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
914 ((ioctl->flags & DRM_MASTER) && !file_priv->master))
917 if (is_driver_ioctl) {
919 /* shared code returns -errno */
920 retcode = -func(dev, data, file_priv);
923 retcode = func(dev, data, file_priv);
927 DRM_DEBUG(" returning %d\n", retcode);
932 drm_local_map_t *drm_getsarea(drm_device_t *dev)
934 drm_local_map_t *map;
936 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
937 TAILQ_FOREACH(map, &dev->maplist, link) {
938 if (map->type == _DRM_SHM && (map->flags & _DRM_CONTAINS_LOCK))
947 #include <sys/sysproto.h>
949 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
951 #define LINUX_IOCTL_DRM_MIN 0x6400
952 #define LINUX_IOCTL_DRM_MAX 0x64ff
954 static linux_ioctl_function_t drm_linux_ioctl;
955 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
956 LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
958 SYSINIT(drm_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
959 linux_ioctl_register_handler, &drm_handler);
960 SYSUNINIT(drm_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
961 linux_ioctl_unregister_handler, &drm_handler);
963 /* The bits for in/out are switched on Linux */
964 #define LINUX_IOC_IN IOC_OUT
965 #define LINUX_IOC_OUT IOC_IN
968 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
973 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
974 if (cmd & LINUX_IOC_IN)
976 if (cmd & LINUX_IOC_OUT)
977 args->cmd |= IOC_OUT;
979 error = ioctl(p, (struct ioctl_args *)args);
983 #endif /* DRM_LINUX */