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