drm: Add support for platform devices to register as DRM devices
[linux-2.6.git] / drivers / gpu / drm / drm_stub.c
1 /**
2  * \file drm_stub.h
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 #include <linux/slab.h>
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 struct dentry *drm_debugfs_root;
55 void drm_ut_debug_printk(unsigned int request_level,
56                          const char *prefix,
57                          const char *function_name,
58                          const char *format, ...)
59 {
60         va_list args;
61
62         if (drm_debug & request_level) {
63                 if (function_name)
64                         printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
65                 va_start(args, format);
66                 vprintk(format, args);
67                 va_end(args);
68         }
69 }
70 EXPORT_SYMBOL(drm_ut_debug_printk);
71 static int drm_minor_get_id(struct drm_device *dev, int type)
72 {
73         int new_id;
74         int ret;
75         int base = 0, limit = 63;
76
77         if (type == DRM_MINOR_CONTROL) {
78                 base += 64;
79                 limit = base + 127;
80         } else if (type == DRM_MINOR_RENDER) {
81                 base += 128;
82                 limit = base + 255;
83         }
84
85 again:
86         if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
87                 DRM_ERROR("Out of memory expanding drawable idr\n");
88                 return -ENOMEM;
89         }
90         mutex_lock(&dev->struct_mutex);
91         ret = idr_get_new_above(&drm_minors_idr, NULL,
92                                 base, &new_id);
93         mutex_unlock(&dev->struct_mutex);
94         if (ret == -EAGAIN) {
95                 goto again;
96         } else if (ret) {
97                 return ret;
98         }
99
100         if (new_id >= limit) {
101                 idr_remove(&drm_minors_idr, new_id);
102                 return -EINVAL;
103         }
104         return new_id;
105 }
106
107 struct drm_master *drm_master_create(struct drm_minor *minor)
108 {
109         struct drm_master *master;
110
111         master = kzalloc(sizeof(*master), GFP_KERNEL);
112         if (!master)
113                 return NULL;
114
115         kref_init(&master->refcount);
116         spin_lock_init(&master->lock.spinlock);
117         init_waitqueue_head(&master->lock.lock_queue);
118         drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
119         INIT_LIST_HEAD(&master->magicfree);
120         master->minor = minor;
121
122         list_add_tail(&master->head, &minor->master_list);
123
124         return master;
125 }
126
127 struct drm_master *drm_master_get(struct drm_master *master)
128 {
129         kref_get(&master->refcount);
130         return master;
131 }
132 EXPORT_SYMBOL(drm_master_get);
133
134 static void drm_master_destroy(struct kref *kref)
135 {
136         struct drm_master *master = container_of(kref, struct drm_master, refcount);
137         struct drm_magic_entry *pt, *next;
138         struct drm_device *dev = master->minor->dev;
139         struct drm_map_list *r_list, *list_temp;
140
141         list_del(&master->head);
142
143         if (dev->driver->master_destroy)
144                 dev->driver->master_destroy(dev, master);
145
146         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
147                 if (r_list->master == master) {
148                         drm_rmmap_locked(dev, r_list->map);
149                         r_list = NULL;
150                 }
151         }
152
153         if (master->unique) {
154                 kfree(master->unique);
155                 master->unique = NULL;
156                 master->unique_len = 0;
157         }
158
159         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
160                 list_del(&pt->head);
161                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
162                 kfree(pt);
163         }
164
165         drm_ht_remove(&master->magiclist);
166
167         kfree(master);
168 }
169
170 void drm_master_put(struct drm_master **master)
171 {
172         kref_put(&(*master)->refcount, drm_master_destroy);
173         *master = NULL;
174 }
175 EXPORT_SYMBOL(drm_master_put);
176
177 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
178                         struct drm_file *file_priv)
179 {
180         int ret = 0;
181
182         if (file_priv->is_master)
183                 return 0;
184
185         if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
186                 return -EINVAL;
187
188         if (!file_priv->master)
189                 return -EINVAL;
190
191         if (!file_priv->minor->master &&
192             file_priv->minor->master != file_priv->master) {
193                 mutex_lock(&dev->struct_mutex);
194                 file_priv->minor->master = drm_master_get(file_priv->master);
195                 file_priv->is_master = 1;
196                 if (dev->driver->master_set) {
197                         ret = dev->driver->master_set(dev, file_priv, false);
198                         if (unlikely(ret != 0)) {
199                                 file_priv->is_master = 0;
200                                 drm_master_put(&file_priv->minor->master);
201                         }
202                 }
203                 mutex_unlock(&dev->struct_mutex);
204         }
205
206         return 0;
207 }
208
209 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
210                          struct drm_file *file_priv)
211 {
212         if (!file_priv->is_master)
213                 return -EINVAL;
214
215         if (!file_priv->minor->master)
216                 return -EINVAL;
217
218         mutex_lock(&dev->struct_mutex);
219         if (dev->driver->master_drop)
220                 dev->driver->master_drop(dev, file_priv, false);
221         drm_master_put(&file_priv->minor->master);
222         file_priv->is_master = 0;
223         mutex_unlock(&dev->struct_mutex);
224         return 0;
225 }
226
227 int drm_fill_in_dev(struct drm_device *dev,
228                            const struct pci_device_id *ent,
229                            struct drm_driver *driver)
230 {
231         int retcode;
232
233         INIT_LIST_HEAD(&dev->filelist);
234         INIT_LIST_HEAD(&dev->ctxlist);
235         INIT_LIST_HEAD(&dev->vmalist);
236         INIT_LIST_HEAD(&dev->maplist);
237         INIT_LIST_HEAD(&dev->vblank_event_list);
238
239         spin_lock_init(&dev->count_lock);
240         spin_lock_init(&dev->drw_lock);
241         spin_lock_init(&dev->event_lock);
242         init_timer(&dev->timer);
243         mutex_init(&dev->struct_mutex);
244         mutex_init(&dev->ctxlist_mutex);
245
246         idr_init(&dev->drw_idr);
247
248         if (drm_ht_create(&dev->map_hash, 12)) {
249                 return -ENOMEM;
250         }
251
252         /* the DRM has 6 basic counters */
253         dev->counters = 6;
254         dev->types[0] = _DRM_STAT_LOCK;
255         dev->types[1] = _DRM_STAT_OPENS;
256         dev->types[2] = _DRM_STAT_CLOSES;
257         dev->types[3] = _DRM_STAT_IOCTLS;
258         dev->types[4] = _DRM_STAT_LOCKS;
259         dev->types[5] = _DRM_STAT_UNLOCKS;
260
261         dev->driver = driver;
262
263         if (drm_core_has_AGP(dev)) {
264                 if (drm_device_is_agp(dev))
265                         dev->agp = drm_agp_init(dev);
266                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
267                     && (dev->agp == NULL)) {
268                         DRM_ERROR("Cannot initialize the agpgart module.\n");
269                         retcode = -EINVAL;
270                         goto error_out_unreg;
271                 }
272                 if (drm_core_has_MTRR(dev)) {
273                         if (dev->agp)
274                                 dev->agp->agp_mtrr =
275                                     mtrr_add(dev->agp->agp_info.aper_base,
276                                              dev->agp->agp_info.aper_size *
277                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
278                 }
279         }
280
281
282         retcode = drm_ctxbitmap_init(dev);
283         if (retcode) {
284                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
285                 goto error_out_unreg;
286         }
287
288         if (driver->driver_features & DRIVER_GEM) {
289                 retcode = drm_gem_init(dev);
290                 if (retcode) {
291                         DRM_ERROR("Cannot initialize graphics execution "
292                                   "manager (GEM)\n");
293                         goto error_out_unreg;
294                 }
295         }
296
297         return 0;
298
299       error_out_unreg:
300         drm_lastclose(dev);
301         return retcode;
302 }
303
304
305 /**
306  * Get a secondary minor number.
307  *
308  * \param dev device data structure
309  * \param sec-minor structure to hold the assigned minor
310  * \return negative number on failure.
311  *
312  * Search an empty entry and initialize it to the given parameters, and
313  * create the proc init entry via proc_init(). This routines assigns
314  * minor numbers to secondary heads of multi-headed cards
315  */
316 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
317 {
318         struct drm_minor *new_minor;
319         int ret;
320         int minor_id;
321
322         DRM_DEBUG("\n");
323
324         minor_id = drm_minor_get_id(dev, type);
325         if (minor_id < 0)
326                 return minor_id;
327
328         new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
329         if (!new_minor) {
330                 ret = -ENOMEM;
331                 goto err_idr;
332         }
333
334         new_minor->type = type;
335         new_minor->device = MKDEV(DRM_MAJOR, minor_id);
336         new_minor->dev = dev;
337         new_minor->index = minor_id;
338         INIT_LIST_HEAD(&new_minor->master_list);
339
340         idr_replace(&drm_minors_idr, new_minor, minor_id);
341
342         if (type == DRM_MINOR_LEGACY) {
343                 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
344                 if (ret) {
345                         DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
346                         goto err_mem;
347                 }
348         } else
349                 new_minor->proc_root = NULL;
350
351 #if defined(CONFIG_DEBUG_FS)
352         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
353         if (ret) {
354                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
355                 goto err_g2;
356         }
357 #endif
358
359         ret = drm_sysfs_device_add(new_minor);
360         if (ret) {
361                 printk(KERN_ERR
362                        "DRM: Error sysfs_device_add.\n");
363                 goto err_g2;
364         }
365         *minor = new_minor;
366
367         DRM_DEBUG("new minor assigned %d\n", minor_id);
368         return 0;
369
370
371 err_g2:
372         if (new_minor->type == DRM_MINOR_LEGACY)
373                 drm_proc_cleanup(new_minor, drm_proc_root);
374 err_mem:
375         kfree(new_minor);
376 err_idr:
377         idr_remove(&drm_minors_idr, minor_id);
378         *minor = NULL;
379         return ret;
380 }
381
382 /**
383  * Put a secondary minor number.
384  *
385  * \param sec_minor - structure to be released
386  * \return always zero
387  *
388  * Cleans up the proc resources. Not legal for this to be the
389  * last minor released.
390  *
391  */
392 int drm_put_minor(struct drm_minor **minor_p)
393 {
394         struct drm_minor *minor = *minor_p;
395
396         DRM_DEBUG("release secondary minor %d\n", minor->index);
397
398         if (minor->type == DRM_MINOR_LEGACY)
399                 drm_proc_cleanup(minor, drm_proc_root);
400 #if defined(CONFIG_DEBUG_FS)
401         drm_debugfs_cleanup(minor);
402 #endif
403
404         drm_sysfs_device_remove(minor);
405
406         idr_remove(&drm_minors_idr, minor->index);
407
408         kfree(minor);
409         *minor_p = NULL;
410         return 0;
411 }
412
413 /**
414  * Called via drm_exit() at module unload time or when pci device is
415  * unplugged.
416  *
417  * Cleans up all DRM device, calling drm_lastclose().
418  *
419  * \sa drm_init
420  */
421 void drm_put_dev(struct drm_device *dev)
422 {
423         struct drm_driver *driver;
424         struct drm_map_list *r_list, *list_temp;
425
426         DRM_DEBUG("\n");
427
428         if (!dev) {
429                 DRM_ERROR("cleanup called no dev\n");
430                 return;
431         }
432         driver = dev->driver;
433
434         drm_lastclose(dev);
435
436         if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
437             dev->agp && dev->agp->agp_mtrr >= 0) {
438                 int retval;
439                 retval = mtrr_del(dev->agp->agp_mtrr,
440                                   dev->agp->agp_info.aper_base,
441                                   dev->agp->agp_info.aper_size * 1024 * 1024);
442                 DRM_DEBUG("mtrr_del=%d\n", retval);
443         }
444
445         if (dev->driver->unload)
446                 dev->driver->unload(dev);
447
448         if (drm_core_has_AGP(dev) && dev->agp) {
449                 kfree(dev->agp);
450                 dev->agp = NULL;
451         }
452
453         drm_vblank_cleanup(dev);
454
455         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
456                 drm_rmmap(dev, r_list->map);
457         drm_ht_remove(&dev->map_hash);
458
459         drm_ctxbitmap_cleanup(dev);
460
461         if (drm_core_check_feature(dev, DRIVER_MODESET))
462                 drm_put_minor(&dev->control);
463
464         if (driver->driver_features & DRIVER_GEM)
465                 drm_gem_destroy(dev);
466
467         drm_put_minor(&dev->primary);
468
469         if (dev->devname) {
470                 kfree(dev->devname);
471                 dev->devname = NULL;
472         }
473         kfree(dev);
474 }
475 EXPORT_SYMBOL(drm_put_dev);