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 <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/slab.h>
40 unsigned int drm_debug = 0; /* 1 to enable debug output */
41 EXPORT_SYMBOL(drm_debug);
43 MODULE_AUTHOR(CORE_AUTHOR);
44 MODULE_DESCRIPTION(CORE_DESC);
45 MODULE_LICENSE("GPL and additional rights");
46 MODULE_PARM_DESC(debug, "Enable debug output");
48 module_param_named(debug, drm_debug, int, 0600);
50 struct idr drm_minors_idr;
52 struct class *drm_class;
53 struct proc_dir_entry *drm_proc_root;
54 struct dentry *drm_debugfs_root;
55 void drm_ut_debug_printk(unsigned int request_level,
57 const char *function_name,
58 const char *format, ...)
62 if (drm_debug & request_level) {
64 printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
65 va_start(args, format);
66 vprintk(format, args);
70 EXPORT_SYMBOL(drm_ut_debug_printk);
71 static int drm_minor_get_id(struct drm_device *dev, int type)
75 int base = 0, limit = 63;
77 if (type == DRM_MINOR_CONTROL) {
80 } else if (type == DRM_MINOR_RENDER) {
86 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
87 DRM_ERROR("Out of memory expanding drawable idr\n");
90 mutex_lock(&dev->struct_mutex);
91 ret = idr_get_new_above(&drm_minors_idr, NULL,
93 mutex_unlock(&dev->struct_mutex);
100 if (new_id >= limit) {
101 idr_remove(&drm_minors_idr, new_id);
107 struct drm_master *drm_master_create(struct drm_minor *minor)
109 struct drm_master *master;
111 master = kzalloc(sizeof(*master), GFP_KERNEL);
115 kref_init(&master->refcount);
116 spin_lock_init(&master->lock.spinlock);
117 init_waitqueue_head(&master->lock.lock_queue);
118 drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
119 INIT_LIST_HEAD(&master->magicfree);
120 master->minor = minor;
122 list_add_tail(&master->head, &minor->master_list);
127 struct drm_master *drm_master_get(struct drm_master *master)
129 kref_get(&master->refcount);
132 EXPORT_SYMBOL(drm_master_get);
134 static void drm_master_destroy(struct kref *kref)
136 struct drm_master *master = container_of(kref, struct drm_master, refcount);
137 struct drm_magic_entry *pt, *next;
138 struct drm_device *dev = master->minor->dev;
139 struct drm_map_list *r_list, *list_temp;
141 list_del(&master->head);
143 if (dev->driver->master_destroy)
144 dev->driver->master_destroy(dev, master);
146 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
147 if (r_list->master == master) {
148 drm_rmmap_locked(dev, r_list->map);
153 if (master->unique) {
154 kfree(master->unique);
155 master->unique = NULL;
156 master->unique_len = 0;
162 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
164 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
168 drm_ht_remove(&master->magiclist);
173 void drm_master_put(struct drm_master **master)
175 kref_put(&(*master)->refcount, drm_master_destroy);
178 EXPORT_SYMBOL(drm_master_put);
180 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
181 struct drm_file *file_priv)
185 if (file_priv->is_master)
188 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
191 if (!file_priv->master)
194 if (!file_priv->minor->master &&
195 file_priv->minor->master != file_priv->master) {
196 mutex_lock(&dev->struct_mutex);
197 file_priv->minor->master = drm_master_get(file_priv->master);
198 file_priv->is_master = 1;
199 if (dev->driver->master_set) {
200 ret = dev->driver->master_set(dev, file_priv, false);
201 if (unlikely(ret != 0)) {
202 file_priv->is_master = 0;
203 drm_master_put(&file_priv->minor->master);
206 mutex_unlock(&dev->struct_mutex);
212 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
215 if (!file_priv->is_master)
218 if (!file_priv->minor->master)
221 mutex_lock(&dev->struct_mutex);
222 if (dev->driver->master_drop)
223 dev->driver->master_drop(dev, file_priv, false);
224 drm_master_put(&file_priv->minor->master);
225 file_priv->is_master = 0;
226 mutex_unlock(&dev->struct_mutex);
230 int drm_fill_in_dev(struct drm_device *dev,
231 const struct pci_device_id *ent,
232 struct drm_driver *driver)
236 INIT_LIST_HEAD(&dev->filelist);
237 INIT_LIST_HEAD(&dev->ctxlist);
238 INIT_LIST_HEAD(&dev->vmalist);
239 INIT_LIST_HEAD(&dev->maplist);
240 INIT_LIST_HEAD(&dev->vblank_event_list);
242 spin_lock_init(&dev->count_lock);
243 spin_lock_init(&dev->drw_lock);
244 spin_lock_init(&dev->event_lock);
245 init_timer(&dev->timer);
246 mutex_init(&dev->struct_mutex);
247 mutex_init(&dev->ctxlist_mutex);
249 idr_init(&dev->drw_idr);
251 if (drm_ht_create(&dev->map_hash, 12)) {
255 /* the DRM has 6 basic counters */
257 dev->types[0] = _DRM_STAT_LOCK;
258 dev->types[1] = _DRM_STAT_OPENS;
259 dev->types[2] = _DRM_STAT_CLOSES;
260 dev->types[3] = _DRM_STAT_IOCTLS;
261 dev->types[4] = _DRM_STAT_LOCKS;
262 dev->types[5] = _DRM_STAT_UNLOCKS;
264 dev->driver = driver;
266 if (drm_core_has_AGP(dev)) {
267 if (drm_device_is_agp(dev))
268 dev->agp = drm_agp_init(dev);
269 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
270 && (dev->agp == NULL)) {
271 DRM_ERROR("Cannot initialize the agpgart module.\n");
273 goto error_out_unreg;
275 if (drm_core_has_MTRR(dev)) {
278 mtrr_add(dev->agp->agp_info.aper_base,
279 dev->agp->agp_info.aper_size *
280 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
285 retcode = drm_ctxbitmap_init(dev);
287 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
288 goto error_out_unreg;
291 if (driver->driver_features & DRIVER_GEM) {
292 retcode = drm_gem_init(dev);
294 DRM_ERROR("Cannot initialize graphics execution "
296 goto error_out_unreg;
309 * Get a secondary minor number.
311 * \param dev device data structure
312 * \param sec-minor structure to hold the assigned minor
313 * \return negative number on failure.
315 * Search an empty entry and initialize it to the given parameters, and
316 * create the proc init entry via proc_init(). This routines assigns
317 * minor numbers to secondary heads of multi-headed cards
319 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
321 struct drm_minor *new_minor;
327 minor_id = drm_minor_get_id(dev, type);
331 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
337 new_minor->type = type;
338 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
339 new_minor->dev = dev;
340 new_minor->index = minor_id;
341 INIT_LIST_HEAD(&new_minor->master_list);
343 idr_replace(&drm_minors_idr, new_minor, minor_id);
345 if (type == DRM_MINOR_LEGACY) {
346 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
348 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
352 new_minor->proc_root = NULL;
354 #if defined(CONFIG_DEBUG_FS)
355 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
357 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
362 ret = drm_sysfs_device_add(new_minor);
365 "DRM: Error sysfs_device_add.\n");
370 DRM_DEBUG("new minor assigned %d\n", minor_id);
375 if (new_minor->type == DRM_MINOR_LEGACY)
376 drm_proc_cleanup(new_minor, drm_proc_root);
380 idr_remove(&drm_minors_idr, minor_id);
386 * Put a secondary minor number.
388 * \param sec_minor - structure to be released
389 * \return always zero
391 * Cleans up the proc resources. Not legal for this to be the
392 * last minor released.
395 int drm_put_minor(struct drm_minor **minor_p)
397 struct drm_minor *minor = *minor_p;
399 DRM_DEBUG("release secondary minor %d\n", minor->index);
401 if (minor->type == DRM_MINOR_LEGACY)
402 drm_proc_cleanup(minor, drm_proc_root);
403 #if defined(CONFIG_DEBUG_FS)
404 drm_debugfs_cleanup(minor);
407 drm_sysfs_device_remove(minor);
409 idr_remove(&drm_minors_idr, minor->index);
417 * Called via drm_exit() at module unload time or when pci device is
420 * Cleans up all DRM device, calling drm_lastclose().
424 void drm_put_dev(struct drm_device *dev)
426 struct drm_driver *driver;
427 struct drm_map_list *r_list, *list_temp;
432 DRM_ERROR("cleanup called no dev\n");
435 driver = dev->driver;
439 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
440 dev->agp && dev->agp->agp_mtrr >= 0) {
442 retval = mtrr_del(dev->agp->agp_mtrr,
443 dev->agp->agp_info.aper_base,
444 dev->agp->agp_info.aper_size * 1024 * 1024);
445 DRM_DEBUG("mtrr_del=%d\n", retval);
448 if (dev->driver->unload)
449 dev->driver->unload(dev);
451 if (drm_core_has_AGP(dev) && dev->agp) {
456 drm_vblank_cleanup(dev);
458 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
459 drm_rmmap(dev, r_list->map);
460 drm_ht_remove(&dev->map_hash);
462 drm_ctxbitmap_cleanup(dev);
464 if (drm_core_check_feature(dev, DRIVER_MODESET))
465 drm_put_minor(&dev->control);
467 if (driver->driver_features & DRIVER_GEM)
468 drm_gem_destroy(dev);
470 drm_put_minor(&dev->primary);
478 EXPORT_SYMBOL(drm_put_dev);