672dbeddbbb282ecefecba284a3650591002d23c
[dragonfly.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  * $FreeBSD: head/sys/dev/drm2/drm_drv.c 247835 2013-03-05 09:49:34Z kib $
30  */
31
32 /** @file drm_drv.c
33  * The catch-all file for DRM device support, including module setup/teardown,
34  * open/close, and ioctl dispatch.
35  */
36
37 #include <sys/devfs.h>
38 #include <machine/limits.h>
39
40 #include <drm/drmP.h>
41 #include <drm/drm_core.h>
42
43 #ifdef DRM_DEBUG_DEFAULT_ON
44 int drm_debug = (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS |
45     DRM_DEBUGBITS_FAILED_IOCTL);
46 #else
47 int drm_debug = 0;
48 #endif
49 int drm_notyet_flag = 0;
50
51 unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
52 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
53
54 static int drm_load(struct drm_device *dev);
55 drm_pci_id_list_t *drm_find_description(int vendor, int device,
56     drm_pci_id_list_t *idlist);
57
58 #define DRIVER_SOFTC(unit) \
59         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
60
61 static int
62 drm_modevent(module_t mod, int type, void *data)
63 {
64
65         switch (type) {
66         case MOD_LOAD:
67                 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
68                 TUNABLE_INT_FETCH("drm.notyet", &drm_notyet_flag);
69                 break;
70         }
71         return (0);
72 }
73
74 static moduledata_t drm_mod = {
75         "drm",
76         drm_modevent,
77         0
78 };
79 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
80 MODULE_VERSION(drm, 1);
81 MODULE_DEPEND(drm, agp, 1, 1, 1);
82 MODULE_DEPEND(drm, pci, 1, 1, 1);
83 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
84
85 static drm_ioctl_desc_t           drm_ioctls[256] = {
86         DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0),
87         DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
88         DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
89         DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
90         DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0),
91         DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
92         DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
93         DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED),
94         DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER|DRM_ROOT_ONLY),
95
96         DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
97         DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
98         DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
99         DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
100
101         DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
102         DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
103
104         DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
105         DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
106
107         DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
108         DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
109
110         DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
111         DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
112         DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
113         DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
114         DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
115         DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
116         DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
117
118         DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
119         DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
120
121         DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
122         DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
123
124         DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
125
126         DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
127         DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
128         DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
129         DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
130         DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
131         DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma, DRM_AUTH),
132
133         DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
134
135         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
136         DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
137         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
138         DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
139         DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
140         DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
141         DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
142         DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
143
144         DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
145         DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
146         DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
147         DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
148
149         DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
150
151         DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED),
152         DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
153         DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
154
155         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
156         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
157         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
158         DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
159         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
160         DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
161         DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
162         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_MASTER|DRM_UNLOCKED),
163         DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
164         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
165         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
166         DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
167         DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
168         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_MASTER | DRM_CONTROL_ALLOW|DRM_UNLOCKED),
169         DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
170         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
171         DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
172         DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
173         DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
174         DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
175         DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
176         DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
177         DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
178         DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
179         DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
180 };
181
182 static struct dev_ops drm_cdevsw = {
183         { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
184         .d_open =       drm_open,
185         .d_close =      drm_close,
186         .d_read =       drm_read,
187         .d_ioctl =      drm_ioctl,
188         .d_kqfilter =   drm_kqfilter,
189         .d_mmap =       drm_mmap,
190         .d_mmap_single = drm_mmap_single,
191 };
192
193 static int drm_msi = 1; /* Enable by default. */
194 TUNABLE_INT("hw.drm.msi", &drm_msi);
195 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
196 SYSCTL_INT(_hw_drm, OID_AUTO, msi, CTLFLAG_RD, &drm_msi, 1,
197     "Enable MSI interrupts for drm devices");
198
199 static struct drm_msi_blacklist_entry drm_msi_blacklist[] = {
200         {0x8086, 0x2772}, /* Intel i945G        */ \
201         {0x8086, 0x27A2}, /* Intel i945GM       */ \
202         {0x8086, 0x27AE}, /* Intel i945GME      */ \
203         {0, 0}
204 };
205
206 static int drm_msi_is_blacklisted(struct drm_device *dev, unsigned long flags)
207 {
208         int i = 0;
209
210         if (dev->driver->use_msi != NULL) {
211                 int use_msi;
212
213                 use_msi = dev->driver->use_msi(dev, flags);
214
215                 return (!use_msi);
216         }
217
218         /* TODO: Maybe move this to a callback in i915? */
219         for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) {
220                 if ((drm_msi_blacklist[i].vendor == dev->pci_vendor) &&
221                     (drm_msi_blacklist[i].device == dev->pci_device)) {
222                         return 1;
223                 }
224         }
225
226         return 0;
227 }
228
229 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
230 {
231         drm_pci_id_list_t *id_entry;
232         int vendor, device;
233
234         vendor = pci_get_vendor(kdev);
235         device = pci_get_device(kdev);
236
237         if (pci_get_class(kdev) != PCIC_DISPLAY)
238                 return ENXIO;
239
240         id_entry = drm_find_description(vendor, device, idlist);
241         if (id_entry != NULL) {
242                 if (!device_get_desc(kdev)) {
243                         DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
244                         device_set_desc(kdev, id_entry->name);
245                 }
246                 return 0;
247         }
248
249         return ENXIO;
250 }
251
252 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
253 {
254         struct drm_device *dev;
255         drm_pci_id_list_t *id_entry;
256         int unit, error, msicount;
257         int rid = 0;
258
259         unit = device_get_unit(kdev);
260         dev = device_get_softc(kdev);
261
262         if (!strcmp(device_get_name(kdev), "drmsub"))
263                 dev->dev = device_get_parent(kdev);
264         else
265                 dev->dev = kdev;
266
267         dev->pci_domain = pci_get_domain(dev->dev);
268         dev->pci_bus = pci_get_bus(dev->dev);
269         dev->pci_slot = pci_get_slot(dev->dev);
270         dev->pci_func = pci_get_function(dev->dev);
271
272         dev->pci_vendor = pci_get_vendor(dev->dev);
273         dev->pci_device = pci_get_device(dev->dev);
274         dev->pci_subvendor = pci_get_subvendor(dev->dev);
275         dev->pci_subdevice = pci_get_subdevice(dev->dev);
276
277         id_entry = drm_find_description(dev->pci_vendor,
278             dev->pci_device, idlist);
279         dev->id_entry = id_entry;
280
281         if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
282                 if (drm_msi &&
283                     !drm_msi_is_blacklisted(dev, dev->id_entry->driver_private)) {
284                         msicount = pci_msi_count(dev->dev);
285                         DRM_DEBUG("MSI count = %d\n", msicount);
286                         if (msicount > 1)
287                                 msicount = 1;
288
289                         if (pci_alloc_msi(dev->dev, &rid, msicount, -1) == 0) {
290                                 DRM_INFO("MSI enabled %d message(s)\n",
291                                     msicount);
292                                 dev->msi_enabled = 1;
293                                 dev->irqrid = rid;
294                         }
295                 }
296
297                 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
298                     &dev->irqrid, RF_SHAREABLE);
299                 if (!dev->irqr) {
300                         return (ENOENT);
301                 }
302
303                 dev->irq = (int) rman_get_start(dev->irqr);
304         }
305
306         lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
307         lwkt_serialize_init(&dev->irq_lock);
308         lockinit(&dev->vbl_lock, "drmvbl", 0, LK_CANRECURSE);
309         lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
310         lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
311
312         error = drm_load(dev);
313         if (error)
314                 goto error;
315
316         error = drm_create_cdevs(kdev);
317         if (error)
318                 goto error;
319
320         return (error);
321 error:
322         if (dev->irqr) {
323                 bus_release_resource(dev->dev, SYS_RES_IRQ,
324                     dev->irqrid, dev->irqr);
325         }
326         if (dev->msi_enabled) {
327                 pci_release_msi(dev->dev);
328         }
329         return (error);
330 }
331
332 int
333 drm_create_cdevs(device_t kdev)
334 {
335         struct drm_device *dev;
336         int error, unit;
337
338         unit = device_get_unit(kdev);
339         dev = device_get_softc(kdev);
340
341         dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
342                                 DRM_DEV_MODE, "dri/card%d", unit);
343         error = 0;
344         if (error == 0)
345                 dev->devnode->si_drv1 = dev;
346         return (error);
347 }
348
349 #ifndef DRM_DEV_NAME
350 #define DRM_DEV_NAME "drm"
351 #endif
352
353 devclass_t drm_devclass;
354
355 drm_pci_id_list_t *drm_find_description(int vendor, int device,
356     drm_pci_id_list_t *idlist)
357 {
358         int i = 0;
359
360         for (i = 0; idlist[i].vendor != 0; i++) {
361                 if ((idlist[i].vendor == vendor) &&
362                     ((idlist[i].device == device) ||
363                     (idlist[i].device == 0))) {
364                         return &idlist[i];
365                 }
366         }
367         return NULL;
368 }
369
370 /**
371  * Take down the DRM device.
372  *
373  * \param dev DRM device structure.
374  *
375  * Frees every resource in \p dev.
376  *
377  * \sa drm_device
378  */
379 int drm_lastclose(struct drm_device * dev)
380 {
381         drm_magic_entry_t *pt, *next;
382         int i;
383
384         DRM_DEBUG("\n");
385
386         if (dev->driver->lastclose != NULL)
387                 dev->driver->lastclose(dev);
388
389         if (!drm_core_check_feature(dev, DRIVER_MODESET) && dev->irq_enabled)
390                 drm_irq_uninstall(dev);
391
392         DRM_LOCK(dev);
393         if (dev->unique) {
394                 drm_free(dev->unique, M_DRM);
395                 dev->unique = NULL;
396                 dev->unique_len = 0;
397         }
398         /* Clear pid list */
399         for (i = 0; i < DRM_HASH_SIZE; i++) {
400                 for (pt = dev->magiclist[i].head; pt; pt = next) {
401                         next = pt->next;
402                         drm_free(pt, M_DRM);
403                 }
404                 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
405         }
406
407         /* Clear AGP information */
408         if (dev->agp) {
409                 drm_agp_mem_t *entry;
410                 drm_agp_mem_t *nexte;
411
412                 /* Remove AGP resources, but leave dev->agp intact until
413                  * drm_unload is called.
414                  */
415                 for (entry = dev->agp->memory; entry; entry = nexte) {
416                         nexte = entry->next;
417                         if (entry->bound)
418                                 drm_agp_unbind_memory(entry->handle);
419                         drm_agp_free_memory(entry->handle);
420                         drm_free(entry, M_DRM);
421                 }
422                 dev->agp->memory = NULL;
423
424                 if (dev->agp->acquired)
425                         drm_agp_release(dev);
426
427                 dev->agp->acquired = 0;
428                 dev->agp->enabled  = 0;
429         }
430         if (dev->sg != NULL) {
431                 drm_sg_cleanup(dev->sg);
432                 dev->sg = NULL;
433         }
434
435         drm_dma_takedown(dev);
436         if (dev->lock.hw_lock) {
437                 dev->lock.hw_lock = NULL; /* SHM removed */
438                 dev->lock.file_priv = NULL;
439                 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
440         }
441         DRM_UNLOCK(dev);
442
443         return 0;
444 }
445
446 static int drm_load(struct drm_device *dev)
447 {
448         int i, retcode;
449
450         DRM_DEBUG("\n");
451
452         INIT_LIST_HEAD(&dev->maplist);
453
454         drm_mem_init();
455         drm_sysctl_init(dev);
456         INIT_LIST_HEAD(&dev->filelist);
457
458         dev->counters  = 6;
459         dev->types[0]  = _DRM_STAT_LOCK;
460         dev->types[1]  = _DRM_STAT_OPENS;
461         dev->types[2]  = _DRM_STAT_CLOSES;
462         dev->types[3]  = _DRM_STAT_IOCTLS;
463         dev->types[4]  = _DRM_STAT_LOCKS;
464         dev->types[5]  = _DRM_STAT_UNLOCKS;
465
466         for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
467                 atomic_set(&dev->counts[i], 0);
468
469         INIT_LIST_HEAD(&dev->vblank_event_list);
470
471         if (drm_core_has_AGP(dev)) {
472                 if (drm_device_is_agp(dev))
473                         dev->agp = drm_agp_init();
474                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
475                     dev->agp == NULL) {
476                         DRM_ERROR("Card isn't AGP, or couldn't initialize "
477                             "AGP.\n");
478                         retcode = ENOMEM;
479                         goto error;
480                 }
481                 if (dev->agp != NULL && dev->agp->agp_info.ai_aperture_base != 0) {
482                         if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
483                             dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
484                                 dev->agp->agp_mtrr = 1;
485                 }
486         }
487
488         retcode = drm_ctxbitmap_init(dev);
489         if (retcode != 0) {
490                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
491                 goto error;
492         }
493
494         if (dev->driver->driver_features & DRIVER_GEM) {
495                 retcode = drm_gem_init(dev);
496                 if (retcode != 0) {
497                         DRM_ERROR("Cannot initialize graphics execution "
498                                   "manager (GEM)\n");
499                         goto error1;
500                 }
501         }
502
503         if (dev->driver->load != NULL) {
504                 DRM_LOCK(dev);
505                 /* Shared code returns -errno. */
506                 retcode = -dev->driver->load(dev,
507                     dev->id_entry->driver_private);
508                 if (pci_enable_busmaster(dev->dev))
509                         DRM_ERROR("Request to enable bus-master failed.\n");
510                 DRM_UNLOCK(dev);
511                 if (retcode != 0)
512                         goto error1;
513         }
514
515         DRM_INFO("Initialized %s %d.%d.%d %s\n",
516             dev->driver->name,
517             dev->driver->major,
518             dev->driver->minor,
519             dev->driver->patchlevel,
520             dev->driver->date);
521
522         return 0;
523
524 error1:
525         drm_gem_destroy(dev);
526 error:
527         drm_ctxbitmap_cleanup(dev);
528         drm_sysctl_cleanup(dev);
529         DRM_LOCK(dev);
530         drm_lastclose(dev);
531         DRM_UNLOCK(dev);
532         if (dev->devnode != NULL)
533                 destroy_dev(dev->devnode);
534
535         lockuninit(&dev->vbl_lock);
536         lockuninit(&dev->dev_lock);
537         lockuninit(&dev->event_lock);
538         lockuninit(&dev->struct_mutex);
539
540         return retcode;
541 }
542
543 int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv)
544 {
545         struct drm_version *version = data;
546         int len;
547
548 #define DRM_COPY( name, value )                                         \
549         len = strlen( value );                                          \
550         if ( len > name##_len ) len = name##_len;                       \
551         name##_len = strlen( value );                                   \
552         if ( len && name ) {                                            \
553                 if ( DRM_COPY_TO_USER( name, value, len ) )             \
554                         return EFAULT;                          \
555         }
556
557         version->version_major          = dev->driver->major;
558         version->version_minor          = dev->driver->minor;
559         version->version_patchlevel     = dev->driver->patchlevel;
560
561         DRM_COPY(version->name, dev->driver->name);
562         DRM_COPY(version->date, dev->driver->date);
563         DRM_COPY(version->desc, dev->driver->desc);
564
565         return 0;
566 }
567
568 /*
569  * Stub is needed for devfs
570  */
571 int drm_close(struct dev_close_args *ap)
572 {
573         return 0;
574 }
575
576 void drm_cdevpriv_dtor(void *cd)
577 {
578         struct drm_file *file_priv = cd;
579         struct drm_device *dev = file_priv->dev;
580         int retcode = 0;
581
582         DRM_DEBUG("open_count = %d\n", dev->open_count);
583
584         DRM_LOCK(dev);
585
586         if (dev->driver->preclose != NULL)
587                 dev->driver->preclose(dev, file_priv);
588
589         /* ========================================================
590          * Begin inline drm_release
591          */
592
593         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
594             DRM_CURRENTPID, (long)dev->dev, dev->open_count);
595
596         if (dev->driver->driver_features & DRIVER_GEM)
597                 drm_gem_release(dev, file_priv);
598
599         if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
600             && dev->lock.file_priv == file_priv) {
601                 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
602                           DRM_CURRENTPID,
603                           _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
604                 if (dev->driver->reclaim_buffers_locked != NULL)
605                         dev->driver->reclaim_buffers_locked(dev, file_priv);
606
607                 drm_lock_free(&dev->lock,
608                     _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
609
610                                 /* FIXME: may require heavy-handed reset of
611                                    hardware at this point, possibly
612                                    processed via a callback to the X
613                                    server. */
614         } else if (dev->driver->reclaim_buffers_locked != NULL &&
615             dev->lock.hw_lock != NULL) {
616                 /* The lock is required to reclaim buffers */
617                 for (;;) {
618                         if (!dev->lock.hw_lock) {
619                                 /* Device has been unregistered */
620                                 retcode = EINTR;
621                                 break;
622                         }
623                         if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) {
624                                 dev->lock.file_priv = file_priv;
625                                 dev->lock.lock_time = jiffies;
626                                 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
627                                 break;  /* Got lock */
628                         }
629                         /* Contention */
630                         retcode = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue,
631                             PCATCH, "drmlk2", 0);
632                         if (retcode)
633                                 break;
634                 }
635                 if (retcode == 0) {
636                         dev->driver->reclaim_buffers_locked(dev, file_priv);
637                         drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT);
638                 }
639         }
640
641         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
642             !dev->driver->reclaim_buffers_locked)
643                 drm_reclaim_buffers(dev, file_priv);
644
645         funsetown(&dev->buf_sigio);
646
647         if (dev->driver->postclose != NULL)
648                 dev->driver->postclose(dev, file_priv);
649         list_del(&file_priv->lhead);
650
651
652         /* ========================================================
653          * End inline drm_release
654          */
655
656         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
657         device_unbusy(dev->dev);
658         if (--dev->open_count == 0) {
659                 retcode = drm_lastclose(dev);
660         }
661
662         DRM_UNLOCK(dev);
663 }
664
665 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm.
666  */
667 int drm_ioctl(struct dev_ioctl_args *ap)
668 {
669         struct cdev *kdev = ap->a_head.a_dev;
670         u_long cmd = ap->a_cmd;
671         caddr_t data = ap->a_data;
672         struct thread *p = curthread;
673         struct drm_device *dev = drm_get_device_from_kdev(kdev);
674         int retcode = 0;
675         drm_ioctl_desc_t *ioctl;
676         int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv);
677         int nr = DRM_IOCTL_NR(cmd);
678         int is_driver_ioctl = 0;
679         struct drm_file *file_priv;
680
681         retcode = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
682         if (retcode !=0) {
683                 DRM_ERROR("can't find authenticator\n");
684                 return EINVAL;
685         }
686
687         atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
688         ++file_priv->ioctl_count;
689
690         DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
691             DRM_CURRENTPID, cmd, nr, (long)dev->dev,
692             file_priv->authenticated);
693
694         switch (cmd) {
695         case FIONBIO:
696         case FIOASYNC:
697                 return 0;
698
699         case FIOSETOWN:
700                 return fsetown(*(int *)data, &dev->buf_sigio);
701
702         case FIOGETOWN:
703                 *(int *) data = fgetown(&dev->buf_sigio);
704                 return 0;
705         }
706
707         if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
708                 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
709                 return EINVAL;
710         }
711
712         ioctl = &drm_ioctls[nr];
713         /* It's not a core DRM ioctl, try driver-specific. */
714         if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
715                 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
716                 nr -= DRM_COMMAND_BASE;
717                 if (nr > dev->driver->num_ioctls) {
718                         return EINVAL;
719                 }
720                 ioctl = &dev->driver->ioctls[nr];
721                 is_driver_ioctl = 1;
722         }
723         func = ioctl->func;
724
725         if (func == NULL) {
726                 DRM_DEBUG("no function\n");
727                 return EINVAL;
728         }
729
730         if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
731             ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
732             ((ioctl->flags & DRM_MASTER) && !file_priv->master))
733                 return EACCES;
734
735         if (is_driver_ioctl) {
736                 if ((ioctl->flags & DRM_UNLOCKED) == 0)
737                         DRM_LOCK(dev);
738                 /* shared code returns -errno */
739                 retcode = -func(dev, data, file_priv);
740                 if ((ioctl->flags & DRM_UNLOCKED) == 0)
741                         DRM_UNLOCK(dev);
742         } else {
743                 retcode = func(dev, data, file_priv);
744         }
745
746         if (retcode != 0)
747                 DRM_DEBUG("    returning %d\n", retcode);
748         if (retcode != 0 &&
749             (drm_debug & DRM_DEBUGBITS_FAILED_IOCTL) != 0) {
750                 kprintf(
751 "pid %d, cmd 0x%02lx, nr 0x%02x/%1d, dev 0x%lx, auth %d, res %d\n",
752                     DRM_CURRENTPID, cmd, nr, is_driver_ioctl, (long)dev->dev,
753                     file_priv->authenticated, retcode);
754         }
755
756         return retcode;
757 }
758
759 drm_local_map_t *drm_getsarea(struct drm_device *dev)
760 {
761         struct drm_map_list *entry;
762
763         list_for_each_entry(entry, &dev->maplist, head) {
764                 if (entry->map && entry->map->type == _DRM_SHM &&
765                     (entry->map->flags & _DRM_CONTAINS_LOCK)) {
766                         return entry->map;
767                 }
768         }
769
770         return NULL;
771 }
772
773 int
774 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
775     struct sysctl_oid *top)
776 {
777         struct sysctl_oid *oid;
778
779         ksnprintf(dev->busid_str, sizeof(dev->busid_str),
780              "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
781              dev->pci_slot, dev->pci_func);
782         oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
783             CTLFLAG_RD, dev->busid_str, 0, NULL);
784         if (oid == NULL)
785                 return (ENOMEM);
786         dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
787         oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
788             "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
789         if (oid == NULL)
790                 return (ENOMEM);
791
792         return (0);
793 }
794
795 int
796 drm_mmap_single(struct dev_mmap_single_args *ap)
797 {
798         struct drm_device *dev;
799         struct cdev *kdev = ap->a_head.a_dev;
800         vm_ooffset_t *offset = ap->a_offset;
801         vm_size_t size = ap->a_size;
802         struct vm_object **obj_res = ap->a_object;
803         int nprot = ap->a_nprot;
804
805         dev = drm_get_device_from_kdev(kdev);
806         if (dev->drm_ttm_bdev != NULL) {
807                 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
808                     obj_res, nprot));
809         } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
810                 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
811         } else {
812                 return (ENODEV);
813         }
814 }
815
816 #if DRM_LINUX
817
818 #include <sys/sysproto.h>
819
820 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
821
822 #define LINUX_IOCTL_DRM_MIN             0x6400
823 #define LINUX_IOCTL_DRM_MAX             0x64ff
824
825 static linux_ioctl_function_t drm_linux_ioctl;
826 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
827     LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
828
829 /* The bits for in/out are switched on Linux */
830 #define LINUX_IOC_IN    IOC_OUT
831 #define LINUX_IOC_OUT   IOC_IN
832
833 static int
834 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
835 {
836         int error;
837         int cmd = args->cmd;
838
839         args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
840         if (cmd & LINUX_IOC_IN)
841                 args->cmd |= IOC_IN;
842         if (cmd & LINUX_IOC_OUT)
843                 args->cmd |= IOC_OUT;
844         
845         error = ioctl(p, (struct ioctl_args *)args);
846
847         return error;
848 }
849 #endif /* DRM_LINUX */
850
851 static int
852 drm_core_init(void *arg)
853 {
854
855         drm_global_init();
856
857 #if DRM_LINUX
858         linux_ioctl_register_handler(&drm_handler);
859 #endif /* DRM_LINUX */
860
861         DRM_INFO("Initialized %s %d.%d.%d %s\n",
862                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
863         return 0;
864 }
865
866 static void
867 drm_core_exit(void *arg)
868 {
869
870 #if DRM_LINUX
871         linux_ioctl_unregister_handler(&drm_handler);
872 #endif /* DRM_LINUX */
873
874         drm_global_release();
875 }
876
877 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
878     drm_core_init, NULL);
879 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
880     drm_core_exit, NULL);
881
882 /*
883  * Check if dmi_system_id structure matches system DMI data
884  */
885 static bool
886 dmi_found(const struct dmi_system_id *dsi)
887 {
888         int i, slot;
889         bool found = false;
890         char *sys_vendor, *board_vendor, *product_name, *board_name;
891
892         sys_vendor = kgetenv("smbios.system.maker");
893         board_vendor = kgetenv("smbios.planar.maker");
894         product_name = kgetenv("smbios.system.product");
895         board_name = kgetenv("smbios.planar.product");
896
897         for (i = 0; i < NELEM(dsi->matches); i++) {
898                 slot = dsi->matches[i].slot;
899                 switch (slot) {
900                 case DMI_NONE:
901                         break;
902                 case DMI_SYS_VENDOR:
903                         if (sys_vendor != NULL &&
904                             !strcmp(sys_vendor, dsi->matches[i].substr))
905                                 break;
906                         else
907                                 goto done;
908                 case DMI_BOARD_VENDOR:
909                         if (board_vendor != NULL &&
910                             !strcmp(board_vendor, dsi->matches[i].substr))
911                                 break;
912                         else
913                                 goto done;
914                 case DMI_PRODUCT_NAME:
915                         if (product_name != NULL &&
916                             !strcmp(product_name, dsi->matches[i].substr))
917                                 break;
918                         else
919                                 goto done;
920                 case DMI_BOARD_NAME:
921                         if (board_name != NULL &&
922                             !strcmp(board_name, dsi->matches[i].substr))
923                                 break;
924                         else
925                                 goto done;
926                 default:
927                         goto done;
928                 }
929         }
930         found = true;
931
932 done:
933         if (sys_vendor != NULL)
934                 kfreeenv(sys_vendor);
935         if (board_vendor != NULL)
936                 kfreeenv(board_vendor);
937         if (product_name != NULL)
938                 kfreeenv(product_name);
939         if (board_name != NULL)
940                 kfreeenv(board_name);
941
942         return found;
943 }
944
945 bool
946 dmi_check_system(const struct dmi_system_id *sysid)
947 {
948         const struct dmi_system_id *dsi;
949         int num = 0;
950
951         for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
952                 if (dmi_found(dsi)) {
953                         num++;
954                         if (dsi->callback && dsi->callback(dsi))
955                                 break;
956                 }
957         }
958         return (num);
959 }
960