blob: a0c365f2e521a393ead336e55950ba8aa966c11e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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 <linux/module.h>
35#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "drmP.h"
38#include "drm_core.h"
39
Dave Airlieb5e89ed2005-09-25 14:28:13 +100040unsigned int drm_debug = 0; /* 1 to enable debug output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041EXPORT_SYMBOL(drm_debug);
42
Dave Airlieb5e89ed2005-09-25 14:28:13 +100043MODULE_AUTHOR(CORE_AUTHOR);
44MODULE_DESCRIPTION(CORE_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL and additional rights");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_PARM_DESC(debug, "Enable debug output");
47
Dave Jonesc0758142005-10-03 15:02:20 -040048module_param_named(debug, drm_debug, int, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Dave Airlie2c14f282008-04-21 16:47:32 +100050struct idr drm_minors_idr;
51
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080052struct class *drm_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct proc_dir_entry *drm_proc_root;
Ben Gamari955b12d2009-02-17 20:08:49 -050054struct dentry *drm_debugfs_root;
yakui_zhao4fefcb22009-06-02 14:09:47 +080055void drm_ut_debug_printk(unsigned int request_level,
56 const char *prefix,
57 const char *function_name,
58 const char *format, ...)
59{
60 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
yakui_zhao4fefcb22009-06-02 14:09:47 +080062 if (drm_debug & request_level) {
63 if (function_name)
64 printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
65 va_start(args, format);
66 vprintk(format, args);
67 va_end(args);
68 }
69}
70EXPORT_SYMBOL(drm_ut_debug_printk);
Dave Airlie2c14f282008-04-21 16:47:32 +100071static int drm_minor_get_id(struct drm_device *dev, int type)
72{
73 int new_id;
74 int ret;
75 int base = 0, limit = 63;
76
Dave Airlief453ba02008-11-07 14:05:41 -080077 if (type == DRM_MINOR_CONTROL) {
78 base += 64;
79 limit = base + 127;
80 } else if (type == DRM_MINOR_RENDER) {
81 base += 128;
82 limit = base + 255;
83 }
84
Dave Airlie2c14f282008-04-21 16:47:32 +100085again:
86 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
87 DRM_ERROR("Out of memory expanding drawable idr\n");
88 return -ENOMEM;
89 }
90 mutex_lock(&dev->struct_mutex);
91 ret = idr_get_new_above(&drm_minors_idr, NULL,
92 base, &new_id);
93 mutex_unlock(&dev->struct_mutex);
94 if (ret == -EAGAIN) {
95 goto again;
96 } else if (ret) {
97 return ret;
98 }
99
100 if (new_id >= limit) {
101 idr_remove(&drm_minors_idr, new_id);
102 return -EINVAL;
103 }
104 return new_id;
105}
106
Dave Airlie7c1c2872008-11-28 14:22:24 +1000107struct drm_master *drm_master_create(struct drm_minor *minor)
108{
109 struct drm_master *master;
110
Eric Anholt9a298b22009-03-24 12:23:04 -0700111 master = kzalloc(sizeof(*master), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000112 if (!master)
113 return NULL;
114
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;
121
122 list_add_tail(&master->head, &minor->master_list);
123
124 return master;
125}
126
127struct drm_master *drm_master_get(struct drm_master *master)
128{
129 kref_get(&master->refcount);
130 return master;
131}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100132EXPORT_SYMBOL(drm_master_get);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000133
134static void drm_master_destroy(struct kref *kref)
135{
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;
Dave Airliec1ff85d2009-01-19 17:17:58 +1000139 struct drm_map_list *r_list, *list_temp;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000140
141 list_del(&master->head);
142
143 if (dev->driver->master_destroy)
144 dev->driver->master_destroy(dev, master);
145
Dave Airliec1ff85d2009-01-19 17:17:58 +1000146 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);
149 r_list = NULL;
150 }
151 }
152
Dave Airlie7c1c2872008-11-28 14:22:24 +1000153 if (master->unique) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700154 kfree(master->unique);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000155 master->unique = NULL;
156 master->unique_len = 0;
157 }
158
159 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
160 list_del(&pt->head);
161 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
Eric Anholt9a298b22009-03-24 12:23:04 -0700162 kfree(pt);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000163 }
164
165 drm_ht_remove(&master->magiclist);
166
Eric Anholt9a298b22009-03-24 12:23:04 -0700167 kfree(master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000168}
169
170void drm_master_put(struct drm_master **master)
171{
172 kref_put(&(*master)->refcount, drm_master_destroy);
173 *master = NULL;
174}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100175EXPORT_SYMBOL(drm_master_put);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000176
177int drm_setmaster_ioctl(struct drm_device *dev, void *data,
178 struct drm_file *file_priv)
179{
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000180 int ret = 0;
181
Jonas Bonn6b008422009-04-16 09:00:02 +0200182 if (file_priv->is_master)
183 return 0;
184
Dave Airlie7c1c2872008-11-28 14:22:24 +1000185 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
186 return -EINVAL;
187
188 if (!file_priv->master)
189 return -EINVAL;
190
191 if (!file_priv->minor->master &&
192 file_priv->minor->master != file_priv->master) {
193 mutex_lock(&dev->struct_mutex);
194 file_priv->minor->master = drm_master_get(file_priv->master);
Jonas Bonn6b008422009-04-16 09:00:02 +0200195 file_priv->is_master = 1;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000196 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 }
Helge Bahmann5ad8b7d2009-03-04 21:49:14 +1000203 mutex_unlock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000204 }
205
206 return 0;
207}
208
209int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
210 struct drm_file *file_priv)
211{
Jonas Bonn6b008422009-04-16 09:00:02 +0200212 if (!file_priv->is_master)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000213 return -EINVAL;
Jonas Bonn6b008422009-04-16 09:00:02 +0200214
Dave Airlie07f1c7a2009-04-20 09:32:50 +1000215 if (!file_priv->minor->master)
216 return -EINVAL;
217
Dave Airlie7c1c2872008-11-28 14:22:24 +1000218 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000219 if (dev->driver->master_drop)
220 dev->driver->master_drop(dev, file_priv, false);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000221 drm_master_put(&file_priv->minor->master);
Jonas Bonn6b008422009-04-16 09:00:02 +0200222 file_priv->is_master = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000223 mutex_unlock(&dev->struct_mutex);
224 return 0;
225}
226
Dave Airlie84b1fd12007-07-11 15:53:27 +1000227static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 const struct pci_device_id *ent,
229 struct drm_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int retcode;
232
Dave Airliebd1b3312007-05-26 05:01:51 +1000233 INIT_LIST_HEAD(&dev->filelist);
Dave Airlie7c158ac2007-07-11 12:05:36 +1000234 INIT_LIST_HEAD(&dev->ctxlist);
235 INIT_LIST_HEAD(&dev->vmalist);
236 INIT_LIST_HEAD(&dev->maplist);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000237 INIT_LIST_HEAD(&dev->vblank_event_list);
Dave Airlie7c158ac2007-07-11 12:05:36 +1000238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 spin_lock_init(&dev->count_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000240 spin_lock_init(&dev->drw_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000241 spin_lock_init(&dev->event_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 init_timer(&dev->timer);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100243 mutex_init(&dev->struct_mutex);
244 mutex_init(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Dave Airlie45ea5dcd2007-07-17 14:20:07 +1000246 idr_init(&dev->drw_idr);
247
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 dev->pdev = pdev;
Eric Anholt2f02cc32006-09-22 04:19:34 +1000249 dev->pci_device = pdev->device;
250 dev->pci_vendor = pdev->vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252#ifdef __alpha__
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 dev->hose = pdev->sysdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000256 if (drm_ht_create(&dev->map_hash, 12)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000257 return -ENOMEM;
258 }
Dave Airlie836cf042005-07-10 19:27:04 +1000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* the DRM has 6 basic counters */
261 dev->counters = 6;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 dev->types[0] = _DRM_STAT_LOCK;
263 dev->types[1] = _DRM_STAT_OPENS;
264 dev->types[2] = _DRM_STAT_CLOSES;
265 dev->types[3] = _DRM_STAT_IOCTLS;
266 dev->types[4] = _DRM_STAT_LOCKS;
267 dev->types[5] = _DRM_STAT_UNLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 dev->driver = driver;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (drm_core_has_AGP(dev)) {
Dave Airliecda17382005-07-10 17:31:26 +1000272 if (drm_device_is_agp(dev))
273 dev->agp = drm_agp_init(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
275 && (dev->agp == NULL)) {
276 DRM_ERROR("Cannot initialize the agpgart module.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 retcode = -EINVAL;
278 goto error_out_unreg;
279 }
280 if (drm_core_has_MTRR(dev)) {
281 if (dev->agp)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 dev->agp->agp_mtrr =
283 mtrr_add(dev->agp->agp_info.aper_base,
284 dev->agp->agp_info.aper_size *
285 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
288
Dave Airlie2716a022007-11-22 18:23:13 +1000289
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 retcode = drm_ctxbitmap_init(dev);
291 if (retcode) {
292 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto error_out_unreg;
294 }
295
Eric Anholt673a3942008-07-30 12:06:12 -0700296 if (driver->driver_features & DRIVER_GEM) {
297 retcode = drm_gem_init(dev);
298 if (retcode) {
299 DRM_ERROR("Cannot initialize graphics execution "
300 "manager (GEM)\n");
301 goto error_out_unreg;
302 }
303 }
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000306
307 error_out_unreg:
Dave Airlie22eae942005-11-10 22:16:34 +1100308 drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return retcode;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/**
314 * Get a secondary minor number.
315 *
316 * \param dev device data structure
317 * \param sec-minor structure to hold the assigned minor
318 * \return negative number on failure.
319 *
320 * Search an empty entry and initialize it to the given parameters, and
321 * create the proc init entry via proc_init(). This routines assigns
322 * minor numbers to secondary heads of multi-headed cards
323 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000324static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Dave Airlie2c14f282008-04-21 16:47:32 +1000326 struct drm_minor *new_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int ret;
Dave Airlie2c14f282008-04-21 16:47:32 +1000328 int minor_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 DRM_DEBUG("\n");
331
Dave Airlie2c14f282008-04-21 16:47:32 +1000332 minor_id = drm_minor_get_id(dev, type);
333 if (minor_id < 0)
334 return minor_id;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335
Dave Airlie2c14f282008-04-21 16:47:32 +1000336 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
337 if (!new_minor) {
338 ret = -ENOMEM;
339 goto err_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Dave Airlie2c14f282008-04-21 16:47:32 +1000341
342 new_minor->type = type;
343 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
344 new_minor->dev = dev;
345 new_minor->index = minor_id;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000346 INIT_LIST_HEAD(&new_minor->master_list);
Dave Airlie2c14f282008-04-21 16:47:32 +1000347
348 idr_replace(&drm_minors_idr, new_minor, minor_id);
349
350 if (type == DRM_MINOR_LEGACY) {
351 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
352 if (ret) {
353 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
354 goto err_mem;
355 }
356 } else
Ben Gamari955b12d2009-02-17 20:08:49 -0500357 new_minor->proc_root = NULL;
358
359#if defined(CONFIG_DEBUG_FS)
360 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
361 if (ret) {
GeunSik Lim156f5a72009-06-02 15:01:37 +0900362 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
Ben Gamari955b12d2009-02-17 20:08:49 -0500363 goto err_g2;
364 }
365#endif
Dave Airlie2c14f282008-04-21 16:47:32 +1000366
367 ret = drm_sysfs_device_add(new_minor);
368 if (ret) {
369 printk(KERN_ERR
370 "DRM: Error sysfs_device_add.\n");
371 goto err_g2;
372 }
373 *minor = new_minor;
374
375 DRM_DEBUG("new minor assigned %d\n", minor_id);
376 return 0;
377
378
379err_g2:
380 if (new_minor->type == DRM_MINOR_LEGACY)
381 drm_proc_cleanup(new_minor, drm_proc_root);
382err_mem:
383 kfree(new_minor);
384err_idr:
385 idr_remove(&drm_minors_idr, minor_id);
386 *minor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return ret;
388}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000389
Dave Airliec94f7022005-07-07 21:03:38 +1000390/**
391 * Register.
392 *
393 * \param pdev - PCI device structure
394 * \param ent entry from the PCI ID table with device type flags
395 * \return zero on success or a negative number on failure.
396 *
397 * Attempt to gets inter module "drm" information. If we are first
398 * then register the character device and inter module information.
399 * Try and register, if we fail to register, backout previous work.
400 */
401int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 struct drm_driver *driver)
Dave Airliec94f7022005-07-07 21:03:38 +1000403{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000404 struct drm_device *dev;
Dave Airliec94f7022005-07-07 21:03:38 +1000405 int ret;
406
407 DRM_DEBUG("\n");
408
Eric Anholt9a298b22009-03-24 12:23:04 -0700409 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dave Airliec94f7022005-07-07 21:03:38 +1000410 if (!dev)
411 return -ENOMEM;
412
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100413 ret = pci_enable_device(pdev);
414 if (ret)
415 goto err_g1;
Dave Airliec94f7022005-07-07 21:03:38 +1000416
Dave Airlie19a8f59a2008-02-07 14:48:32 +1000417 pci_set_master(pdev);
Dave Airliec94f7022005-07-07 21:03:38 +1000418 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
419 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100420 goto err_g2;
Dave Airliec94f7022005-07-07 21:03:38 +1000421 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000422
Dave Airlief453ba02008-11-07 14:05:41 -0800423 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500424 pci_set_drvdata(pdev, dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800425 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
426 if (ret)
427 goto err_g2;
428 }
429
430 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
431 goto err_g3;
432
433 if (dev->driver->load) {
434 ret = dev->driver->load(dev, ent->driver_data);
435 if (ret)
Ben Skeggs3788f482009-03-02 10:37:44 +1000436 goto err_g4;
Dave Airlief453ba02008-11-07 14:05:41 -0800437 }
438
439 /* setup the grouping for the legacy output */
440 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
441 ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
442 if (ret)
Ben Skeggs3788f482009-03-02 10:37:44 +1000443 goto err_g4;
Dave Airlief453ba02008-11-07 14:05:41 -0800444 }
Dave Airliea9d51a52008-12-07 12:02:21 +1000445
Dave Airliee7f7ab42008-11-28 13:43:47 +1000446 list_add_tail(&dev->driver_item, &driver->device_list);
447
Benjamin Herrenschmidtcd00f952009-02-06 16:46:27 +1100448 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
Dave Airlie22eae942005-11-10 22:16:34 +1100449 driver->name, driver->major, driver->minor, driver->patchlevel,
Benjamin Herrenschmidtcd00f952009-02-06 16:46:27 +1100450 driver->date, pci_name(pdev), dev->primary->index);
Dave Airliec94f7022005-07-07 21:03:38 +1000451
452 return 0;
453
Ben Skeggs3788f482009-03-02 10:37:44 +1000454err_g4:
Dave Airliea9d51a52008-12-07 12:02:21 +1000455 drm_put_minor(&dev->primary);
Ben Skeggs3788f482009-03-02 10:37:44 +1000456err_g3:
457 if (drm_core_check_feature(dev, DRIVER_MODESET))
458 drm_put_minor(&dev->control);
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100459err_g2:
460 pci_disable_device(pdev);
461err_g1:
Eric Anholt9a298b22009-03-24 12:23:04 -0700462 kfree(dev);
Dave Airliec94f7022005-07-07 21:03:38 +1000463 return ret;
464}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500465EXPORT_SYMBOL(drm_get_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467/**
468 * Put a secondary minor number.
469 *
470 * \param sec_minor - structure to be released
471 * \return always zero
472 *
473 * Cleans up the proc resources. Not legal for this to be the
474 * last minor released.
475 *
476 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000477int drm_put_minor(struct drm_minor **minor_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Dave Airlie2c14f282008-04-21 16:47:32 +1000479 struct drm_minor *minor = *minor_p;
Eric Anholt673a3942008-07-30 12:06:12 -0700480
Dave Airlie2c14f282008-04-21 16:47:32 +1000481 DRM_DEBUG("release secondary minor %d\n", minor->index);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482
Dave Airlie2c14f282008-04-21 16:47:32 +1000483 if (minor->type == DRM_MINOR_LEGACY)
484 drm_proc_cleanup(minor, drm_proc_root);
Ben Gamari955b12d2009-02-17 20:08:49 -0500485#if defined(CONFIG_DEBUG_FS)
486 drm_debugfs_cleanup(minor);
487#endif
488
Dave Airlie2c14f282008-04-21 16:47:32 +1000489 drm_sysfs_device_remove(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490
Dave Airlie2c14f282008-04-21 16:47:32 +1000491 idr_remove(&drm_minors_idr, minor->index);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492
Dave Airlie2c14f282008-04-21 16:47:32 +1000493 kfree(minor);
494 *minor_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500497
498/**
499 * Called via drm_exit() at module unload time or when pci device is
500 * unplugged.
501 *
502 * Cleans up all DRM device, calling drm_lastclose().
503 *
504 * \sa drm_init
505 */
506void drm_put_dev(struct drm_device *dev)
507{
Julia Lawallecca0682009-07-11 09:50:09 +0200508 struct drm_driver *driver;
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500509 struct drm_map_list *r_list, *list_temp;
510
511 DRM_DEBUG("\n");
512
513 if (!dev) {
514 DRM_ERROR("cleanup called no dev\n");
515 return;
516 }
Julia Lawallecca0682009-07-11 09:50:09 +0200517 driver = dev->driver;
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500518
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500519 drm_lastclose(dev);
520
521 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
522 dev->agp && dev->agp->agp_mtrr >= 0) {
523 int retval;
524 retval = mtrr_del(dev->agp->agp_mtrr,
525 dev->agp->agp_info.aper_base,
526 dev->agp->agp_info.aper_size * 1024 * 1024);
527 DRM_DEBUG("mtrr_del=%d\n", retval);
528 }
529
530 if (dev->driver->unload)
531 dev->driver->unload(dev);
532
533 if (drm_core_has_AGP(dev) && dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700534 kfree(dev->agp);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500535 dev->agp = NULL;
536 }
537
Jesse Barnesb78315f2010-03-26 11:07:16 -0700538 drm_vblank_cleanup(dev);
539
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500540 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
541 drm_rmmap(dev, r_list->map);
Ben Skeggs30ddbd92009-03-02 11:13:04 +1000542 drm_ht_remove(&dev->map_hash);
543
544 drm_ctxbitmap_cleanup(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500545
546 if (drm_core_check_feature(dev, DRIVER_MODESET))
547 drm_put_minor(&dev->control);
548
549 if (driver->driver_features & DRIVER_GEM)
550 drm_gem_destroy(dev);
551
552 drm_put_minor(&dev->primary);
553
554 if (dev->devname) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700555 kfree(dev->devname);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500556 dev->devname = NULL;
557 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700558 kfree(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500559}
560EXPORT_SYMBOL(drm_put_dev);