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