5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
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
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>
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;
55 static int drm_minor_get_id(struct drm_device *dev, int type)
59 int base = 0, limit = 63;
61 if (type == DRM_MINOR_CONTROL) {
64 } else if (type == DRM_MINOR_RENDER) {
70 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
71 DRM_ERROR("Out of memory expanding drawable idr\n");
74 mutex_lock(&dev->struct_mutex);
75 ret = idr_get_new_above(&drm_minors_idr, NULL,
77 mutex_unlock(&dev->struct_mutex);
84 if (new_id >= limit) {
85 idr_remove(&drm_minors_idr, new_id);
91 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
92 struct drm_file *file_priv)
94 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
97 if (!file_priv->master)
100 if (!file_priv->minor->master && file_priv->minor->master != file_priv->master)
101 file_priv->minor->master = file_priv->master;
105 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
106 struct drm_file *file_priv)
108 if (!file_priv->master)
110 file_priv->minor->master = NULL;
114 struct drm_master *drm_get_master(struct drm_minor *minor)
116 struct drm_master *master;
118 master = drm_calloc(1, sizeof(*master), DRM_MEM_DRIVER);
122 // INIT_LIST_HEAD(&master->filelist);
123 spin_lock_init(&master->lock.spinlock);
124 init_waitqueue_head(&master->lock.lock_queue);
125 drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
126 INIT_LIST_HEAD(&master->magicfree);
127 master->minor = minor;
129 list_add_tail(&master->head, &minor->master_list);
134 void drm_put_master(struct drm_master *master)
136 struct drm_magic_entry *pt, *next;
137 struct drm_device *dev = master->minor->dev;
139 list_del(&master->head);
141 if (dev->driver->master_destroy)
142 dev->driver->master_destroy(dev, master);
144 if (master->unique) {
145 drm_free(master->unique, strlen(master->unique) + 1, DRM_MEM_DRIVER);
146 master->unique = NULL;
147 master->unique_len = 0;
150 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
152 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
153 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
156 drm_ht_remove(&master->magiclist);
158 if (master->lock.hw_lock) {
159 if (dev->sigdata.lock == master->lock.hw_lock)
160 dev->sigdata.lock = NULL;
161 master->lock.hw_lock = NULL; /* SHM removed */
162 master->lock.file_priv = NULL;
163 wake_up_interruptible(&master->lock.lock_queue);
166 drm_free(master, sizeof(*master), DRM_MEM_DRIVER);
169 static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
170 const struct pci_device_id *ent,
171 struct drm_driver *driver)
175 INIT_LIST_HEAD(&dev->ctxlist);
176 INIT_LIST_HEAD(&dev->vmalist);
177 INIT_LIST_HEAD(&dev->maplist);
178 INIT_LIST_HEAD(&dev->filelist);
180 spin_lock_init(&dev->count_lock);
181 spin_lock_init(&dev->drw_lock);
182 spin_lock_init(&dev->tasklet_lock);
183 // spin_lock_init(&dev->lock.spinlock);
184 init_timer(&dev->timer);
185 mutex_init(&dev->struct_mutex);
186 mutex_init(&dev->ctxlist_mutex);
187 mutex_init(&dev->bm.evict_mutex);
189 idr_init(&dev->drw_idr);
192 dev->pci_device = pdev->device;
193 dev->pci_vendor = pdev->vendor;
196 dev->hose = pdev->sysdata;
198 dev->irq = pdev->irq;
199 dev->irq_enabled = 0;
201 if (drm_ht_create(&dev->map_hash, DRM_MAP_HASH_ORDER))
203 if (drm_mm_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START,
204 DRM_FILE_PAGE_OFFSET_SIZE)) {
205 drm_ht_remove(&dev->map_hash);
209 if (drm_ht_create(&dev->object_hash, DRM_OBJECT_HASH_ORDER)) {
210 drm_ht_remove(&dev->map_hash);
211 drm_mm_takedown(&dev->offset_manager);
215 /* the DRM has 6 counters */
217 dev->types[0] = _DRM_STAT_LOCK;
218 dev->types[1] = _DRM_STAT_OPENS;
219 dev->types[2] = _DRM_STAT_CLOSES;
220 dev->types[3] = _DRM_STAT_IOCTLS;
221 dev->types[4] = _DRM_STAT_LOCKS;
222 dev->types[5] = _DRM_STAT_UNLOCKS;
224 dev->driver = driver;
226 if (drm_core_has_AGP(dev)) {
227 if (drm_device_is_agp(dev))
228 dev->agp = drm_agp_init(dev);
229 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
230 && (dev->agp == NULL)) {
231 DRM_ERROR("Cannot initialize the agpgart module.\n");
233 goto error_out_unreg;
236 if (drm_core_has_MTRR(dev)) {
239 mtrr_add(dev->agp->agp_info.aper_base,
240 dev->agp->agp_info.aper_size *
241 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
245 retcode = drm_ctxbitmap_init(dev);
247 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
248 goto error_out_unreg;
251 if (driver->driver_features & DRIVER_GEM) {
252 retcode = drm_gem_init (dev);
254 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
255 goto error_out_unreg;
259 drm_fence_manager_init(dev);
269 * Get a secondary minor number.
271 * \param dev device data structure
272 * \param sec-minor structure to hold the assigned minor
273 * \return negative number on failure.
275 * Search an empty entry and initialize it to the given parameters, and
276 * create the proc init entry via proc_init(). This routines assigns
277 * minor numbers to secondary heads of multi-headed cards
279 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
281 struct drm_minor *new_minor;
287 minor_id = drm_minor_get_id(dev, type);
291 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
297 new_minor->type = type;
298 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
299 new_minor->dev = dev;
300 new_minor->index = minor_id;
301 INIT_LIST_HEAD(&new_minor->master_list);
303 idr_replace(&drm_minors_idr, new_minor, minor_id);
305 if (type == DRM_MINOR_LEGACY) {
306 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
308 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
311 if (dev->driver->proc_init) {
312 ret = dev->driver->proc_init(new_minor);
314 DRM_ERROR("DRM: Driver failed to initialize /proc/dri.\n");
319 new_minor->dev_root = NULL;
321 ret = drm_sysfs_device_add(new_minor);
324 "DRM: Error sysfs_device_add.\n");
329 DRM_DEBUG("new minor assigned %d\n", minor_id);
334 if (new_minor->type == DRM_MINOR_LEGACY) {
335 if (dev->driver->proc_cleanup)
336 dev->driver->proc_cleanup(new_minor);
337 drm_proc_cleanup(new_minor, drm_proc_root);
342 idr_remove(&drm_minors_idr, minor_id);
350 * \param pdev - PCI device structure
351 * \param ent entry from the PCI ID table with device type flags
352 * \return zero on success or a negative number on failure.
354 * Attempt to gets inter module "drm" information. If we are first
355 * then register the character device and inter module information.
356 * Try and register, if we fail to register, backout previous work.
358 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
359 struct drm_driver *driver)
361 struct drm_device *dev;
366 dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
370 if (!drm_fb_loaded) {
371 pci_set_drvdata(pdev, dev);
372 ret = pci_request_regions(pdev, driver->pci_driver.name);
377 ret = pci_enable_device(pdev);
380 pci_set_master(pdev);
382 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
383 printk(KERN_ERR "DRM: fill_in_dev failed\n");
387 /* only add the control node on a modesetting platform */
388 if (drm_core_check_feature(dev, DRIVER_MODESET))
389 if ((ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL)))
392 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
395 if (dev->driver->load)
396 if ((ret = dev->driver->load(dev, ent->driver_data)))
399 /* setup the grouping for the legacy output */
400 if (drm_core_check_feature(dev, DRIVER_MODESET))
401 if (drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group))
404 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
405 driver->name, driver->major, driver->minor, driver->patchlevel,
406 driver->date, dev->primary->index);
410 drm_put_minor(dev, &dev->primary);
412 if (drm_core_check_feature(dev, DRIVER_MODESET))
413 drm_put_minor(dev, &dev->control);
416 pci_disable_device(pdev);
419 pci_release_regions(pdev);
422 pci_set_drvdata(pdev, NULL);
424 drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
425 printk(KERN_ERR "DRM: drm_get_dev failed.\n");
428 EXPORT_SYMBOL(drm_get_dev);
432 * Put a device minor number.
434 * \param dev device data structure
435 * \return always zero
437 * Cleans up the proc resources. If it is the last minor then release the foreign
438 * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
439 * unregisters the character device.
441 int drm_put_dev(struct drm_device * dev)
443 DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
446 drm_free(dev->devname, strlen(dev->devname) + 1,
450 drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
455 * Put a secondary minor number.
457 * \param sec_minor - structure to be released
458 * \return always zero
460 * Cleans up the proc resources. Not legal for this to be the
461 * last minor released.
464 int drm_put_minor(struct drm_device *dev, struct drm_minor **minor_p)
466 struct drm_minor *minor = *minor_p;
467 DRM_DEBUG("release secondary minor %d\n", minor->index);
469 if (minor->type == DRM_MINOR_LEGACY) {
470 if (dev->driver->proc_cleanup)
471 dev->driver->proc_cleanup(minor);
472 drm_proc_cleanup(minor, drm_proc_root);
474 drm_sysfs_device_remove(minor);
476 idr_remove(&drm_minors_idr, minor->index);