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