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