OSDN Git Service

Merge commit 'origin/drm-gem' into modesetting-gem
[android-x86/external-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         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);
206                 return -ENOMEM;
207         }
208
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);
212                 return -ENOMEM;
213         }
214
215         /* the DRM has 6 counters */
216         dev->counters = 6;
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;
223
224         dev->driver = driver;
225
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");
232                         retcode = -EINVAL;
233                         goto error_out_unreg;
234                 }
235
236                 if (drm_core_has_MTRR(dev)) {
237                         if (dev->agp)
238                                 dev->agp->agp_mtrr =
239                                     mtrr_add(dev->agp->agp_info.aper_base,
240                                              dev->agp->agp_info.aper_size *
241                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
242                 }
243         }
244
245         retcode = drm_ctxbitmap_init(dev);
246         if (retcode) {
247                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
248                 goto error_out_unreg;
249         }
250
251         if (driver->driver_features & DRIVER_GEM) {
252                 retcode = drm_gem_init (dev);
253                 if (retcode) {
254                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
255                         goto error_out_unreg;
256                 }
257         }
258
259         drm_fence_manager_init(dev);
260
261         return 0;
262
263 error_out_unreg:
264         drm_lastclose(dev);
265         return retcode;
266 }
267
268 /**
269  * Get a secondary minor number.
270  *
271  * \param dev device data structure
272  * \param sec-minor structure to hold the assigned minor
273  * \return negative number on failure.
274  *
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
278  */
279 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
280 {
281         struct drm_minor *new_minor;
282         int ret;
283         int minor_id;
284
285         DRM_DEBUG("\n");
286
287         minor_id = drm_minor_get_id(dev, type);
288         if (minor_id < 0)
289                 return minor_id;
290
291         new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
292         if (!new_minor) {
293                 ret = -ENOMEM;
294                 goto err_idr;
295         }
296
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);
302
303         idr_replace(&drm_minors_idr, new_minor, minor_id);
304         
305         if (type == DRM_MINOR_LEGACY) {
306                 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
307                 if (ret) {
308                         DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
309                         goto err_mem;
310                 }
311                 if (dev->driver->proc_init) {
312                         ret = dev->driver->proc_init(new_minor);
313                         if (ret) {
314                                 DRM_ERROR("DRM: Driver failed to initialize /proc/dri.\n");
315                                 goto err_mem;
316                         }
317                 }
318         } else
319                 new_minor->dev_root = NULL;
320
321         ret = drm_sysfs_device_add(new_minor);
322         if (ret) {
323                 printk(KERN_ERR
324                        "DRM: Error sysfs_device_add.\n");
325                 goto err_g2;
326         }
327         *minor = new_minor;
328         
329         DRM_DEBUG("new minor assigned %d\n", minor_id);
330         return 0;
331
332
333 err_g2:
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);
338         }
339 err_mem:
340         kfree(new_minor);
341 err_idr:
342         idr_remove(&drm_minors_idr, minor_id);
343         *minor = NULL;
344         return ret;
345 }
346
347 /**
348  * Register.
349  *
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.
353  *
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.
357  */
358 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
359                 struct drm_driver *driver)
360 {
361         struct drm_device *dev;
362         int ret;
363
364         DRM_DEBUG("\n");
365
366         dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
367         if (!dev)
368                 return -ENOMEM;
369
370         if (!drm_fb_loaded) {
371                 pci_set_drvdata(pdev, dev);
372                 ret = pci_request_regions(pdev, driver->pci_driver.name);
373                 if (ret)
374                         goto err_g1;
375         }
376
377         ret = pci_enable_device(pdev);
378         if (ret)
379                 goto err_g2;
380         pci_set_master(pdev);
381
382         if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
383                 printk(KERN_ERR "DRM: fill_in_dev failed\n");
384                 goto err_g3;
385         }
386
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)))
390                         goto err_g3;
391
392         if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
393                 goto err_g4;
394
395         if (dev->driver->load)
396                 if ((ret = dev->driver->load(dev, ent->driver_data)))
397                         goto err_g5;
398
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))
402                     goto err_g5;
403
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);
407
408         return 0;
409 err_g5:
410         drm_put_minor(dev, &dev->primary);
411 err_g4:
412         if (drm_core_check_feature(dev, DRIVER_MODESET))
413                 drm_put_minor(dev, &dev->control);
414 err_g3:
415         if (!drm_fb_loaded)
416                 pci_disable_device(pdev);
417 err_g2:
418         if (!drm_fb_loaded)
419                 pci_release_regions(pdev);
420 err_g1:
421         if (!drm_fb_loaded)
422                 pci_set_drvdata(pdev, NULL);
423
424         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
425         printk(KERN_ERR "DRM: drm_get_dev failed.\n");
426         return ret;
427 }
428 EXPORT_SYMBOL(drm_get_dev);
429
430
431 /**
432  * Put a device minor number.
433  *
434  * \param dev device data structure
435  * \return always zero
436  *
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.
440  */
441 int drm_put_dev(struct drm_device * dev)
442 {
443         DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
444
445         if (dev->devname) {
446                 drm_free(dev->devname, strlen(dev->devname) + 1,
447                          DRM_MEM_DRIVER);
448                 dev->devname = NULL;
449         }
450         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
451         return 0;
452 }
453
454 /**
455  * Put a secondary minor number.
456  *
457  * \param sec_minor - structure to be released
458  * \return always zero
459  *
460  * Cleans up the proc resources. Not legal for this to be the
461  * last minor released.
462  *
463  */
464 int drm_put_minor(struct drm_device *dev, struct drm_minor **minor_p)
465 {
466         struct drm_minor *minor = *minor_p;
467         DRM_DEBUG("release secondary minor %d\n", minor->index);
468
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);
473         }
474         drm_sysfs_device_remove(minor);
475
476         idr_remove(&drm_minors_idr, minor->index);
477
478         kfree(minor);
479         *minor_p = NULL;
480         return 0;
481 }