5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
11 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
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:
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
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <dev/drm2/drmP.h>
38 #include <dev/drm2/drm_core.h>
40 #ifdef DRM_DEBUG_DEFAULT_ON
41 unsigned int drm_debug = (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS |
42 DRM_DEBUGBITS_FAILED_IOCTL);
44 unsigned int drm_debug = 0; /* 1 to enable debug output */
46 EXPORT_SYMBOL(drm_debug);
48 unsigned int drm_notyet = 0;
50 unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
51 EXPORT_SYMBOL(drm_vblank_offdelay);
53 unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
54 EXPORT_SYMBOL(drm_timestamp_precision);
57 * Default to use monotonic timestamps for wait-for-vblank and page-flip
60 unsigned int drm_timestamp_monotonic = 1;
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");
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);
75 static struct cdevsw drm_cdevsw = {
76 .d_version = D_VERSION,
81 .d_mmap_single = drm_mmap_single,
83 .d_flags = D_TRACKCLOSE
86 static int drm_minor_get_id(struct drm_device *dev, int type)
90 new_id = device_get_unit(dev->dev);
95 if (type == DRM_MINOR_CONTROL) {
97 } else if (type == DRM_MINOR_RENDER) {
104 struct drm_master *drm_master_create(struct drm_minor *minor)
106 struct drm_master *master;
108 master = malloc(sizeof(*master), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
112 refcount_init(&master->refcount, 1);
113 mtx_init(&master->lock.spinlock, "drm_master__lock__spinlock",
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;
120 list_add_tail(&master->head, &minor->master_list);
125 struct drm_master *drm_master_get(struct drm_master *master)
127 refcount_acquire(&master->refcount);
130 EXPORT_SYMBOL(drm_master_get);
132 static void drm_master_destroy(struct drm_master *master)
134 struct drm_magic_entry *pt, *next;
135 struct drm_device *dev = master->minor->dev;
136 struct drm_map_list *r_list, *list_temp;
138 list_del(&master->head);
140 if (dev->driver->master_destroy)
141 dev->driver->master_destroy(dev, master);
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);
150 if (master->unique) {
151 free(master->unique, DRM_MEM_DRIVER);
152 master->unique = NULL;
153 master->unique_len = 0;
156 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
158 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
159 free(pt, DRM_MEM_MAGIC);
162 drm_ht_remove(&master->magiclist);
164 free(master, DRM_MEM_KMS);
167 void drm_master_put(struct drm_master **master)
169 if (refcount_release(&(*master)->refcount))
170 drm_master_destroy(*master);
173 EXPORT_SYMBOL(drm_master_put);
175 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
176 struct drm_file *file_priv)
180 if (file_priv->is_master)
183 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
186 if (!file_priv->master)
189 if (file_priv->minor->master)
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);
207 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
208 struct drm_file *file_priv)
210 if (!file_priv->is_master)
213 if (!file_priv->minor->master)
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;
225 int drm_fill_in_dev(struct drm_device *dev,
226 struct drm_driver *driver)
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);
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);
242 if (drm_ht_create(&dev->map_hash, 12)) {
246 /* the DRM has 6 basic counters */
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;
256 * FIXME Linux<->FreeBSD: this is done in drm_setup() on Linux.
258 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
259 atomic_set(&dev->counts[i], 0);
261 dev->driver = driver;
263 retcode = drm_pci_agp_init(dev);
265 goto error_out_unreg;
269 retcode = drm_ctxbitmap_init(dev);
271 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
272 goto error_out_unreg;
275 if (driver->driver_features & DRIVER_GEM) {
276 retcode = drm_gem_init(dev);
278 DRM_ERROR("Cannot initialize graphics execution "
280 goto error_out_unreg;
284 retcode = drm_sysctl_init(dev);
286 DRM_ERROR("Failed to create hw.dri sysctl entry: %d\n",
293 drm_cancel_fill_in_dev(dev);
296 EXPORT_SYMBOL(drm_fill_in_dev);
298 void drm_cancel_fill_in_dev(struct drm_device *dev)
300 struct drm_driver *driver;
302 driver = dev->driver;
304 drm_sysctl_cleanup(dev);
305 if (driver->driver_features & DRIVER_GEM)
306 drm_gem_destroy(dev);
307 drm_ctxbitmap_cleanup(dev);
309 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
310 dev->agp && dev->agp->agp_mtrr >= 0) {
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,
316 DRM_DEBUG("mtrr_del=%d\n", retval);
318 free(dev->agp, DRM_MEM_AGPLISTS);
321 drm_ht_remove(&dev->map_hash);
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);
332 * Get a secondary minor number.
334 * \param dev device data structure
335 * \param sec-minor structure to hold the assigned minor
336 * \return negative number on failure.
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
342 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
344 struct drm_minor *new_minor;
347 const char *minor_devname;
351 minor_id = drm_minor_get_id(dev, type);
355 new_minor = malloc(sizeof(struct drm_minor), DRM_MEM_MINOR,
362 new_minor->type = type;
363 new_minor->dev = dev;
364 new_minor->index = minor_id;
365 INIT_LIST_HEAD(&new_minor->master_list);
367 new_minor->buf_sigio = NULL;
370 case DRM_MINOR_CONTROL:
371 minor_devname = "dri/controlD%d";
373 case DRM_MINOR_RENDER:
374 minor_devname = "dri/renderD%d";
377 minor_devname = "dri/card%d";
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);
385 DRM_ERROR("Failed to create cdev: %d\n", ret);
388 new_minor->device->si_drv1 = new_minor;
391 DRM_DEBUG("new minor assigned %d\n", minor_id);
396 free(new_minor, DRM_MEM_MINOR);
401 EXPORT_SYMBOL(drm_get_minor);
404 * Put a secondary minor number.
406 * \param sec_minor - structure to be released
407 * \return always zero
409 * Cleans up the proc resources. Not legal for this to be the
410 * last minor released.
413 int drm_put_minor(struct drm_minor **minor_p)
415 struct drm_minor *minor = *minor_p;
417 DRM_DEBUG("release secondary minor %d\n", minor->index);
419 funsetown(&minor->buf_sigio);
421 destroy_dev(minor->device);
423 free(minor, DRM_MEM_MINOR);
427 EXPORT_SYMBOL(drm_put_minor);
430 * Called via drm_exit() at module unload time or when pci device is
433 * Cleans up all DRM device, calling drm_lastclose().
436 void drm_put_dev(struct drm_device *dev)
438 struct drm_driver *driver;
439 struct drm_map_list *r_list, *list_temp;
444 DRM_ERROR("cleanup called no dev\n");
447 driver = dev->driver;
451 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
452 dev->agp && dev->agp->agp_mtrr >= 0) {
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,
458 DRM_DEBUG("mtrr_del=%d\n", retval);
461 if (drm_core_check_feature(dev, DRIVER_MODESET))
462 drm_mode_group_free(&dev->primary->mode_group);
464 if (dev->driver->unload)
465 dev->driver->unload(dev);
467 drm_sysctl_cleanup(dev);
469 if (drm_core_has_AGP(dev) && dev->agp) {
470 free(dev->agp, DRM_MEM_AGPLISTS);
474 drm_vblank_cleanup(dev);
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);
480 drm_ctxbitmap_cleanup(dev);
482 if (drm_core_check_feature(dev, DRIVER_MODESET))
483 drm_put_minor(&dev->control);
485 if (driver->driver_features & DRIVER_GEM)
486 drm_gem_destroy(dev);
488 drm_put_minor(&dev->primary);
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);
497 #ifdef FREEBSD_NOTYET
498 list_del(&dev->driver_item);
499 #endif /* FREEBSD_NOTYET */
501 EXPORT_SYMBOL(drm_put_dev);