xref: /freebsd/sys/dev/drm2/drm_stub.c (revision b4b4b5304bd22eab265c9c049cb7fc6b55c4ef3f)
1 /**
2  * \file drm_stub.h
3  * Stub support
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  */
7 
8 /*
9  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
10  *
11  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12  * All Rights Reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the next
22  * paragraph) shall be included in all copies or substantial portions of the
23  * Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <dev/drm2/drmP.h>
38 #include <dev/drm2/drm_core.h>
39 #include <linux/slab.h>
40 
41 #ifdef DRM_DEBUG_DEFAULT_ON
42 unsigned int drm_debug = (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS |
43     DRM_DEBUGBITS_FAILED_IOCTL);
44 #else
45 unsigned int drm_debug = 0;	/* 1 to enable debug output */
46 #endif
47 EXPORT_SYMBOL(drm_debug);
48 
49 unsigned int drm_notyet = 0;
50 
51 unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
52 EXPORT_SYMBOL(drm_vblank_offdelay);
53 
54 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
55 EXPORT_SYMBOL(drm_timestamp_precision);
56 
57 /*
58  * Default to use monotonic timestamps for wait-for-vblank and page-flip
59  * complete events.
60  */
61 unsigned int drm_timestamp_monotonic = 1;
62 
63 MODULE_AUTHOR(CORE_AUTHOR);
64 MODULE_DESCRIPTION(CORE_DESC);
65 MODULE_LICENSE("GPL and additional rights");
66 MODULE_PARM_DESC(debug, "Enable debug output");
67 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
68 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
69 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
70 
71 module_param_named(debug, drm_debug, int, 0600);
72 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
73 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
74 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
75 
76 static struct cdevsw drm_cdevsw = {
77 	.d_version =	D_VERSION,
78 	.d_open =	drm_open,
79 	.d_read =	drm_read,
80 	.d_ioctl =	drm_ioctl,
81 	.d_poll =	drm_poll,
82 	.d_mmap_single = drm_mmap_single,
83 	.d_name =	"drm",
84 	.d_flags =	D_TRACKCLOSE
85 };
86 
87 static int drm_minor_get_id(struct drm_device *dev, int type)
88 {
89 	int new_id;
90 
91 	new_id = device_get_unit(dev->dev);
92 
93 	if (new_id >= 64)
94 		return -EINVAL;
95 
96 	if (type == DRM_MINOR_CONTROL) {
97 		new_id += 64;
98 	} else if (type == DRM_MINOR_RENDER) {
99 		new_id += 128;
100 	}
101 
102 	return new_id;
103 }
104 
105 struct drm_master *drm_master_create(struct drm_minor *minor)
106 {
107 	struct drm_master *master;
108 
109 	master = malloc(sizeof(*master), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
110 	if (!master)
111 		return NULL;
112 
113 	refcount_init(&master->refcount, 1);
114 	mtx_init(&master->lock.spinlock, "drm_master__lock__spinlock",
115 	    NULL, MTX_DEF);
116 	DRM_INIT_WAITQUEUE(&master->lock.lock_queue);
117 	drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
118 	INIT_LIST_HEAD(&master->magicfree);
119 	master->minor = minor;
120 
121 	list_add_tail(&master->head, &minor->master_list);
122 
123 	return master;
124 }
125 
126 struct drm_master *drm_master_get(struct drm_master *master)
127 {
128 	refcount_acquire(&master->refcount);
129 	return master;
130 }
131 EXPORT_SYMBOL(drm_master_get);
132 
133 static void drm_master_destroy(struct drm_master *master)
134 {
135 	struct drm_magic_entry *pt, *next;
136 	struct drm_device *dev = master->minor->dev;
137 	struct drm_map_list *r_list, *list_temp;
138 
139 	list_del(&master->head);
140 
141 	if (dev->driver->master_destroy)
142 		dev->driver->master_destroy(dev, master);
143 
144 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
145 		if (r_list->master == master) {
146 			drm_rmmap_locked(dev, r_list->map);
147 			r_list = NULL;
148 		}
149 	}
150 
151 	if (master->unique) {
152 		free(master->unique, DRM_MEM_DRIVER);
153 		master->unique = NULL;
154 		master->unique_len = 0;
155 	}
156 
157 	list_for_each_entry_safe(pt, next, &master->magicfree, head) {
158 		list_del(&pt->head);
159 		drm_ht_remove_item(&master->magiclist, &pt->hash_item);
160 		free(pt, DRM_MEM_MAGIC);
161 	}
162 
163 	drm_ht_remove(&master->magiclist);
164 
165 	free(master, DRM_MEM_KMS);
166 }
167 
168 void drm_master_put(struct drm_master **master)
169 {
170 	if (refcount_release(&(*master)->refcount))
171 		drm_master_destroy(*master);
172 	*master = NULL;
173 }
174 EXPORT_SYMBOL(drm_master_put);
175 
176 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
177 			struct drm_file *file_priv)
178 {
179 	int ret;
180 
181 	if (file_priv->is_master)
182 		return 0;
183 
184 	if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
185 		return -EINVAL;
186 
187 	if (!file_priv->master)
188 		return -EINVAL;
189 
190 	if (file_priv->minor->master)
191 		return -EINVAL;
192 
193 	DRM_LOCK(dev);
194 	file_priv->minor->master = drm_master_get(file_priv->master);
195 	file_priv->is_master = 1;
196 	if (dev->driver->master_set) {
197 		ret = dev->driver->master_set(dev, file_priv, false);
198 		if (unlikely(ret != 0)) {
199 			file_priv->is_master = 0;
200 			drm_master_put(&file_priv->minor->master);
201 		}
202 	}
203 	DRM_UNLOCK(dev);
204 
205 	return 0;
206 }
207 
208 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
209 			 struct drm_file *file_priv)
210 {
211 	if (!file_priv->is_master)
212 		return -EINVAL;
213 
214 	if (!file_priv->minor->master)
215 		return -EINVAL;
216 
217 	DRM_LOCK(dev);
218 	if (dev->driver->master_drop)
219 		dev->driver->master_drop(dev, file_priv, false);
220 	drm_master_put(&file_priv->minor->master);
221 	file_priv->is_master = 0;
222 	DRM_UNLOCK(dev);
223 	return 0;
224 }
225 
226 int drm_fill_in_dev(struct drm_device *dev,
227 			   struct drm_driver *driver)
228 {
229 	int retcode, i;
230 
231 	INIT_LIST_HEAD(&dev->filelist);
232 	INIT_LIST_HEAD(&dev->ctxlist);
233 	INIT_LIST_HEAD(&dev->maplist);
234 	INIT_LIST_HEAD(&dev->vblank_event_list);
235 
236 	mtx_init(&dev->irq_lock, "drmirq", NULL, MTX_DEF);
237 	mtx_init(&dev->count_lock, "drmcount", NULL, MTX_DEF);
238 	mtx_init(&dev->event_lock, "drmev", NULL, MTX_DEF);
239 	sx_init(&dev->dev_struct_lock, "drmslk");
240 	mtx_init(&dev->ctxlist_mutex, "drmctxlist", NULL, MTX_DEF);
241 	mtx_init(&dev->pcir_lock, "drmpcir", NULL, MTX_DEF);
242 
243 	if (drm_ht_create(&dev->map_hash, 12)) {
244 		return -ENOMEM;
245 	}
246 
247 	/* the DRM has 6 basic counters */
248 	dev->counters = 6;
249 	dev->types[0] = _DRM_STAT_LOCK;
250 	dev->types[1] = _DRM_STAT_OPENS;
251 	dev->types[2] = _DRM_STAT_CLOSES;
252 	dev->types[3] = _DRM_STAT_IOCTLS;
253 	dev->types[4] = _DRM_STAT_LOCKS;
254 	dev->types[5] = _DRM_STAT_UNLOCKS;
255 
256 	/*
257 	 * FIXME Linux<->FreeBSD: this is done in drm_setup() on Linux.
258 	 */
259 	for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
260 		atomic_set(&dev->counts[i], 0);
261 
262 	dev->driver = driver;
263 
264 	retcode = drm_pci_agp_init(dev);
265 	if (retcode)
266 		goto error_out_unreg;
267 
268 
269 
270 	retcode = drm_ctxbitmap_init(dev);
271 	if (retcode) {
272 		DRM_ERROR("Cannot allocate memory for context bitmap.\n");
273 		goto error_out_unreg;
274 	}
275 
276 	if (driver->driver_features & DRIVER_GEM) {
277 		retcode = drm_gem_init(dev);
278 		if (retcode) {
279 			DRM_ERROR("Cannot initialize graphics execution "
280 				  "manager (GEM)\n");
281 			goto error_out_unreg;
282 		}
283 	}
284 
285 	retcode = drm_sysctl_init(dev);
286 	if (retcode != 0) {
287 		DRM_ERROR("Failed to create hw.dri sysctl entry: %d\n",
288 		    retcode);
289 	}
290 
291 	return 0;
292 
293       error_out_unreg:
294 	drm_cancel_fill_in_dev(dev);
295 	return retcode;
296 }
297 EXPORT_SYMBOL(drm_fill_in_dev);
298 
299 void drm_cancel_fill_in_dev(struct drm_device *dev)
300 {
301 	struct drm_driver *driver;
302 
303 	driver = dev->driver;
304 
305 	drm_sysctl_cleanup(dev);
306 	if (driver->driver_features & DRIVER_GEM)
307 		drm_gem_destroy(dev);
308 	drm_ctxbitmap_cleanup(dev);
309 
310 	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
311 	    dev->agp && dev->agp->agp_mtrr >= 0) {
312 		int retval;
313 		retval = drm_mtrr_del(dev->agp->agp_mtrr,
314 				  dev->agp->agp_info.ai_aperture_base,
315 				  dev->agp->agp_info.ai_aperture_size,
316 				  DRM_MTRR_WC);
317 		DRM_DEBUG("mtrr_del=%d\n", retval);
318 	}
319 	kfree(dev->agp);
320 	dev->agp = NULL;
321 
322 	drm_ht_remove(&dev->map_hash);
323 
324 	mtx_destroy(&dev->irq_lock);
325 	mtx_destroy(&dev->count_lock);
326 	mtx_destroy(&dev->event_lock);
327 	sx_destroy(&dev->dev_struct_lock);
328 	mtx_destroy(&dev->ctxlist_mutex);
329 	mtx_destroy(&dev->pcir_lock);
330 }
331 
332 /**
333  * Get a secondary minor number.
334  *
335  * \param dev device data structure
336  * \param sec-minor structure to hold the assigned minor
337  * \return negative number on failure.
338  *
339  * Search an empty entry and initialize it to the given parameters, and
340  * create the proc init entry via proc_init(). This routines assigns
341  * minor numbers to secondary heads of multi-headed cards
342  */
343 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
344 {
345 	struct drm_minor *new_minor;
346 	int ret;
347 	int minor_id;
348 	const char *minor_devname;
349 
350 	DRM_DEBUG("\n");
351 
352 	minor_id = drm_minor_get_id(dev, type);
353 	if (minor_id < 0)
354 		return minor_id;
355 
356 	new_minor = malloc(sizeof(struct drm_minor), DRM_MEM_MINOR,
357 	    M_NOWAIT | M_ZERO);
358 	if (!new_minor) {
359 		ret = -ENOMEM;
360 		goto err_idr;
361 	}
362 
363 	new_minor->type = type;
364 	new_minor->dev = dev;
365 	new_minor->index = minor_id;
366 	INIT_LIST_HEAD(&new_minor->master_list);
367 
368 	new_minor->buf_sigio = NULL;
369 
370 	switch (type) {
371 	case DRM_MINOR_CONTROL:
372 		minor_devname = "dri/controlD%d";
373 		break;
374 	case DRM_MINOR_RENDER:
375 		minor_devname = "dri/renderD%d";
376 		break;
377 	default:
378 		minor_devname = "dri/card%d";
379 		break;
380 	}
381 
382 	ret = make_dev_p(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME, &new_minor->device,
383 	    &drm_cdevsw, 0, DRM_DEV_UID, DRM_DEV_GID,
384 	    DRM_DEV_MODE, minor_devname, minor_id);
385 	if (ret) {
386 		DRM_ERROR("Failed to create cdev: %d\n", ret);
387 		goto err_mem;
388 	}
389 	new_minor->device->si_drv1 = new_minor;
390 	*minor = new_minor;
391 
392 	DRM_DEBUG("new minor assigned %d\n", minor_id);
393 	return 0;
394 
395 
396 err_mem:
397 	free(new_minor, DRM_MEM_MINOR);
398 err_idr:
399 	*minor = NULL;
400 	return ret;
401 }
402 EXPORT_SYMBOL(drm_get_minor);
403 
404 /**
405  * Put a secondary minor number.
406  *
407  * \param sec_minor - structure to be released
408  * \return always zero
409  *
410  * Cleans up the proc resources. Not legal for this to be the
411  * last minor released.
412  *
413  */
414 int drm_put_minor(struct drm_minor **minor_p)
415 {
416 	struct drm_minor *minor = *minor_p;
417 
418 	DRM_DEBUG("release secondary minor %d\n", minor->index);
419 
420 	funsetown(&minor->buf_sigio);
421 
422 	destroy_dev(minor->device);
423 
424 	free(minor, DRM_MEM_MINOR);
425 	*minor_p = NULL;
426 	return 0;
427 }
428 EXPORT_SYMBOL(drm_put_minor);
429 
430 /**
431  * Called via drm_exit() at module unload time or when pci device is
432  * unplugged.
433  *
434  * Cleans up all DRM device, calling drm_lastclose().
435  *
436  */
437 void drm_put_dev(struct drm_device *dev)
438 {
439 	struct drm_driver *driver;
440 	struct drm_map_list *r_list, *list_temp;
441 
442 	DRM_DEBUG("\n");
443 
444 	if (!dev) {
445 		DRM_ERROR("cleanup called no dev\n");
446 		return;
447 	}
448 	driver = dev->driver;
449 
450 	drm_lastclose(dev);
451 
452 	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
453 	    dev->agp && dev->agp->agp_mtrr >= 0) {
454 		int retval;
455 		retval = drm_mtrr_del(dev->agp->agp_mtrr,
456 				  dev->agp->agp_info.ai_aperture_base,
457 				  dev->agp->agp_info.ai_aperture_size,
458 				  DRM_MTRR_WC);
459 		DRM_DEBUG("mtrr_del=%d\n", retval);
460 	}
461 
462 	if (drm_core_check_feature(dev, DRIVER_MODESET))
463 		drm_mode_group_free(&dev->primary->mode_group);
464 
465 	if (dev->driver->unload)
466 		dev->driver->unload(dev);
467 
468 	drm_sysctl_cleanup(dev);
469 
470 	if (drm_core_has_AGP(dev) && dev->agp) {
471 		kfree(dev->agp);
472 		dev->agp = NULL;
473 	}
474 
475 	drm_vblank_cleanup(dev);
476 
477 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
478 		drm_rmmap(dev, r_list->map);
479 	drm_ht_remove(&dev->map_hash);
480 
481 	drm_ctxbitmap_cleanup(dev);
482 
483 	if (drm_core_check_feature(dev, DRIVER_MODESET))
484 		drm_put_minor(&dev->control);
485 
486 	if (driver->driver_features & DRIVER_GEM)
487 		drm_gem_destroy(dev);
488 
489 	drm_put_minor(&dev->primary);
490 
491 	mtx_destroy(&dev->irq_lock);
492 	mtx_destroy(&dev->count_lock);
493 	mtx_destroy(&dev->event_lock);
494 	sx_destroy(&dev->dev_struct_lock);
495 	mtx_destroy(&dev->ctxlist_mutex);
496 	mtx_destroy(&dev->pcir_lock);
497 
498 #ifdef FREEBSD_NOTYET
499 	list_del(&dev->driver_item);
500 #endif /* FREEBSD_NOTYET */
501 }
502 EXPORT_SYMBOL(drm_put_dev);
503