kernel - Fix auto port assignment collision in network code
[dragonfly.git] / sys / dev / drm / drm_drv.c
blob107dfeb987e73c82ff274709115fae742fb5f6fa
1 /*
2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5 * All Rights Reserved.
7 * Author Rickard E. (Rik) Faith <faith@valinux.com>
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:
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.
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.
29 #include <linux/debugfs.h>
30 #include <linux/fs.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
36 #include <drm/drm_drv.h>
37 #include <drm/drmP.h>
39 #include "drm_crtc_internal.h"
40 #include "drm_legacy.h"
41 #include "drm_internal.h"
44 * drm_debug: Enable debug output.
45 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
47 #ifdef __DragonFly__
48 /* Provides three levels of debug: off, minimal, verbose */
49 #if DRM_DEBUG_DEFAULT_ON == 1
50 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
51 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL)
52 #elif DRM_DEBUG_DEFAULT_ON == 2
53 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
54 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \
55 DRM_UT_PID | DRM_UT_IOCTL )
56 #else
57 #define DRM_DEBUGBITS_ON (0x0)
58 #endif
59 unsigned int drm_debug = DRM_DEBUGBITS_ON; /* defaults to 0 */
60 #else
61 unsigned int drm_debug = 0;
62 #endif /* __DragonFly__ */
63 EXPORT_SYMBOL(drm_debug);
65 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
66 MODULE_DESCRIPTION("DRM shared core routines");
67 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
68 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
69 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
70 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
71 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
72 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
73 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
74 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)");
75 module_param_named(debug, drm_debug, int, 0600);
77 static DEFINE_MUTEX(drm_minor_lock);
78 static struct idr drm_minors_idr;
81 * If the drm core fails to init for whatever reason,
82 * we should prevent any drivers from registering with it.
83 * It's best to check this at drm_dev_init(), as some drivers
84 * prefer to embed struct drm_device into their own device
85 * structure and call drm_dev_init() themselves.
87 static bool drm_core_init_complete = false;
89 #if 0
90 static struct dentry *drm_debugfs_root;
91 #endif
93 void drm_err(const char *func, const char *format, ...)
95 va_list args;
97 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func);
99 va_start(args, format);
100 kvprintf(format, args);
101 va_end(args);
104 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
106 va_list args;
108 if (unlikely(drm_debug & DRM_UT_PID)) {
109 kprintf("[" DRM_NAME ":pid%d:%s] ",
110 DRM_CURRENTPID, function_name);
111 } else {
112 kprintf("[" DRM_NAME ":%s] ", function_name);
115 va_start(args, format);
116 kvprintf(format, args);
117 va_end(args);
120 #define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV"
121 #define DRM_PRINTK_FMT_DFLY "[" DRM_NAME ":%s]%s "
123 void drm_dev_printk(const struct device *dev, const char *level,
124 unsigned int category, const char *function_name,
125 const char *prefix, const char *format, ...)
127 struct va_format vaf;
128 va_list args;
130 if (category != DRM_UT_NONE && !(drm_debug & category))
131 return;
133 va_start(args, format);
134 vaf.fmt = format;
135 vaf.va = &args;
137 if (dev)
138 #if 0
139 dev_printk(level, dev, DRM_PRINTK_FMT, function_name, prefix,
140 &vaf);
141 else
142 printk("%s" DRM_PRINTK_FMT, level, function_name, prefix, &vaf);
143 #else
145 kprintf("drm_dev_printk: ");
146 dev_printk(level, dev, DRM_PRINTK_FMT_DFLY, function_name, prefix);
147 kprintf(vaf.fmt, vaf.va);
148 } else {
149 kprintf("drm_dev_printk: ");
150 printk("%s" DRM_PRINTK_FMT_DFLY, level, function_name, prefix);
151 kprintf(vaf.fmt, vaf.va);
153 #endif
155 va_end(args);
157 EXPORT_SYMBOL(drm_dev_printk);
159 void drm_printk(const char *level, unsigned int category,
160 const char *format, ...)
162 struct va_format vaf;
163 va_list args;
165 if (category != DRM_UT_NONE && !(drm_debug & category))
166 return;
168 va_start(args, format);
169 vaf.fmt = format;
170 vaf.va = &args;
172 #if 0
173 printk("%s" "[" DRM_NAME ":%ps]%s %pV",
174 level, __builtin_return_address(0),
175 strcmp(level, KERN_ERR) == 0 ? " *ERROR*" : "", &vaf);
176 #else
177 printk("%s" "[" DRM_NAME ":%p]%s ",
178 level, __builtin_return_address(0),
179 strcmp(level, KERN_ERR) == 0 ? " *ERROR*" : "");
180 kprintf(vaf.fmt, vaf.va);
181 #endif
183 va_end(args);
185 EXPORT_SYMBOL(drm_printk);
188 * DRM Minors
189 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
190 * of them is represented by a drm_minor object. Depending on the capabilities
191 * of the device-driver, different interfaces are registered.
193 * Minors can be accessed via dev->$minor_name. This pointer is either
194 * NULL or a valid drm_minor pointer and stays valid as long as the device is
195 * valid. This means, DRM minors have the same life-time as the underlying
196 * device. However, this doesn't mean that the minor is active. Minors are
197 * registered and unregistered dynamically according to device-state.
200 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
201 unsigned int type)
203 switch (type) {
204 case DRM_MINOR_PRIMARY:
205 return &dev->primary;
206 case DRM_MINOR_RENDER:
207 return &dev->render;
208 case DRM_MINOR_CONTROL:
209 return &dev->control;
210 default:
211 return NULL;
215 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
217 struct drm_minor *minor;
218 unsigned long flags;
219 int r;
221 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
222 if (!minor)
223 return -ENOMEM;
225 minor->type = type;
226 minor->dev = dev;
228 idr_preload(GFP_KERNEL);
229 spin_lock_irqsave(&drm_minor_lock, flags);
230 r = idr_alloc(&drm_minors_idr,
231 NULL,
232 64 * type,
233 64 * (type + 1),
234 GFP_NOWAIT);
235 spin_unlock_irqrestore(&drm_minor_lock, flags);
236 idr_preload_end();
238 if (r < 0)
239 goto err_free;
241 minor->index = r;
243 minor->kdev = drm_sysfs_minor_alloc(minor);
244 if (IS_ERR(minor->kdev)) {
245 r = PTR_ERR(minor->kdev);
246 goto err_index;
249 *drm_minor_get_slot(dev, type) = minor;
250 return 0;
252 err_index:
253 spin_lock_irqsave(&drm_minor_lock, flags);
254 idr_remove(&drm_minors_idr, minor->index);
255 spin_unlock_irqrestore(&drm_minor_lock, flags);
256 err_free:
257 kfree(minor);
258 return r;
261 static void drm_minor_free(struct drm_device *dev, unsigned int type)
263 struct drm_minor **slot, *minor;
264 unsigned long flags;
266 slot = drm_minor_get_slot(dev, type);
267 minor = *slot;
268 if (!minor)
269 return;
271 #if 0
272 put_device(minor->kdev);
273 #endif
275 spin_lock_irqsave(&drm_minor_lock, flags);
276 idr_remove(&drm_minors_idr, minor->index);
277 spin_unlock_irqrestore(&drm_minor_lock, flags);
279 kfree(minor);
280 *slot = NULL;
283 static int drm_minor_register(struct drm_device *dev, unsigned int type)
285 struct drm_minor *minor;
286 unsigned long flags;
287 int ret;
289 DRM_DEBUG("\n");
291 minor = *drm_minor_get_slot(dev, type);
292 if (!minor)
293 return 0;
295 #if 0
296 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
297 if (ret) {
298 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
299 goto err_debugfs;
302 ret = device_add(minor->kdev);
303 if (ret)
304 goto err_debugfs;
305 #endif
307 /* replace NULL with @minor so lookups will succeed from now on */
308 spin_lock_irqsave(&drm_minor_lock, flags);
309 idr_replace(&drm_minors_idr, minor, minor->index);
310 spin_unlock_irqrestore(&drm_minor_lock, flags);
312 DRM_DEBUG("new minor registered %d\n", minor->index);
313 return 0;
315 #if 0
316 err_debugfs:
317 drm_debugfs_cleanup(minor);
318 #endif
319 return ret;
322 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
324 struct drm_minor *minor;
325 unsigned long flags;
327 minor = *drm_minor_get_slot(dev, type);
328 #if 0
329 if (!minor || !device_is_registered(minor->kdev))
330 #else
331 if (!minor)
332 #endif
333 return;
335 /* replace @minor with NULL so lookups will fail from now on */
336 spin_lock_irqsave(&drm_minor_lock, flags);
337 idr_replace(&drm_minors_idr, NULL, minor->index);
338 spin_unlock_irqrestore(&drm_minor_lock, flags);
340 #if 0
341 device_del(minor->kdev);
342 #endif
343 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
344 drm_debugfs_cleanup(minor);
348 * Looks up the given minor-ID and returns the respective DRM-minor object. The
349 * refence-count of the underlying device is increased so you must release this
350 * object with drm_minor_release().
352 * As long as you hold this minor, it is guaranteed that the object and the
353 * minor->dev pointer will stay valid! However, the device may get unplugged and
354 * unregistered while you hold the minor.
356 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
358 struct drm_minor *minor;
359 unsigned long flags;
361 spin_lock_irqsave(&drm_minor_lock, flags);
362 minor = idr_find(&drm_minors_idr, minor_id);
363 if (minor)
364 drm_dev_get(minor->dev);
365 spin_unlock_irqrestore(&drm_minor_lock, flags);
367 if (!minor) {
368 return ERR_PTR(-ENODEV);
369 } else if (drm_dev_is_unplugged(minor->dev)) {
370 drm_dev_put(minor->dev);
371 return ERR_PTR(-ENODEV);
374 return minor;
377 void drm_minor_release(struct drm_minor *minor)
379 drm_dev_put(minor->dev);
383 * DOC: driver instance overview
385 * A device instance for a drm driver is represented by &struct drm_device. This
386 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
387 * callbacks implemented by the driver. The driver then needs to initialize all
388 * the various subsystems for the drm device like memory management, vblank
389 * handling, modesetting support and intial output configuration plus obviously
390 * initialize all the corresponding hardware bits. An important part of this is
391 * also calling drm_dev_set_unique() to set the userspace-visible unique name of
392 * this device instance. Finally when everything is up and running and ready for
393 * userspace the device instance can be published using drm_dev_register().
395 * There is also deprecated support for initalizing device instances using
396 * bus-specific helpers and the &drm_driver.load callback. But due to
397 * backwards-compatibility needs the device instance have to be published too
398 * early, which requires unpretty global locking to make safe and is therefore
399 * only support for existing drivers not yet converted to the new scheme.
401 * When cleaning up a device instance everything needs to be done in reverse:
402 * First unpublish the device instance with drm_dev_unregister(). Then clean up
403 * any other resources allocated at device initialization and drop the driver's
404 * reference to &drm_device using drm_dev_put().
406 * Note that the lifetime rules for &drm_device instance has still a lot of
407 * historical baggage. Hence use the reference counting provided by
408 * drm_dev_get() and drm_dev_put() only carefully.
410 * It is recommended that drivers embed &struct drm_device into their own device
411 * structure, which is supported through drm_dev_init().
414 #if 0
416 * drm_put_dev - Unregister and release a DRM device
417 * @dev: DRM device
419 * Called at module unload time or when a PCI device is unplugged.
421 * Cleans up all DRM device, calling drm_lastclose().
423 * Note: Use of this function is deprecated. It will eventually go away
424 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
425 * instead to make sure that the device isn't userspace accessible any more
426 * while teardown is in progress, ensuring that userspace can't access an
427 * inconsistent state.
429 void drm_put_dev(struct drm_device *dev)
431 DRM_DEBUG("\n");
433 if (!dev) {
434 DRM_ERROR("cleanup called no dev\n");
435 return;
438 drm_dev_unregister(dev);
439 drm_dev_put(dev);
441 EXPORT_SYMBOL(drm_put_dev);
443 static void drm_device_set_unplugged(struct drm_device *dev)
445 smp_wmb();
446 atomic_set(&dev->unplugged, 1);
450 * drm_dev_unplug - unplug a DRM device
451 * @dev: DRM device
453 * This unplugs a hotpluggable DRM device, which makes it inaccessible to
454 * userspace operations. Entry-points can use drm_dev_is_unplugged(). This
455 * essentially unregisters the device like drm_dev_unregister(), but can be
456 * called while there are still open users of @dev.
458 void drm_dev_unplug(struct drm_device *dev)
460 drm_dev_unregister(dev);
462 mutex_lock(&drm_global_mutex);
463 drm_device_set_unplugged(dev);
464 if (dev->open_count == 0)
465 drm_dev_put(dev);
466 mutex_unlock(&drm_global_mutex);
468 EXPORT_SYMBOL(drm_dev_unplug);
471 * DRM internal mount
472 * We want to be able to allocate our own "struct address_space" to control
473 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
474 * stand-alone address_space objects, so we need an underlying inode. As there
475 * is no way to allocate an independent inode easily, we need a fake internal
476 * VFS mount-point.
478 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
479 * frees it again. You are allowed to use iget() and iput() to get references to
480 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
481 * drm_fs_inode_free() call (which does not have to be the last iput()).
482 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
483 * between multiple inode-users. You could, technically, call
484 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
485 * iput(), but this way you'd end up with a new vfsmount for each inode.
488 static int drm_fs_cnt;
489 static struct vfsmount *drm_fs_mnt;
491 static const struct dentry_operations drm_fs_dops = {
492 .d_dname = simple_dname,
495 static const struct super_operations drm_fs_sops = {
496 .statfs = simple_statfs,
499 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
500 const char *dev_name, void *data)
502 return mount_pseudo(fs_type,
503 "drm:",
504 &drm_fs_sops,
505 &drm_fs_dops,
506 0x010203ff);
509 static struct file_system_type drm_fs_type = {
510 .name = "drm",
511 .owner = THIS_MODULE,
512 .mount = drm_fs_mount,
513 .kill_sb = kill_anon_super,
516 static struct inode *drm_fs_inode_new(void)
518 struct inode *inode;
519 int r;
521 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
522 if (r < 0) {
523 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
524 return ERR_PTR(r);
527 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
528 if (IS_ERR(inode))
529 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
531 return inode;
534 static void drm_fs_inode_free(struct inode *inode)
536 if (inode) {
537 iput(inode);
538 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
541 #endif
544 * drm_dev_init - Initialise new DRM device
545 * @dev: DRM device
546 * @driver: DRM driver
547 * @parent: Parent device object
549 * Initialize a new DRM device. No device registration is done.
550 * Call drm_dev_register() to advertice the device to user space and register it
551 * with other core subsystems. This should be done last in the device
552 * initialization sequence to make sure userspace can't access an inconsistent
553 * state.
555 * The initial ref-count of the object is 1. Use drm_dev_get() and
556 * drm_dev_put() to take and drop further ref-counts.
558 * Note that for purely virtual devices @parent can be NULL.
560 * Drivers that do not want to allocate their own device struct
561 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers
562 * that do embed &struct drm_device it must be placed first in the overall
563 * structure, and the overall structure must be allocated using kmalloc(): The
564 * drm core's release function unconditionally calls kfree() on the @dev pointer
565 * when the final reference is released. To override this behaviour, and so
566 * allow embedding of the drm_device inside the driver's device struct at an
567 * arbitrary offset, you must supply a &drm_driver.release callback and control
568 * the finalization explicitly.
570 * RETURNS:
571 * 0 on success, or error code on failure.
573 int drm_dev_init(struct drm_device *dev,
574 struct drm_driver *driver,
575 struct device *parent)
577 int ret;
578 #ifdef __DragonFly__
579 struct drm_softc *softc = device_get_softc(parent->bsddev);
581 softc->drm_driver_data = dev;
582 #endif
584 if (!drm_core_init_complete) {
585 DRM_ERROR("DRM core is not initialized\n");
586 return -ENODEV;
589 kref_init(&dev->ref);
590 dev->dev = parent;
591 dev->driver = driver;
593 INIT_LIST_HEAD(&dev->filelist);
594 INIT_LIST_HEAD(&dev->ctxlist);
595 INIT_LIST_HEAD(&dev->vmalist);
596 INIT_LIST_HEAD(&dev->maplist);
597 INIT_LIST_HEAD(&dev->vblank_event_list);
599 lockinit(&dev->buf_lock, "drmdbl", 0, 0);
600 lockinit(&dev->event_lock, "drmev", 0, 0);
601 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
602 lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE);
603 lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE);
604 lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE);
606 #ifndef __DragonFly__
607 dev->anon_inode = drm_fs_inode_new();
608 if (IS_ERR(dev->anon_inode)) {
609 ret = PTR_ERR(dev->anon_inode);
610 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
611 goto err_free;
613 #else
614 dev->anon_inode = NULL;
615 dev->pci_domain = pci_get_domain(dev->dev->bsddev);
616 dev->pci_bus = pci_get_bus(dev->dev->bsddev);
617 dev->pci_slot = pci_get_slot(dev->dev->bsddev);
618 dev->pci_func = pci_get_function(dev->dev->bsddev);
619 drm_sysctl_init(dev);
620 #endif
622 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
623 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
624 if (ret)
625 goto err_minors;
628 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
629 if (ret)
630 goto err_minors;
632 ret = drm_ht_create(&dev->map_hash, 12);
633 if (ret)
634 goto err_minors;
636 drm_legacy_ctxbitmap_init(dev);
638 if (drm_core_check_feature(dev, DRIVER_GEM)) {
639 ret = drm_gem_init(dev);
640 if (ret) {
641 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
642 goto err_ctxbitmap;
646 /* Use the parent device name as DRM device unique identifier, but fall
647 * back to the driver name for virtual devices like vgem. */
648 #if 0
649 ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name);
650 if (ret)
651 goto err_setunique;
652 #endif
654 return 0;
656 #if 0
657 err_setunique:
658 if (drm_core_check_feature(dev, DRIVER_GEM))
659 drm_gem_destroy(dev);
660 #endif
661 err_ctxbitmap:
662 drm_legacy_ctxbitmap_cleanup(dev);
663 drm_ht_remove(&dev->map_hash);
664 err_minors:
665 drm_minor_free(dev, DRM_MINOR_PRIMARY);
666 drm_minor_free(dev, DRM_MINOR_RENDER);
667 drm_minor_free(dev, DRM_MINOR_CONTROL);
668 #ifndef __DragonFly__
669 drm_fs_inode_free(dev->anon_inode);
670 err_free:
671 #endif
672 mutex_destroy(&dev->master_mutex);
673 mutex_destroy(&dev->ctxlist_mutex);
674 mutex_destroy(&dev->filelist_mutex);
675 mutex_destroy(&dev->struct_mutex);
676 #ifdef __DragonFly__
677 drm_sysctl_cleanup(dev);
678 #endif
679 return ret;
681 EXPORT_SYMBOL(drm_dev_init);
684 * drm_dev_fini - Finalize a dead DRM device
685 * @dev: DRM device
687 * Finalize a dead DRM device. This is the converse to drm_dev_init() and
688 * frees up all data allocated by it. All driver private data should be
689 * finalized first. Note that this function does not free the @dev, that is
690 * left to the caller.
692 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
693 * from a &drm_driver.release callback.
695 void drm_dev_fini(struct drm_device *dev)
697 drm_vblank_cleanup(dev);
699 if (drm_core_check_feature(dev, DRIVER_GEM))
700 drm_gem_destroy(dev);
702 drm_legacy_ctxbitmap_cleanup(dev);
703 drm_ht_remove(&dev->map_hash);
704 #if 0
705 drm_fs_inode_free(dev->anon_inode);
706 #endif
708 drm_minor_free(dev, DRM_MINOR_PRIMARY);
709 drm_minor_free(dev, DRM_MINOR_RENDER);
710 drm_minor_free(dev, DRM_MINOR_CONTROL);
712 mutex_destroy(&dev->master_mutex);
713 mutex_destroy(&dev->ctxlist_mutex);
714 mutex_destroy(&dev->filelist_mutex);
715 mutex_destroy(&dev->struct_mutex);
716 kfree(dev->unique);
718 EXPORT_SYMBOL(drm_dev_fini);
721 * drm_dev_alloc - Allocate new DRM device
722 * @driver: DRM driver to allocate device for
723 * @parent: Parent device object
725 * Allocate and initialize a new DRM device. No device registration is done.
726 * Call drm_dev_register() to advertice the device to user space and register it
727 * with other core subsystems. This should be done last in the device
728 * initialization sequence to make sure userspace can't access an inconsistent
729 * state.
731 * The initial ref-count of the object is 1. Use drm_dev_get() and
732 * drm_dev_put() to take and drop further ref-counts.
734 * Note that for purely virtual devices @parent can be NULL.
736 * Drivers that wish to subclass or embed &struct drm_device into their
737 * own struct should look at using drm_dev_init() instead.
739 * RETURNS:
740 * Pointer to new DRM device, or ERR_PTR on failure.
742 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
743 struct device *parent)
745 struct drm_device *dev;
746 int ret;
748 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
749 if (!dev)
750 return ERR_PTR(-ENOMEM);
752 ret = drm_dev_init(dev, driver, parent);
753 if (ret) {
754 kfree(dev);
755 return ERR_PTR(ret);
758 return dev;
760 EXPORT_SYMBOL(drm_dev_alloc);
762 static void drm_dev_release(struct kref *ref)
764 struct drm_device *dev = container_of(ref, struct drm_device, ref);
766 if (dev->driver->release) {
767 dev->driver->release(dev);
768 } else {
769 drm_dev_fini(dev);
770 kfree(dev);
775 * drm_dev_get - Take reference of a DRM device
776 * @dev: device to take reference of or NULL
778 * This increases the ref-count of @dev by one. You *must* already own a
779 * reference when calling this. Use drm_dev_put() to drop this reference
780 * again.
782 * This function never fails. However, this function does not provide *any*
783 * guarantee whether the device is alive or running. It only provides a
784 * reference to the object and the memory associated with it.
786 void drm_dev_get(struct drm_device *dev)
788 if (dev)
789 kref_get(&dev->ref);
791 EXPORT_SYMBOL(drm_dev_get);
794 * drm_dev_put - Drop reference of a DRM device
795 * @dev: device to drop reference of or NULL
797 * This decreases the ref-count of @dev by one. The device is destroyed if the
798 * ref-count drops to zero.
800 void drm_dev_put(struct drm_device *dev)
802 if (dev)
803 kref_put(&dev->ref, drm_dev_release);
805 EXPORT_SYMBOL(drm_dev_put);
808 * drm_dev_unref - Drop reference of a DRM device
809 * @dev: device to drop reference of or NULL
811 * This is a compatibility alias for drm_dev_put() and should not be used by new
812 * code.
814 void drm_dev_unref(struct drm_device *dev)
816 drm_dev_put(dev);
818 EXPORT_SYMBOL(drm_dev_unref);
820 static int create_compat_control_link(struct drm_device *dev)
822 struct drm_minor *minor;
823 char *name;
824 int ret;
826 if (!drm_core_check_feature(dev, DRIVER_MODESET))
827 return 0;
829 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
830 if (!minor)
831 return 0;
834 * Some existing userspace out there uses the existing of the controlD*
835 * sysfs files to figure out whether it's a modeset driver. It only does
836 * readdir, hence a symlink is sufficient (and the least confusing
837 * option). Otherwise controlD* is entirely unused.
839 * Old controlD chardev have been allocated in the range
840 * 64-127.
842 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
843 if (!name)
844 return -ENOMEM;
846 #ifndef __DragonFly__ /* DragonFly's libdrm does not need this */
847 ret = sysfs_create_link(minor->kdev->kobj.parent,
848 &minor->kdev->kobj,
849 name);
850 #else
851 ret = 0;
852 #endif
854 kfree(name);
856 return ret;
859 static void remove_compat_control_link(struct drm_device *dev)
861 struct drm_minor *minor;
862 char *name;
864 if (!drm_core_check_feature(dev, DRIVER_MODESET))
865 return;
867 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
868 if (!minor)
869 return;
871 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index);
872 if (!name)
873 return;
875 #ifndef __DragonFly__
876 sysfs_remove_link(minor->kdev->kobj.parent, name);
877 #endif
879 kfree(name);
883 * drm_dev_register - Register DRM device
884 * @dev: Device to register
885 * @flags: Flags passed to the driver's .load() function
887 * Register the DRM device @dev with the system, advertise device to user-space
888 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
889 * previously.
891 * Never call this twice on any device!
893 * NOTE: To ensure backward compatibility with existing drivers method this
894 * function calls the &drm_driver.load method after registering the device
895 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
896 * therefore deprecated, drivers must perform all initialization before calling
897 * drm_dev_register().
899 * RETURNS:
900 * 0 on success, negative error code on failure.
902 int drm_dev_register(struct drm_device *dev, unsigned long flags)
904 struct drm_driver *driver = dev->driver;
905 int ret;
907 mutex_lock(&drm_global_mutex);
909 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
910 if (ret)
911 goto err_minors;
913 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
914 if (ret)
915 goto err_minors;
917 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
918 if (ret)
919 goto err_minors;
921 ret = create_compat_control_link(dev);
922 if (ret)
923 goto err_minors;
925 dev->registered = true;
927 if (dev->driver->load) {
928 ret = dev->driver->load(dev, flags);
929 if (ret)
930 goto err_minors;
933 if (drm_core_check_feature(dev, DRIVER_MODESET))
934 drm_modeset_register_all(dev);
936 ret = 0;
938 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
939 driver->name, driver->major, driver->minor,
940 driver->patchlevel, driver->date,
941 dev->dev ? dev_name(dev->dev) : "virtual device",
942 dev->primary->index);
944 goto out_unlock;
946 err_minors:
947 remove_compat_control_link(dev);
948 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
949 drm_minor_unregister(dev, DRM_MINOR_RENDER);
950 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
951 out_unlock:
952 mutex_unlock(&drm_global_mutex);
953 return ret;
955 EXPORT_SYMBOL(drm_dev_register);
958 * drm_dev_unregister - Unregister DRM device
959 * @dev: Device to unregister
961 * Unregister the DRM device from the system. This does the reverse of
962 * drm_dev_register() but does not deallocate the device. The caller must call
963 * drm_dev_put() to drop their final reference.
965 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
966 * which can be called while there are still open users of @dev.
968 * This should be called first in the device teardown code to make sure
969 * userspace can't access the device instance any more.
971 void drm_dev_unregister(struct drm_device *dev)
973 struct drm_map_list *r_list, *list_temp;
975 if (drm_core_check_feature(dev, DRIVER_LEGACY))
976 drm_lastclose(dev);
978 dev->registered = false;
980 if (drm_core_check_feature(dev, DRIVER_MODESET))
981 drm_modeset_unregister_all(dev);
983 if (dev->driver->unload)
984 dev->driver->unload(dev);
986 #if 0
987 if (dev->agp)
988 drm_pci_agp_destroy(dev);
989 #endif
991 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
992 drm_legacy_rmmap(dev, r_list->map);
994 remove_compat_control_link(dev);
995 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
996 drm_minor_unregister(dev, DRM_MINOR_RENDER);
997 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
999 EXPORT_SYMBOL(drm_dev_unregister);
1001 #if 0
1003 * drm_dev_set_unique - Set the unique name of a DRM device
1004 * @dev: device of which to set the unique name
1005 * @name: unique name
1007 * Sets the unique name of a DRM device using the specified string. Drivers
1008 * can use this at driver probe time if the unique name of the devices they
1009 * drive is static.
1011 * Return: 0 on success or a negative error code on failure.
1013 int drm_dev_set_unique(struct drm_device *dev, const char *name)
1015 kfree(dev->unique);
1016 dev->unique = kstrdup(name, GFP_KERNEL);
1018 return dev->unique ? 0 : -ENOMEM;
1020 EXPORT_SYMBOL(drm_dev_set_unique);
1023 * DRM Core
1024 * The DRM core module initializes all global DRM objects and makes them
1025 * available to drivers. Once setup, drivers can probe their respective
1026 * devices.
1027 * Currently, core management includes:
1028 * - The "DRM-Global" key/value database
1029 * - Global ID management for connectors
1030 * - DRM major number allocation
1031 * - DRM minor management
1032 * - DRM sysfs class
1033 * - DRM debugfs root
1035 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1036 * interface registered on a DRM device, you can request minor numbers from DRM
1037 * core. DRM core takes care of major-number management and char-dev
1038 * registration. A stub ->open() callback forwards any open() requests to the
1039 * registered minor.
1042 static int drm_stub_open(struct inode *inode, struct file *filp)
1044 const struct file_operations *new_fops;
1045 struct drm_minor *minor;
1046 int err;
1048 DRM_DEBUG("\n");
1050 mutex_lock(&drm_global_mutex);
1051 minor = drm_minor_acquire(iminor(inode));
1052 if (IS_ERR(minor)) {
1053 err = PTR_ERR(minor);
1054 goto out_unlock;
1057 new_fops = fops_get(minor->dev->driver->fops);
1058 if (!new_fops) {
1059 err = -ENODEV;
1060 goto out_release;
1063 replace_fops(filp, new_fops);
1064 if (filp->f_op->open)
1065 err = filp->f_op->open(inode, filp);
1066 else
1067 err = 0;
1069 out_release:
1070 drm_minor_release(minor);
1071 out_unlock:
1072 mutex_unlock(&drm_global_mutex);
1073 return err;
1076 static const struct file_operations drm_stub_fops = {
1077 .owner = THIS_MODULE,
1078 .open = drm_stub_open,
1079 .llseek = noop_llseek,
1081 #endif
1083 static void drm_core_exit(void)
1085 #if 0
1086 unregister_chrdev(DRM_MAJOR, "drm");
1087 debugfs_remove(drm_debugfs_root);
1088 drm_sysfs_destroy();
1089 #endif
1090 idr_destroy(&drm_minors_idr);
1091 drm_connector_ida_destroy();
1092 drm_global_release();
1095 static int __init drm_core_init(void)
1097 #if 0
1098 int ret;
1099 #endif
1101 drm_global_init();
1102 drm_connector_ida_init();
1103 idr_init(&drm_minors_idr);
1105 #if 0
1106 ret = drm_sysfs_init();
1107 if (ret < 0) {
1108 DRM_ERROR("Cannot create DRM class: %d\n", ret);
1109 goto error;
1112 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1113 if (!drm_debugfs_root) {
1114 ret = -ENOMEM;
1115 DRM_ERROR("Cannot create debugfs-root: %d\n", ret);
1116 goto error;
1119 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1120 if (ret < 0)
1121 goto error;
1122 #endif
1124 drm_core_init_complete = true;
1126 DRM_DEBUG("Initialized\n");
1127 return 0;
1129 #if 0
1130 error:
1131 drm_core_exit();
1132 return ret;
1133 #endif
1136 module_init(drm_core_init);
1137 module_exit(drm_core_exit);
1139 #include <sys/devfs.h>
1141 #include <linux/export.h>
1142 #include <linux/dmi.h>
1143 #include <drm/drmP.h>
1145 static int
1146 drm_modevent(module_t mod, int type, void *data)
1149 switch (type) {
1150 case MOD_LOAD:
1151 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
1152 linux_task_drop_callback = linux_task_drop;
1153 linux_proc_drop_callback = linux_proc_drop;
1154 break;
1155 case MOD_UNLOAD:
1156 linux_task_drop_callback = NULL;
1157 linux_proc_drop_callback = NULL;
1158 break;
1160 return (0);
1163 static moduledata_t drm_mod = {
1164 "drm",
1165 drm_modevent,
1168 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1169 MODULE_VERSION(drm, 1);
1170 MODULE_DEPEND(drm, agp, 1, 1, 1);
1171 MODULE_DEPEND(drm, pci, 1, 1, 1);
1172 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
1174 struct dev_ops drm_cdevsw = {
1175 { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
1176 .d_open = drm_open,
1177 .d_close = drm_close,
1178 .d_read = drm_read,
1179 .d_ioctl = drm_ioctl,
1180 .d_kqfilter = drm_kqfilter,
1181 .d_mmap = drm_mmap,
1182 .d_mmap_single = drm_mmap_single,
1185 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
1186 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
1187 "DRM debugging");
1188 int drm_vma_debug;
1189 SYSCTL_INT(_hw_drm, OID_AUTO, vma_debug, CTLFLAG_RW, &drm_vma_debug, 0,
1190 "DRM debugging");
1192 #if 0
1194 drm_create_cdevs(device_t kdev)
1196 struct drm_device *dev;
1197 int error, unit;
1198 #ifdef __DragonFly__
1199 struct drm_softc *softc = device_get_softc(kdev);
1201 dev = softc->drm_driver_data;
1202 #endif
1203 unit = device_get_unit(kdev);
1205 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1206 DRM_DEV_MODE, "dri/card%d", unit);
1207 error = 0;
1208 if (error == 0)
1209 dev->devnode->si_drv1 = dev;
1210 return (error);
1212 #endif
1214 #ifndef DRM_DEV_NAME
1215 #define DRM_DEV_NAME "drm"
1216 #endif
1218 devclass_t drm_devclass;
1220 /* XXX: this is supposed to be drm_release() */
1221 void drm_cdevpriv_dtor(void *cd)
1223 struct drm_file *file_priv = cd;
1224 struct drm_device *dev = file_priv->dev;
1226 DRM_DEBUG("open_count = %d\n", dev->open_count);
1228 DRM_LOCK(dev);
1230 if (dev->driver->preclose != NULL)
1231 dev->driver->preclose(dev, file_priv);
1233 /* ========================================================
1234 * Begin inline drm_release
1237 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1238 DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1240 if (dev->driver->driver_features & DRIVER_GEM)
1241 drm_gem_release(dev, file_priv);
1243 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1244 drm_legacy_reclaim_buffers(dev, file_priv);
1246 funsetown(&dev->buf_sigio);
1248 if (dev->driver->postclose != NULL)
1249 dev->driver->postclose(dev, file_priv);
1250 list_del(&file_priv->lhead);
1253 /* ========================================================
1254 * End inline drm_release
1257 device_unbusy(dev->dev->bsddev);
1258 if (--dev->open_count == 0) {
1259 drm_lastclose(dev);
1262 DRM_UNLOCK(dev);
1266 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1267 struct sysctl_oid *top)
1269 struct sysctl_oid *oid;
1271 ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1272 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1273 dev->pci_slot, dev->pci_func);
1274 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1275 CTLFLAG_RD, dev->busid_str, 0, NULL);
1276 if (oid == NULL)
1277 return (ENOMEM);
1279 return (0);
1283 drm_mmap_single(struct dev_mmap_single_args *ap)
1285 struct drm_device *dev;
1286 struct cdev *kdev = ap->a_head.a_dev;
1287 vm_ooffset_t *offset = ap->a_offset;
1288 vm_size_t size = ap->a_size;
1289 struct vm_object **obj_res = ap->a_object;
1290 int nprot = ap->a_nprot;
1292 dev = drm_get_device_from_kdev(kdev);
1293 if (dev->drm_ttm_bdev != NULL) {
1294 return (ttm_bo_mmap_single(ap->a_fp, dev,
1295 offset, size, obj_res, nprot));
1296 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1297 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1298 } else {
1299 return (ENODEV);
1303 #include <linux/dmi.h>
1306 * Check if dmi_system_id structure matches system DMI data
1308 static bool
1309 dmi_found(const struct dmi_system_id *dsi)
1311 int i, slot;
1312 bool found = false;
1313 char *sys_vendor, *board_vendor, *product_name, *board_name;
1315 sys_vendor = kgetenv("smbios.system.maker");
1316 board_vendor = kgetenv("smbios.planar.maker");
1317 product_name = kgetenv("smbios.system.product");
1318 board_name = kgetenv("smbios.planar.product");
1320 for (i = 0; i < NELEM(dsi->matches); i++) {
1321 slot = dsi->matches[i].slot;
1322 switch (slot) {
1323 case DMI_NONE:
1324 break;
1325 case DMI_SYS_VENDOR:
1326 if (sys_vendor != NULL &&
1327 !strcmp(sys_vendor, dsi->matches[i].substr))
1328 break;
1329 else
1330 goto done;
1331 case DMI_BOARD_VENDOR:
1332 if (board_vendor != NULL &&
1333 !strcmp(board_vendor, dsi->matches[i].substr))
1334 break;
1335 else
1336 goto done;
1337 case DMI_PRODUCT_NAME:
1338 if (product_name != NULL &&
1339 !strcmp(product_name, dsi->matches[i].substr))
1340 break;
1341 else
1342 goto done;
1343 case DMI_BOARD_NAME:
1344 if (board_name != NULL &&
1345 !strcmp(board_name, dsi->matches[i].substr))
1346 break;
1347 else
1348 goto done;
1349 default:
1350 goto done;
1353 found = true;
1355 done:
1356 if (sys_vendor != NULL)
1357 kfreeenv(sys_vendor);
1358 if (board_vendor != NULL)
1359 kfreeenv(board_vendor);
1360 if (product_name != NULL)
1361 kfreeenv(product_name);
1362 if (board_name != NULL)
1363 kfreeenv(board_name);
1365 return found;
1368 int dmi_check_system(const struct dmi_system_id *sysid)
1370 const struct dmi_system_id *dsi;
1371 int num = 0;
1373 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1374 if (dmi_found(dsi)) {
1375 num++;
1376 if (dsi->callback && dsi->callback(dsi))
1377 break;
1380 return (num);