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