]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_drv.c
lualoader: Refactor config line expressions
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_drv.c
1 /*-
2  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
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:
12  *
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
15  * Software.
16  *
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.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /** @file drm_drv.c
35  * The catch-all file for DRM device support, including module setup/teardown,
36  * open/close, and ioctl dispatch.
37  */
38
39
40 #include <sys/limits.h>
41 #include "dev/drm/drmP.h"
42 #include "dev/drm/drm.h"
43 #include "dev/drm/drm_sarea.h"
44
45 #ifdef DRM_DEBUG_DEFAULT_ON
46 int drm_debug_flag = 1;
47 #else
48 int drm_debug_flag = 0;
49 #endif
50
51 static int drm_load(struct drm_device *dev);
52 static void drm_unload(struct drm_device *dev);
53 static drm_pci_id_list_t *drm_find_description(int vendor, int device,
54     drm_pci_id_list_t *idlist);
55
56 MODULE_VERSION(drm, 1);
57 MODULE_DEPEND(drm, agp, 1, 1, 1);
58 MODULE_DEPEND(drm, pci, 1, 1, 1);
59 MODULE_DEPEND(drm, mem, 1, 1, 1);
60
61 static drm_ioctl_desc_t           drm_ioctls[256] = {
62         DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0),
63         DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
64         DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
65         DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
66         DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0),
67         DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
68         DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
69         DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER|DRM_ROOT_ONLY),
70
71         DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
72         DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
73         DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
74         DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
75
76         DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
77         DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
78
79         DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
80         DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
81
82         DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
83         DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
84         DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
85         DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
86         DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
87         DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
88         DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
89
90         DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
91         DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
92
93         DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
94         DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
95
96         DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
97
98         DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
99         DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
100         DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
101         DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
102         DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
103         DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma, DRM_AUTH),
104
105         DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
106
107         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
108         DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
109         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
110         DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
111         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
112         DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
113         DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
114         DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
115
116         DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
117         DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
118         DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, 0),
119         DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
120         DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_update_draw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
121 };
122
123 static struct cdevsw drm_cdevsw = {
124         .d_version =    D_VERSION,
125         .d_open =       drm_open,
126         .d_read =       drm_read,
127         .d_ioctl =      drm_ioctl,
128         .d_poll =       drm_poll,
129         .d_mmap =       drm_mmap,
130         .d_name =       "drm",
131         .d_flags =      D_TRACKCLOSE
132 };
133
134 static int drm_msi = 1; /* Enable by default. */
135 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
136 SYSCTL_INT(_hw_drm, OID_AUTO, msi, CTLFLAG_RDTUN, &drm_msi, 1,
137     "Enable MSI interrupts for drm devices");
138
139 static struct drm_msi_blacklist_entry drm_msi_blacklist[] = {
140         {0x8086, 0x2772}, /* Intel i945G        */ \
141         {0x8086, 0x27A2}, /* Intel i945GM       */ \
142         {0x8086, 0x27AE}, /* Intel i945GME      */ \
143         {0, 0}
144 };
145
146 static int drm_msi_is_blacklisted(int vendor, int device)
147 {
148         int i = 0;
149         
150         for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) {
151                 if ((drm_msi_blacklist[i].vendor == vendor) &&
152                     (drm_msi_blacklist[i].device == device)) {
153                         return 1;
154                 }
155         }
156
157         return 0;
158 }
159
160 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
161 {
162         drm_pci_id_list_t *id_entry;
163         int vendor, device;
164         vendor = pci_get_vendor(kdev);
165         device = pci_get_device(kdev);
166
167         if (pci_get_class(kdev) != PCIC_DISPLAY
168             || pci_get_subclass(kdev) != PCIS_DISPLAY_VGA)
169                 return ENXIO;
170
171         id_entry = drm_find_description(vendor, device, idlist);
172         if (id_entry != NULL) {
173                 if (!device_get_desc(kdev)) {
174                         DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
175                         device_set_desc(kdev, id_entry->name);
176                 }
177                 return 0;
178         }
179
180         return ENXIO;
181 }
182
183 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
184 {
185         struct drm_device *dev;
186         drm_pci_id_list_t *id_entry;
187         int unit, msicount;
188
189         unit = device_get_unit(kdev);
190         dev = device_get_softc(kdev);
191
192         dev->device = kdev;
193         dev->devnode = make_dev(&drm_cdevsw,
194                         0,
195                         DRM_DEV_UID,
196                         DRM_DEV_GID,
197                         DRM_DEV_MODE,
198                         "dri/card%d", unit);
199         dev->devnode->si_drv1 = dev;
200
201         dev->pci_domain = pci_get_domain(dev->device);
202         dev->pci_bus = pci_get_bus(dev->device);
203         dev->pci_slot = pci_get_slot(dev->device);
204         dev->pci_func = pci_get_function(dev->device);
205
206         dev->pci_vendor = pci_get_vendor(dev->device);
207         dev->pci_device = pci_get_device(dev->device);
208
209         if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
210                 if (drm_msi &&
211                     !drm_msi_is_blacklisted(dev->pci_vendor, dev->pci_device)) {
212                         msicount = pci_msi_count(dev->device);
213                         DRM_DEBUG("MSI count = %d\n", msicount);
214                         if (msicount > 1)
215                                 msicount = 1;
216
217                         if (pci_alloc_msi(dev->device, &msicount) == 0) {
218                                 DRM_INFO("MSI enabled %d message(s)\n",
219                                     msicount);
220                                 dev->msi_enabled = 1;
221                                 dev->irqrid = 1;
222                         }
223                 }
224
225                 dev->irqr = bus_alloc_resource_any(dev->device, SYS_RES_IRQ,
226                     &dev->irqrid, RF_SHAREABLE);
227                 if (!dev->irqr) {
228                         return ENOENT;
229                 }
230
231                 dev->irq = (int) rman_get_start(dev->irqr);
232         }
233
234         mtx_init(&dev->dev_lock, "drmdev", NULL, MTX_DEF);
235         mtx_init(&dev->irq_lock, "drmirq", NULL, MTX_DEF);
236         mtx_init(&dev->vbl_lock, "drmvbl", NULL, MTX_DEF);
237         mtx_init(&dev->drw_lock, "drmdrw", NULL, MTX_DEF);
238
239         id_entry = drm_find_description(dev->pci_vendor,
240             dev->pci_device, idlist);
241         dev->id_entry = id_entry;
242
243         return drm_load(dev);
244 }
245
246 int drm_detach(device_t kdev)
247 {
248         struct drm_device *dev;
249
250         dev = device_get_softc(kdev);
251
252         drm_unload(dev);
253
254         if (dev->irqr) {
255                 bus_release_resource(dev->device, SYS_RES_IRQ, dev->irqrid,
256                     dev->irqr);
257
258                 if (dev->msi_enabled) {
259                         pci_release_msi(dev->device);
260                         DRM_INFO("MSI released\n");
261                 }
262         }
263
264         return 0;
265 }
266
267 #ifndef DRM_DEV_NAME
268 #define DRM_DEV_NAME "drm"
269 #endif
270
271 devclass_t drm_devclass;
272
273 drm_pci_id_list_t *drm_find_description(int vendor, int device,
274     drm_pci_id_list_t *idlist)
275 {
276         int i = 0;
277         
278         for (i = 0; idlist[i].vendor != 0; i++) {
279                 if ((idlist[i].vendor == vendor) &&
280                     ((idlist[i].device == device) ||
281                     (idlist[i].device == 0))) {
282                         return &idlist[i];
283                 }
284         }
285         return NULL;
286 }
287
288 static int drm_firstopen(struct drm_device *dev)
289 {
290         drm_local_map_t *map;
291         int i;
292
293         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
294
295         /* prebuild the SAREA */
296         i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
297             _DRM_CONTAINS_LOCK, &map);
298         if (i != 0)
299                 return i;
300
301         if (dev->driver->firstopen)
302                 dev->driver->firstopen(dev);
303
304         dev->buf_use = 0;
305
306         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
307                 i = drm_dma_setup(dev);
308                 if (i != 0)
309                         return i;
310         }
311
312         for (i = 0; i < DRM_HASH_SIZE; i++) {
313                 dev->magiclist[i].head = NULL;
314                 dev->magiclist[i].tail = NULL;
315         }
316
317         dev->lock.lock_queue = 0;
318         dev->irq_enabled = 0;
319         dev->context_flag = 0;
320         dev->last_context = 0;
321         dev->if_version = 0;
322
323         dev->buf_sigio = NULL;
324
325         DRM_DEBUG("\n");
326
327         return 0;
328 }
329
330 static int drm_lastclose(struct drm_device *dev)
331 {
332         drm_magic_entry_t *pt, *next;
333         drm_local_map_t *map, *mapsave;
334         int i;
335
336         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
337
338         DRM_DEBUG("\n");
339
340         if (dev->driver->lastclose != NULL)
341                 dev->driver->lastclose(dev);
342
343         if (dev->irq_enabled)
344                 drm_irq_uninstall(dev);
345
346         if (dev->unique) {
347                 free(dev->unique, DRM_MEM_DRIVER);
348                 dev->unique = NULL;
349                 dev->unique_len = 0;
350         }
351         /* Clear pid list */
352         for (i = 0; i < DRM_HASH_SIZE; i++) {
353                 for (pt = dev->magiclist[i].head; pt; pt = next) {
354                         next = pt->next;
355                         free(pt, DRM_MEM_MAGIC);
356                 }
357                 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
358         }
359
360         DRM_UNLOCK();
361         drm_drawable_free_all(dev);
362         DRM_LOCK();
363
364         /* Clear AGP information */
365         if (dev->agp) {
366                 drm_agp_mem_t *entry;
367                 drm_agp_mem_t *nexte;
368
369                 /* Remove AGP resources, but leave dev->agp intact until
370                  * drm_unload is called.
371                  */
372                 for (entry = dev->agp->memory; entry; entry = nexte) {
373                         nexte = entry->next;
374                         if (entry->bound)
375                                 drm_agp_unbind_memory(entry->handle);
376                         drm_agp_free_memory(entry->handle);
377                         free(entry, DRM_MEM_AGPLISTS);
378                 }
379                 dev->agp->memory = NULL;
380
381                 if (dev->agp->acquired)
382                         drm_agp_release(dev);
383
384                 dev->agp->acquired = 0;
385                 dev->agp->enabled  = 0;
386         }
387         if (dev->sg != NULL) {
388                 drm_sg_cleanup(dev->sg);
389                 dev->sg = NULL;
390         }
391
392         TAILQ_FOREACH_SAFE(map, &dev->maplist, link, mapsave) {
393                 if (!(map->flags & _DRM_DRIVER))
394                         drm_rmmap(dev, map);
395         }
396
397         drm_dma_takedown(dev);
398         if (dev->lock.hw_lock) {
399                 dev->lock.hw_lock = NULL; /* SHM removed */
400                 dev->lock.file_priv = NULL;
401                 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
402         }
403
404         return 0;
405 }
406
407 static int drm_load(struct drm_device *dev)
408 {
409         int i, retcode;
410
411         DRM_DEBUG("\n");
412
413         TAILQ_INIT(&dev->maplist);
414         dev->map_unrhdr = new_unrhdr(1, ((1 << DRM_MAP_HANDLE_BITS) - 1), NULL);
415         if (dev->map_unrhdr == NULL) {
416                 DRM_ERROR("Couldn't allocate map number allocator\n");
417                 return EINVAL;
418         }
419
420
421         drm_mem_init();
422         drm_sysctl_init(dev);
423         TAILQ_INIT(&dev->files);
424
425         dev->counters  = 6;
426         dev->types[0]  = _DRM_STAT_LOCK;
427         dev->types[1]  = _DRM_STAT_OPENS;
428         dev->types[2]  = _DRM_STAT_CLOSES;
429         dev->types[3]  = _DRM_STAT_IOCTLS;
430         dev->types[4]  = _DRM_STAT_LOCKS;
431         dev->types[5]  = _DRM_STAT_UNLOCKS;
432
433         for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
434                 atomic_set(&dev->counts[i], 0);
435
436         if (dev->driver->load != NULL) {
437                 DRM_LOCK();
438                 /* Shared code returns -errno. */
439                 retcode = -dev->driver->load(dev,
440                     dev->id_entry->driver_private);
441                 if (pci_enable_busmaster(dev->device))
442                         DRM_ERROR("Request to enable bus-master failed.\n");
443                 DRM_UNLOCK();
444                 if (retcode != 0)
445                         goto error;
446         }
447
448         if (drm_core_has_AGP(dev)) {
449                 if (drm_device_is_agp(dev))
450                         dev->agp = drm_agp_init();
451                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
452                     dev->agp == NULL) {
453                         DRM_ERROR("Card isn't AGP, or couldn't initialize "
454                             "AGP.\n");
455                         retcode = ENOMEM;
456                         goto error;
457                 }
458                 if (dev->agp != NULL && dev->agp->info.ai_aperture_base != 0) {
459                         if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
460                             dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
461                                 dev->agp->mtrr = 1;
462                 }
463         }
464
465         retcode = drm_ctxbitmap_init(dev);
466         if (retcode != 0) {
467                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
468                 goto error;
469         }
470
471         dev->drw_unrhdr = new_unrhdr(1, INT_MAX, NULL);
472         if (dev->drw_unrhdr == NULL) {
473                 DRM_ERROR("Couldn't allocate drawable number allocator\n");
474                 goto error;
475         }
476
477         DRM_INFO("Initialized %s %d.%d.%d %s\n",
478             dev->driver->name,
479             dev->driver->major,
480             dev->driver->minor,
481             dev->driver->patchlevel,
482             dev->driver->date);
483
484         return 0;
485
486 error:
487         drm_sysctl_cleanup(dev);
488         DRM_LOCK();
489         drm_lastclose(dev);
490         DRM_UNLOCK();
491         destroy_dev(dev->devnode);
492
493         mtx_destroy(&dev->drw_lock);
494         mtx_destroy(&dev->vbl_lock);
495         mtx_destroy(&dev->irq_lock);
496         mtx_destroy(&dev->dev_lock);
497
498         return retcode;
499 }
500
501 static void drm_unload(struct drm_device *dev)
502 {
503         int i;
504
505         DRM_DEBUG("\n");
506
507         drm_sysctl_cleanup(dev);
508         destroy_dev(dev->devnode);
509
510         drm_ctxbitmap_cleanup(dev);
511
512         if (dev->agp && dev->agp->mtrr) {
513                 int __unused retcode;
514
515                 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base,
516                     dev->agp->info.ai_aperture_size, DRM_MTRR_WC);
517                 DRM_DEBUG("mtrr_del = %d", retcode);
518         }
519
520         drm_vblank_cleanup(dev);
521
522         DRM_LOCK();
523         drm_lastclose(dev);
524         DRM_UNLOCK();
525
526         /* Clean up PCI resources allocated by drm_bufs.c.  We're not really
527          * worried about resource consumption while the DRM is inactive (between
528          * lastclose and firstopen or unload) because these aren't actually
529          * taking up KVA, just keeping the PCI resource allocated.
530          */
531         for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
532                 if (dev->pcir[i] == NULL)
533                         continue;
534                 bus_release_resource(dev->device, SYS_RES_MEMORY,
535                     dev->pcirid[i], dev->pcir[i]);
536                 dev->pcir[i] = NULL;
537         }
538
539         if (dev->agp) {
540                 free(dev->agp, DRM_MEM_AGPLISTS);
541                 dev->agp = NULL;
542         }
543
544         if (dev->driver->unload != NULL) {
545                 DRM_LOCK();
546                 dev->driver->unload(dev);
547                 DRM_UNLOCK();
548         }
549
550         delete_unrhdr(dev->drw_unrhdr);
551         delete_unrhdr(dev->map_unrhdr);
552
553         drm_mem_uninit();
554
555         if (pci_disable_busmaster(dev->device))
556                 DRM_ERROR("Request to disable bus-master failed.\n");
557
558         mtx_destroy(&dev->drw_lock);
559         mtx_destroy(&dev->vbl_lock);
560         mtx_destroy(&dev->irq_lock);
561         mtx_destroy(&dev->dev_lock);
562 }
563
564 int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv)
565 {
566         struct drm_version *version = data;
567         int len;
568
569 #define DRM_COPY( name, value )                                         \
570         len = strlen( value );                                          \
571         if ( len > name##_len ) len = name##_len;                       \
572         name##_len = strlen( value );                                   \
573         if ( len && name ) {                                            \
574                 if ( DRM_COPY_TO_USER( name, value, len ) )             \
575                         return EFAULT;                          \
576         }
577
578         version->version_major          = dev->driver->major;
579         version->version_minor          = dev->driver->minor;
580         version->version_patchlevel     = dev->driver->patchlevel;
581
582         DRM_COPY(version->name, dev->driver->name);
583         DRM_COPY(version->date, dev->driver->date);
584         DRM_COPY(version->desc, dev->driver->desc);
585
586         return 0;
587 }
588
589 int drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
590 {
591         struct drm_device *dev = NULL;
592         int retcode = 0;
593
594         dev = kdev->si_drv1;
595
596         DRM_DEBUG("open_count = %d\n", dev->open_count);
597
598         retcode = drm_open_helper(kdev, flags, fmt, p, dev);
599
600         if (!retcode) {
601                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
602                 DRM_LOCK();
603                 device_busy(dev->device);
604                 if (!dev->open_count++)
605                         retcode = drm_firstopen(dev);
606                 DRM_UNLOCK();
607         }
608
609         return retcode;
610 }
611
612 void drm_close(void *data)
613 {
614         struct drm_file *file_priv = data;
615         struct drm_device *dev = file_priv->dev;
616         int retcode = 0;
617
618         DRM_DEBUG("open_count = %d\n", dev->open_count);
619
620         DRM_LOCK();
621
622         if (dev->driver->preclose != NULL)
623                 dev->driver->preclose(dev, file_priv);
624
625         /* ========================================================
626          * Begin inline drm_release
627          */
628
629         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
630             DRM_CURRENTPID, (long)dev->device, dev->open_count);
631
632         if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
633             && dev->lock.file_priv == file_priv) {
634                 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
635                           DRM_CURRENTPID,
636                           _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
637                 if (dev->driver->reclaim_buffers_locked != NULL)
638                         dev->driver->reclaim_buffers_locked(dev, file_priv);
639
640                 drm_lock_free(&dev->lock,
641                     _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
642                 
643                                 /* FIXME: may require heavy-handed reset of
644                                    hardware at this point, possibly
645                                    processed via a callback to the X
646                                    server. */
647         } else if (dev->driver->reclaim_buffers_locked != NULL &&
648             dev->lock.hw_lock != NULL) {
649                 /* The lock is required to reclaim buffers */
650                 for (;;) {
651                         if (!dev->lock.hw_lock) {
652                                 /* Device has been unregistered */
653                                 retcode = EINTR;
654                                 break;
655                         }
656                         if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) {
657                                 dev->lock.file_priv = file_priv;
658                                 dev->lock.lock_time = jiffies;
659                                 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
660                                 break;  /* Got lock */
661                         }
662                         /* Contention */
663                         retcode = mtx_sleep((void *)&dev->lock.lock_queue,
664                             &dev->dev_lock, PCATCH, "drmlk2", 0);
665                         if (retcode)
666                                 break;
667                 }
668                 if (retcode == 0) {
669                         dev->driver->reclaim_buffers_locked(dev, file_priv);
670                         drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT);
671                 }
672         }
673
674         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
675             !dev->driver->reclaim_buffers_locked)
676                 drm_reclaim_buffers(dev, file_priv);
677
678         funsetown(&dev->buf_sigio);
679
680         if (dev->driver->postclose != NULL)
681                 dev->driver->postclose(dev, file_priv);
682         TAILQ_REMOVE(&dev->files, file_priv, link);
683         free(file_priv, DRM_MEM_FILES);
684
685         /* ========================================================
686          * End inline drm_release
687          */
688
689         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
690         device_unbusy(dev->device);
691         if (--dev->open_count == 0) {
692                 retcode = drm_lastclose(dev);
693         }
694
695         DRM_UNLOCK();
696 }
697
698 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm.
699  */
700 int drm_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int flags, 
701     DRM_STRUCTPROC *p)
702 {
703         struct drm_device *dev = drm_get_device_from_kdev(kdev);
704         int retcode = 0;
705         drm_ioctl_desc_t *ioctl;
706         int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv);
707         int nr = DRM_IOCTL_NR(cmd);
708         int is_driver_ioctl = 0;
709         struct drm_file *file_priv;
710
711         retcode = devfs_get_cdevpriv((void **)&file_priv);
712         if (retcode != 0) {
713                 DRM_ERROR("can't find authenticator\n");
714                 return EINVAL;
715         }
716
717         atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
718         ++file_priv->ioctl_count;
719
720         DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
721             DRM_CURRENTPID, cmd, nr, (long)dev->device,
722             file_priv->authenticated);
723
724         switch (cmd) {
725         case FIONBIO:
726         case FIOASYNC:
727                 return 0;
728
729         case FIOSETOWN:
730                 return fsetown(*(int *)data, &dev->buf_sigio);
731
732         case FIOGETOWN:
733                 *(int *) data = fgetown(&dev->buf_sigio);
734                 return 0;
735         }
736
737         if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
738                 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
739                 return EINVAL;
740         }
741
742         ioctl = &drm_ioctls[nr];
743         /* It's not a core DRM ioctl, try driver-specific. */
744         if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
745                 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
746                 nr -= DRM_COMMAND_BASE;
747                 if (nr > dev->driver->max_ioctl) {
748                         DRM_DEBUG("Bad driver ioctl number, 0x%x (of 0x%x)\n",
749                             nr, dev->driver->max_ioctl);
750                         return EINVAL;
751                 }
752                 ioctl = &dev->driver->ioctls[nr];
753                 is_driver_ioctl = 1;
754         }
755         func = ioctl->func;
756
757         if (func == NULL) {
758                 DRM_DEBUG("no function\n");
759                 return EINVAL;
760         }
761
762         if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
763             ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
764             ((ioctl->flags & DRM_MASTER) && !file_priv->master))
765                 return EACCES;
766
767         if (is_driver_ioctl) {
768                 DRM_LOCK();
769                 /* shared code returns -errno */
770                 retcode = -func(dev, data, file_priv);
771                 DRM_UNLOCK();
772         } else {
773                 retcode = func(dev, data, file_priv);
774         }
775
776         if (retcode != 0)
777                 DRM_DEBUG("    returning %d\n", retcode);
778
779         return retcode;
780 }
781
782 drm_local_map_t *drm_getsarea(struct drm_device *dev)
783 {
784         drm_local_map_t *map;
785
786         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
787         TAILQ_FOREACH(map, &dev->maplist, link) {
788                 if (map->type == _DRM_SHM && (map->flags & _DRM_CONTAINS_LOCK))
789                         return map;
790         }
791
792         return NULL;
793 }
794
795 #if DRM_LINUX
796
797 #include <sys/sysproto.h>
798
799 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
800
801 #define LINUX_IOCTL_DRM_MIN             0x6400
802 #define LINUX_IOCTL_DRM_MAX             0x64ff
803
804 static linux_ioctl_function_t drm_linux_ioctl;
805 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl, 
806     LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
807
808 SYSINIT(drm_register, SI_SUB_KLD, SI_ORDER_MIDDLE, 
809     linux_ioctl_register_handler, &drm_handler);
810 SYSUNINIT(drm_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE, 
811     linux_ioctl_unregister_handler, &drm_handler);
812
813 /* The bits for in/out are switched on Linux */
814 #define LINUX_IOC_IN    IOC_OUT
815 #define LINUX_IOC_OUT   IOC_IN
816
817 static int
818 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
819 {
820         int error;
821         int cmd = args->cmd;
822
823         args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
824         if (cmd & LINUX_IOC_IN)
825                 args->cmd |= IOC_IN;
826         if (cmd & LINUX_IOC_OUT)
827                 args->cmd |= IOC_OUT;
828         
829         error = ioctl(p, (struct ioctl_args *)args);
830
831         return error;
832 }
833 #endif /* DRM_LINUX */