Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 39 | unsigned int drm_debug = 0; /* 1 to enable debug output */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | EXPORT_SYMBOL(drm_debug); |
| 41 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 42 | MODULE_AUTHOR(CORE_AUTHOR); |
| 43 | MODULE_DESCRIPTION(CORE_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | MODULE_LICENSE("GPL and additional rights"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | MODULE_PARM_DESC(debug, "Enable debug output"); |
| 46 | |
Dave Jones | c075814 | 2005-10-03 15:02:20 -0400 | [diff] [blame] | 47 | module_param_named(debug, drm_debug, int, 0600); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 49 | struct idr drm_minors_idr; |
| 50 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 51 | struct class *drm_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct proc_dir_entry *drm_proc_root; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 53 | struct dentry *drm_debugfs_root; |
yakui_zhao | 4fefcb2 | 2009-06-02 14:09:47 +0800 | [diff] [blame] | 54 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
yakui_zhao | 4fefcb2 | 2009-06-02 14:09:47 +0800 | [diff] [blame] | 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 | } |
| 69 | EXPORT_SYMBOL(drm_ut_debug_printk); |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 70 | static 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 Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 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 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 84 | again: |
| 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 Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 106 | struct drm_master *drm_master_create(struct drm_minor *minor) |
| 107 | { |
| 108 | struct drm_master *master; |
| 109 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 110 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 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 | |
| 126 | struct drm_master *drm_master_get(struct drm_master *master) |
| 127 | { |
| 128 | kref_get(&master->refcount); |
| 129 | return master; |
| 130 | } |
| 131 | |
| 132 | static void drm_master_destroy(struct kref *kref) |
| 133 | { |
| 134 | struct drm_master *master = container_of(kref, struct drm_master, refcount); |
| 135 | struct drm_magic_entry *pt, *next; |
| 136 | struct drm_device *dev = master->minor->dev; |
Dave Airlie | c1ff85d | 2009-01-19 17:17:58 +1000 | [diff] [blame] | 137 | struct drm_map_list *r_list, *list_temp; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 138 | |
| 139 | list_del(&master->head); |
| 140 | |
| 141 | if (dev->driver->master_destroy) |
| 142 | dev->driver->master_destroy(dev, master); |
| 143 | |
Dave Airlie | c1ff85d | 2009-01-19 17:17:58 +1000 | [diff] [blame] | 144 | list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) { |
| 145 | if (r_list->master == master) { |
| 146 | drm_rmmap_locked(dev, r_list->map); |
| 147 | r_list = NULL; |
| 148 | } |
| 149 | } |
| 150 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 151 | if (master->unique) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 152 | kfree(master->unique); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 153 | master->unique = NULL; |
| 154 | master->unique_len = 0; |
| 155 | } |
| 156 | |
| 157 | list_for_each_entry_safe(pt, next, &master->magicfree, head) { |
| 158 | list_del(&pt->head); |
| 159 | drm_ht_remove_item(&master->magiclist, &pt->hash_item); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 160 | kfree(pt); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | drm_ht_remove(&master->magiclist); |
| 164 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 165 | kfree(master); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void drm_master_put(struct drm_master **master) |
| 169 | { |
| 170 | kref_put(&(*master)->refcount, drm_master_destroy); |
| 171 | *master = NULL; |
| 172 | } |
| 173 | |
| 174 | int drm_setmaster_ioctl(struct drm_device *dev, void *data, |
| 175 | struct drm_file *file_priv) |
| 176 | { |
Thomas Hellstrom | 862302f | 2009-12-02 18:15:25 +0000 | [diff] [blame^] | 177 | int ret = 0; |
| 178 | |
Jonas Bonn | 6b00842 | 2009-04-16 09:00:02 +0200 | [diff] [blame] | 179 | if (file_priv->is_master) |
| 180 | return 0; |
| 181 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 182 | if (file_priv->minor->master && file_priv->minor->master != file_priv->master) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | if (!file_priv->master) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | if (!file_priv->minor->master && |
| 189 | file_priv->minor->master != file_priv->master) { |
| 190 | mutex_lock(&dev->struct_mutex); |
| 191 | file_priv->minor->master = drm_master_get(file_priv->master); |
Jonas Bonn | 6b00842 | 2009-04-16 09:00:02 +0200 | [diff] [blame] | 192 | file_priv->is_master = 1; |
Thomas Hellstrom | 862302f | 2009-12-02 18:15:25 +0000 | [diff] [blame^] | 193 | if (dev->driver->master_set) { |
| 194 | ret = dev->driver->master_set(dev, file_priv, false); |
| 195 | if (unlikely(ret != 0)) { |
| 196 | file_priv->is_master = 0; |
| 197 | drm_master_put(&file_priv->minor->master); |
| 198 | } |
| 199 | } |
Helge Bahmann | 5ad8b7d | 2009-03-04 21:49:14 +1000 | [diff] [blame] | 200 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int drm_dropmaster_ioctl(struct drm_device *dev, void *data, |
| 207 | struct drm_file *file_priv) |
| 208 | { |
Jonas Bonn | 6b00842 | 2009-04-16 09:00:02 +0200 | [diff] [blame] | 209 | if (!file_priv->is_master) |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 210 | return -EINVAL; |
Jonas Bonn | 6b00842 | 2009-04-16 09:00:02 +0200 | [diff] [blame] | 211 | |
Dave Airlie | 07f1c7a | 2009-04-20 09:32:50 +1000 | [diff] [blame] | 212 | if (!file_priv->minor->master) |
| 213 | return -EINVAL; |
| 214 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 215 | mutex_lock(&dev->struct_mutex); |
Thomas Hellstrom | 862302f | 2009-12-02 18:15:25 +0000 | [diff] [blame^] | 216 | if (dev->driver->master_drop) |
| 217 | dev->driver->master_drop(dev, file_priv, false); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 218 | drm_master_put(&file_priv->minor->master); |
Jonas Bonn | 6b00842 | 2009-04-16 09:00:02 +0200 | [diff] [blame] | 219 | file_priv->is_master = 0; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 220 | mutex_unlock(&dev->struct_mutex); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 224 | static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 225 | const struct pci_device_id *ent, |
| 226 | struct drm_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | int retcode; |
| 229 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 230 | INIT_LIST_HEAD(&dev->filelist); |
Dave Airlie | 7c158ac | 2007-07-11 12:05:36 +1000 | [diff] [blame] | 231 | INIT_LIST_HEAD(&dev->ctxlist); |
| 232 | INIT_LIST_HEAD(&dev->vmalist); |
| 233 | INIT_LIST_HEAD(&dev->maplist); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 234 | INIT_LIST_HEAD(&dev->vblank_event_list); |
Dave Airlie | 7c158ac | 2007-07-11 12:05:36 +1000 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | spin_lock_init(&dev->count_lock); |
=?utf-8?q?Michel_D=C3=A4nzer?= | bea5679 | 2006-10-24 23:04:19 +1000 | [diff] [blame] | 237 | spin_lock_init(&dev->drw_lock); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 238 | spin_lock_init(&dev->event_lock); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 239 | init_timer(&dev->timer); |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 240 | mutex_init(&dev->struct_mutex); |
| 241 | mutex_init(&dev->ctxlist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Dave Airlie | 45ea5dcd | 2007-07-17 14:20:07 +1000 | [diff] [blame] | 243 | idr_init(&dev->drw_idr); |
| 244 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 245 | dev->pdev = pdev; |
Eric Anholt | 2f02cc3 | 2006-09-22 04:19:34 +1000 | [diff] [blame] | 246 | dev->pci_device = pdev->device; |
| 247 | dev->pci_vendor = pdev->vendor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | #ifdef __alpha__ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 250 | dev->hose = pdev->sysdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 253 | if (drm_ht_create(&dev->map_hash, 12)) { |
Thomas Hellstrom | 8d153f7 | 2006-08-07 22:36:47 +1000 | [diff] [blame] | 254 | return -ENOMEM; |
| 255 | } |
Dave Airlie | 836cf04 | 2005-07-10 19:27:04 +1000 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | /* the DRM has 6 basic counters */ |
| 258 | dev->counters = 6; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 259 | dev->types[0] = _DRM_STAT_LOCK; |
| 260 | dev->types[1] = _DRM_STAT_OPENS; |
| 261 | dev->types[2] = _DRM_STAT_CLOSES; |
| 262 | dev->types[3] = _DRM_STAT_IOCTLS; |
| 263 | dev->types[4] = _DRM_STAT_LOCKS; |
| 264 | dev->types[5] = _DRM_STAT_UNLOCKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | dev->driver = driver; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (drm_core_has_AGP(dev)) { |
Dave Airlie | cda1738 | 2005-07-10 17:31:26 +1000 | [diff] [blame] | 269 | if (drm_device_is_agp(dev)) |
| 270 | dev->agp = drm_agp_init(dev); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 271 | if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) |
| 272 | && (dev->agp == NULL)) { |
| 273 | DRM_ERROR("Cannot initialize the agpgart module.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | retcode = -EINVAL; |
| 275 | goto error_out_unreg; |
| 276 | } |
| 277 | if (drm_core_has_MTRR(dev)) { |
| 278 | if (dev->agp) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 279 | dev->agp->agp_mtrr = |
| 280 | mtrr_add(dev->agp->agp_info.aper_base, |
| 281 | dev->agp->agp_info.aper_size * |
| 282 | 1024 * 1024, MTRR_TYPE_WRCOMB, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Dave Airlie | 2716a02 | 2007-11-22 18:23:13 +1000 | [diff] [blame] | 286 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 287 | retcode = drm_ctxbitmap_init(dev); |
| 288 | if (retcode) { |
| 289 | DRM_ERROR("Cannot allocate memory for context bitmap.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | goto error_out_unreg; |
| 291 | } |
| 292 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 293 | if (driver->driver_features & DRIVER_GEM) { |
| 294 | retcode = drm_gem_init(dev); |
| 295 | if (retcode) { |
| 296 | DRM_ERROR("Cannot initialize graphics execution " |
| 297 | "manager (GEM)\n"); |
| 298 | goto error_out_unreg; |
| 299 | } |
| 300 | } |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 303 | |
| 304 | error_out_unreg: |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 305 | drm_lastclose(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return retcode; |
| 307 | } |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Get a secondary minor number. |
| 312 | * |
| 313 | * \param dev device data structure |
| 314 | * \param sec-minor structure to hold the assigned minor |
| 315 | * \return negative number on failure. |
| 316 | * |
| 317 | * Search an empty entry and initialize it to the given parameters, and |
| 318 | * create the proc init entry via proc_init(). This routines assigns |
| 319 | * minor numbers to secondary heads of multi-headed cards |
| 320 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 321 | static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 323 | struct drm_minor *new_minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | int ret; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 325 | int minor_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | DRM_DEBUG("\n"); |
| 328 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 329 | minor_id = drm_minor_get_id(dev, type); |
| 330 | if (minor_id < 0) |
| 331 | return minor_id; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 332 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 333 | new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL); |
| 334 | if (!new_minor) { |
| 335 | ret = -ENOMEM; |
| 336 | goto err_idr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 338 | |
| 339 | new_minor->type = type; |
| 340 | new_minor->device = MKDEV(DRM_MAJOR, minor_id); |
| 341 | new_minor->dev = dev; |
| 342 | new_minor->index = minor_id; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 343 | INIT_LIST_HEAD(&new_minor->master_list); |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 344 | |
| 345 | idr_replace(&drm_minors_idr, new_minor, minor_id); |
| 346 | |
| 347 | if (type == DRM_MINOR_LEGACY) { |
| 348 | ret = drm_proc_init(new_minor, minor_id, drm_proc_root); |
| 349 | if (ret) { |
| 350 | DRM_ERROR("DRM: Failed to initialize /proc/dri.\n"); |
| 351 | goto err_mem; |
| 352 | } |
| 353 | } else |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 354 | new_minor->proc_root = NULL; |
| 355 | |
| 356 | #if defined(CONFIG_DEBUG_FS) |
| 357 | ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root); |
| 358 | if (ret) { |
GeunSik Lim | 156f5a7 | 2009-06-02 15:01:37 +0900 | [diff] [blame] | 359 | DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n"); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 360 | goto err_g2; |
| 361 | } |
| 362 | #endif |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 363 | |
| 364 | ret = drm_sysfs_device_add(new_minor); |
| 365 | if (ret) { |
| 366 | printk(KERN_ERR |
| 367 | "DRM: Error sysfs_device_add.\n"); |
| 368 | goto err_g2; |
| 369 | } |
| 370 | *minor = new_minor; |
| 371 | |
| 372 | DRM_DEBUG("new minor assigned %d\n", minor_id); |
| 373 | return 0; |
| 374 | |
| 375 | |
| 376 | err_g2: |
| 377 | if (new_minor->type == DRM_MINOR_LEGACY) |
| 378 | drm_proc_cleanup(new_minor, drm_proc_root); |
| 379 | err_mem: |
| 380 | kfree(new_minor); |
| 381 | err_idr: |
| 382 | idr_remove(&drm_minors_idr, minor_id); |
| 383 | *minor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return ret; |
| 385 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 386 | |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 387 | /** |
| 388 | * Register. |
| 389 | * |
| 390 | * \param pdev - PCI device structure |
| 391 | * \param ent entry from the PCI ID table with device type flags |
| 392 | * \return zero on success or a negative number on failure. |
| 393 | * |
| 394 | * Attempt to gets inter module "drm" information. If we are first |
| 395 | * then register the character device and inter module information. |
| 396 | * Try and register, if we fail to register, backout previous work. |
| 397 | */ |
| 398 | int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 399 | struct drm_driver *driver) |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 400 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 401 | struct drm_device *dev; |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 402 | int ret; |
| 403 | |
| 404 | DRM_DEBUG("\n"); |
| 405 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 406 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 407 | if (!dev) |
| 408 | return -ENOMEM; |
| 409 | |
Jeff Garzik | 2c3f0ed | 2006-12-09 10:50:22 +1100 | [diff] [blame] | 410 | ret = pci_enable_device(pdev); |
| 411 | if (ret) |
| 412 | goto err_g1; |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 413 | |
Dave Airlie | 19a8f59a | 2008-02-07 14:48:32 +1000 | [diff] [blame] | 414 | pci_set_master(pdev); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 415 | if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) { |
| 416 | printk(KERN_ERR "DRM: Fill_in_dev failed.\n"); |
Jeff Garzik | 2c3f0ed | 2006-12-09 10:50:22 +1100 | [diff] [blame] | 417 | goto err_g2; |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 418 | } |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 419 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 420 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 421 | pci_set_drvdata(pdev, dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 422 | ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL); |
| 423 | if (ret) |
| 424 | goto err_g2; |
| 425 | } |
| 426 | |
| 427 | if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY))) |
| 428 | goto err_g3; |
| 429 | |
| 430 | if (dev->driver->load) { |
| 431 | ret = dev->driver->load(dev, ent->driver_data); |
| 432 | if (ret) |
Ben Skeggs | 3788f48 | 2009-03-02 10:37:44 +1000 | [diff] [blame] | 433 | goto err_g4; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* setup the grouping for the legacy output */ |
| 437 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
| 438 | ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group); |
| 439 | if (ret) |
Ben Skeggs | 3788f48 | 2009-03-02 10:37:44 +1000 | [diff] [blame] | 440 | goto err_g4; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 441 | } |
Dave Airlie | a9d51a5 | 2008-12-07 12:02:21 +1000 | [diff] [blame] | 442 | |
Dave Airlie | e7f7ab4 | 2008-11-28 13:43:47 +1000 | [diff] [blame] | 443 | list_add_tail(&dev->driver_item, &driver->device_list); |
| 444 | |
Benjamin Herrenschmidt | cd00f95 | 2009-02-06 16:46:27 +1100 | [diff] [blame] | 445 | DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n", |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 446 | driver->name, driver->major, driver->minor, driver->patchlevel, |
Benjamin Herrenschmidt | cd00f95 | 2009-02-06 16:46:27 +1100 | [diff] [blame] | 447 | driver->date, pci_name(pdev), dev->primary->index); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | |
Ben Skeggs | 3788f48 | 2009-03-02 10:37:44 +1000 | [diff] [blame] | 451 | err_g4: |
Dave Airlie | a9d51a5 | 2008-12-07 12:02:21 +1000 | [diff] [blame] | 452 | drm_put_minor(&dev->primary); |
Ben Skeggs | 3788f48 | 2009-03-02 10:37:44 +1000 | [diff] [blame] | 453 | err_g3: |
| 454 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 455 | drm_put_minor(&dev->control); |
Jeff Garzik | 2c3f0ed | 2006-12-09 10:50:22 +1100 | [diff] [blame] | 456 | err_g2: |
| 457 | pci_disable_device(pdev); |
| 458 | err_g1: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 459 | kfree(dev); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 460 | return ret; |
| 461 | } |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 462 | EXPORT_SYMBOL(drm_get_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * Put a secondary minor number. |
| 466 | * |
| 467 | * \param sec_minor - structure to be released |
| 468 | * \return always zero |
| 469 | * |
| 470 | * Cleans up the proc resources. Not legal for this to be the |
| 471 | * last minor released. |
| 472 | * |
| 473 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 474 | int drm_put_minor(struct drm_minor **minor_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 476 | struct drm_minor *minor = *minor_p; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 477 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 478 | DRM_DEBUG("release secondary minor %d\n", minor->index); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 479 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 480 | if (minor->type == DRM_MINOR_LEGACY) |
| 481 | drm_proc_cleanup(minor, drm_proc_root); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 482 | #if defined(CONFIG_DEBUG_FS) |
| 483 | drm_debugfs_cleanup(minor); |
| 484 | #endif |
| 485 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 486 | drm_sysfs_device_remove(minor); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 487 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 488 | idr_remove(&drm_minors_idr, minor->index); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 489 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 490 | kfree(minor); |
| 491 | *minor_p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return 0; |
| 493 | } |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Called via drm_exit() at module unload time or when pci device is |
| 497 | * unplugged. |
| 498 | * |
| 499 | * Cleans up all DRM device, calling drm_lastclose(). |
| 500 | * |
| 501 | * \sa drm_init |
| 502 | */ |
| 503 | void drm_put_dev(struct drm_device *dev) |
| 504 | { |
Julia Lawall | ecca068 | 2009-07-11 09:50:09 +0200 | [diff] [blame] | 505 | struct drm_driver *driver; |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 506 | struct drm_map_list *r_list, *list_temp; |
| 507 | |
| 508 | DRM_DEBUG("\n"); |
| 509 | |
| 510 | if (!dev) { |
| 511 | DRM_ERROR("cleanup called no dev\n"); |
| 512 | return; |
| 513 | } |
Julia Lawall | ecca068 | 2009-07-11 09:50:09 +0200 | [diff] [blame] | 514 | driver = dev->driver; |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 515 | |
| 516 | drm_vblank_cleanup(dev); |
| 517 | |
| 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) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 533 | kfree(dev->agp); |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 534 | dev->agp = NULL; |
| 535 | } |
| 536 | |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 537 | list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) |
| 538 | drm_rmmap(dev, r_list->map); |
Ben Skeggs | 30ddbd9 | 2009-03-02 11:13:04 +1000 | [diff] [blame] | 539 | drm_ht_remove(&dev->map_hash); |
| 540 | |
| 541 | drm_ctxbitmap_cleanup(dev); |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 542 | |
| 543 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 544 | drm_put_minor(&dev->control); |
| 545 | |
| 546 | if (driver->driver_features & DRIVER_GEM) |
| 547 | drm_gem_destroy(dev); |
| 548 | |
| 549 | drm_put_minor(&dev->primary); |
| 550 | |
| 551 | if (dev->devname) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 552 | kfree(dev->devname); |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 553 | dev->devname = NULL; |
| 554 | } |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 555 | kfree(dev); |
Kristian Høgsberg | 112b715 | 2009-01-04 16:55:33 -0500 | [diff] [blame] | 556 | } |
| 557 | EXPORT_SYMBOL(drm_put_dev); |