Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / gpu / drm / drm_stub.c
bloba0c365f2e521a393ead336e55950ba8aa966c11e
1 /**
2 * \file drm_stub.h
3 * Stub support
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 */
8 /*
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
23 * Software.
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>
37 #include "drmP.h"
38 #include "drm_core.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,
56 const char *prefix,
57 const char *function_name,
58 const char *format, ...)
60 va_list args;
62 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);
70 EXPORT_SYMBOL(drm_ut_debug_printk);
71 static int drm_minor_get_id(struct drm_device *dev, int type)
73 int new_id;
74 int ret;
75 int base = 0, limit = 63;
77 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;
85 again:
86 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
87 DRM_ERROR("Out of memory expanding drawable idr\n");
88 return -ENOMEM;
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;
100 if (new_id >= limit) {
101 idr_remove(&drm_minors_idr, new_id);
102 return -EINVAL;
104 return 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);
112 if (!master)
113 return NULL;
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);
124 return master;
127 struct drm_master *drm_master_get(struct drm_master *master)
129 kref_get(&master->refcount);
130 return master;
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);
149 r_list = NULL;
153 if (master->unique) {
154 kfree(master->unique);
155 master->unique = NULL;
156 master->unique_len = 0;
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);
162 kfree(pt);
165 drm_ht_remove(&master->magiclist);
167 kfree(master);
170 void drm_master_put(struct drm_master **master)
172 kref_put(&(*master)->refcount, drm_master_destroy);
173 *master = NULL;
175 EXPORT_SYMBOL(drm_master_put);
177 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
178 struct drm_file *file_priv)
180 int ret = 0;
182 if (file_priv->is_master)
183 return 0;
185 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
186 return -EINVAL;
188 if (!file_priv->master)
189 return -EINVAL;
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);
195 file_priv->is_master = 1;
196 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);
203 mutex_unlock(&dev->struct_mutex);
206 return 0;
209 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
210 struct drm_file *file_priv)
212 if (!file_priv->is_master)
213 return -EINVAL;
215 if (!file_priv->minor->master)
216 return -EINVAL;
218 mutex_lock(&dev->struct_mutex);
219 if (dev->driver->master_drop)
220 dev->driver->master_drop(dev, file_priv, false);
221 drm_master_put(&file_priv->minor->master);
222 file_priv->is_master = 0;
223 mutex_unlock(&dev->struct_mutex);
224 return 0;
227 static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
228 const struct pci_device_id *ent,
229 struct drm_driver *driver)
231 int retcode;
233 INIT_LIST_HEAD(&dev->filelist);
234 INIT_LIST_HEAD(&dev->ctxlist);
235 INIT_LIST_HEAD(&dev->vmalist);
236 INIT_LIST_HEAD(&dev->maplist);
237 INIT_LIST_HEAD(&dev->vblank_event_list);
239 spin_lock_init(&dev->count_lock);
240 spin_lock_init(&dev->drw_lock);
241 spin_lock_init(&dev->event_lock);
242 init_timer(&dev->timer);
243 mutex_init(&dev->struct_mutex);
244 mutex_init(&dev->ctxlist_mutex);
246 idr_init(&dev->drw_idr);
248 dev->pdev = pdev;
249 dev->pci_device = pdev->device;
250 dev->pci_vendor = pdev->vendor;
252 #ifdef __alpha__
253 dev->hose = pdev->sysdata;
254 #endif
256 if (drm_ht_create(&dev->map_hash, 12)) {
257 return -ENOMEM;
260 /* the DRM has 6 basic counters */
261 dev->counters = 6;
262 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;
269 dev->driver = driver;
271 if (drm_core_has_AGP(dev)) {
272 if (drm_device_is_agp(dev))
273 dev->agp = drm_agp_init(dev);
274 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
275 && (dev->agp == NULL)) {
276 DRM_ERROR("Cannot initialize the agpgart module.\n");
277 retcode = -EINVAL;
278 goto error_out_unreg;
280 if (drm_core_has_MTRR(dev)) {
281 if (dev->agp)
282 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);
290 retcode = drm_ctxbitmap_init(dev);
291 if (retcode) {
292 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
293 goto error_out_unreg;
296 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;
305 return 0;
307 error_out_unreg:
308 drm_lastclose(dev);
309 return retcode;
314 * Get a secondary minor number.
316 * \param dev device data structure
317 * \param sec-minor structure to hold the assigned minor
318 * \return negative number on failure.
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
324 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
326 struct drm_minor *new_minor;
327 int ret;
328 int minor_id;
330 DRM_DEBUG("\n");
332 minor_id = drm_minor_get_id(dev, type);
333 if (minor_id < 0)
334 return minor_id;
336 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
337 if (!new_minor) {
338 ret = -ENOMEM;
339 goto err_idr;
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;
346 INIT_LIST_HEAD(&new_minor->master_list);
348 idr_replace(&drm_minors_idr, new_minor, minor_id);
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;
356 } else
357 new_minor->proc_root = NULL;
359 #if defined(CONFIG_DEBUG_FS)
360 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
361 if (ret) {
362 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
363 goto err_g2;
365 #endif
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;
373 *minor = new_minor;
375 DRM_DEBUG("new minor assigned %d\n", minor_id);
376 return 0;
379 err_g2:
380 if (new_minor->type == DRM_MINOR_LEGACY)
381 drm_proc_cleanup(new_minor, drm_proc_root);
382 err_mem:
383 kfree(new_minor);
384 err_idr:
385 idr_remove(&drm_minors_idr, minor_id);
386 *minor = NULL;
387 return ret;
391 * Register.
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.
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.
401 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
402 struct drm_driver *driver)
404 struct drm_device *dev;
405 int ret;
407 DRM_DEBUG("\n");
409 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
410 if (!dev)
411 return -ENOMEM;
413 ret = pci_enable_device(pdev);
414 if (ret)
415 goto err_g1;
417 pci_set_master(pdev);
418 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
419 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
420 goto err_g2;
423 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
424 pci_set_drvdata(pdev, dev);
425 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
426 if (ret)
427 goto err_g2;
430 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
431 goto err_g3;
433 if (dev->driver->load) {
434 ret = dev->driver->load(dev, ent->driver_data);
435 if (ret)
436 goto err_g4;
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)
443 goto err_g4;
446 list_add_tail(&dev->driver_item, &driver->device_list);
448 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
449 driver->name, driver->major, driver->minor, driver->patchlevel,
450 driver->date, pci_name(pdev), dev->primary->index);
452 return 0;
454 err_g4:
455 drm_put_minor(&dev->primary);
456 err_g3:
457 if (drm_core_check_feature(dev, DRIVER_MODESET))
458 drm_put_minor(&dev->control);
459 err_g2:
460 pci_disable_device(pdev);
461 err_g1:
462 kfree(dev);
463 return ret;
465 EXPORT_SYMBOL(drm_get_dev);
468 * Put a secondary minor number.
470 * \param sec_minor - structure to be released
471 * \return always zero
473 * Cleans up the proc resources. Not legal for this to be the
474 * last minor released.
477 int drm_put_minor(struct drm_minor **minor_p)
479 struct drm_minor *minor = *minor_p;
481 DRM_DEBUG("release secondary minor %d\n", minor->index);
483 if (minor->type == DRM_MINOR_LEGACY)
484 drm_proc_cleanup(minor, drm_proc_root);
485 #if defined(CONFIG_DEBUG_FS)
486 drm_debugfs_cleanup(minor);
487 #endif
489 drm_sysfs_device_remove(minor);
491 idr_remove(&drm_minors_idr, minor->index);
493 kfree(minor);
494 *minor_p = NULL;
495 return 0;
499 * Called via drm_exit() at module unload time or when pci device is
500 * unplugged.
502 * Cleans up all DRM device, calling drm_lastclose().
504 * \sa drm_init
506 void drm_put_dev(struct drm_device *dev)
508 struct drm_driver *driver;
509 struct drm_map_list *r_list, *list_temp;
511 DRM_DEBUG("\n");
513 if (!dev) {
514 DRM_ERROR("cleanup called no dev\n");
515 return;
517 driver = dev->driver;
519 drm_lastclose(dev);
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);
530 if (dev->driver->unload)
531 dev->driver->unload(dev);
533 if (drm_core_has_AGP(dev) && dev->agp) {
534 kfree(dev->agp);
535 dev->agp = NULL;
538 drm_vblank_cleanup(dev);
540 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
541 drm_rmmap(dev, r_list->map);
542 drm_ht_remove(&dev->map_hash);
544 drm_ctxbitmap_cleanup(dev);
546 if (drm_core_check_feature(dev, DRIVER_MODESET))
547 drm_put_minor(&dev->control);
549 if (driver->driver_features & DRIVER_GEM)
550 drm_gem_destroy(dev);
552 drm_put_minor(&dev->primary);
554 if (dev->devname) {
555 kfree(dev->devname);
556 dev->devname = NULL;
558 kfree(dev);
560 EXPORT_SYMBOL(drm_put_dev);