Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/hch/hfsplus
[sfrench/cifs-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         kfree(dev->devname);
160         dev->devname = NULL;
161
162         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
163                 list_del(&pt->head);
164                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
165                 kfree(pt);
166         }
167
168         drm_ht_remove(&master->magiclist);
169
170         kfree(master);
171 }
172
173 void drm_master_put(struct drm_master **master)
174 {
175         kref_put(&(*master)->refcount, drm_master_destroy);
176         *master = NULL;
177 }
178 EXPORT_SYMBOL(drm_master_put);
179
180 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
181                         struct drm_file *file_priv)
182 {
183         int ret = 0;
184
185         if (file_priv->is_master)
186                 return 0;
187
188         if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
189                 return -EINVAL;
190
191         if (!file_priv->master)
192                 return -EINVAL;
193
194         if (!file_priv->minor->master &&
195             file_priv->minor->master != file_priv->master) {
196                 mutex_lock(&dev->struct_mutex);
197                 file_priv->minor->master = drm_master_get(file_priv->master);
198                 file_priv->is_master = 1;
199                 if (dev->driver->master_set) {
200                         ret = dev->driver->master_set(dev, file_priv, false);
201                         if (unlikely(ret != 0)) {
202                                 file_priv->is_master = 0;
203                                 drm_master_put(&file_priv->minor->master);
204                         }
205                 }
206                 mutex_unlock(&dev->struct_mutex);
207         }
208
209         return 0;
210 }
211
212 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
213                          struct drm_file *file_priv)
214 {
215         if (!file_priv->is_master)
216                 return -EINVAL;
217
218         if (!file_priv->minor->master)
219                 return -EINVAL;
220
221         mutex_lock(&dev->struct_mutex);
222         if (dev->driver->master_drop)
223                 dev->driver->master_drop(dev, file_priv, false);
224         drm_master_put(&file_priv->minor->master);
225         file_priv->is_master = 0;
226         mutex_unlock(&dev->struct_mutex);
227         return 0;
228 }
229
230 int drm_fill_in_dev(struct drm_device *dev,
231                            const struct pci_device_id *ent,
232                            struct drm_driver *driver)
233 {
234         int retcode;
235
236         INIT_LIST_HEAD(&dev->filelist);
237         INIT_LIST_HEAD(&dev->ctxlist);
238         INIT_LIST_HEAD(&dev->vmalist);
239         INIT_LIST_HEAD(&dev->maplist);
240         INIT_LIST_HEAD(&dev->vblank_event_list);
241
242         spin_lock_init(&dev->count_lock);
243         spin_lock_init(&dev->event_lock);
244         mutex_init(&dev->struct_mutex);
245         mutex_init(&dev->ctxlist_mutex);
246
247         if (drm_ht_create(&dev->map_hash, 12)) {
248                 return -ENOMEM;
249         }
250
251         /* the DRM has 6 basic counters */
252         dev->counters = 6;
253         dev->types[0] = _DRM_STAT_LOCK;
254         dev->types[1] = _DRM_STAT_OPENS;
255         dev->types[2] = _DRM_STAT_CLOSES;
256         dev->types[3] = _DRM_STAT_IOCTLS;
257         dev->types[4] = _DRM_STAT_LOCKS;
258         dev->types[5] = _DRM_STAT_UNLOCKS;
259
260         dev->driver = driver;
261
262         if (drm_core_has_AGP(dev)) {
263                 if (drm_device_is_agp(dev))
264                         dev->agp = drm_agp_init(dev);
265                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
266                     && (dev->agp == NULL)) {
267                         DRM_ERROR("Cannot initialize the agpgart module.\n");
268                         retcode = -EINVAL;
269                         goto error_out_unreg;
270                 }
271                 if (drm_core_has_MTRR(dev)) {
272                         if (dev->agp)
273                                 dev->agp->agp_mtrr =
274                                     mtrr_add(dev->agp->agp_info.aper_base,
275                                              dev->agp->agp_info.aper_size *
276                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
277                 }
278         }
279
280
281         retcode = drm_ctxbitmap_init(dev);
282         if (retcode) {
283                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
284                 goto error_out_unreg;
285         }
286
287         if (driver->driver_features & DRIVER_GEM) {
288                 retcode = drm_gem_init(dev);
289                 if (retcode) {
290                         DRM_ERROR("Cannot initialize graphics execution "
291                                   "manager (GEM)\n");
292                         goto error_out_unreg;
293                 }
294         }
295
296         return 0;
297
298       error_out_unreg:
299         drm_lastclose(dev);
300         return retcode;
301 }
302
303
304 /**
305  * Get a secondary minor number.
306  *
307  * \param dev device data structure
308  * \param sec-minor structure to hold the assigned minor
309  * \return negative number on failure.
310  *
311  * Search an empty entry and initialize it to the given parameters, and
312  * create the proc init entry via proc_init(). This routines assigns
313  * minor numbers to secondary heads of multi-headed cards
314  */
315 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
316 {
317         struct drm_minor *new_minor;
318         int ret;
319         int minor_id;
320
321         DRM_DEBUG("\n");
322
323         minor_id = drm_minor_get_id(dev, type);
324         if (minor_id < 0)
325                 return minor_id;
326
327         new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
328         if (!new_minor) {
329                 ret = -ENOMEM;
330                 goto err_idr;
331         }
332
333         new_minor->type = type;
334         new_minor->device = MKDEV(DRM_MAJOR, minor_id);
335         new_minor->dev = dev;
336         new_minor->index = minor_id;
337         INIT_LIST_HEAD(&new_minor->master_list);
338
339         idr_replace(&drm_minors_idr, new_minor, minor_id);
340
341         if (type == DRM_MINOR_LEGACY) {
342                 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
343                 if (ret) {
344                         DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
345                         goto err_mem;
346                 }
347         } else
348                 new_minor->proc_root = NULL;
349
350 #if defined(CONFIG_DEBUG_FS)
351         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
352         if (ret) {
353                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
354                 goto err_g2;
355         }
356 #endif
357
358         ret = drm_sysfs_device_add(new_minor);
359         if (ret) {
360                 printk(KERN_ERR
361                        "DRM: Error sysfs_device_add.\n");
362                 goto err_g2;
363         }
364         *minor = new_minor;
365
366         DRM_DEBUG("new minor assigned %d\n", minor_id);
367         return 0;
368
369
370 err_g2:
371         if (new_minor->type == DRM_MINOR_LEGACY)
372                 drm_proc_cleanup(new_minor, drm_proc_root);
373 err_mem:
374         kfree(new_minor);
375 err_idr:
376         idr_remove(&drm_minors_idr, minor_id);
377         *minor = NULL;
378         return ret;
379 }
380
381 /**
382  * Put a secondary minor number.
383  *
384  * \param sec_minor - structure to be released
385  * \return always zero
386  *
387  * Cleans up the proc resources. Not legal for this to be the
388  * last minor released.
389  *
390  */
391 int drm_put_minor(struct drm_minor **minor_p)
392 {
393         struct drm_minor *minor = *minor_p;
394
395         DRM_DEBUG("release secondary minor %d\n", minor->index);
396
397         if (minor->type == DRM_MINOR_LEGACY)
398                 drm_proc_cleanup(minor, drm_proc_root);
399 #if defined(CONFIG_DEBUG_FS)
400         drm_debugfs_cleanup(minor);
401 #endif
402
403         drm_sysfs_device_remove(minor);
404
405         idr_remove(&drm_minors_idr, minor->index);
406
407         kfree(minor);
408         *minor_p = NULL;
409         return 0;
410 }
411
412 /**
413  * Called via drm_exit() at module unload time or when pci device is
414  * unplugged.
415  *
416  * Cleans up all DRM device, calling drm_lastclose().
417  *
418  * \sa drm_init
419  */
420 void drm_put_dev(struct drm_device *dev)
421 {
422         struct drm_driver *driver;
423         struct drm_map_list *r_list, *list_temp;
424
425         DRM_DEBUG("\n");
426
427         if (!dev) {
428                 DRM_ERROR("cleanup called no dev\n");
429                 return;
430         }
431         driver = dev->driver;
432
433         drm_lastclose(dev);
434
435         if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
436             dev->agp && dev->agp->agp_mtrr >= 0) {
437                 int retval;
438                 retval = mtrr_del(dev->agp->agp_mtrr,
439                                   dev->agp->agp_info.aper_base,
440                                   dev->agp->agp_info.aper_size * 1024 * 1024);
441                 DRM_DEBUG("mtrr_del=%d\n", retval);
442         }
443
444         if (dev->driver->unload)
445                 dev->driver->unload(dev);
446
447         if (drm_core_has_AGP(dev) && dev->agp) {
448                 kfree(dev->agp);
449                 dev->agp = NULL;
450         }
451
452         drm_vblank_cleanup(dev);
453
454         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
455                 drm_rmmap(dev, r_list->map);
456         drm_ht_remove(&dev->map_hash);
457
458         drm_ctxbitmap_cleanup(dev);
459
460         if (drm_core_check_feature(dev, DRIVER_MODESET))
461                 drm_put_minor(&dev->control);
462
463         if (driver->driver_features & DRIVER_GEM)
464                 drm_gem_destroy(dev);
465
466         drm_put_minor(&dev->primary);
467
468         if (dev->devname) {
469                 kfree(dev->devname);
470                 dev->devname = NULL;
471         }
472         kfree(dev);
473 }
474 EXPORT_SYMBOL(drm_put_dev);