OSDN Git Service

drm_sysfs: update sysfs code from kernel
[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_cards_limit = 16;      /* Enough for one machine */
41 unsigned int drm_debug = 0;             /* 1 to enable debug output */
42 EXPORT_SYMBOL(drm_debug);
43
44 MODULE_AUTHOR(CORE_AUTHOR);
45 MODULE_DESCRIPTION(CORE_DESC);
46 MODULE_LICENSE("GPL and additional rights");
47 MODULE_PARM_DESC(cards_limit, "Maximum number of graphics cards");
48 MODULE_PARM_DESC(debug, "Enable debug output");
49
50 module_param_named(cards_limit, drm_cards_limit, int, 0444);
51 module_param_named(debug, drm_debug, int, 0600);
52
53 struct drm_head **drm_heads;
54 struct class *drm_class;
55 struct proc_dir_entry *drm_proc_root;
56
57 static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
58                        const struct pci_device_id *ent,
59                        struct drm_driver *driver)
60 {
61         int retcode;
62
63         INIT_LIST_HEAD(&dev->filelist);
64         INIT_LIST_HEAD(&dev->ctxlist);
65         INIT_LIST_HEAD(&dev->vmalist);
66         INIT_LIST_HEAD(&dev->maplist);
67
68         spin_lock_init(&dev->count_lock);
69         spin_lock_init(&dev->drw_lock);
70         spin_lock_init(&dev->tasklet_lock);
71         spin_lock_init(&dev->lock.spinlock);
72         init_timer(&dev->timer);
73         mutex_init(&dev->struct_mutex);
74         mutex_init(&dev->ctxlist_mutex);
75         mutex_init(&dev->bm.init_mutex);
76         mutex_init(&dev->bm.evict_mutex);
77
78         idr_init(&dev->drw_idr);
79         
80         dev->pdev = pdev;
81         dev->pci_device = pdev->device;
82         dev->pci_vendor = pdev->vendor;
83
84 #ifdef __alpha__
85         dev->hose = pdev->sysdata;
86 #endif
87         dev->irq = pdev->irq;
88
89         if (drm_ht_create(&dev->map_hash, DRM_MAP_HASH_ORDER)) {
90                 return -ENOMEM;
91         }
92         if (drm_mm_init(&dev->offset_manager, DRM_FILE_PAGE_OFFSET_START,
93                         DRM_FILE_PAGE_OFFSET_SIZE)) {
94                 drm_ht_remove(&dev->map_hash);
95                 return -ENOMEM;
96         }
97
98         if (drm_ht_create(&dev->object_hash, DRM_OBJECT_HASH_ORDER)) {
99                 drm_ht_remove(&dev->map_hash);
100                 drm_mm_takedown(&dev->offset_manager);
101                 return -ENOMEM;
102         }
103
104         /* the DRM has 6 counters */
105         dev->counters = 6;
106         dev->types[0] = _DRM_STAT_LOCK;
107         dev->types[1] = _DRM_STAT_OPENS;
108         dev->types[2] = _DRM_STAT_CLOSES;
109         dev->types[3] = _DRM_STAT_IOCTLS;
110         dev->types[4] = _DRM_STAT_LOCKS;
111         dev->types[5] = _DRM_STAT_UNLOCKS;
112
113         dev->driver = driver;
114
115         if (dev->driver->load)
116                 if ((retcode = dev->driver->load(dev, ent->driver_data)))
117                         goto error_out_unreg;
118
119         if (drm_core_has_AGP(dev)) {
120                 if (drm_device_is_agp(dev))
121                         dev->agp = drm_agp_init(dev);
122                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
123                     && (dev->agp == NULL)) {
124                         DRM_ERROR("Cannot initialize the agpgart module.\n");
125                         retcode = -EINVAL;
126                         goto error_out_unreg;
127                 }
128
129                 if (drm_core_has_MTRR(dev)) {
130                         if (dev->agp)
131                                 dev->agp->agp_mtrr =
132                                     mtrr_add(dev->agp->agp_info.aper_base,
133                                              dev->agp->agp_info.aper_size *
134                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
135                 }
136         }
137
138         retcode = drm_ctxbitmap_init(dev);
139         if (retcode) {
140                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
141                 goto error_out_unreg;
142         }
143
144         drm_fence_manager_init(dev);
145         return 0;
146
147 error_out_unreg:
148         drm_lastclose(dev);
149         return retcode;
150 }
151
152 /**
153  * Get a secondary minor number.
154  *
155  * \param dev device data structure
156  * \param sec-minor structure to hold the assigned minor
157  * \return negative number on failure.
158  *
159  * Search an empty entry and initialize it to the given parameters, and
160  * create the proc init entry via proc_init(). This routines assigns
161  * minor numbers to secondary heads of multi-headed cards
162  */
163 static int drm_get_head(struct drm_device * dev, struct drm_head * head)
164 {
165         struct drm_head **heads = drm_heads;
166         int ret;
167         int minor;
168
169         DRM_DEBUG("\n");
170
171         for (minor = 0; minor < drm_cards_limit; minor++, heads++) {
172                 if (!*heads) {
173
174                         *head = (struct drm_head) {
175                                 .dev = dev,
176                                 .device = MKDEV(DRM_MAJOR, minor),
177                                 .minor = minor,
178                         };
179                         if ((ret =
180                              drm_proc_init(dev, minor, drm_proc_root,
181                                            &head->dev_root))) {
182                                 printk(KERN_ERR
183                                        "DRM: Failed to initialize /proc/dri.\n");
184                                 goto err_g1;
185                         }
186
187                         head->dev_class = drm_sysfs_device_add(drm_class, head);
188                         if (IS_ERR(head->dev_class)) {
189                                 printk(KERN_ERR
190                                        "DRM: Error sysfs_device_add.\n");
191                                 ret = PTR_ERR(head->dev_class);
192                                 goto err_g2;
193                         }
194                         *heads = head;
195
196                         DRM_DEBUG("new minor assigned %d\n", minor);
197                         return 0;
198                 }
199         }
200         DRM_ERROR("out of minors\n");
201         return -ENOMEM;
202 err_g2:
203         drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
204 err_g1:
205         *head = (struct drm_head) {
206                 .dev = NULL};
207         return ret;
208 }
209
210 /**
211  * Register.
212  *
213  * \param pdev - PCI device structure
214  * \param ent entry from the PCI ID table with device type flags
215  * \return zero on success or a negative number on failure.
216  *
217  * Attempt to gets inter module "drm" information. If we are first
218  * then register the character device and inter module information.
219  * Try and register, if we fail to register, backout previous work.
220  */
221 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
222               struct drm_driver *driver)
223 {
224         struct drm_device *dev;
225         int ret;
226
227         DRM_DEBUG("\n");
228
229         dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
230         if (!dev)
231                 return -ENOMEM;
232
233         if (!drm_fb_loaded) {
234                 pci_set_drvdata(pdev, dev);
235                 ret = pci_request_regions(pdev, driver->pci_driver.name);
236                 if (ret)
237                         goto err_g1;
238         }
239
240         ret = pci_enable_device(pdev);
241         if (ret)
242                 goto err_g2;
243         pci_set_master(pdev);
244
245         if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
246                 printk(KERN_ERR "DRM: fill_in_dev failed\n");
247                 goto err_g3;
248         }
249         if ((ret = drm_get_head(dev, &dev->primary)))
250                 goto err_g3;
251
252         DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
253                  driver->name, driver->major, driver->minor, driver->patchlevel,
254                  driver->date, dev->primary.minor);
255
256         return 0;
257
258  err_g3:
259         if (!drm_fb_loaded)
260                 pci_disable_device(pdev);
261  err_g2:
262         if (!drm_fb_loaded)
263                 pci_release_regions(pdev);
264  err_g1:
265         if (!drm_fb_loaded)
266                 pci_set_drvdata(pdev, NULL);
267
268         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
269         printk(KERN_ERR "DRM: drm_get_dev failed.\n");
270         return ret;
271 }
272 EXPORT_SYMBOL(drm_get_dev);
273
274
275 /**
276  * Put a device minor number.
277  *
278  * \param dev device data structure
279  * \return always zero
280  *
281  * Cleans up the proc resources. If it is the last minor then release the foreign
282  * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
283  * unregisters the character device.
284  */
285 int drm_put_dev(struct drm_device * dev)
286 {
287         DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
288
289         if (dev->unique) {
290                 drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
291                 dev->unique = NULL;
292                 dev->unique_len = 0;
293         }
294         if (dev->devname) {
295                 drm_free(dev->devname, strlen(dev->devname) + 1,
296                          DRM_MEM_DRIVER);
297                 dev->devname = NULL;
298         }
299         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
300         return 0;
301 }
302
303 /**
304  * Put a secondary minor number.
305  *
306  * \param sec_minor - structure to be released
307  * \return always zero
308  *
309  * Cleans up the proc resources. Not legal for this to be the
310  * last minor released.
311  *
312  */
313 int drm_put_head(struct drm_head * head)
314 {
315         int minor = head->minor;
316
317         DRM_DEBUG("release secondary minor %d\n", minor);
318
319         drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
320         drm_sysfs_device_remove(head->dev_class);
321
322         *head = (struct drm_head){.dev = NULL};
323
324         drm_heads[minor] = NULL;
325         return 0;
326 }