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