dc853b23764d12949bb18e77274fa9254b6f6544
[platform/upstream/libdrm.git] / linux-core / drm_stub.c
1 /**
2  * \file drm_stub.c
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
37 #include "drmP.h"
38 #include "drm_core.h"
39
40 unsigned int drm_debug = 0;             /* 1 to enable debug output */
41 EXPORT_SYMBOL(drm_debug);
42
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");
47
48 module_param_named(debug, drm_debug, int, 0600);
49
50 struct idr drm_minors_idr;
51
52 struct class *drm_class;
53 struct proc_dir_entry *drm_proc_root;
54
55 static int drm_minor_get_id(struct drm_device *dev, int type)
56 {
57         int new_id;
58         int ret;
59         int base = 0, limit = 63;
60
61         if (type == DRM_MINOR_CONTROL) {
62                 base += 64;
63                 limit = base + 127;
64         } else if (type == DRM_MINOR_RENDER) {
65                 base += 128;
66                 limit = base + 255;
67         }       
68
69 again:
70         if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
71                 DRM_ERROR("Out of memory expanding drawable idr\n");
72                 return -ENOMEM;
73         }
74         mutex_lock(&dev->struct_mutex);
75         ret = idr_get_new_above(&drm_minors_idr, NULL,
76                                 base, &new_id);
77         mutex_unlock(&dev->struct_mutex);
78         if (ret == -EAGAIN) {
79                 goto again;
80         } else if (ret) {
81                 return ret;
82         }
83
84         if (new_id >= limit) {
85                 idr_remove(&drm_minors_idr, new_id);
86                 return -EINVAL;
87         }
88         return new_id;
89 }
90
91 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
92                         struct drm_file *file_priv)
93 {
94         if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
95                 return -EINVAL;
96
97         if (!file_priv->master)
98                 return -EINVAL;
99
100         if (!file_priv->minor->master && file_priv->minor->master != file_priv->master)
101                 file_priv->minor->master = file_priv->master;
102         return 0;
103 }
104
105 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
106                          struct drm_file *file_priv)
107 {
108         if (!file_priv->master)
109                 return -EINVAL;
110         file_priv->minor->master = NULL;
111         return 0;
112 }
113
114 struct drm_master *drm_get_master(struct drm_minor *minor)
115 {
116         struct drm_master *master;
117
118         master = drm_calloc(1, sizeof(*master), DRM_MEM_DRIVER);
119         if (!master)
120                 return NULL;
121
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;
128
129         list_add_tail(&master->head, &minor->master_list);
130
131         return master;
132 }
133
134 void drm_put_master(struct drm_master *master)
135 {
136         struct drm_magic_entry *pt, *next;
137         struct drm_device *dev = master->minor->dev;
138
139         list_del(&master->head);
140
141         if (dev->driver->master_destroy)
142                 dev->driver->master_destroy(dev, master);
143
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;
148         }
149
150         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
151                 list_del(&pt->head);
152                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
153                 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
154         }
155
156         drm_ht_remove(&master->magiclist);
157
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);
164         }
165
166         drm_free(master, sizeof(*master), DRM_MEM_DRIVER);
167 }
168
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)
172 {
173         int retcode;
174
175         INIT_LIST_HEAD(&dev->ctxlist);
176         INIT_LIST_HEAD(&dev->vmalist);
177         INIT_LIST_HEAD(&dev->maplist);
178         INIT_LIST_HEAD(&dev->filelist);
179
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);
188
189         idr_init(&dev->drw_idr);
190
191         dev->pdev = pdev;
192         dev->pci_device = pdev->device;
193         dev->pci_vendor = pdev->vendor;
194
195 #ifdef __alpha__
196         dev->hose = pdev->sysdata;
197 #endif
198         dev->irq = pdev->irq;
199         dev->irq_enabled = 0;
200
201         if (drm_ht_create(&dev->map_hash, DRM_MAP_HASH_ORDER))
202                 return -ENOMEM;
203
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);
207                 return -ENOMEM;
208         }
209
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);
213                 return -ENOMEM;
214         }
215
216         /* the DRM has 6 counters */
217         dev->counters = 6;
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;
224
225         dev->driver = driver;
226
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");
233                         retcode = -EINVAL;
234                         goto error_out_unreg;
235                 }
236
237                 if (drm_core_has_MTRR(dev)) {
238                         if (dev->agp)
239                                 dev->agp->agp_mtrr =
240                                     mtrr_add(dev->agp->agp_info.aper_base,
241                                              dev->agp->agp_info.aper_size *
242                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
243                 }
244         }
245
246         retcode = drm_ctxbitmap_init(dev);
247         if (retcode) {
248                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
249                 goto error_out_unreg;
250         }
251
252         if (driver->driver_features & DRIVER_GEM) {
253                 retcode = drm_gem_init (dev);
254                 if (retcode) {
255                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
256                         goto error_out_unreg;
257                 }
258         }
259
260         drm_fence_manager_init(dev);
261
262         return 0;
263
264 error_out_unreg:
265         drm_lastclose(dev);
266         return retcode;
267 }
268
269 /**
270  * Get a secondary minor number.
271  *
272  * \param dev device data structure
273  * \param sec-minor structure to hold the assigned minor
274  * \return negative number on failure.
275  *
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
279  */
280 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
281 {
282         struct drm_minor *new_minor;
283         int ret;
284         int minor_id;
285
286         DRM_DEBUG("\n");
287
288         minor_id = drm_minor_get_id(dev, type);
289         if (minor_id < 0)
290                 return minor_id;
291
292         new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
293         if (!new_minor) {
294                 ret = -ENOMEM;
295                 goto err_idr;
296         }
297
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);
303
304         idr_replace(&drm_minors_idr, new_minor, minor_id);
305         
306         if (type == DRM_MINOR_LEGACY) {
307                 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
308                 if (ret) {
309                         DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
310                         goto err_mem;
311                 }
312         } else
313                 new_minor->dev_root = NULL;
314
315         ret = drm_sysfs_device_add(new_minor);
316         if (ret) {
317                 printk(KERN_ERR
318                        "DRM: Error sysfs_device_add.\n");
319                 goto err_g2;
320         }
321         *minor = new_minor;
322         
323         DRM_DEBUG("new minor assigned %d\n", minor_id);
324         return 0;
325
326
327 err_g2:
328         if (new_minor->type == DRM_MINOR_LEGACY)
329                 drm_proc_cleanup(new_minor, drm_proc_root);
330 err_mem:
331         kfree(new_minor);
332 err_idr:
333         idr_remove(&drm_minors_idr, minor_id);
334         *minor = NULL;
335         return ret;
336 }
337
338 /**
339  * Register.
340  *
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.
344  *
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.
348  */
349 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
350                 struct drm_driver *driver)
351 {
352         struct drm_device *dev;
353         int ret;
354
355         DRM_DEBUG("\n");
356
357         dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
358         if (!dev)
359                 return -ENOMEM;
360
361         if (!drm_fb_loaded) {
362                 pci_set_drvdata(pdev, dev);
363                 ret = pci_request_regions(pdev, driver->pci_driver.name);
364                 if (ret)
365                         goto err_g1;
366         }
367
368         ret = pci_enable_device(pdev);
369         if (ret)
370                 goto err_g2;
371         pci_set_master(pdev);
372
373         if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
374                 printk(KERN_ERR "DRM: fill_in_dev failed\n");
375                 goto err_g3;
376         }
377
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)))
381                         goto err_g3;
382
383         if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
384                 goto err_g4;
385
386         if (dev->driver->load)
387                 if ((ret = dev->driver->load(dev, ent->driver_data)))
388                         goto err_g5;
389
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))
393                     goto err_g5;
394
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);
398
399         return 0;
400 err_g5:
401         drm_put_minor(&dev->primary);
402 err_g4:
403         if (drm_core_check_feature(dev, DRIVER_MODESET))
404                 drm_put_minor(&dev->control);
405 err_g3:
406         if (!drm_fb_loaded)
407                 pci_disable_device(pdev);
408 err_g2:
409         if (!drm_fb_loaded)
410                 pci_release_regions(pdev);
411 err_g1:
412         if (!drm_fb_loaded)
413                 pci_set_drvdata(pdev, NULL);
414
415         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
416         printk(KERN_ERR "DRM: drm_get_dev failed.\n");
417         return ret;
418 }
419 EXPORT_SYMBOL(drm_get_dev);
420
421
422 /**
423  * Put a device minor number.
424  *
425  * \param dev device data structure
426  * \return always zero
427  *
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.
431  */
432 int drm_put_dev(struct drm_device * dev)
433 {
434         DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
435
436         if (dev->devname) {
437                 drm_free(dev->devname, strlen(dev->devname) + 1,
438                          DRM_MEM_DRIVER);
439                 dev->devname = NULL;
440         }
441         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
442         return 0;
443 }
444
445 /**
446  * Put a secondary minor number.
447  *
448  * \param sec_minor - structure to be released
449  * \return always zero
450  *
451  * Cleans up the proc resources. Not legal for this to be the
452  * last minor released.
453  *
454  */
455 int drm_put_minor(struct drm_minor **minor_p)
456 {
457         struct drm_minor *minor = *minor_p;
458         DRM_DEBUG("release secondary minor %d\n", minor->index);
459
460         if (minor->type == DRM_MINOR_LEGACY)
461                 drm_proc_cleanup(minor, drm_proc_root);
462         drm_sysfs_device_remove(minor);
463
464         idr_remove(&drm_minors_idr, minor->index);
465
466         kfree(minor);
467         *minor_p = NULL;
468         return 0;
469 }