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