drm - Add valleyview support
[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 = 0; /* Disable by default. This is because there are issues with
228                            freezes using MSI and i915 
229                          */
230 TUNABLE_INT("hw.drm.msi.enable", &drm_msi);
231 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
232 SYSCTL_NODE(_hw_drm, OID_AUTO, msi, CTLFLAG_RW, NULL, "DRM device msi");
233 SYSCTL_INT(_hw_drm_msi, OID_AUTO, enable, CTLFLAG_RD, &drm_msi, 0,
234     "Enable MSI interrupts for drm devices");
235 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
236     "DRM debugging");
237
238 static struct drm_msi_blacklist_entry drm_msi_blacklist[] = {
239         {0x8086, 0x2772}, /* Intel i945G        */ \
240         {0x8086, 0x27A2}, /* Intel i945GM       */ \
241         {0x8086, 0x27AE}, /* Intel i945GME      */ \
242         {0, 0}
243 };
244
245 static int drm_msi_is_blacklisted(struct drm_device *dev, unsigned long flags)
246 {
247         int i = 0;
248
249         if (dev->driver->use_msi != NULL) {
250                 int use_msi;
251
252                 use_msi = dev->driver->use_msi(dev, flags);
253
254                 return (!use_msi);
255         }
256
257         /* TODO: Maybe move this to a callback in i915? */
258         for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) {
259                 if ((drm_msi_blacklist[i].vendor == dev->pci_vendor) &&
260                     (drm_msi_blacklist[i].device == dev->pci_device)) {
261                         return 1;
262                 }
263         }
264
265         return 0;
266 }
267
268 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
269 {
270         drm_pci_id_list_t *id_entry;
271         int vendor, device;
272
273         vendor = pci_get_vendor(kdev);
274         device = pci_get_device(kdev);
275
276         if (pci_get_class(kdev) != PCIC_DISPLAY)
277                 return ENXIO;
278
279         id_entry = drm_find_description(vendor, device, idlist);
280         if (id_entry != NULL) {
281                 if (!device_get_desc(kdev)) {
282                         DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
283                         device_set_desc(kdev, id_entry->name);
284                 }
285                 return 0;
286         }
287
288         return ENXIO;
289 }
290
291 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
292 {
293         struct drm_device *dev;
294         drm_pci_id_list_t *id_entry;
295         int unit, error;
296         u_int irq_flags;
297         int msi_enable;
298
299         unit = device_get_unit(kdev);
300         dev = device_get_softc(kdev);
301
302         if (!strcmp(device_get_name(kdev), "drmsub"))
303                 dev->dev = device_get_parent(kdev);
304         else
305                 dev->dev = kdev;
306
307         dev->pci_domain = pci_get_domain(dev->dev);
308         dev->pci_bus = pci_get_bus(dev->dev);
309         dev->pci_slot = pci_get_slot(dev->dev);
310         dev->pci_func = pci_get_function(dev->dev);
311
312         dev->pci_vendor = pci_get_vendor(dev->dev);
313         dev->pci_device = pci_get_device(dev->dev);
314         dev->pci_subvendor = pci_get_subvendor(dev->dev);
315         dev->pci_subdevice = pci_get_subdevice(dev->dev);
316
317         id_entry = drm_find_description(dev->pci_vendor,
318             dev->pci_device, idlist);
319         dev->id_entry = id_entry;
320
321         if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
322                 msi_enable = drm_msi;
323
324                 if (drm_msi_is_blacklisted(dev, dev->id_entry->driver_private)) {
325                         msi_enable = 0;
326                 }
327
328                 dev->irq_type = pci_alloc_1intr(dev->dev, msi_enable,
329                     &dev->irqrid, &irq_flags);
330
331                 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
332                     &dev->irqrid, irq_flags);
333
334                 if (!dev->irqr) {
335                         return (ENOENT);
336                 }
337
338                 dev->irq = (int) rman_get_start(dev->irqr);
339         }
340
341         lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
342         lwkt_serialize_init(&dev->irq_lock);
343         lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
344         lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
345
346         error = drm_load(dev);
347         if (error)
348                 goto error;
349
350         error = drm_create_cdevs(kdev);
351         if (error)
352                 goto error;
353
354         return (error);
355 error:
356         if (dev->irqr) {
357                 bus_release_resource(dev->dev, SYS_RES_IRQ,
358                     dev->irqrid, dev->irqr);
359         }
360         if (dev->irq_type == PCI_INTR_TYPE_MSI) {
361                 pci_release_msi(dev->dev);
362         }
363         return (error);
364 }
365
366 int
367 drm_create_cdevs(device_t kdev)
368 {
369         struct drm_device *dev;
370         int error, unit;
371
372         unit = device_get_unit(kdev);
373         dev = device_get_softc(kdev);
374
375         dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
376                                 DRM_DEV_MODE, "dri/card%d", unit);
377         error = 0;
378         if (error == 0)
379                 dev->devnode->si_drv1 = dev;
380         return (error);
381 }
382
383 #ifndef DRM_DEV_NAME
384 #define DRM_DEV_NAME "drm"
385 #endif
386
387 devclass_t drm_devclass;
388
389 drm_pci_id_list_t *drm_find_description(int vendor, int device,
390     drm_pci_id_list_t *idlist)
391 {
392         int i = 0;
393
394         for (i = 0; idlist[i].vendor != 0; i++) {
395                 if ((idlist[i].vendor == vendor) &&
396                     ((idlist[i].device == device) ||
397                     (idlist[i].device == 0))) {
398                         return &idlist[i];
399                 }
400         }
401         return NULL;
402 }
403
404 /**
405  * Take down the DRM device.
406  *
407  * \param dev DRM device structure.
408  *
409  * Frees every resource in \p dev.
410  *
411  * \sa drm_device
412  */
413 int drm_lastclose(struct drm_device * dev)
414 {
415         drm_magic_entry_t *pt, *next;
416
417         DRM_DEBUG("\n");
418
419         if (dev->driver->lastclose != NULL)
420                 dev->driver->lastclose(dev);
421
422         if (!drm_core_check_feature(dev, DRIVER_MODESET) && dev->irq_enabled)
423                 drm_irq_uninstall(dev);
424
425         DRM_LOCK(dev);
426         if (dev->unique) {
427                 drm_free(dev->unique, M_DRM);
428                 dev->unique = NULL;
429                 dev->unique_len = 0;
430         }
431
432         /* Clear pid list */
433         if (dev->magicfree.next) {
434                 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
435                         list_del(&pt->head);
436                         drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
437                         kfree(pt);
438                 }
439                 drm_ht_remove(&dev->magiclist);
440         }
441         
442         /* Clear AGP information */
443         if (dev->agp) {
444                 drm_agp_mem_t *entry;
445                 drm_agp_mem_t *nexte;
446
447                 /* Remove AGP resources, but leave dev->agp intact until
448                  * drm_unload is called.
449                  */
450                 for (entry = dev->agp->memory; entry; entry = nexte) {
451                         nexte = entry->next;
452                         if (entry->bound)
453                                 drm_agp_unbind_memory(entry->handle);
454                         drm_agp_free_memory(entry->handle);
455                         drm_free(entry, M_DRM);
456                 }
457                 dev->agp->memory = NULL;
458
459                 if (dev->agp->acquired)
460                         drm_agp_release(dev);
461
462                 dev->agp->acquired = 0;
463                 dev->agp->enabled  = 0;
464         }
465         if (dev->sg != NULL) {
466                 drm_sg_cleanup(dev->sg);
467                 dev->sg = NULL;
468         }
469
470         drm_dma_takedown(dev);
471         if (dev->lock.hw_lock) {
472                 dev->lock.hw_lock = NULL; /* SHM removed */
473                 dev->lock.file_priv = NULL;
474                 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
475         }
476         DRM_UNLOCK(dev);
477
478         return 0;
479 }
480
481 static int drm_load(struct drm_device *dev)
482 {
483         int i, retcode;
484
485         DRM_DEBUG("\n");
486
487         INIT_LIST_HEAD(&dev->maplist);
488
489         drm_mem_init();
490         drm_sysctl_init(dev);
491         INIT_LIST_HEAD(&dev->filelist);
492
493         dev->counters  = 6;
494         dev->types[0]  = _DRM_STAT_LOCK;
495         dev->types[1]  = _DRM_STAT_OPENS;
496         dev->types[2]  = _DRM_STAT_CLOSES;
497         dev->types[3]  = _DRM_STAT_IOCTLS;
498         dev->types[4]  = _DRM_STAT_LOCKS;
499         dev->types[5]  = _DRM_STAT_UNLOCKS;
500
501         for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
502                 atomic_set(&dev->counts[i], 0);
503
504         INIT_LIST_HEAD(&dev->vblank_event_list);
505
506         if (drm_core_has_AGP(dev)) {
507                 if (drm_device_is_agp(dev))
508                         dev->agp = drm_agp_init();
509                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
510                     dev->agp == NULL) {
511                         DRM_ERROR("Card isn't AGP, or couldn't initialize "
512                             "AGP.\n");
513                         retcode = ENOMEM;
514                         goto error;
515                 }
516                 if (dev->agp != NULL && dev->agp->agp_info.ai_aperture_base != 0) {
517                         if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
518                             dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
519                                 dev->agp->agp_mtrr = 1;
520                 }
521         }
522
523         retcode = drm_ctxbitmap_init(dev);
524         if (retcode != 0) {
525                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
526                 goto error;
527         }
528
529         if (dev->driver->driver_features & DRIVER_GEM) {
530                 retcode = drm_gem_init(dev);
531                 if (retcode != 0) {
532                         DRM_ERROR("Cannot initialize graphics execution "
533                                   "manager (GEM)\n");
534                         goto error1;
535                 }
536         }
537
538         if (dev->driver->load != NULL) {
539                 DRM_LOCK(dev);
540                 /* Shared code returns -errno. */
541                 retcode = -dev->driver->load(dev,
542                     dev->id_entry->driver_private);
543                 if (pci_enable_busmaster(dev->dev))
544                         DRM_ERROR("Request to enable bus-master failed.\n");
545                 DRM_UNLOCK(dev);
546                 if (retcode != 0)
547                         goto error1;
548         }
549
550         DRM_INFO("Initialized %s %d.%d.%d %s\n",
551             dev->driver->name,
552             dev->driver->major,
553             dev->driver->minor,
554             dev->driver->patchlevel,
555             dev->driver->date);
556
557         return 0;
558
559 error1:
560         drm_gem_destroy(dev);
561 error:
562         drm_ctxbitmap_cleanup(dev);
563         drm_sysctl_cleanup(dev);
564         DRM_LOCK(dev);
565         drm_lastclose(dev);
566         DRM_UNLOCK(dev);
567         if (dev->devnode != NULL)
568                 destroy_dev(dev->devnode);
569
570         lockuninit(&dev->vbl_lock);
571         lockuninit(&dev->dev_lock);
572         lockuninit(&dev->event_lock);
573         lockuninit(&dev->struct_mutex);
574
575         return retcode;
576 }
577
578 /**
579  * Get version information
580  *
581  * \param inode device inode.
582  * \param filp file pointer.
583  * \param cmd command.
584  * \param arg user argument, pointing to a drm_version structure.
585  * \return zero on success or negative number on failure.
586  *
587  * Fills in the version information in \p arg.
588  */
589 int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv)
590 {
591         struct drm_version *version = data;
592         int len;
593
594 #define DRM_COPY( name, value )                                         \
595         len = strlen( value );                                          \
596         if ( len > name##_len ) len = name##_len;                       \
597         name##_len = strlen( value );                                   \
598         if ( len && name ) {                                            \
599                 if ( DRM_COPY_TO_USER( name, value, len ) )             \
600                         return EFAULT;                          \
601         }
602
603         version->version_major          = dev->driver->major;
604         version->version_minor          = dev->driver->minor;
605         version->version_patchlevel     = dev->driver->patchlevel;
606
607         DRM_COPY(version->name, dev->driver->name);
608         DRM_COPY(version->date, dev->driver->date);
609         DRM_COPY(version->desc, dev->driver->desc);
610
611         return 0;
612 }
613
614 /*
615  * Stub is needed for devfs
616  */
617 int drm_close(struct dev_close_args *ap)
618 {
619         return 0;
620 }
621
622 void drm_cdevpriv_dtor(void *cd)
623 {
624         struct drm_file *file_priv = cd;
625         struct drm_device *dev = file_priv->dev;
626         int retcode = 0;
627
628         DRM_DEBUG("open_count = %d\n", dev->open_count);
629
630         DRM_LOCK(dev);
631
632         if (dev->driver->preclose != NULL)
633                 dev->driver->preclose(dev, file_priv);
634
635         /* ========================================================
636          * Begin inline drm_release
637          */
638
639         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
640             DRM_CURRENTPID, (long)dev->dev, dev->open_count);
641
642         if (dev->driver->driver_features & DRIVER_GEM)
643                 drm_gem_release(dev, file_priv);
644
645         if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
646             && dev->lock.file_priv == file_priv) {
647                 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
648                           DRM_CURRENTPID,
649                           _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
650                 if (dev->driver->reclaim_buffers_locked != NULL)
651                         dev->driver->reclaim_buffers_locked(dev, file_priv);
652
653                 drm_lock_free(&dev->lock,
654                     _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
655
656                                 /* FIXME: may require heavy-handed reset of
657                                    hardware at this point, possibly
658                                    processed via a callback to the X
659                                    server. */
660         } else if (dev->driver->reclaim_buffers_locked != NULL &&
661             dev->lock.hw_lock != NULL) {
662                 /* The lock is required to reclaim buffers */
663                 for (;;) {
664                         if (!dev->lock.hw_lock) {
665                                 /* Device has been unregistered */
666                                 retcode = EINTR;
667                                 break;
668                         }
669                         if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) {
670                                 dev->lock.file_priv = file_priv;
671                                 dev->lock.lock_time = jiffies;
672                                 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
673                                 break;  /* Got lock */
674                         }
675                         /* Contention */
676                         retcode = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue,
677                             PCATCH, "drmlk2", 0);
678                         if (retcode)
679                                 break;
680                 }
681                 if (retcode == 0) {
682                         dev->driver->reclaim_buffers_locked(dev, file_priv);
683                         drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT);
684                 }
685         }
686
687         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
688             !dev->driver->reclaim_buffers_locked)
689                 drm_reclaim_buffers(dev, file_priv);
690
691         funsetown(&dev->buf_sigio);
692
693         if (dev->driver->postclose != NULL)
694                 dev->driver->postclose(dev, file_priv);
695         list_del(&file_priv->lhead);
696
697
698         /* ========================================================
699          * End inline drm_release
700          */
701
702         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
703         device_unbusy(dev->dev);
704         if (--dev->open_count == 0) {
705                 retcode = drm_lastclose(dev);
706         }
707
708         DRM_UNLOCK(dev);
709 }
710
711 /**
712  * Called whenever a process performs an ioctl on /dev/drm.
713  *
714  * \param inode device inode.
715  * \param file_priv DRM file private.
716  * \param cmd command.
717  * \param arg user argument.
718  * \return zero on success or negative number on failure.
719  *
720  * Looks up the ioctl function in the ::ioctls table, checking for root
721  * previleges if so required, and dispatches to the respective function.
722  */
723 int drm_ioctl(struct dev_ioctl_args *ap)
724 {
725         struct cdev *kdev = ap->a_head.a_dev;
726         struct drm_device *dev;
727         struct drm_ioctl_desc *ioctl = NULL;
728         u_long cmd = ap->a_cmd;
729         unsigned int nr = DRM_IOCTL_NR(cmd);
730         int retcode = 0;
731         caddr_t data = ap->a_data;
732         struct thread *p = curthread;
733         int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv);
734         int is_driver_ioctl = 0;
735         struct drm_file *file_priv;
736
737         dev = drm_get_device_from_kdev(kdev);
738
739         retcode = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
740         if (retcode !=0) {
741                 DRM_ERROR("can't find authenticator\n");
742                 return EINVAL;
743         }
744
745         atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
746
747         if (drm_device_is_unplugged(dev))
748                 return -ENODEV;
749
750         if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
751                 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
752                 return EINVAL;
753         }
754
755         ioctl = &drm_ioctls[nr];
756         /* It's not a core DRM ioctl, try driver-specific. */
757         if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
758                 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
759                 nr -= DRM_COMMAND_BASE;
760                 if (nr > dev->driver->num_ioctls) {
761                         return EINVAL;
762                 }
763                 ioctl = &dev->driver->ioctls[nr];
764                 is_driver_ioctl = 1;
765         }
766
767         DRM_DEBUG_VERBOSE("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
768             DRM_CURRENTPID, cmd, nr, (long)dev->dev,
769             file_priv->authenticated);
770
771         /* Do not trust userspace, use our own definition */
772         func = ioctl->func;
773
774         if (func == NULL) {
775                 DRM_DEBUG("no function\n");
776                 return EINVAL;
777         }
778
779         if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
780             ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
781             ((ioctl->flags & DRM_MASTER) && !file_priv->master))
782                 return EACCES;
783
784         if (is_driver_ioctl) {
785                 if ((ioctl->flags & DRM_UNLOCKED) == 0)
786                         mutex_lock(&drm_global_mutex);
787                 /* shared code returns -errno */
788                 retcode = -func(dev, data, file_priv);
789                 if ((ioctl->flags & DRM_UNLOCKED) == 0)
790                         mutex_unlock(&drm_global_mutex);
791         } else {
792                 retcode = func(dev, data, file_priv);
793         }
794
795         if (!ioctl)
796                 DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02lx, nr=0x%02x\n",
797                           DRM_CURRENTPID,
798                           (long)dev->dev,
799                           file_priv->authenticated, cmd, nr);
800
801         if (retcode)
802                 DRM_DEBUG("ret = %d\n", retcode);
803         return retcode;
804 }
805
806 drm_local_map_t *drm_getsarea(struct drm_device *dev)
807 {
808         struct drm_map_list *entry;
809
810         list_for_each_entry(entry, &dev->maplist, head) {
811                 if (entry->map && entry->map->type == _DRM_SHM &&
812                     (entry->map->flags & _DRM_CONTAINS_LOCK)) {
813                         return entry->map;
814                 }
815         }
816
817         return NULL;
818 }
819
820 int
821 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
822     struct sysctl_oid *top)
823 {
824         struct sysctl_oid *oid;
825
826         ksnprintf(dev->busid_str, sizeof(dev->busid_str),
827              "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
828              dev->pci_slot, dev->pci_func);
829         oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
830             CTLFLAG_RD, dev->busid_str, 0, NULL);
831         if (oid == NULL)
832                 return (ENOMEM);
833         dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
834         oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
835             "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
836         if (oid == NULL)
837                 return (ENOMEM);
838
839         return (0);
840 }
841
842 int
843 drm_mmap_single(struct dev_mmap_single_args *ap)
844 {
845         struct drm_device *dev;
846         struct cdev *kdev = ap->a_head.a_dev;
847         vm_ooffset_t *offset = ap->a_offset;
848         vm_size_t size = ap->a_size;
849         struct vm_object **obj_res = ap->a_object;
850         int nprot = ap->a_nprot;
851
852         dev = drm_get_device_from_kdev(kdev);
853         if (dev->drm_ttm_bdev != NULL) {
854                 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
855                     obj_res, nprot));
856         } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
857                 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
858         } else {
859                 return (ENODEV);
860         }
861 }
862
863 #if DRM_LINUX
864
865 #include <sys/sysproto.h>
866
867 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
868
869 #define LINUX_IOCTL_DRM_MIN             0x6400
870 #define LINUX_IOCTL_DRM_MAX             0x64ff
871
872 static linux_ioctl_function_t drm_linux_ioctl;
873 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
874     LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
875
876 /* The bits for in/out are switched on Linux */
877 #define LINUX_IOC_IN    IOC_OUT
878 #define LINUX_IOC_OUT   IOC_IN
879
880 static int
881 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
882 {
883         int error;
884         int cmd = args->cmd;
885
886         args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
887         if (cmd & LINUX_IOC_IN)
888                 args->cmd |= IOC_IN;
889         if (cmd & LINUX_IOC_OUT)
890                 args->cmd |= IOC_OUT;
891         
892         error = ioctl(p, (struct ioctl_args *)args);
893
894         return error;
895 }
896 #endif /* DRM_LINUX */
897
898 static int
899 drm_core_init(void *arg)
900 {
901
902         drm_global_init();
903
904 #if DRM_LINUX
905         linux_ioctl_register_handler(&drm_handler);
906 #endif /* DRM_LINUX */
907
908         DRM_INFO("Initialized %s %d.%d.%d %s\n",
909                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
910         return 0;
911 }
912
913 static void
914 drm_core_exit(void *arg)
915 {
916
917 #if DRM_LINUX
918         linux_ioctl_unregister_handler(&drm_handler);
919 #endif /* DRM_LINUX */
920
921         drm_global_release();
922 }
923
924 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
925     drm_core_init, NULL);
926 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
927     drm_core_exit, NULL);
928
929 /*
930  * Check if dmi_system_id structure matches system DMI data
931  */
932 static bool
933 dmi_found(const struct dmi_system_id *dsi)
934 {
935         int i, slot;
936         bool found = false;
937         char *sys_vendor, *board_vendor, *product_name, *board_name;
938
939         sys_vendor = kgetenv("smbios.system.maker");
940         board_vendor = kgetenv("smbios.planar.maker");
941         product_name = kgetenv("smbios.system.product");
942         board_name = kgetenv("smbios.planar.product");
943
944         for (i = 0; i < NELEM(dsi->matches); i++) {
945                 slot = dsi->matches[i].slot;
946                 switch (slot) {
947                 case DMI_NONE:
948                         break;
949                 case DMI_SYS_VENDOR:
950                         if (sys_vendor != NULL &&
951                             !strcmp(sys_vendor, dsi->matches[i].substr))
952                                 break;
953                         else
954                                 goto done;
955                 case DMI_BOARD_VENDOR:
956                         if (board_vendor != NULL &&
957                             !strcmp(board_vendor, dsi->matches[i].substr))
958                                 break;
959                         else
960                                 goto done;
961                 case DMI_PRODUCT_NAME:
962                         if (product_name != NULL &&
963                             !strcmp(product_name, dsi->matches[i].substr))
964                                 break;
965                         else
966                                 goto done;
967                 case DMI_BOARD_NAME:
968                         if (board_name != NULL &&
969                             !strcmp(board_name, dsi->matches[i].substr))
970                                 break;
971                         else
972                                 goto done;
973                 default:
974                         goto done;
975                 }
976         }
977         found = true;
978
979 done:
980         if (sys_vendor != NULL)
981                 kfreeenv(sys_vendor);
982         if (board_vendor != NULL)
983                 kfreeenv(board_vendor);
984         if (product_name != NULL)
985                 kfreeenv(product_name);
986         if (board_name != NULL)
987                 kfreeenv(board_name);
988
989         return found;
990 }
991
992 bool
993 dmi_check_system(const struct dmi_system_id *sysid)
994 {
995         const struct dmi_system_id *dsi;
996         int num = 0;
997
998         for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
999                 if (dmi_found(dsi)) {
1000                         num++;
1001                         if (dsi->callback && dsi->callback(dsi))
1002                                 break;
1003                 }
1004         }
1005         return (num);
1006 }