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))
204 if (drm_memrange_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START,
205 DRM_FILE_PAGE_OFFSET_SIZE)) {
206 drm_ht_remove(&dev->map_hash);
210 if (drm_ht_create(&dev->object_hash, DRM_OBJECT_HASH_ORDER)) {
211 drm_ht_remove(&dev->map_hash);
212 drm_memrange_takedown(&dev->offset_manager);
216 /* the DRM has 6 counters */
218 dev->types[0] = _DRM_STAT_LOCK;
219 dev->types[1] = _DRM_STAT_OPENS;
220 dev->types[2] = _DRM_STAT_CLOSES;
221 dev->types[3] = _DRM_STAT_IOCTLS;
222 dev->types[4] = _DRM_STAT_LOCKS;
223 dev->types[5] = _DRM_STAT_UNLOCKS;
225 dev->driver = driver;
227 if (drm_core_has_AGP(dev)) {
228 if (drm_device_is_agp(dev))
229 dev->agp = drm_agp_init(dev);
230 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
231 && (dev->agp == NULL)) {
232 DRM_ERROR("Cannot initialize the agpgart module.\n");
234 goto error_out_unreg;
237 if (drm_core_has_MTRR(dev)) {
240 mtrr_add(dev->agp->agp_info.aper_base,
241 dev->agp->agp_info.aper_size *
242 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
246 retcode = drm_ctxbitmap_init(dev);
248 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
249 goto error_out_unreg;
252 if (driver->driver_features & DRIVER_GEM) {
253 retcode = drm_gem_init (dev);
255 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
256 goto error_out_unreg;
260 drm_fence_manager_init(dev);
270 * Get a secondary minor number.
272 * \param dev device data structure
273 * \param sec-minor structure to hold the assigned minor
274 * \return negative number on failure.
276 * Search an empty entry and initialize it to the given parameters, and
277 * create the proc init entry via proc_init(). This routines assigns
278 * minor numbers to secondary heads of multi-headed cards
280 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
282 struct drm_minor *new_minor;
288 minor_id = drm_minor_get_id(dev, type);
292 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
298 new_minor->type = type;
299 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
300 new_minor->dev = dev;
301 new_minor->index = minor_id;
302 INIT_LIST_HEAD(&new_minor->master_list);
304 idr_replace(&drm_minors_idr, new_minor, minor_id);
306 if (type == DRM_MINOR_LEGACY) {
307 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
309 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
313 new_minor->dev_root = NULL;
315 ret = drm_sysfs_device_add(new_minor);
318 "DRM: Error sysfs_device_add.\n");
323 DRM_DEBUG("new minor assigned %d\n", minor_id);
328 if (new_minor->type == DRM_MINOR_LEGACY)
329 drm_proc_cleanup(new_minor, drm_proc_root);
333 idr_remove(&drm_minors_idr, minor_id);
341 * \param pdev - PCI device structure
342 * \param ent entry from the PCI ID table with device type flags
343 * \return zero on success or a negative number on failure.
345 * Attempt to gets inter module "drm" information. If we are first
346 * then register the character device and inter module information.
347 * Try and register, if we fail to register, backout previous work.
349 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
350 struct drm_driver *driver)
352 struct drm_device *dev;
357 dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
361 if (!drm_fb_loaded) {
362 pci_set_drvdata(pdev, dev);
363 ret = pci_request_regions(pdev, driver->pci_driver.name);
368 ret = pci_enable_device(pdev);
371 pci_set_master(pdev);
373 if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
374 printk(KERN_ERR "DRM: fill_in_dev failed\n");
378 /* only add the control node on a modesetting platform */
379 if (drm_core_check_feature(dev, DRIVER_MODESET))
380 if ((ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL)))
383 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
386 if (dev->driver->load)
387 if ((ret = dev->driver->load(dev, ent->driver_data)))
390 /* setup the grouping for the legacy output */
391 if (drm_core_check_feature(dev, DRIVER_MODESET))
392 if (drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group))
395 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
396 driver->name, driver->major, driver->minor, driver->patchlevel,
397 driver->date, dev->primary->index);
401 drm_put_minor(&dev->primary);
403 if (drm_core_check_feature(dev, DRIVER_MODESET))
404 drm_put_minor(&dev->control);
407 pci_disable_device(pdev);
410 pci_release_regions(pdev);
413 pci_set_drvdata(pdev, NULL);
415 drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
416 printk(KERN_ERR "DRM: drm_get_dev failed.\n");
419 EXPORT_SYMBOL(drm_get_dev);
423 * Put a device minor number.
425 * \param dev device data structure
426 * \return always zero
428 * Cleans up the proc resources. If it is the last minor then release the foreign
429 * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
430 * unregisters the character device.
432 int drm_put_dev(struct drm_device * dev)
434 DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
437 drm_free(dev->devname, strlen(dev->devname) + 1,
441 drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
446 * Put a secondary minor number.
448 * \param sec_minor - structure to be released
449 * \return always zero
451 * Cleans up the proc resources. Not legal for this to be the
452 * last minor released.
455 int drm_put_minor(struct drm_minor **minor_p)
457 struct drm_minor *minor = *minor_p;
458 DRM_DEBUG("release secondary minor %d\n", minor->index);
460 if (minor->type == DRM_MINOR_LEGACY)
461 drm_proc_cleanup(minor, drm_proc_root);
462 drm_sysfs_device_remove(minor);
464 idr_remove(&drm_minors_idr, minor->index);