]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/drm_stub.c
drm: delay vblank cleanup until after driver unload
[net-next-2.6.git] / drivers / gpu / drm / drm_stub.c
CommitLineData
1da177e4
LT
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 <linux/module.h>
35#include <linux/moduleparam.h>
36#include "drmP.h"
37#include "drm_core.h"
38
b5e89ed5 39unsigned int drm_debug = 0; /* 1 to enable debug output */
1da177e4
LT
40EXPORT_SYMBOL(drm_debug);
41
b5e89ed5
DA
42MODULE_AUTHOR(CORE_AUTHOR);
43MODULE_DESCRIPTION(CORE_DESC);
1da177e4 44MODULE_LICENSE("GPL and additional rights");
1da177e4
LT
45MODULE_PARM_DESC(debug, "Enable debug output");
46
c0758146 47module_param_named(debug, drm_debug, int, 0600);
1da177e4 48
2c14f28b
DA
49struct idr drm_minors_idr;
50
0650fd58 51struct class *drm_class;
1da177e4 52struct proc_dir_entry *drm_proc_root;
955b12de 53struct dentry *drm_debugfs_root;
4fefcb27 54void 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;
1da177e4 60
4fefcb27 61 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);
2c14f28b
DA
70static 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
f453ba04
DA
76 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
2c14f28b
DA
84again:
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
7c1c2871
DA
106struct drm_master *drm_master_create(struct drm_minor *minor)
107{
108 struct drm_master *master;
109
9a298b2a 110 master = kzalloc(sizeof(*master), GFP_KERNEL);
7c1c2871
DA
111 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}
85bb0c37 131EXPORT_SYMBOL(drm_master_get);
7c1c2871
DA
132
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;
c1ff85d9 138 struct drm_map_list *r_list, *list_temp;
7c1c2871
DA
139
140 list_del(&master->head);
141
142 if (dev->driver->master_destroy)
143 dev->driver->master_destroy(dev, master);
144
c1ff85d9
DA
145 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
7c1c2871 152 if (master->unique) {
9a298b2a 153 kfree(master->unique);
7c1c2871
DA
154 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);
9a298b2a 161 kfree(pt);
7c1c2871
DA
162 }
163
164 drm_ht_remove(&master->magiclist);
165
9a298b2a 166 kfree(master);
7c1c2871
DA
167}
168
169void drm_master_put(struct drm_master **master)
170{
171 kref_put(&(*master)->refcount, drm_master_destroy);
172 *master = NULL;
173}
85bb0c37 174EXPORT_SYMBOL(drm_master_put);
7c1c2871
DA
175
176int drm_setmaster_ioctl(struct drm_device *dev, void *data,
177 struct drm_file *file_priv)
178{
862302ff
TH
179 int ret = 0;
180
6b008426
JB
181 if (file_priv->is_master)
182 return 0;
183
7c1c2871
DA
184 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);
6b008426 194 file_priv->is_master = 1;
862302ff
TH
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 }
5ad8b7d1 202 mutex_unlock(&dev->struct_mutex);
7c1c2871
DA
203 }
204
205 return 0;
206}
207
208int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
209 struct drm_file *file_priv)
210{
6b008426 211 if (!file_priv->is_master)
7c1c2871 212 return -EINVAL;
6b008426 213
07f1c7a7
DA
214 if (!file_priv->minor->master)
215 return -EINVAL;
216
7c1c2871 217 mutex_lock(&dev->struct_mutex);
862302ff
TH
218 if (dev->driver->master_drop)
219 dev->driver->master_drop(dev, file_priv, false);
7c1c2871 220 drm_master_put(&file_priv->minor->master);
6b008426 221 file_priv->is_master = 0;
7c1c2871
DA
222 mutex_unlock(&dev->struct_mutex);
223 return 0;
224}
225
84b1fd10 226static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
b5e89ed5
DA
227 const struct pci_device_id *ent,
228 struct drm_driver *driver)
1da177e4
LT
229{
230 int retcode;
231
bd1b331f 232 INIT_LIST_HEAD(&dev->filelist);
7c158ace
DA
233 INIT_LIST_HEAD(&dev->ctxlist);
234 INIT_LIST_HEAD(&dev->vmalist);
235 INIT_LIST_HEAD(&dev->maplist);
c9a9c5e0 236 INIT_LIST_HEAD(&dev->vblank_event_list);
7c158ace 237
1da177e4 238 spin_lock_init(&dev->count_lock);
bea5679f 239 spin_lock_init(&dev->drw_lock);
c9a9c5e0 240 spin_lock_init(&dev->event_lock);
b5e89ed5 241 init_timer(&dev->timer);
30e2fb18
DA
242 mutex_init(&dev->struct_mutex);
243 mutex_init(&dev->ctxlist_mutex);
1da177e4 244
45ea5dcd
DA
245 idr_init(&dev->drw_idr);
246
b5e89ed5 247 dev->pdev = pdev;
2f02cc3f
EA
248 dev->pci_device = pdev->device;
249 dev->pci_vendor = pdev->vendor;
1da177e4
LT
250
251#ifdef __alpha__
b5e89ed5 252 dev->hose = pdev->sysdata;
1da177e4 253#endif
1da177e4 254
8d153f71 255 if (drm_ht_create(&dev->map_hash, 12)) {
8d153f71
TH
256 return -ENOMEM;
257 }
836cf046 258
1da177e4
LT
259 /* the DRM has 6 basic counters */
260 dev->counters = 6;
b5e89ed5
DA
261 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;
1da177e4
LT
267
268 dev->driver = driver;
b5e89ed5 269
1da177e4 270 if (drm_core_has_AGP(dev)) {
cda17380
DA
271 if (drm_device_is_agp(dev))
272 dev->agp = drm_agp_init(dev);
b5e89ed5
DA
273 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
274 && (dev->agp == NULL)) {
275 DRM_ERROR("Cannot initialize the agpgart module.\n");
1da177e4
LT
276 retcode = -EINVAL;
277 goto error_out_unreg;
278 }
279 if (drm_core_has_MTRR(dev)) {
280 if (dev->agp)
b5e89ed5
DA
281 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);
1da177e4
LT
285 }
286 }
287
2716a02f 288
b5e89ed5
DA
289 retcode = drm_ctxbitmap_init(dev);
290 if (retcode) {
291 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
1da177e4
LT
292 goto error_out_unreg;
293 }
294
673a394b
EA
295 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
1da177e4 304 return 0;
b5e89ed5
DA
305
306 error_out_unreg:
22eae947 307 drm_lastclose(dev);
1da177e4
LT
308 return retcode;
309}
310
1da177e4 311
1da177e4
LT
312/**
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 */
2c14f28b 323static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
1da177e4 324{
2c14f28b 325 struct drm_minor *new_minor;
1da177e4 326 int ret;
2c14f28b 327 int minor_id;
1da177e4
LT
328
329 DRM_DEBUG("\n");
330
2c14f28b
DA
331 minor_id = drm_minor_get_id(dev, type);
332 if (minor_id < 0)
333 return minor_id;
334
335 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
336 if (!new_minor) {
337 ret = -ENOMEM;
338 goto err_idr;
339 }
340
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;
7c1c2871 345 INIT_LIST_HEAD(&new_minor->master_list);
2c14f28b
DA
346
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;
1da177e4 354 }
2c14f28b 355 } else
955b12de
BG
356 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) {
156f5a78 361 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
955b12de
BG
362 goto err_g2;
363 }
364#endif
2c14f28b
DA
365
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;
1da177e4 371 }
2c14f28b
DA
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;
1da177e4
LT
386 return ret;
387}
b5e89ed5 388
c94f7029
DA
389/**
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,
b5e89ed5 401 struct drm_driver *driver)
c94f7029 402{
84b1fd10 403 struct drm_device *dev;
c94f7029
DA
404 int ret;
405
406 DRM_DEBUG("\n");
407
9a298b2a 408 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
c94f7029
DA
409 if (!dev)
410 return -ENOMEM;
411
2c3f0edd
JG
412 ret = pci_enable_device(pdev);
413 if (ret)
414 goto err_g1;
c94f7029 415
19a8f59a 416 pci_set_master(pdev);
c94f7029
DA
417 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
418 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
2c3f0edd 419 goto err_g2;
c94f7029 420 }
f453ba04
DA
421
422 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
112b715e 423 pci_set_drvdata(pdev, dev);
f453ba04
DA
424 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
425 if (ret)
426 goto err_g2;
427 }
428
2c14f28b 429 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
f453ba04 430 goto err_g3;
bc5f4523 431
f453ba04
DA
432 if (dev->driver->load) {
433 ret = dev->driver->load(dev, ent->driver_data);
434 if (ret)
3788f48a 435 goto err_g4;
f453ba04
DA
436 }
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)
3788f48a 442 goto err_g4;
f453ba04 443 }
a9d51a5a 444
e7f7ab45
DA
445 list_add_tail(&dev->driver_item, &driver->device_list);
446
cd00f95a 447 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
22eae947 448 driver->name, driver->major, driver->minor, driver->patchlevel,
cd00f95a 449 driver->date, pci_name(pdev), dev->primary->index);
c94f7029
DA
450
451 return 0;
452
3788f48a 453err_g4:
a9d51a5a 454 drm_put_minor(&dev->primary);
3788f48a
BS
455err_g3:
456 if (drm_core_check_feature(dev, DRIVER_MODESET))
457 drm_put_minor(&dev->control);
2c3f0edd
JG
458err_g2:
459 pci_disable_device(pdev);
460err_g1:
9a298b2a 461 kfree(dev);
c94f7029
DA
462 return ret;
463}
112b715e 464EXPORT_SYMBOL(drm_get_dev);
1da177e4
LT
465
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 */
2c14f28b 476int drm_put_minor(struct drm_minor **minor_p)
1da177e4 477{
2c14f28b 478 struct drm_minor *minor = *minor_p;
673a394b 479
2c14f28b 480 DRM_DEBUG("release secondary minor %d\n", minor->index);
b5e89ed5 481
2c14f28b
DA
482 if (minor->type == DRM_MINOR_LEGACY)
483 drm_proc_cleanup(minor, drm_proc_root);
955b12de
BG
484#if defined(CONFIG_DEBUG_FS)
485 drm_debugfs_cleanup(minor);
486#endif
487
2c14f28b 488 drm_sysfs_device_remove(minor);
1da177e4 489
2c14f28b 490 idr_remove(&drm_minors_idr, minor->index);
b5e89ed5 491
2c14f28b
DA
492 kfree(minor);
493 *minor_p = NULL;
1da177e4
LT
494 return 0;
495}
112b715e
KH
496
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{
ecca0683 507 struct drm_driver *driver;
112b715e
KH
508 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 }
ecca0683 516 driver = dev->driver;
112b715e 517
112b715e
KH
518 drm_lastclose(dev);
519
520 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
521 dev->agp && dev->agp->agp_mtrr >= 0) {
522 int retval;
523 retval = mtrr_del(dev->agp->agp_mtrr,
524 dev->agp->agp_info.aper_base,
525 dev->agp->agp_info.aper_size * 1024 * 1024);
526 DRM_DEBUG("mtrr_del=%d\n", retval);
527 }
528
529 if (dev->driver->unload)
530 dev->driver->unload(dev);
531
532 if (drm_core_has_AGP(dev) && dev->agp) {
9a298b2a 533 kfree(dev->agp);
112b715e
KH
534 dev->agp = NULL;
535 }
536
b78315f0
JB
537 drm_vblank_cleanup(dev);
538
112b715e
KH
539 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
540 drm_rmmap(dev, r_list->map);
30ddbd94
BS
541 drm_ht_remove(&dev->map_hash);
542
543 drm_ctxbitmap_cleanup(dev);
112b715e
KH
544
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) {
9a298b2a 554 kfree(dev->devname);
112b715e
KH
555 dev->devname = NULL;
556 }
9a298b2a 557 kfree(dev);
112b715e
KH
558}
559EXPORT_SYMBOL(drm_put_dev);