blob: ad73e141afdbd8096e2908d75e5849962ae1f069 [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>
36#include "drmP.h"
37#include "drm_core.h"
38
Dave Airlieb5e89ed2005-09-25 14:28:13 +100039unsigned int drm_debug = 0; /* 1 to enable debug output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040EXPORT_SYMBOL(drm_debug);
41
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042MODULE_AUTHOR(CORE_AUTHOR);
43MODULE_DESCRIPTION(CORE_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_LICENSE("GPL and additional rights");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_PARM_DESC(debug, "Enable debug output");
46
Dave Jonesc0758142005-10-03 15:02:20 -040047module_param_named(debug, drm_debug, int, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Dave Airlie2c14f282008-04-21 16:47:32 +100049struct idr drm_minors_idr;
50
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080051struct class *drm_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct proc_dir_entry *drm_proc_root;
Ben Gamari955b12d2009-02-17 20:08:49 -050053struct dentry *drm_debugfs_root;
yakui_zhao4fefcb22009-06-02 14:09:47 +080054void drm_ut_debug_printk(unsigned int request_level,
55 const char *prefix,
56 const char *function_name,
57 const char *format, ...)
58{
59 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
yakui_zhao4fefcb22009-06-02 14:09:47 +080061 if (drm_debug & request_level) {
62 if (function_name)
63 printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
64 va_start(args, format);
65 vprintk(format, args);
66 va_end(args);
67 }
68}
69EXPORT_SYMBOL(drm_ut_debug_printk);
Dave Airlie2c14f282008-04-21 16:47:32 +100070static int drm_minor_get_id(struct drm_device *dev, int type)
71{
72 int new_id;
73 int ret;
74 int base = 0, limit = 63;
75
Dave Airlief453ba02008-11-07 14:05:41 -080076 if (type == DRM_MINOR_CONTROL) {
77 base += 64;
78 limit = base + 127;
79 } else if (type == DRM_MINOR_RENDER) {
80 base += 128;
81 limit = base + 255;
82 }
83
Dave Airlie2c14f282008-04-21 16:47:32 +100084again:
85 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
86 DRM_ERROR("Out of memory expanding drawable idr\n");
87 return -ENOMEM;
88 }
89 mutex_lock(&dev->struct_mutex);
90 ret = idr_get_new_above(&drm_minors_idr, NULL,
91 base, &new_id);
92 mutex_unlock(&dev->struct_mutex);
93 if (ret == -EAGAIN) {
94 goto again;
95 } else if (ret) {
96 return ret;
97 }
98
99 if (new_id >= limit) {
100 idr_remove(&drm_minors_idr, new_id);
101 return -EINVAL;
102 }
103 return new_id;
104}
105
Dave Airlie7c1c2872008-11-28 14:22:24 +1000106struct drm_master *drm_master_create(struct drm_minor *minor)
107{
108 struct drm_master *master;
109
Eric Anholt9a298b22009-03-24 12:23:04 -0700110 master = kzalloc(sizeof(*master), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000111 if (!master)
112 return NULL;
113
114 kref_init(&master->refcount);
115 spin_lock_init(&master->lock.spinlock);
116 init_waitqueue_head(&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
126struct drm_master *drm_master_get(struct drm_master *master)
127{
128 kref_get(&master->refcount);
129 return master;
130}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100131EXPORT_SYMBOL(drm_master_get);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132
133static void drm_master_destroy(struct kref *kref)
134{
135 struct drm_master *master = container_of(kref, struct drm_master, refcount);
136 struct drm_magic_entry *pt, *next;
137 struct drm_device *dev = master->minor->dev;
Dave Airliec1ff85d2009-01-19 17:17:58 +1000138 struct drm_map_list *r_list, *list_temp;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000139
140 list_del(&master->head);
141
142 if (dev->driver->master_destroy)
143 dev->driver->master_destroy(dev, master);
144
Dave Airliec1ff85d2009-01-19 17:17:58 +1000145 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
146 if (r_list->master == master) {
147 drm_rmmap_locked(dev, r_list->map);
148 r_list = NULL;
149 }
150 }
151
Dave Airlie7c1c2872008-11-28 14:22:24 +1000152 if (master->unique) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700153 kfree(master->unique);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000154 master->unique = NULL;
155 master->unique_len = 0;
156 }
157
158 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
159 list_del(&pt->head);
160 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
Eric Anholt9a298b22009-03-24 12:23:04 -0700161 kfree(pt);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000162 }
163
164 drm_ht_remove(&master->magiclist);
165
Eric Anholt9a298b22009-03-24 12:23:04 -0700166 kfree(master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000167}
168
169void drm_master_put(struct drm_master **master)
170{
171 kref_put(&(*master)->refcount, drm_master_destroy);
172 *master = NULL;
173}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100174EXPORT_SYMBOL(drm_master_put);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000175
176int drm_setmaster_ioctl(struct drm_device *dev, void *data,
177 struct drm_file *file_priv)
178{
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000179 int ret = 0;
180
Jonas Bonn6b008422009-04-16 09:00:02 +0200181 if (file_priv->is_master)
182 return 0;
183
Dave Airlie7c1c2872008-11-28 14:22:24 +1000184 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 file_priv->minor->master != file_priv->master) {
192 mutex_lock(&dev->struct_mutex);
193 file_priv->minor->master = drm_master_get(file_priv->master);
Jonas Bonn6b008422009-04-16 09:00:02 +0200194 file_priv->is_master = 1;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000195 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 }
Helge Bahmann5ad8b7d2009-03-04 21:49:14 +1000202 mutex_unlock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000203 }
204
205 return 0;
206}
207
208int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
209 struct drm_file *file_priv)
210{
Jonas Bonn6b008422009-04-16 09:00:02 +0200211 if (!file_priv->is_master)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000212 return -EINVAL;
Jonas Bonn6b008422009-04-16 09:00:02 +0200213
Dave Airlie07f1c7a2009-04-20 09:32:50 +1000214 if (!file_priv->minor->master)
215 return -EINVAL;
216
Dave Airlie7c1c2872008-11-28 14:22:24 +1000217 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000218 if (dev->driver->master_drop)
219 dev->driver->master_drop(dev, file_priv, false);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000220 drm_master_put(&file_priv->minor->master);
Jonas Bonn6b008422009-04-16 09:00:02 +0200221 file_priv->is_master = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000222 mutex_unlock(&dev->struct_mutex);
223 return 0;
224}
225
Dave Airlie84b1fd12007-07-11 15:53:27 +1000226static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 const struct pci_device_id *ent,
228 struct drm_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int retcode;
231
Dave Airliebd1b3312007-05-26 05:01:51 +1000232 INIT_LIST_HEAD(&dev->filelist);
Dave Airlie7c158ac2007-07-11 12:05:36 +1000233 INIT_LIST_HEAD(&dev->ctxlist);
234 INIT_LIST_HEAD(&dev->vmalist);
235 INIT_LIST_HEAD(&dev->maplist);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000236 INIT_LIST_HEAD(&dev->vblank_event_list);
Dave Airlie7c158ac2007-07-11 12:05:36 +1000237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_lock_init(&dev->count_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000239 spin_lock_init(&dev->drw_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +1000240 spin_lock_init(&dev->event_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 init_timer(&dev->timer);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100242 mutex_init(&dev->struct_mutex);
243 mutex_init(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Dave Airlie45ea5dcd2007-07-17 14:20:07 +1000245 idr_init(&dev->drw_idr);
246
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 dev->pdev = pdev;
Eric Anholt2f02cc32006-09-22 04:19:34 +1000248 dev->pci_device = pdev->device;
249 dev->pci_vendor = pdev->vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#ifdef __alpha__
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 dev->hose = pdev->sysdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000255 if (drm_ht_create(&dev->map_hash, 12)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000256 return -ENOMEM;
257 }
Dave Airlie836cf042005-07-10 19:27:04 +1000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* the DRM has 6 basic counters */
260 dev->counters = 6;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 dev->types[0] = _DRM_STAT_LOCK;
262 dev->types[1] = _DRM_STAT_OPENS;
263 dev->types[2] = _DRM_STAT_CLOSES;
264 dev->types[3] = _DRM_STAT_IOCTLS;
265 dev->types[4] = _DRM_STAT_LOCKS;
266 dev->types[5] = _DRM_STAT_UNLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 dev->driver = driver;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (drm_core_has_AGP(dev)) {
Dave Airliecda17382005-07-10 17:31:26 +1000271 if (drm_device_is_agp(dev))
272 dev->agp = drm_agp_init(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
274 && (dev->agp == NULL)) {
275 DRM_ERROR("Cannot initialize the agpgart module.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 retcode = -EINVAL;
277 goto error_out_unreg;
278 }
279 if (drm_core_has_MTRR(dev)) {
280 if (dev->agp)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 dev->agp->agp_mtrr =
282 mtrr_add(dev->agp->agp_info.aper_base,
283 dev->agp->agp_info.aper_size *
284 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
287
Dave Airlie2716a022007-11-22 18:23:13 +1000288
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 retcode = drm_ctxbitmap_init(dev);
290 if (retcode) {
291 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto error_out_unreg;
293 }
294
Eric Anholt673a3942008-07-30 12:06:12 -0700295 if (driver->driver_features & DRIVER_GEM) {
296 retcode = drm_gem_init(dev);
297 if (retcode) {
298 DRM_ERROR("Cannot initialize graphics execution "
299 "manager (GEM)\n");
300 goto error_out_unreg;
301 }
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305
306 error_out_unreg:
Dave Airlie22eae942005-11-10 22:16:34 +1100307 drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return retcode;
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/**
313 * Get a secondary minor number.
314 *
315 * \param dev device data structure
316 * \param sec-minor structure to hold the assigned minor
317 * \return negative number on failure.
318 *
319 * Search an empty entry and initialize it to the given parameters, and
320 * create the proc init entry via proc_init(). This routines assigns
321 * minor numbers to secondary heads of multi-headed cards
322 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000323static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Dave Airlie2c14f282008-04-21 16:47:32 +1000325 struct drm_minor *new_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int ret;
Dave Airlie2c14f282008-04-21 16:47:32 +1000327 int minor_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 DRM_DEBUG("\n");
330
Dave Airlie2c14f282008-04-21 16:47:32 +1000331 minor_id = drm_minor_get_id(dev, type);
332 if (minor_id < 0)
333 return minor_id;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334
Dave Airlie2c14f282008-04-21 16:47:32 +1000335 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
336 if (!new_minor) {
337 ret = -ENOMEM;
338 goto err_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Dave Airlie2c14f282008-04-21 16:47:32 +1000340
341 new_minor->type = type;
342 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
343 new_minor->dev = dev;
344 new_minor->index = minor_id;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000345 INIT_LIST_HEAD(&new_minor->master_list);
Dave Airlie2c14f282008-04-21 16:47:32 +1000346
347 idr_replace(&drm_minors_idr, new_minor, minor_id);
348
349 if (type == DRM_MINOR_LEGACY) {
350 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
351 if (ret) {
352 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
353 goto err_mem;
354 }
355 } else
Ben Gamari955b12d2009-02-17 20:08:49 -0500356 new_minor->proc_root = NULL;
357
358#if defined(CONFIG_DEBUG_FS)
359 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
360 if (ret) {
GeunSik Lim156f5a72009-06-02 15:01:37 +0900361 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
Ben Gamari955b12d2009-02-17 20:08:49 -0500362 goto err_g2;
363 }
364#endif
Dave Airlie2c14f282008-04-21 16:47:32 +1000365
366 ret = drm_sysfs_device_add(new_minor);
367 if (ret) {
368 printk(KERN_ERR
369 "DRM: Error sysfs_device_add.\n");
370 goto err_g2;
371 }
372 *minor = new_minor;
373
374 DRM_DEBUG("new minor assigned %d\n", minor_id);
375 return 0;
376
377
378err_g2:
379 if (new_minor->type == DRM_MINOR_LEGACY)
380 drm_proc_cleanup(new_minor, drm_proc_root);
381err_mem:
382 kfree(new_minor);
383err_idr:
384 idr_remove(&drm_minors_idr, minor_id);
385 *minor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return ret;
387}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388
Dave Airliec94f7022005-07-07 21:03:38 +1000389/**
390 * Register.
391 *
392 * \param pdev - PCI device structure
393 * \param ent entry from the PCI ID table with device type flags
394 * \return zero on success or a negative number on failure.
395 *
396 * Attempt to gets inter module "drm" information. If we are first
397 * then register the character device and inter module information.
398 * Try and register, if we fail to register, backout previous work.
399 */
400int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 struct drm_driver *driver)
Dave Airliec94f7022005-07-07 21:03:38 +1000402{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000403 struct drm_device *dev;
Dave Airliec94f7022005-07-07 21:03:38 +1000404 int ret;
405
406 DRM_DEBUG("\n");
407
Eric Anholt9a298b22009-03-24 12:23:04 -0700408 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dave Airliec94f7022005-07-07 21:03:38 +1000409 if (!dev)
410 return -ENOMEM;
411
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100412 ret = pci_enable_device(pdev);
413 if (ret)
414 goto err_g1;
Dave Airliec94f7022005-07-07 21:03:38 +1000415
Dave Airlie19a8f59a2008-02-07 14:48:32 +1000416 pci_set_master(pdev);
Dave Airliec94f7022005-07-07 21:03:38 +1000417 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
418 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100419 goto err_g2;
Dave Airliec94f7022005-07-07 21:03:38 +1000420 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000421
Dave Airlief453ba02008-11-07 14:05:41 -0800422 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500423 pci_set_drvdata(pdev, dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800424 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
425 if (ret)
426 goto err_g2;
427 }
428
429 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
430 goto err_g3;
431
432 if (dev->driver->load) {
433 ret = dev->driver->load(dev, ent->driver_data);
434 if (ret)
Ben Skeggs3788f482009-03-02 10:37:44 +1000435 goto err_g4;
Dave Airlief453ba02008-11-07 14:05:41 -0800436 }
437
438 /* setup the grouping for the legacy output */
439 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
440 ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
441 if (ret)
Ben Skeggs3788f482009-03-02 10:37:44 +1000442 goto err_g4;
Dave Airlief453ba02008-11-07 14:05:41 -0800443 }
Dave Airliea9d51a52008-12-07 12:02:21 +1000444
Dave Airliee7f7ab42008-11-28 13:43:47 +1000445 list_add_tail(&dev->driver_item, &driver->device_list);
446
Benjamin Herrenschmidtcd00f952009-02-06 16:46:27 +1100447 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
Dave Airlie22eae942005-11-10 22:16:34 +1100448 driver->name, driver->major, driver->minor, driver->patchlevel,
Benjamin Herrenschmidtcd00f952009-02-06 16:46:27 +1100449 driver->date, pci_name(pdev), dev->primary->index);
Dave Airliec94f7022005-07-07 21:03:38 +1000450
451 return 0;
452
Ben Skeggs3788f482009-03-02 10:37:44 +1000453err_g4:
Dave Airliea9d51a52008-12-07 12:02:21 +1000454 drm_put_minor(&dev->primary);
Ben Skeggs3788f482009-03-02 10:37:44 +1000455err_g3:
456 if (drm_core_check_feature(dev, DRIVER_MODESET))
457 drm_put_minor(&dev->control);
Jeff Garzik2c3f0ed2006-12-09 10:50:22 +1100458err_g2:
459 pci_disable_device(pdev);
460err_g1:
Eric Anholt9a298b22009-03-24 12:23:04 -0700461 kfree(dev);
Dave Airliec94f7022005-07-07 21:03:38 +1000462 return ret;
463}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500464EXPORT_SYMBOL(drm_get_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/**
467 * Put a secondary minor number.
468 *
469 * \param sec_minor - structure to be released
470 * \return always zero
471 *
472 * Cleans up the proc resources. Not legal for this to be the
473 * last minor released.
474 *
475 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000476int drm_put_minor(struct drm_minor **minor_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Dave Airlie2c14f282008-04-21 16:47:32 +1000478 struct drm_minor *minor = *minor_p;
Eric Anholt673a3942008-07-30 12:06:12 -0700479
Dave Airlie2c14f282008-04-21 16:47:32 +1000480 DRM_DEBUG("release secondary minor %d\n", minor->index);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481
Dave Airlie2c14f282008-04-21 16:47:32 +1000482 if (minor->type == DRM_MINOR_LEGACY)
483 drm_proc_cleanup(minor, drm_proc_root);
Ben Gamari955b12d2009-02-17 20:08:49 -0500484#if defined(CONFIG_DEBUG_FS)
485 drm_debugfs_cleanup(minor);
486#endif
487
Dave Airlie2c14f282008-04-21 16:47:32 +1000488 drm_sysfs_device_remove(minor);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489
Dave Airlie2c14f282008-04-21 16:47:32 +1000490 idr_remove(&drm_minors_idr, minor->index);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491
Dave Airlie2c14f282008-04-21 16:47:32 +1000492 kfree(minor);
493 *minor_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500496
497/**
498 * Called via drm_exit() at module unload time or when pci device is
499 * unplugged.
500 *
501 * Cleans up all DRM device, calling drm_lastclose().
502 *
503 * \sa drm_init
504 */
505void drm_put_dev(struct drm_device *dev)
506{
Julia Lawallecca0682009-07-11 09:50:09 +0200507 struct drm_driver *driver;
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500508 struct drm_map_list *r_list, *list_temp;
509
510 DRM_DEBUG("\n");
511
512 if (!dev) {
513 DRM_ERROR("cleanup called no dev\n");
514 return;
515 }
Julia Lawallecca0682009-07-11 09:50:09 +0200516 driver = dev->driver;
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500517
518 drm_vblank_cleanup(dev);
519
520 drm_lastclose(dev);
521
522 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
523 dev->agp && dev->agp->agp_mtrr >= 0) {
524 int retval;
525 retval = mtrr_del(dev->agp->agp_mtrr,
526 dev->agp->agp_info.aper_base,
527 dev->agp->agp_info.aper_size * 1024 * 1024);
528 DRM_DEBUG("mtrr_del=%d\n", retval);
529 }
530
531 if (dev->driver->unload)
532 dev->driver->unload(dev);
533
534 if (drm_core_has_AGP(dev) && dev->agp) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700535 kfree(dev->agp);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500536 dev->agp = NULL;
537 }
538
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500539 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
540 drm_rmmap(dev, r_list->map);
Ben Skeggs30ddbd92009-03-02 11:13:04 +1000541 drm_ht_remove(&dev->map_hash);
542
543 drm_ctxbitmap_cleanup(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500544
545 if (drm_core_check_feature(dev, DRIVER_MODESET))
546 drm_put_minor(&dev->control);
547
548 if (driver->driver_features & DRIVER_GEM)
549 drm_gem_destroy(dev);
550
551 drm_put_minor(&dev->primary);
552
553 if (dev->devname) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700554 kfree(dev->devname);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500555 dev->devname = NULL;
556 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700557 kfree(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500558}
559EXPORT_SYMBOL(drm_put_dev);