drm cleanup 18/x: Gently remove drm_core_has_AGP, DRIVER_REQUIRE_AGP
[dragonfly.git] / sys / dev / drm / drm_drv.c
1 /*
2  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3  *
4  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Author Rickard E. (Rik) Faith <faith@valinux.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <drm/drmP.h>
32 #include <drm/drm_core.h>
33 #include "drm_legacy.h"
34 #include "drm_internal.h"
35
36 /* Provides three levels of debug: off, minimal, verbose */
37 #ifdef __DragonFly__
38 #if DRM_DEBUG_DEFAULT_ON == 1
39 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS |    \
40                           DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL)
41 #elif DRM_DEBUG_DEFAULT_ON == 2
42 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS |    \
43                           DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \
44                           DRM_UT_PID  | DRM_UT_IOCTL  | DRM_UT_VBLANK)
45 #else
46 #define DRM_DEBUGBITS_ON (0x0)
47 #endif
48 unsigned int drm_debug = DRM_DEBUGBITS_ON;      /* defaults to 0 */
49 #else
50 unsigned int drm_debug = 0;     /* 1 to enable debug output */
51 #endif /* __DragonFly__ */
52 EXPORT_SYMBOL(drm_debug);
53
54 MODULE_AUTHOR(CORE_AUTHOR);
55 MODULE_DESCRIPTION(CORE_DESC);
56 MODULE_PARM_DESC(debug, "Enable debug output");
57 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
58 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
59 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
60
61 module_param_named(debug, drm_debug, int, 0600);
62
63 #if 0
64 static DEFINE_SPINLOCK(drm_minor_lock);
65 static struct idr drm_minors_idr;
66 #endif
67
68 struct class *drm_class;
69 #if 0
70 static struct dentry *drm_debugfs_root;
71 #endif
72
73 void drm_err(const char *func, const char *format, ...)
74 {
75         __va_list args;
76
77         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func);
78
79         __va_start(args, format);
80         kvprintf(format, args);
81         __va_end(args);
82 }
83 EXPORT_SYMBOL(drm_err);
84
85 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
86 {
87         __va_list args;
88
89         if (unlikely(drm_debug & DRM_UT_PID)) {
90                 kprintf("[" DRM_NAME ":pid%d:%s] ",
91                     DRM_CURRENTPID, function_name);
92         } else {
93                 kprintf("[" DRM_NAME ":%s] ", function_name);
94         }
95
96         __va_start(args, format);
97         kvprintf(format, args);
98         __va_end(args);
99 }
100 EXPORT_SYMBOL(drm_ut_debug_printk);
101
102 #if 0
103 struct drm_master *drm_master_create(struct drm_minor *minor)
104 {
105         struct drm_master *master;
106
107         master = kzalloc(sizeof(*master), GFP_KERNEL);
108         if (!master)
109                 return NULL;
110
111         kref_init(&master->refcount);
112         spin_lock_init(&master->lock.spinlock);
113         init_waitqueue_head(&master->lock.lock_queue);
114         if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
115                 kfree(master);
116                 return NULL;
117         }
118         master->minor = minor;
119
120         return master;
121 }
122
123 struct drm_master *drm_master_get(struct drm_master *master)
124 {
125         kref_get(&master->refcount);
126         return master;
127 }
128 EXPORT_SYMBOL(drm_master_get);
129
130 static void drm_master_destroy(struct kref *kref)
131 {
132         struct drm_master *master = container_of(kref, struct drm_master, refcount);
133         struct drm_device *dev = master->minor->dev;
134         struct drm_map_list *r_list, *list_temp;
135
136         mutex_lock(&dev->struct_mutex);
137         if (dev->driver->master_destroy)
138                 dev->driver->master_destroy(dev, master);
139
140         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
141                 if (r_list->master == master) {
142                         drm_legacy_rmmap_locked(dev, r_list->map);
143                         r_list = NULL;
144                 }
145         }
146
147         if (master->unique) {
148                 kfree(master->unique);
149                 master->unique = NULL;
150                 master->unique_len = 0;
151         }
152
153         drm_ht_remove(&master->magiclist);
154
155         mutex_unlock(&dev->struct_mutex);
156         kfree(master);
157 }
158
159 void drm_master_put(struct drm_master **master)
160 {
161         kref_put(&(*master)->refcount, drm_master_destroy);
162         *master = NULL;
163 }
164 EXPORT_SYMBOL(drm_master_put);
165 #endif
166
167 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
168                         struct drm_file *file_priv)
169 {
170         DRM_DEBUG("setmaster\n");
171
172         if (file_priv->master != 0)
173                 return (0);
174
175         return (-EPERM);
176 }
177
178 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
179                          struct drm_file *file_priv)
180 {
181         DRM_DEBUG("dropmaster\n");
182         if (file_priv->master != 0)
183                 return -EINVAL;
184         return 0;
185 }
186
187 #if 0
188 /*
189  * DRM Minors
190  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
191  * of them is represented by a drm_minor object. Depending on the capabilities
192  * of the device-driver, different interfaces are registered.
193  *
194  * Minors can be accessed via dev->$minor_name. This pointer is either
195  * NULL or a valid drm_minor pointer and stays valid as long as the device is
196  * valid. This means, DRM minors have the same life-time as the underlying
197  * device. However, this doesn't mean that the minor is active. Minors are
198  * registered and unregistered dynamically according to device-state.
199  */
200
201 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
202                                              unsigned int type)
203 {
204         switch (type) {
205         case DRM_MINOR_LEGACY:
206                 return &dev->primary;
207         case DRM_MINOR_RENDER:
208                 return &dev->render;
209         case DRM_MINOR_CONTROL:
210                 return &dev->control;
211         default:
212                 return NULL;
213         }
214 }
215
216 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
217 {
218         struct drm_minor *minor;
219         unsigned long flags;
220         int r;
221
222         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
223         if (!minor)
224                 return -ENOMEM;
225
226         minor->type = type;
227         minor->dev = dev;
228
229         idr_preload(GFP_KERNEL);
230         spin_lock_irqsave(&drm_minor_lock, flags);
231         r = idr_alloc(&drm_minors_idr,
232                       NULL,
233                       64 * type,
234                       64 * (type + 1),
235                       GFP_NOWAIT);
236         spin_unlock_irqrestore(&drm_minor_lock, flags);
237         idr_preload_end();
238
239         if (r < 0)
240                 goto err_free;
241
242         minor->index = r;
243
244         minor->kdev = drm_sysfs_minor_alloc(minor);
245         if (IS_ERR(minor->kdev)) {
246                 r = PTR_ERR(minor->kdev);
247                 goto err_index;
248         }
249
250         *drm_minor_get_slot(dev, type) = minor;
251         return 0;
252
253 err_index:
254         spin_lock_irqsave(&drm_minor_lock, flags);
255         idr_remove(&drm_minors_idr, minor->index);
256         spin_unlock_irqrestore(&drm_minor_lock, flags);
257 err_free:
258         kfree(minor);
259         return r;
260 }
261
262 static void drm_minor_free(struct drm_device *dev, unsigned int type)
263 {
264         struct drm_minor **slot, *minor;
265         unsigned long flags;
266
267         slot = drm_minor_get_slot(dev, type);
268         minor = *slot;
269         if (!minor)
270                 return;
271
272         put_device(minor->kdev);
273
274         spin_lock_irqsave(&drm_minor_lock, flags);
275         idr_remove(&drm_minors_idr, minor->index);
276         spin_unlock_irqrestore(&drm_minor_lock, flags);
277
278         kfree(minor);
279         *slot = NULL;
280 }
281
282 static int drm_minor_register(struct drm_device *dev, unsigned int type)
283 {
284         struct drm_minor *minor;
285         unsigned long flags;
286         int ret;
287
288         DRM_DEBUG("\n");
289
290         minor = *drm_minor_get_slot(dev, type);
291         if (!minor)
292                 return 0;
293
294         ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
295         if (ret) {
296                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
297                 return ret;
298         }
299
300         ret = device_add(minor->kdev);
301         if (ret)
302                 goto err_debugfs;
303
304         /* replace NULL with @minor so lookups will succeed from now on */
305         spin_lock_irqsave(&drm_minor_lock, flags);
306         idr_replace(&drm_minors_idr, minor, minor->index);
307         spin_unlock_irqrestore(&drm_minor_lock, flags);
308
309         DRM_DEBUG("new minor registered %d\n", minor->index);
310         return 0;
311
312 err_debugfs:
313         drm_debugfs_cleanup(minor);
314         return ret;
315 }
316
317 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
318 {
319         struct drm_minor *minor;
320         unsigned long flags;
321
322         minor = *drm_minor_get_slot(dev, type);
323         if (!minor || !device_is_registered(minor->kdev))
324                 return;
325
326         /* replace @minor with NULL so lookups will fail from now on */
327         spin_lock_irqsave(&drm_minor_lock, flags);
328         idr_replace(&drm_minors_idr, NULL, minor->index);
329         spin_unlock_irqrestore(&drm_minor_lock, flags);
330
331         device_del(minor->kdev);
332         dev_set_drvdata(minor->kdev, NULL); /* safety belt */
333         drm_debugfs_cleanup(minor);
334 }
335
336 /**
337  * drm_minor_acquire - Acquire a DRM minor
338  * @minor_id: Minor ID of the DRM-minor
339  *
340  * Looks up the given minor-ID and returns the respective DRM-minor object. The
341  * refence-count of the underlying device is increased so you must release this
342  * object with drm_minor_release().
343  *
344  * As long as you hold this minor, it is guaranteed that the object and the
345  * minor->dev pointer will stay valid! However, the device may get unplugged and
346  * unregistered while you hold the minor.
347  *
348  * Returns:
349  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
350  * failure.
351  */
352 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
353 {
354         struct drm_minor *minor;
355         unsigned long flags;
356
357         spin_lock_irqsave(&drm_minor_lock, flags);
358         minor = idr_find(&drm_minors_idr, minor_id);
359         if (minor)
360                 drm_dev_ref(minor->dev);
361         spin_unlock_irqrestore(&drm_minor_lock, flags);
362
363         if (!minor) {
364                 return ERR_PTR(-ENODEV);
365         } else if (drm_device_is_unplugged(minor->dev)) {
366                 drm_dev_unref(minor->dev);
367                 return ERR_PTR(-ENODEV);
368         }
369
370         return minor;
371 }
372
373 /**
374  * drm_minor_release - Release DRM minor
375  * @minor: Pointer to DRM minor object
376  *
377  * Release a minor that was previously acquired via drm_minor_acquire().
378  */
379 void drm_minor_release(struct drm_minor *minor)
380 {
381         drm_dev_unref(minor->dev);
382 }
383
384 /**
385  * drm_put_dev - Unregister and release a DRM device
386  * @dev: DRM device
387  *
388  * Called at module unload time or when a PCI device is unplugged.
389  *
390  * Use of this function is discouraged. It will eventually go away completely.
391  * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
392  *
393  * Cleans up all DRM device, calling drm_lastclose().
394  */
395 void drm_put_dev(struct drm_device *dev)
396 {
397         DRM_DEBUG("\n");
398
399         if (!dev) {
400                 DRM_ERROR("cleanup called no dev\n");
401                 return;
402         }
403
404         drm_dev_unregister(dev);
405         drm_dev_unref(dev);
406 }
407 EXPORT_SYMBOL(drm_put_dev);
408
409 void drm_unplug_dev(struct drm_device *dev)
410 {
411         /* for a USB device */
412         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
413         drm_minor_unregister(dev, DRM_MINOR_RENDER);
414         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
415
416         mutex_lock(&drm_global_mutex);
417
418         drm_device_set_unplugged(dev);
419
420         if (dev->open_count == 0) {
421                 drm_put_dev(dev);
422         }
423         mutex_unlock(&drm_global_mutex);
424 }
425 EXPORT_SYMBOL(drm_unplug_dev);
426
427 /*
428  * DRM internal mount
429  * We want to be able to allocate our own "struct address_space" to control
430  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
431  * stand-alone address_space objects, so we need an underlying inode. As there
432  * is no way to allocate an independent inode easily, we need a fake internal
433  * VFS mount-point.
434  *
435  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
436  * frees it again. You are allowed to use iget() and iput() to get references to
437  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
438  * drm_fs_inode_free() call (which does not have to be the last iput()).
439  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
440  * between multiple inode-users. You could, technically, call
441  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
442  * iput(), but this way you'd end up with a new vfsmount for each inode.
443  */
444
445 static int drm_fs_cnt;
446 static struct vfsmount *drm_fs_mnt;
447
448 static const struct dentry_operations drm_fs_dops = {
449         .d_dname        = simple_dname,
450 };
451
452 static const struct super_operations drm_fs_sops = {
453         .statfs         = simple_statfs,
454 };
455
456 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
457                                    const char *dev_name, void *data)
458 {
459         return mount_pseudo(fs_type,
460                             "drm:",
461                             &drm_fs_sops,
462                             &drm_fs_dops,
463                             0x010203ff);
464 }
465
466 static struct file_system_type drm_fs_type = {
467         .name           = "drm",
468         .owner          = THIS_MODULE,
469         .mount          = drm_fs_mount,
470         .kill_sb        = kill_anon_super,
471 };
472
473 static struct inode *drm_fs_inode_new(void)
474 {
475         struct inode *inode;
476         int r;
477
478         r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
479         if (r < 0) {
480                 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
481                 return ERR_PTR(r);
482         }
483
484         inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
485         if (IS_ERR(inode))
486                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
487
488         return inode;
489 }
490
491 static void drm_fs_inode_free(struct inode *inode)
492 {
493         if (inode) {
494                 iput(inode);
495                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
496         }
497 }
498
499 /**
500  * drm_dev_alloc - Allocate new DRM device
501  * @driver: DRM driver to allocate device for
502  * @parent: Parent device object
503  *
504  * Allocate and initialize a new DRM device. No device registration is done.
505  * Call drm_dev_register() to advertice the device to user space and register it
506  * with other core subsystems.
507  *
508  * The initial ref-count of the object is 1. Use drm_dev_ref() and
509  * drm_dev_unref() to take and drop further ref-counts.
510  *
511  * Note that for purely virtual devices @parent can be NULL.
512  *
513  * RETURNS:
514  * Pointer to new DRM device, or NULL if out of memory.
515  */
516 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
517                                  struct device *parent)
518 {
519         struct drm_device *dev;
520         int ret;
521
522         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
523         if (!dev)
524                 return NULL;
525
526         kref_init(&dev->ref);
527         dev->dev = parent;
528         dev->driver = driver;
529
530         INIT_LIST_HEAD(&dev->filelist);
531         INIT_LIST_HEAD(&dev->ctxlist);
532         INIT_LIST_HEAD(&dev->vmalist);
533         INIT_LIST_HEAD(&dev->maplist);
534         INIT_LIST_HEAD(&dev->vblank_event_list);
535
536         spin_lock_init(&dev->buf_lock);
537         spin_lock_init(&dev->event_lock);
538         mutex_init(&dev->struct_mutex);
539         mutex_init(&dev->ctxlist_mutex);
540         mutex_init(&dev->master_mutex);
541
542         dev->anon_inode = drm_fs_inode_new();
543         if (IS_ERR(dev->anon_inode)) {
544                 ret = PTR_ERR(dev->anon_inode);
545                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
546                 goto err_free;
547         }
548
549         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
550                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
551                 if (ret)
552                         goto err_minors;
553         }
554
555         if (drm_core_check_feature(dev, DRIVER_RENDER)) {
556                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
557                 if (ret)
558                         goto err_minors;
559         }
560
561         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
562         if (ret)
563                 goto err_minors;
564
565         if (drm_ht_create(&dev->map_hash, 12))
566                 goto err_minors;
567
568         drm_legacy_ctxbitmap_init(dev);
569
570         if (drm_core_check_feature(dev, DRIVER_GEM)) {
571                 ret = drm_gem_init(dev);
572                 if (ret) {
573                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
574                         goto err_ctxbitmap;
575                 }
576         }
577
578         return dev;
579
580 err_ctxbitmap:
581         drm_legacy_ctxbitmap_cleanup(dev);
582         drm_ht_remove(&dev->map_hash);
583 err_minors:
584         drm_minor_free(dev, DRM_MINOR_LEGACY);
585         drm_minor_free(dev, DRM_MINOR_RENDER);
586         drm_minor_free(dev, DRM_MINOR_CONTROL);
587         drm_fs_inode_free(dev->anon_inode);
588 err_free:
589         mutex_destroy(&dev->master_mutex);
590         kfree(dev);
591         return NULL;
592 }
593 EXPORT_SYMBOL(drm_dev_alloc);
594
595 static void drm_dev_release(struct kref *ref)
596 {
597         struct drm_device *dev = container_of(ref, struct drm_device, ref);
598
599         if (drm_core_check_feature(dev, DRIVER_GEM))
600                 drm_gem_destroy(dev);
601
602         drm_legacy_ctxbitmap_cleanup(dev);
603         drm_ht_remove(&dev->map_hash);
604         drm_fs_inode_free(dev->anon_inode);
605
606         drm_minor_free(dev, DRM_MINOR_LEGACY);
607         drm_minor_free(dev, DRM_MINOR_RENDER);
608         drm_minor_free(dev, DRM_MINOR_CONTROL);
609
610         mutex_destroy(&dev->master_mutex);
611         kfree(dev->unique);
612         kfree(dev);
613 }
614
615 /**
616  * drm_dev_ref - Take reference of a DRM device
617  * @dev: device to take reference of or NULL
618  *
619  * This increases the ref-count of @dev by one. You *must* already own a
620  * reference when calling this. Use drm_dev_unref() to drop this reference
621  * again.
622  *
623  * This function never fails. However, this function does not provide *any*
624  * guarantee whether the device is alive or running. It only provides a
625  * reference to the object and the memory associated with it.
626  */
627 void drm_dev_ref(struct drm_device *dev)
628 {
629         if (dev)
630                 kref_get(&dev->ref);
631 }
632 EXPORT_SYMBOL(drm_dev_ref);
633
634 /**
635  * drm_dev_unref - Drop reference of a DRM device
636  * @dev: device to drop reference of or NULL
637  *
638  * This decreases the ref-count of @dev by one. The device is destroyed if the
639  * ref-count drops to zero.
640  */
641 void drm_dev_unref(struct drm_device *dev)
642 {
643         if (dev)
644                 kref_put(&dev->ref, drm_dev_release);
645 }
646 EXPORT_SYMBOL(drm_dev_unref);
647
648 /**
649  * drm_dev_register - Register DRM device
650  * @dev: Device to register
651  * @flags: Flags passed to the driver's .load() function
652  *
653  * Register the DRM device @dev with the system, advertise device to user-space
654  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
655  * previously.
656  *
657  * Never call this twice on any device!
658  *
659  * RETURNS:
660  * 0 on success, negative error code on failure.
661  */
662 int drm_dev_register(struct drm_device *dev, unsigned long flags)
663 {
664         int ret;
665
666         mutex_lock(&drm_global_mutex);
667
668         ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
669         if (ret)
670                 goto err_minors;
671
672         ret = drm_minor_register(dev, DRM_MINOR_RENDER);
673         if (ret)
674                 goto err_minors;
675
676         ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
677         if (ret)
678                 goto err_minors;
679
680         if (dev->driver->load) {
681                 ret = dev->driver->load(dev, flags);
682                 if (ret)
683                         goto err_minors;
684         }
685
686         ret = 0;
687         goto out_unlock;
688
689 err_minors:
690         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
691         drm_minor_unregister(dev, DRM_MINOR_RENDER);
692         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
693 out_unlock:
694         mutex_unlock(&drm_global_mutex);
695         return ret;
696 }
697 EXPORT_SYMBOL(drm_dev_register);
698
699 /**
700  * drm_dev_unregister - Unregister DRM device
701  * @dev: Device to unregister
702  *
703  * Unregister the DRM device from the system. This does the reverse of
704  * drm_dev_register() but does not deallocate the device. The caller must call
705  * drm_dev_unref() to drop their final reference.
706  */
707 void drm_dev_unregister(struct drm_device *dev)
708 {
709         struct drm_map_list *r_list, *list_temp;
710
711         drm_lastclose(dev);
712
713         if (dev->driver->unload)
714                 dev->driver->unload(dev);
715
716         if (dev->agp)
717                 drm_pci_agp_destroy(dev);
718
719         drm_vblank_cleanup(dev);
720
721         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
722                 drm_legacy_rmmap(dev, r_list->map);
723
724         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
725         drm_minor_unregister(dev, DRM_MINOR_RENDER);
726         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
727 }
728 EXPORT_SYMBOL(drm_dev_unregister);
729
730 /**
731  * drm_dev_set_unique - Set the unique name of a DRM device
732  * @dev: device of which to set the unique name
733  * @fmt: format string for unique name
734  *
735  * Sets the unique name of a DRM device using the specified format string and
736  * a variable list of arguments. Drivers can use this at driver probe time if
737  * the unique name of the devices they drive is static.
738  *
739  * Return: 0 on success or a negative error code on failure.
740  */
741 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
742 {
743         va_list ap;
744
745         kfree(dev->unique);
746
747         va_start(ap, fmt);
748         dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
749         va_end(ap);
750
751         return dev->unique ? 0 : -ENOMEM;
752 }
753 EXPORT_SYMBOL(drm_dev_set_unique);
754 #endif
755
756 /*
757  * DRM Core
758  * The DRM core module initializes all global DRM objects and makes them
759  * available to drivers. Once setup, drivers can probe their respective
760  * devices.
761  * Currently, core management includes:
762  *  - The "DRM-Global" key/value database
763  *  - Global ID management for connectors
764  *  - DRM major number allocation
765  *  - DRM minor management
766  *  - DRM sysfs class
767  *  - DRM debugfs root
768  *
769  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
770  * interface registered on a DRM device, you can request minor numbers from DRM
771  * core. DRM core takes care of major-number management and char-dev
772  * registration. A stub ->open() callback forwards any open() requests to the
773  * registered minor.
774  */
775
776 #if 0
777 static int drm_stub_open(struct inode *inode, struct file *filp)
778 {
779         const struct file_operations *new_fops;
780         struct drm_minor *minor;
781         int err;
782
783         DRM_DEBUG("\n");
784
785         mutex_lock(&drm_global_mutex);
786         minor = drm_minor_acquire(iminor(inode));
787         if (IS_ERR(minor)) {
788                 err = PTR_ERR(minor);
789                 goto out_unlock;
790         }
791
792         new_fops = fops_get(minor->dev->driver->fops);
793         if (!new_fops) {
794                 err = -ENODEV;
795                 goto out_release;
796         }
797
798         replace_fops(filp, new_fops);
799         if (filp->f_op->open)
800                 err = filp->f_op->open(inode, filp);
801         else
802                 err = 0;
803
804 out_release:
805         drm_minor_release(minor);
806 out_unlock:
807         mutex_unlock(&drm_global_mutex);
808         return err;
809 }
810
811 static const struct file_operations drm_stub_fops = {
812         .owner = THIS_MODULE,
813         .open = drm_stub_open,
814         .llseek = noop_llseek,
815 };
816
817 static int __init drm_core_init(void)
818 {
819         int ret = -ENOMEM;
820
821         drm_global_init();
822         drm_connector_ida_init();
823         idr_init(&drm_minors_idr);
824
825         if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
826                 goto err_p1;
827
828         drm_class = drm_sysfs_create(THIS_MODULE, "drm");
829         if (IS_ERR(drm_class)) {
830                 printk(KERN_ERR "DRM: Error creating drm class.\n");
831                 ret = PTR_ERR(drm_class);
832                 goto err_p2;
833         }
834
835         drm_debugfs_root = debugfs_create_dir("dri", NULL);
836         if (!drm_debugfs_root) {
837                 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
838                 ret = -1;
839                 goto err_p3;
840         }
841
842         DRM_INFO("Initialized %s %d.%d.%d %s\n",
843                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
844         return 0;
845 err_p3:
846         drm_sysfs_destroy();
847 err_p2:
848         unregister_chrdev(DRM_MAJOR, "drm");
849
850         idr_destroy(&drm_minors_idr);
851 err_p1:
852         return ret;
853 }
854
855 static void __exit drm_core_exit(void)
856 {
857         debugfs_remove(drm_debugfs_root);
858         drm_sysfs_destroy();
859
860         unregister_chrdev(DRM_MAJOR, "drm");
861
862         drm_connector_ida_destroy();
863         idr_destroy(&drm_minors_idr);
864 }
865
866 module_init(drm_core_init);
867 module_exit(drm_core_exit);
868 #endif
869
870 #include <sys/devfs.h>
871
872 #include <linux/export.h>
873 #include <linux/dmi.h>
874 #include <drm/drmP.h>
875 #include <drm/drm_core.h>
876
877 static int drm_load(struct drm_device *dev);
878 drm_pci_id_list_t *drm_find_description(int vendor, int device,
879     drm_pci_id_list_t *idlist);
880
881 #define DRIVER_SOFTC(unit) \
882         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
883
884 static int
885 drm_modevent(module_t mod, int type, void *data)
886 {
887
888         switch (type) {
889         case MOD_LOAD:
890                 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
891                 break;
892         }
893         return (0);
894 }
895
896 static moduledata_t drm_mod = {
897         "drm",
898         drm_modevent,
899         0
900 };
901 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
902 MODULE_VERSION(drm, 1);
903 MODULE_DEPEND(drm, agp, 1, 1, 1);
904 MODULE_DEPEND(drm, pci, 1, 1, 1);
905 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
906
907 static struct dev_ops drm_cdevsw = {
908         { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
909         .d_open =       drm_open,
910         .d_close =      drm_close,
911         .d_read =       drm_read,
912         .d_ioctl =      drm_ioctl,
913         .d_kqfilter =   drm_kqfilter,
914         .d_mmap =       drm_mmap,
915         .d_mmap_single = drm_mmap_single,
916 };
917
918 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
919 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
920     "DRM debugging");
921
922 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
923 {
924         drm_pci_id_list_t *id_entry;
925         int vendor, device;
926
927         vendor = pci_get_vendor(kdev);
928         device = pci_get_device(kdev);
929
930         if (pci_get_class(kdev) != PCIC_DISPLAY)
931                 return ENXIO;
932
933         id_entry = drm_find_description(vendor, device, idlist);
934         if (id_entry != NULL) {
935                 if (!device_get_desc(kdev)) {
936                         DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
937                         device_set_desc(kdev, id_entry->name);
938                 }
939                 return 0;
940         }
941
942         return ENXIO;
943 }
944
945 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
946 {
947         struct drm_device *dev;
948         drm_pci_id_list_t *id_entry;
949         int unit, error;
950         u_int irq_flags;
951         int msi_enable;
952
953         unit = device_get_unit(kdev);
954         dev = device_get_softc(kdev);
955
956         if (!strcmp(device_get_name(kdev), "drmsub"))
957                 dev->dev = device_get_parent(kdev);
958         else
959                 dev->dev = kdev;
960
961         dev->pci_domain = pci_get_domain(dev->dev);
962         dev->pci_bus = pci_get_bus(dev->dev);
963         dev->pci_slot = pci_get_slot(dev->dev);
964         dev->pci_func = pci_get_function(dev->dev);
965         drm_init_pdev(dev->dev, &dev->pdev);
966
967         id_entry = drm_find_description(dev->pdev->vendor,
968             dev->pdev->device, idlist);
969         dev->id_entry = id_entry;
970
971         if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
972                 msi_enable = 1;
973
974                 dev->irq_type = pci_alloc_1intr(dev->dev, msi_enable,
975                     &dev->irqrid, &irq_flags);
976
977                 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
978                     &dev->irqrid, irq_flags);
979
980                 if (!dev->irqr) {
981                         return (ENOENT);
982                 }
983
984                 dev->irq = (int) rman_get_start(dev->irqr);
985         }
986
987         lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
988         lwkt_serialize_init(&dev->irq_lock);
989         lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
990         lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
991
992         error = drm_load(dev);
993         if (error)
994                 goto error;
995
996         error = drm_create_cdevs(kdev);
997         if (error)
998                 goto error;
999
1000         return (error);
1001 error:
1002         if (dev->irqr) {
1003                 bus_release_resource(dev->dev, SYS_RES_IRQ,
1004                     dev->irqrid, dev->irqr);
1005         }
1006         if (dev->irq_type == PCI_INTR_TYPE_MSI) {
1007                 pci_release_msi(dev->dev);
1008         }
1009         return (error);
1010 }
1011
1012 int
1013 drm_create_cdevs(device_t kdev)
1014 {
1015         struct drm_device *dev;
1016         int error, unit;
1017
1018         unit = device_get_unit(kdev);
1019         dev = device_get_softc(kdev);
1020
1021         dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1022                                 DRM_DEV_MODE, "dri/card%d", unit);
1023         error = 0;
1024         if (error == 0)
1025                 dev->devnode->si_drv1 = dev;
1026         return (error);
1027 }
1028
1029 #ifndef DRM_DEV_NAME
1030 #define DRM_DEV_NAME "drm"
1031 #endif
1032
1033 devclass_t drm_devclass;
1034
1035 drm_pci_id_list_t *drm_find_description(int vendor, int device,
1036     drm_pci_id_list_t *idlist)
1037 {
1038         int i = 0;
1039
1040         for (i = 0; idlist[i].vendor != 0; i++) {
1041                 if ((idlist[i].vendor == vendor) &&
1042                     ((idlist[i].device == device) ||
1043                     (idlist[i].device == 0))) {
1044                         return &idlist[i];
1045                 }
1046         }
1047         return NULL;
1048 }
1049
1050 static int drm_load(struct drm_device *dev)
1051 {
1052         int i, retcode;
1053
1054         DRM_DEBUG("\n");
1055
1056         INIT_LIST_HEAD(&dev->maplist);
1057
1058         drm_sysctl_init(dev);
1059         INIT_LIST_HEAD(&dev->filelist);
1060
1061         dev->counters  = 6;
1062         dev->types[0]  = _DRM_STAT_LOCK;
1063         dev->types[1]  = _DRM_STAT_OPENS;
1064         dev->types[2]  = _DRM_STAT_CLOSES;
1065         dev->types[3]  = _DRM_STAT_IOCTLS;
1066         dev->types[4]  = _DRM_STAT_LOCKS;
1067         dev->types[5]  = _DRM_STAT_UNLOCKS;
1068
1069         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
1070                 atomic_set(&dev->counts[i], 0);
1071
1072         INIT_LIST_HEAD(&dev->vblank_event_list);
1073
1074         if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
1075                 if (drm_pci_device_is_agp(dev))
1076                         dev->agp = drm_agp_init(dev);
1077                 if (dev->agp != NULL && dev->agp->agp_info.ai_aperture_base != 0) {
1078                         if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
1079                             dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
1080                                 dev->agp->agp_mtrr = 1;
1081                 }
1082         }
1083
1084         if (dev->driver->driver_features & DRIVER_GEM) {
1085                 retcode = drm_gem_init(dev);
1086                 if (retcode != 0) {
1087                         DRM_ERROR("Cannot initialize graphics execution "
1088                                   "manager (GEM)\n");
1089                         goto error1;
1090                 }
1091         }
1092
1093         if (dev->driver->load != NULL) {
1094                 DRM_LOCK(dev);
1095                 /* Shared code returns -errno. */
1096                 retcode = -dev->driver->load(dev,
1097                     dev->id_entry->driver_private);
1098                 if (pci_enable_busmaster(dev->dev))
1099                         DRM_ERROR("Request to enable bus-master failed.\n");
1100                 DRM_UNLOCK(dev);
1101                 if (retcode != 0)
1102                         goto error1;
1103         }
1104
1105         DRM_INFO("Initialized %s %d.%d.%d %s\n",
1106             dev->driver->name,
1107             dev->driver->major,
1108             dev->driver->minor,
1109             dev->driver->patchlevel,
1110             dev->driver->date);
1111
1112         return 0;
1113
1114 error1:
1115         drm_gem_destroy(dev);
1116         drm_sysctl_cleanup(dev);
1117         DRM_LOCK(dev);
1118         drm_lastclose(dev);
1119         DRM_UNLOCK(dev);
1120         if (dev->devnode != NULL)
1121                 destroy_dev(dev->devnode);
1122
1123         lockuninit(&dev->vbl_lock);
1124         lockuninit(&dev->dev_lock);
1125         lockuninit(&dev->event_lock);
1126         lockuninit(&dev->struct_mutex);
1127
1128         return retcode;
1129 }
1130
1131 /*
1132  * Stub is needed for devfs
1133  */
1134 int drm_close(struct dev_close_args *ap)
1135 {
1136         return 0;
1137 }
1138
1139 void drm_cdevpriv_dtor(void *cd)
1140 {
1141         struct drm_file *file_priv = cd;
1142         struct drm_device *dev = file_priv->dev;
1143         int retcode = 0;
1144
1145         DRM_DEBUG("open_count = %d\n", dev->open_count);
1146
1147         DRM_LOCK(dev);
1148
1149         if (dev->driver->preclose != NULL)
1150                 dev->driver->preclose(dev, file_priv);
1151
1152         /* ========================================================
1153          * Begin inline drm_release
1154          */
1155
1156         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1157             DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1158
1159         if (dev->driver->driver_features & DRIVER_GEM)
1160                 drm_gem_release(dev, file_priv);
1161
1162         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
1163             !dev->driver->reclaim_buffers_locked)
1164                 drm_legacy_reclaim_buffers(dev, file_priv);
1165
1166         funsetown(&dev->buf_sigio);
1167
1168         if (dev->driver->postclose != NULL)
1169                 dev->driver->postclose(dev, file_priv);
1170         list_del(&file_priv->lhead);
1171
1172
1173         /* ========================================================
1174          * End inline drm_release
1175          */
1176
1177         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1178         device_unbusy(dev->dev);
1179         if (--dev->open_count == 0) {
1180                 retcode = drm_lastclose(dev);
1181         }
1182
1183         DRM_UNLOCK(dev);
1184 }
1185
1186 int
1187 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1188     struct sysctl_oid *top)
1189 {
1190         struct sysctl_oid *oid;
1191
1192         ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1193              "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1194              dev->pci_slot, dev->pci_func);
1195         oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1196             CTLFLAG_RD, dev->busid_str, 0, NULL);
1197         if (oid == NULL)
1198                 return (ENOMEM);
1199         dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
1200         oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
1201             "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
1202         if (oid == NULL)
1203                 return (ENOMEM);
1204
1205         return (0);
1206 }
1207
1208 int
1209 drm_mmap_single(struct dev_mmap_single_args *ap)
1210 {
1211         struct drm_device *dev;
1212         struct cdev *kdev = ap->a_head.a_dev;
1213         vm_ooffset_t *offset = ap->a_offset;
1214         vm_size_t size = ap->a_size;
1215         struct vm_object **obj_res = ap->a_object;
1216         int nprot = ap->a_nprot;
1217
1218         dev = drm_get_device_from_kdev(kdev);
1219         if (dev->drm_ttm_bdev != NULL) {
1220                 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
1221                     obj_res, nprot));
1222         } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1223                 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1224         } else {
1225                 return (ENODEV);
1226         }
1227 }
1228
1229 /* XXX broken code */
1230 #if DRM_LINUX
1231
1232 #include <sys/sysproto.h>
1233
1234 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
1235
1236 #define LINUX_IOCTL_DRM_MIN             0x6400
1237 #define LINUX_IOCTL_DRM_MAX             0x64ff
1238
1239 static linux_ioctl_function_t drm_linux_ioctl;
1240 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
1241     LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
1242
1243 /* The bits for in/out are switched on Linux */
1244 #define LINUX_IOC_IN    IOC_OUT
1245 #define LINUX_IOC_OUT   IOC_IN
1246
1247 static int
1248 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
1249 {
1250         int error;
1251         int cmd = args->cmd;
1252
1253         args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
1254         if (cmd & LINUX_IOC_IN)
1255                 args->cmd |= IOC_IN;
1256         if (cmd & LINUX_IOC_OUT)
1257                 args->cmd |= IOC_OUT;
1258         
1259         error = ioctl(p, (struct ioctl_args *)args);
1260
1261         return error;
1262 }
1263 #endif /* DRM_LINUX */
1264
1265 static int
1266 drm_core_init(void *arg)
1267 {
1268
1269         drm_global_init();
1270
1271 #if DRM_LINUX
1272         linux_ioctl_register_handler(&drm_handler);
1273 #endif /* DRM_LINUX */
1274
1275         DRM_INFO("Initialized %s %d.%d.%d %s\n",
1276                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1277         return 0;
1278 }
1279
1280 static void
1281 drm_core_exit(void *arg)
1282 {
1283
1284 #if DRM_LINUX
1285         linux_ioctl_unregister_handler(&drm_handler);
1286 #endif /* DRM_LINUX */
1287
1288         drm_global_release();
1289 }
1290
1291 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1292     drm_core_init, NULL);
1293 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1294     drm_core_exit, NULL);
1295
1296
1297 #include <linux/dmi.h>
1298
1299 /*
1300  * Check if dmi_system_id structure matches system DMI data
1301  */
1302 static bool
1303 dmi_found(const struct dmi_system_id *dsi)
1304 {
1305         int i, slot;
1306         bool found = false;
1307         char *sys_vendor, *board_vendor, *product_name, *board_name;
1308
1309         sys_vendor = kgetenv("smbios.system.maker");
1310         board_vendor = kgetenv("smbios.planar.maker");
1311         product_name = kgetenv("smbios.system.product");
1312         board_name = kgetenv("smbios.planar.product");
1313
1314         for (i = 0; i < NELEM(dsi->matches); i++) {
1315                 slot = dsi->matches[i].slot;
1316                 switch (slot) {
1317                 case DMI_NONE:
1318                         break;
1319                 case DMI_SYS_VENDOR:
1320                         if (sys_vendor != NULL &&
1321                             !strcmp(sys_vendor, dsi->matches[i].substr))
1322                                 break;
1323                         else
1324                                 goto done;
1325                 case DMI_BOARD_VENDOR:
1326                         if (board_vendor != NULL &&
1327                             !strcmp(board_vendor, dsi->matches[i].substr))
1328                                 break;
1329                         else
1330                                 goto done;
1331                 case DMI_PRODUCT_NAME:
1332                         if (product_name != NULL &&
1333                             !strcmp(product_name, dsi->matches[i].substr))
1334                                 break;
1335                         else
1336                                 goto done;
1337                 case DMI_BOARD_NAME:
1338                         if (board_name != NULL &&
1339                             !strcmp(board_name, dsi->matches[i].substr))
1340                                 break;
1341                         else
1342                                 goto done;
1343                 default:
1344                         goto done;
1345                 }
1346         }
1347         found = true;
1348
1349 done:
1350         if (sys_vendor != NULL)
1351                 kfreeenv(sys_vendor);
1352         if (board_vendor != NULL)
1353                 kfreeenv(board_vendor);
1354         if (product_name != NULL)
1355                 kfreeenv(product_name);
1356         if (board_name != NULL)
1357                 kfreeenv(board_name);
1358
1359         return found;
1360 }
1361
1362 int dmi_check_system(const struct dmi_system_id *sysid)
1363 {
1364         const struct dmi_system_id *dsi;
1365         int num = 0;
1366
1367         for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1368                 if (dmi_found(dsi)) {
1369                         num++;
1370                         if (dsi->callback && dsi->callback(dsi))
1371                                 break;
1372                 }
1373         }
1374         return (num);
1375 }