drm_drv.c revision 1.23 1 /* $NetBSD: drm_drv.c,v 1.23 2022/07/17 14:11:18 riastradh Exp $ */
2
3 /*
4 * Created: Fri Jan 19 10:48:35 2001 by faith (at) acm.org
5 *
6 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
7 * All Rights Reserved.
8 *
9 * Author Rickard E. (Rik) Faith <faith (at) valinux.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: drm_drv.c,v 1.23 2022/07/17 14:11:18 riastradh Exp $");
33
34 #include <linux/debugfs.h>
35 #include <linux/fs.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/mount.h>
39 #include <linux/pseudo_fs.h>
40 #include <linux/slab.h>
41 #include <linux/srcu.h>
42
43 #include <drm/drm_client.h>
44 #include <drm/drm_color_mgmt.h>
45 #include <drm/drm_drv.h>
46 #include <drm/drm_file.h>
47 #include <drm/drm_mode_object.h>
48 #include <drm/drm_print.h>
49
50 #include "drm_crtc_internal.h"
51 #include "drm_internal.h"
52 #include "drm_legacy.h"
53
54 #include <linux/nbsd-namespace.h>
55
56 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, Jos Fonseca, Jon Smirl");
57 MODULE_DESCRIPTION("DRM shared core routines");
58 MODULE_LICENSE("GPL and additional rights");
59
60 #ifdef __NetBSD__
61 spinlock_t drm_minor_lock;
62 struct idr drm_minors_idr;
63 #else
64 static DEFINE_SPINLOCK(drm_minor_lock);
65 static struct idr drm_minors_idr;
66 #endif
67
68 /*
69 * If the drm core fails to init for whatever reason,
70 * we should prevent any drivers from registering with it.
71 * It's best to check this at drm_dev_init(), as some drivers
72 * prefer to embed struct drm_device into their own device
73 * structure and call drm_dev_init() themselves.
74 */
75 bool drm_core_init_complete = false;
76
77 #ifndef __NetBSD__
78 static struct dentry *drm_debugfs_root;
79 #endif
80
81 #ifdef __NetBSD__
82 struct srcu_struct drm_unplug_srcu;
83 #else
84 DEFINE_STATIC_SRCU(drm_unplug_srcu);
85 #endif
86
87 /*
88 * DRM Minors
89 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
90 * of them is represented by a drm_minor object. Depending on the capabilities
91 * of the device-driver, different interfaces are registered.
92 *
93 * Minors can be accessed via dev->$minor_name. This pointer is either
94 * NULL or a valid drm_minor pointer and stays valid as long as the device is
95 * valid. This means, DRM minors have the same life-time as the underlying
96 * device. However, this doesn't mean that the minor is active. Minors are
97 * registered and unregistered dynamically according to device-state.
98 */
99
100 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
101 unsigned int type)
102 {
103 switch (type) {
104 case DRM_MINOR_PRIMARY:
105 return &dev->primary;
106 case DRM_MINOR_RENDER:
107 return &dev->render;
108 default:
109 BUG();
110 }
111 }
112
113 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
114 {
115 struct drm_minor *minor;
116 unsigned long flags;
117 int r;
118
119 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
120 if (!minor)
121 return -ENOMEM;
122
123 minor->type = type;
124 minor->dev = dev;
125
126 idr_preload(GFP_KERNEL);
127 spin_lock_irqsave(&drm_minor_lock, flags);
128 r = idr_alloc(&drm_minors_idr,
129 NULL,
130 64 * type,
131 64 * (type + 1),
132 GFP_NOWAIT);
133 spin_unlock_irqrestore(&drm_minor_lock, flags);
134 idr_preload_end();
135
136 if (r < 0)
137 goto err_free;
138
139 minor->index = r;
140
141 #ifndef __NetBSD__ /* XXX drm sysfs */
142 minor->kdev = drm_sysfs_minor_alloc(minor);
143 if (IS_ERR(minor->kdev)) {
144 r = PTR_ERR(minor->kdev);
145 goto err_index;
146 }
147 #endif
148
149 *drm_minor_get_slot(dev, type) = minor;
150 return 0;
151
152 err_index: __unused
153 spin_lock_irqsave(&drm_minor_lock, flags);
154 idr_remove(&drm_minors_idr, minor->index);
155 spin_unlock_irqrestore(&drm_minor_lock, flags);
156 err_free:
157 kfree(minor);
158 return r;
159 }
160
161 static void drm_minor_free(struct drm_device *dev, unsigned int type)
162 {
163 struct drm_minor **slot, *minor;
164 unsigned long flags;
165
166 slot = drm_minor_get_slot(dev, type);
167 minor = *slot;
168 if (!minor)
169 return;
170
171 #ifndef __NetBSD__ /* XXX drm sysfs */
172 put_device(minor->kdev);
173 #endif
174
175 spin_lock_irqsave(&drm_minor_lock, flags);
176 idr_remove(&drm_minors_idr, minor->index);
177 spin_unlock_irqrestore(&drm_minor_lock, flags);
178
179 kfree(minor);
180 *slot = NULL;
181 }
182
183 static int drm_minor_register(struct drm_device *dev, unsigned int type)
184 {
185 struct drm_minor *minor;
186 unsigned long flags;
187 #ifndef __NetBSD__
188 int ret;
189 #endif
190
191 DRM_DEBUG("\n");
192
193 minor = *drm_minor_get_slot(dev, type);
194 if (!minor)
195 return 0;
196
197 #ifndef __NetBSD__
198 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
199 if (ret) {
200 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
201 goto err_debugfs;
202 }
203
204 ret = device_add(minor->kdev);
205 if (ret)
206 goto err_debugfs;
207 #endif
208
209 /* replace NULL with @minor so lookups will succeed from now on */
210 spin_lock_irqsave(&drm_minor_lock, flags);
211 idr_replace(&drm_minors_idr, minor, minor->index);
212 spin_unlock_irqrestore(&drm_minor_lock, flags);
213
214 DRM_DEBUG("new minor registered %d\n", minor->index);
215 return 0;
216
217 #ifndef __NetBSD__
218 err_debugfs:
219 drm_debugfs_cleanup(minor);
220 return ret;
221 #endif
222 }
223
224 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
225 {
226 struct drm_minor *minor;
227 unsigned long flags;
228
229 minor = *drm_minor_get_slot(dev, type);
230 #ifdef __NetBSD__
231 if (!minor)
232 #else
233 if (!minor || !device_is_registered(minor->kdev))
234 #endif
235 return;
236
237 /* replace @minor with NULL so lookups will fail from now on */
238 spin_lock_irqsave(&drm_minor_lock, flags);
239 idr_replace(&drm_minors_idr, NULL, minor->index);
240 spin_unlock_irqrestore(&drm_minor_lock, flags);
241
242 #ifndef __NetBSD__
243 device_del(minor->kdev);
244 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
245 drm_debugfs_cleanup(minor);
246 #endif
247 }
248
249 /*
250 * Looks up the given minor-ID and returns the respective DRM-minor object. The
251 * refence-count of the underlying device is increased so you must release this
252 * object with drm_minor_release().
253 *
254 * As long as you hold this minor, it is guaranteed that the object and the
255 * minor->dev pointer will stay valid! However, the device may get unplugged and
256 * unregistered while you hold the minor.
257 */
258 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
259 {
260 struct drm_minor *minor;
261 unsigned long flags;
262
263 spin_lock_irqsave(&drm_minor_lock, flags);
264 minor = idr_find(&drm_minors_idr, minor_id);
265 if (minor)
266 drm_dev_get(minor->dev);
267 spin_unlock_irqrestore(&drm_minor_lock, flags);
268
269 if (!minor) {
270 return ERR_PTR(-ENODEV);
271 } else if (drm_dev_is_unplugged(minor->dev)) {
272 drm_dev_put(minor->dev);
273 return ERR_PTR(-ENODEV);
274 }
275
276 return minor;
277 }
278
279 void drm_minor_release(struct drm_minor *minor)
280 {
281 drm_dev_put(minor->dev);
282 }
283
284 /**
285 * DOC: driver instance overview
286 *
287 * A device instance for a drm driver is represented by &struct drm_device. This
288 * is initialized with drm_dev_init(), usually from bus-specific ->probe()
289 * callbacks implemented by the driver. The driver then needs to initialize all
290 * the various subsystems for the drm device like memory management, vblank
291 * handling, modesetting support and intial output configuration plus obviously
292 * initialize all the corresponding hardware bits. Finally when everything is up
293 * and running and ready for userspace the device instance can be published
294 * using drm_dev_register().
295 *
296 * There is also deprecated support for initalizing device instances using
297 * bus-specific helpers and the &drm_driver.load callback. But due to
298 * backwards-compatibility needs the device instance have to be published too
299 * early, which requires unpretty global locking to make safe and is therefore
300 * only support for existing drivers not yet converted to the new scheme.
301 *
302 * When cleaning up a device instance everything needs to be done in reverse:
303 * First unpublish the device instance with drm_dev_unregister(). Then clean up
304 * any other resources allocated at device initialization and drop the driver's
305 * reference to &drm_device using drm_dev_put().
306 *
307 * Note that the lifetime rules for &drm_device instance has still a lot of
308 * historical baggage. Hence use the reference counting provided by
309 * drm_dev_get() and drm_dev_put() only carefully.
310 *
311 * Display driver example
312 * ~~~~~~~~~~~~~~~~~~~~~~
313 *
314 * The following example shows a typical structure of a DRM display driver.
315 * The example focus on the probe() function and the other functions that is
316 * almost always present and serves as a demonstration of devm_drm_dev_init()
317 * usage with its accompanying drm_driver->release callback.
318 *
319 * .. code-block:: c
320 *
321 * struct driver_device {
322 * struct drm_device drm;
323 * void *userspace_facing;
324 * struct clk *pclk;
325 * };
326 *
327 * static void driver_drm_release(struct drm_device *drm)
328 * {
329 * struct driver_device *priv = container_of(...);
330 *
331 * drm_mode_config_cleanup(drm);
332 * drm_dev_fini(drm);
333 * kfree(priv->userspace_facing);
334 * kfree(priv);
335 * }
336 *
337 * static struct drm_driver driver_drm_driver = {
338 * [...]
339 * .release = driver_drm_release,
340 * };
341 *
342 * static int driver_probe(struct platform_device *pdev)
343 * {
344 * struct driver_device *priv;
345 * struct drm_device *drm;
346 * int ret;
347 *
348 * // devm_kzalloc() can't be used here because the drm_device '
349 * // lifetime can exceed the device lifetime if driver unbind
350 * // happens when userspace still has open file descriptors.
351 * priv = kzalloc(sizeof(*priv), GFP_KERNEL);
352 * if (!priv)
353 * return -ENOMEM;
354 *
355 * drm = &priv->drm;
356 *
357 * ret = devm_drm_dev_init(&pdev->dev, drm, &driver_drm_driver);
358 * if (ret) {
359 * kfree(drm);
360 * return ret;
361 * }
362 *
363 * drm_mode_config_init(drm);
364 *
365 * priv->userspace_facing = kzalloc(..., GFP_KERNEL);
366 * if (!priv->userspace_facing)
367 * return -ENOMEM;
368 *
369 * priv->pclk = devm_clk_get(dev, "PCLK");
370 * if (IS_ERR(priv->pclk))
371 * return PTR_ERR(priv->pclk);
372 *
373 * // Further setup, display pipeline etc
374 *
375 * platform_set_drvdata(pdev, drm);
376 *
377 * drm_mode_config_reset(drm);
378 *
379 * ret = drm_dev_register(drm);
380 * if (ret)
381 * return ret;
382 *
383 * drm_fbdev_generic_setup(drm, 32);
384 *
385 * return 0;
386 * }
387 *
388 * // This function is called before the devm_ resources are released
389 * static int driver_remove(struct platform_device *pdev)
390 * {
391 * struct drm_device *drm = platform_get_drvdata(pdev);
392 *
393 * drm_dev_unregister(drm);
394 * drm_atomic_helper_shutdown(drm)
395 *
396 * return 0;
397 * }
398 *
399 * // This function is called on kernel restart and shutdown
400 * static void driver_shutdown(struct platform_device *pdev)
401 * {
402 * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
403 * }
404 *
405 * static int __maybe_unused driver_pm_suspend(struct device *dev)
406 * {
407 * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
408 * }
409 *
410 * static int __maybe_unused driver_pm_resume(struct device *dev)
411 * {
412 * drm_mode_config_helper_resume(dev_get_drvdata(dev));
413 *
414 * return 0;
415 * }
416 *
417 * static const struct dev_pm_ops driver_pm_ops = {
418 * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
419 * };
420 *
421 * static struct platform_driver driver_driver = {
422 * .driver = {
423 * [...]
424 * .pm = &driver_pm_ops,
425 * },
426 * .probe = driver_probe,
427 * .remove = driver_remove,
428 * .shutdown = driver_shutdown,
429 * };
430 * module_platform_driver(driver_driver);
431 *
432 * Drivers that want to support device unplugging (USB, DT overlay unload) should
433 * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
434 * regions that is accessing device resources to prevent use after they're
435 * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
436 * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
437 * drm_atomic_helper_shutdown() is called. This means that if the disable code
438 * paths are protected, they will not run on regular driver module unload,
439 * possibily leaving the hardware enabled.
440 */
441
442 /**
443 * drm_put_dev - Unregister and release a DRM device
444 * @dev: DRM device
445 *
446 * Called at module unload time or when a PCI device is unplugged.
447 *
448 * Cleans up all DRM device, calling drm_lastclose().
449 *
450 * Note: Use of this function is deprecated. It will eventually go away
451 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
452 * instead to make sure that the device isn't userspace accessible any more
453 * while teardown is in progress, ensuring that userspace can't access an
454 * inconsistent state.
455 */
456 void drm_put_dev(struct drm_device *dev)
457 {
458 DRM_DEBUG("\n");
459
460 if (!dev) {
461 DRM_ERROR("cleanup called no dev\n");
462 return;
463 }
464
465 drm_dev_unregister(dev);
466 drm_dev_put(dev);
467 }
468 EXPORT_SYMBOL(drm_put_dev);
469
470 /**
471 * drm_dev_enter - Enter device critical section
472 * @dev: DRM device
473 * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
474 *
475 * This function marks and protects the beginning of a section that should not
476 * be entered after the device has been unplugged. The section end is marked
477 * with drm_dev_exit(). Calls to this function can be nested.
478 *
479 * Returns:
480 * True if it is OK to enter the section, false otherwise.
481 */
482 bool drm_dev_enter(struct drm_device *dev, int *idx)
483 {
484 *idx = srcu_read_lock(&drm_unplug_srcu);
485
486 if (dev->unplugged) {
487 srcu_read_unlock(&drm_unplug_srcu, *idx);
488 return false;
489 }
490
491 return true;
492 }
493 EXPORT_SYMBOL(drm_dev_enter);
494
495 /**
496 * drm_dev_exit - Exit device critical section
497 * @idx: index returned from drm_dev_enter()
498 *
499 * This function marks the end of a section that should not be entered after
500 * the device has been unplugged.
501 */
502 void drm_dev_exit(int idx)
503 {
504 srcu_read_unlock(&drm_unplug_srcu, idx);
505 }
506 EXPORT_SYMBOL(drm_dev_exit);
507
508 /**
509 * drm_dev_unplug - unplug a DRM device
510 * @dev: DRM device
511 *
512 * This unplugs a hotpluggable DRM device, which makes it inaccessible to
513 * userspace operations. Entry-points can use drm_dev_enter() and
514 * drm_dev_exit() to protect device resources in a race free manner. This
515 * essentially unregisters the device like drm_dev_unregister(), but can be
516 * called while there are still open users of @dev.
517 */
518 void drm_dev_unplug(struct drm_device *dev)
519 {
520 /*
521 * After synchronizing any critical read section is guaranteed to see
522 * the new value of ->unplugged, and any critical section which might
523 * still have seen the old value of ->unplugged is guaranteed to have
524 * finished.
525 */
526 dev->unplugged = true;
527 synchronize_srcu(&drm_unplug_srcu);
528
529 drm_dev_unregister(dev);
530 }
531 EXPORT_SYMBOL(drm_dev_unplug);
532
533 #ifdef __NetBSD__
534
535 static void *
536 drm_fs_inode_new(void)
537 {
538 return NULL;
539 }
540
541 static void
542 drm_fs_inode_free(void *inode)
543 {
544 KASSERT(inode == NULL);
545 }
546
547 #else
548
549 /*
550 * DRM internal mount
551 * We want to be able to allocate our own "struct address_space" to control
552 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
553 * stand-alone address_space objects, so we need an underlying inode. As there
554 * is no way to allocate an independent inode easily, we need a fake internal
555 * VFS mount-point.
556 *
557 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
558 * frees it again. You are allowed to use iget() and iput() to get references to
559 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
560 * drm_fs_inode_free() call (which does not have to be the last iput()).
561 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
562 * between multiple inode-users. You could, technically, call
563 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
564 * iput(), but this way you'd end up with a new vfsmount for each inode.
565 */
566
567 static int drm_fs_cnt;
568 static struct vfsmount *drm_fs_mnt;
569
570 static int drm_fs_init_fs_context(struct fs_context *fc)
571 {
572 return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
573 }
574
575 static struct file_system_type drm_fs_type = {
576 .name = "drm",
577 .owner = THIS_MODULE,
578 .init_fs_context = drm_fs_init_fs_context,
579 .kill_sb = kill_anon_super,
580 };
581
582 static struct inode *drm_fs_inode_new(void)
583 {
584 struct inode *inode;
585 int r;
586
587 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
588 if (r < 0) {
589 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
590 return ERR_PTR(r);
591 }
592
593 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
594 if (IS_ERR(inode))
595 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
596
597 return inode;
598 }
599
600 static void drm_fs_inode_free(struct inode *inode)
601 {
602 if (inode) {
603 iput(inode);
604 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
605 }
606 }
607
608 #endif
609
610 /**
611 * DOC: component helper usage recommendations
612 *
613 * DRM drivers that drive hardware where a logical device consists of a pile of
614 * independent hardware blocks are recommended to use the :ref:`component helper
615 * library<component>`. For consistency and better options for code reuse the
616 * following guidelines apply:
617 *
618 * - The entire device initialization procedure should be run from the
619 * &component_master_ops.master_bind callback, starting with drm_dev_init(),
620 * then binding all components with component_bind_all() and finishing with
621 * drm_dev_register().
622 *
623 * - The opaque pointer passed to all components through component_bind_all()
624 * should point at &struct drm_device of the device instance, not some driver
625 * specific private structure.
626 *
627 * - The component helper fills the niche where further standardization of
628 * interfaces is not practical. When there already is, or will be, a
629 * standardized interface like &drm_bridge or &drm_panel, providing its own
630 * functions to find such components at driver load time, like
631 * drm_of_find_panel_or_bridge(), then the component helper should not be
632 * used.
633 */
634
635 /**
636 * drm_dev_init - Initialise new DRM device
637 * @dev: DRM device
638 * @driver: DRM driver
639 * @parent: Parent device object
640 *
641 * Initialize a new DRM device. No device registration is done.
642 * Call drm_dev_register() to advertice the device to user space and register it
643 * with other core subsystems. This should be done last in the device
644 * initialization sequence to make sure userspace can't access an inconsistent
645 * state.
646 *
647 * The initial ref-count of the object is 1. Use drm_dev_get() and
648 * drm_dev_put() to take and drop further ref-counts.
649 *
650 * It is recommended that drivers embed &struct drm_device into their own device
651 * structure.
652 *
653 * Drivers that do not want to allocate their own device struct
654 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers
655 * that do embed &struct drm_device it must be placed first in the overall
656 * structure, and the overall structure must be allocated using kmalloc(): The
657 * drm core's release function unconditionally calls kfree() on the @dev pointer
658 * when the final reference is released. To override this behaviour, and so
659 * allow embedding of the drm_device inside the driver's device struct at an
660 * arbitrary offset, you must supply a &drm_driver.release callback and control
661 * the finalization explicitly.
662 *
663 * RETURNS:
664 * 0 on success, or error code on failure.
665 */
666 int drm_dev_init(struct drm_device *dev,
667 struct drm_driver *driver,
668 struct device *parent)
669 {
670 int ret;
671
672 if (!drm_core_init_complete) {
673 DRM_ERROR("DRM core is not initialized\n");
674 return -ENODEV;
675 }
676
677 if (WARN_ON(!parent))
678 return -EINVAL;
679
680 kref_init(&dev->ref);
681 dev->dev = get_device(parent);
682 dev->driver = driver;
683
684 /* no per-device feature limits by default */
685 dev->driver_features = ~0u;
686
687 drm_legacy_init_members(dev);
688 INIT_LIST_HEAD(&dev->filelist);
689 INIT_LIST_HEAD(&dev->filelist_internal);
690 INIT_LIST_HEAD(&dev->clientlist);
691 INIT_LIST_HEAD(&dev->vblank_event_list);
692
693 spin_lock_init(&dev->event_lock);
694 mutex_init(&dev->struct_mutex);
695 mutex_init(&dev->filelist_mutex);
696 mutex_init(&dev->clientlist_mutex);
697 mutex_init(&dev->master_mutex);
698
699 dev->sc_monitor_hotplug.smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
700 dev->sc_monitor_hotplug.smpsw_type = PSWITCH_TYPE_HOTKEY;
701 ret = sysmon_pswitch_register(&dev->sc_monitor_hotplug);
702 if (ret)
703 goto err_pswitch;
704
705 dev->anon_inode = drm_fs_inode_new();
706 if (IS_ERR(dev->anon_inode)) {
707 ret = PTR_ERR(dev->anon_inode);
708 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
709 goto err_free;
710 }
711
712 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
713 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
714 if (ret)
715 goto err_minors;
716 }
717
718 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
719 if (ret)
720 goto err_minors;
721
722 ret = drm_legacy_create_map_hash(dev);
723 if (ret)
724 goto err_minors;
725
726 drm_legacy_ctxbitmap_init(dev);
727
728 if (drm_core_check_feature(dev, DRIVER_GEM)) {
729 ret = drm_gem_init(dev);
730 if (ret) {
731 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
732 goto err_ctxbitmap;
733 }
734 }
735
736 ret = drm_dev_set_unique(dev, dev_name(parent));
737 if (ret)
738 goto err_setunique;
739
740 return 0;
741
742 err_setunique:
743 if (drm_core_check_feature(dev, DRIVER_GEM))
744 drm_gem_destroy(dev);
745 err_ctxbitmap:
746 drm_legacy_ctxbitmap_cleanup(dev);
747 drm_legacy_remove_map_hash(dev);
748 err_minors:
749 drm_minor_free(dev, DRM_MINOR_PRIMARY);
750 drm_minor_free(dev, DRM_MINOR_RENDER);
751 drm_fs_inode_free(dev->anon_inode);
752 err_free:
753 #ifdef __NetBSD__
754 sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
755 err_pswitch:
756 #endif
757 #ifndef __NetBSD__ /* XXX drm sysfs */
758 put_device(dev->dev);
759 #endif
760 mutex_destroy(&dev->master_mutex);
761 mutex_destroy(&dev->clientlist_mutex);
762 mutex_destroy(&dev->filelist_mutex);
763 mutex_destroy(&dev->struct_mutex);
764 spin_lock_destroy(&dev->event_lock);
765 drm_legacy_destroy_members(dev);
766 return ret;
767 }
768 EXPORT_SYMBOL(drm_dev_init);
769
770 #ifndef __NetBSD__
771
772 static void devm_drm_dev_init_release(void *data)
773 {
774 drm_dev_put(data);
775 }
776
777 /**
778 * devm_drm_dev_init - Resource managed drm_dev_init()
779 * @parent: Parent device object
780 * @dev: DRM device
781 * @driver: DRM driver
782 *
783 * Managed drm_dev_init(). The DRM device initialized with this function is
784 * automatically put on driver detach using drm_dev_put(). You must supply a
785 * &drm_driver.release callback to control the finalization explicitly.
786 *
787 * RETURNS:
788 * 0 on success, or error code on failure.
789 */
790 int devm_drm_dev_init(struct device *parent,
791 struct drm_device *dev,
792 struct drm_driver *driver)
793 {
794 int ret;
795
796 if (WARN_ON(!driver->release))
797 return -EINVAL;
798
799 ret = drm_dev_init(dev, driver, parent);
800 if (ret)
801 return ret;
802
803 ret = devm_add_action(parent, devm_drm_dev_init_release, dev);
804 if (ret)
805 devm_drm_dev_init_release(dev);
806
807 return ret;
808 }
809 EXPORT_SYMBOL(devm_drm_dev_init);
810
811 #endif
812
813 /**
814 * drm_dev_fini - Finalize a dead DRM device
815 * @dev: DRM device
816 *
817 * Finalize a dead DRM device. This is the converse to drm_dev_init() and
818 * frees up all data allocated by it. All driver private data should be
819 * finalized first. Note that this function does not free the @dev, that is
820 * left to the caller.
821 *
822 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
823 * from a &drm_driver.release callback.
824 */
825 void drm_dev_fini(struct drm_device *dev)
826 {
827 drm_vblank_cleanup(dev);
828
829 if (drm_core_check_feature(dev, DRIVER_GEM))
830 drm_gem_destroy(dev);
831
832 drm_legacy_ctxbitmap_cleanup(dev);
833 drm_legacy_remove_map_hash(dev);
834 drm_fs_inode_free(dev->anon_inode);
835
836 drm_minor_free(dev, DRM_MINOR_PRIMARY);
837 drm_minor_free(dev, DRM_MINOR_RENDER);
838
839 #ifdef __NetBSD__
840 sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
841 #endif
842
843 #ifndef __NetBSD__ /* XXX drm sysfs */
844 put_device(dev->dev);
845 #endif
846
847 mutex_destroy(&dev->master_mutex);
848 mutex_destroy(&dev->clientlist_mutex);
849 mutex_destroy(&dev->filelist_mutex);
850 mutex_destroy(&dev->struct_mutex);
851 spin_lock_destroy(&dev->event_lock);
852 drm_legacy_destroy_members(dev);
853 kfree(dev->unique);
854 }
855 EXPORT_SYMBOL(drm_dev_fini);
856
857 /**
858 * drm_dev_alloc - Allocate new DRM device
859 * @driver: DRM driver to allocate device for
860 * @parent: Parent device object
861 *
862 * Allocate and initialize a new DRM device. No device registration is done.
863 * Call drm_dev_register() to advertice the device to user space and register it
864 * with other core subsystems. This should be done last in the device
865 * initialization sequence to make sure userspace can't access an inconsistent
866 * state.
867 *
868 * The initial ref-count of the object is 1. Use drm_dev_get() and
869 * drm_dev_put() to take and drop further ref-counts.
870 *
871 * Note that for purely virtual devices @parent can be NULL.
872 *
873 * Drivers that wish to subclass or embed &struct drm_device into their
874 * own struct should look at using drm_dev_init() instead.
875 *
876 * RETURNS:
877 * Pointer to new DRM device, or ERR_PTR on failure.
878 */
879 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
880 struct device *parent)
881 {
882 struct drm_device *dev;
883 int ret;
884
885 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
886 if (!dev)
887 return ERR_PTR(-ENOMEM);
888
889 ret = drm_dev_init(dev, driver, parent);
890 if (ret) {
891 kfree(dev);
892 return ERR_PTR(ret);
893 }
894
895 return dev;
896 }
897 EXPORT_SYMBOL(drm_dev_alloc);
898
899 static void drm_dev_release(struct kref *ref)
900 {
901 struct drm_device *dev = container_of(ref, struct drm_device, ref);
902
903 if (dev->driver->release) {
904 dev->driver->release(dev);
905 } else {
906 drm_dev_fini(dev);
907 kfree(dev);
908 }
909 }
910
911 /**
912 * drm_dev_get - Take reference of a DRM device
913 * @dev: device to take reference of or NULL
914 *
915 * This increases the ref-count of @dev by one. You *must* already own a
916 * reference when calling this. Use drm_dev_put() to drop this reference
917 * again.
918 *
919 * This function never fails. However, this function does not provide *any*
920 * guarantee whether the device is alive or running. It only provides a
921 * reference to the object and the memory associated with it.
922 */
923 void drm_dev_get(struct drm_device *dev)
924 {
925 if (dev)
926 kref_get(&dev->ref);
927 }
928 EXPORT_SYMBOL(drm_dev_get);
929
930 /**
931 * drm_dev_put - Drop reference of a DRM device
932 * @dev: device to drop reference of or NULL
933 *
934 * This decreases the ref-count of @dev by one. The device is destroyed if the
935 * ref-count drops to zero.
936 */
937 void drm_dev_put(struct drm_device *dev)
938 {
939 if (dev)
940 kref_put(&dev->ref, drm_dev_release);
941 }
942 EXPORT_SYMBOL(drm_dev_put);
943
944 static int create_compat_control_link(struct drm_device *dev)
945 {
946 struct drm_minor *minor;
947 char *name;
948 int ret;
949
950 if (!drm_core_check_feature(dev, DRIVER_MODESET))
951 return 0;
952
953 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
954 if (!minor)
955 return 0;
956
957 /*
958 * Some existing userspace out there uses the existing of the controlD*
959 * sysfs files to figure out whether it's a modeset driver. It only does
960 * readdir, hence a symlink is sufficient (and the least confusing
961 * option). Otherwise controlD* is entirely unused.
962 *
963 * Old controlD chardev have been allocated in the range
964 * 64-127.
965 */
966 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
967 if (!name)
968 return -ENOMEM;
969
970 #ifdef __NetBSD__ /* XXX sysfs */
971 ret = 0;
972 #else
973 ret = sysfs_create_link(minor->kdev->kobj.parent,
974 &minor->kdev->kobj,
975 name);
976 #endif
977
978 kfree(name);
979
980 return ret;
981 }
982
983 static void remove_compat_control_link(struct drm_device *dev)
984 {
985 struct drm_minor *minor;
986 char *name;
987
988 if (!drm_core_check_feature(dev, DRIVER_MODESET))
989 return;
990
991 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
992 if (!minor)
993 return;
994
995 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
996 if (!name)
997 return;
998
999 #ifndef __NetBSD__ /* XXX sysfs */
1000 sysfs_remove_link(minor->kdev->kobj.parent, name);
1001 #endif
1002
1003 kfree(name);
1004 }
1005
1006 /**
1007 * drm_dev_register - Register DRM device
1008 * @dev: Device to register
1009 * @flags: Flags passed to the driver's .load() function
1010 *
1011 * Register the DRM device @dev with the system, advertise device to user-space
1012 * and start normal device operation. @dev must be initialized via drm_dev_init()
1013 * previously.
1014 *
1015 * Never call this twice on any device!
1016 *
1017 * NOTE: To ensure backward compatibility with existing drivers method this
1018 * function calls the &drm_driver.load method after registering the device
1019 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
1020 * therefore deprecated, drivers must perform all initialization before calling
1021 * drm_dev_register().
1022 *
1023 * RETURNS:
1024 * 0 on success, negative error code on failure.
1025 */
1026 int drm_dev_register(struct drm_device *dev, unsigned long flags)
1027 {
1028 struct drm_driver *driver = dev->driver;
1029 int ret;
1030
1031 #ifndef __NetBSD__
1032 mutex_lock(&drm_global_mutex);
1033 #endif
1034
1035 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
1036 if (ret)
1037 goto err_minors;
1038
1039 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
1040 if (ret)
1041 goto err_minors;
1042
1043 ret = create_compat_control_link(dev);
1044 if (ret)
1045 goto err_minors;
1046
1047 dev->registered = true;
1048
1049 if (dev->driver->load) {
1050 ret = dev->driver->load(dev, flags);
1051 if (ret)
1052 goto err_minors;
1053 }
1054
1055 if (drm_core_check_feature(dev, DRIVER_MODESET))
1056 drm_modeset_register_all(dev);
1057
1058 ret = 0;
1059
1060 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
1061 driver->name, driver->major, driver->minor,
1062 driver->patchlevel, driver->date,
1063 dev->dev ? dev_name(dev->dev) : "virtual device",
1064 dev->primary->index);
1065
1066 goto out_unlock;
1067
1068 err_minors:
1069 remove_compat_control_link(dev);
1070 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1071 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1072 out_unlock:
1073 #ifndef __NetBSD__
1074 mutex_unlock(&drm_global_mutex);
1075 #endif
1076 return ret;
1077 }
1078 EXPORT_SYMBOL(drm_dev_register);
1079
1080 /**
1081 * drm_dev_unregister - Unregister DRM device
1082 * @dev: Device to unregister
1083 *
1084 * Unregister the DRM device from the system. This does the reverse of
1085 * drm_dev_register() but does not deallocate the device. The caller must call
1086 * drm_dev_put() to drop their final reference.
1087 *
1088 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
1089 * which can be called while there are still open users of @dev.
1090 *
1091 * This should be called first in the device teardown code to make sure
1092 * userspace can't access the device instance any more.
1093 */
1094 void drm_dev_unregister(struct drm_device *dev)
1095 {
1096 if (drm_core_check_feature(dev, DRIVER_LEGACY))
1097 drm_lastclose(dev);
1098
1099 dev->registered = false;
1100
1101 drm_client_dev_unregister(dev);
1102
1103 if (drm_core_check_feature(dev, DRIVER_MODESET))
1104 drm_modeset_unregister_all(dev);
1105
1106 if (dev->driver->unload)
1107 dev->driver->unload(dev);
1108
1109 #ifndef __NetBSD__ /* Moved to drm_pci. */
1110 if (dev->agp)
1111 drm_pci_agp_destroy(dev);
1112 #endif
1113
1114 drm_legacy_rmmaps(dev);
1115
1116 remove_compat_control_link(dev);
1117 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1118 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1119 }
1120 EXPORT_SYMBOL(drm_dev_unregister);
1121
1122 /**
1123 * drm_dev_set_unique - Set the unique name of a DRM device
1124 * @dev: device of which to set the unique name
1125 * @name: unique name
1126 *
1127 * Sets the unique name of a DRM device using the specified string. This is
1128 * already done by drm_dev_init(), drivers should only override the default
1129 * unique name for backwards compatibility reasons.
1130 *
1131 * Return: 0 on success or a negative error code on failure.
1132 */
1133 int drm_dev_set_unique(struct drm_device *dev, const char *name)
1134 {
1135 kfree(dev->unique);
1136 dev->unique = kstrdup(name, GFP_KERNEL);
1137
1138 return dev->unique ? 0 : -ENOMEM;
1139 }
1140 EXPORT_SYMBOL(drm_dev_set_unique);
1141
1142 #ifndef __NetBSD__
1143
1144 /*
1145 * DRM Core
1146 * The DRM core module initializes all global DRM objects and makes them
1147 * available to drivers. Once setup, drivers can probe their respective
1148 * devices.
1149 * Currently, core management includes:
1150 * - The "DRM-Global" key/value database
1151 * - Global ID management for connectors
1152 * - DRM major number allocation
1153 * - DRM minor management
1154 * - DRM sysfs class
1155 * - DRM debugfs root
1156 *
1157 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1158 * interface registered on a DRM device, you can request minor numbers from DRM
1159 * core. DRM core takes care of major-number management and char-dev
1160 * registration. A stub ->open() callback forwards any open() requests to the
1161 * registered minor.
1162 */
1163
1164 static int drm_stub_open(struct inode *inode, struct file *filp)
1165 {
1166 const struct file_operations *new_fops;
1167 struct drm_minor *minor;
1168 int err;
1169
1170 DRM_DEBUG("\n");
1171
1172 mutex_lock(&drm_global_mutex);
1173 minor = drm_minor_acquire(iminor(inode));
1174 if (IS_ERR(minor)) {
1175 err = PTR_ERR(minor);
1176 goto out_unlock;
1177 }
1178
1179 new_fops = fops_get(minor->dev->driver->fops);
1180 if (!new_fops) {
1181 err = -ENODEV;
1182 goto out_release;
1183 }
1184
1185 replace_fops(filp, new_fops);
1186 if (filp->f_op->open)
1187 err = filp->f_op->open(inode, filp);
1188 else
1189 err = 0;
1190
1191 out_release:
1192 drm_minor_release(minor);
1193 out_unlock:
1194 mutex_unlock(&drm_global_mutex);
1195 return err;
1196 }
1197
1198 static const struct file_operations drm_stub_fops = {
1199 .owner = THIS_MODULE,
1200 .open = drm_stub_open,
1201 .llseek = noop_llseek,
1202 };
1203
1204 static void drm_core_exit(void)
1205 {
1206 unregister_chrdev(DRM_MAJOR, "drm");
1207 debugfs_remove(drm_debugfs_root);
1208 drm_sysfs_destroy();
1209 idr_destroy(&drm_minors_idr);
1210 drm_connector_ida_destroy();
1211 }
1212
1213 static int __init drm_core_init(void)
1214 {
1215 int ret;
1216
1217 drm_connector_ida_init();
1218 idr_init(&drm_minors_idr);
1219
1220 ret = drm_sysfs_init();
1221 if (ret < 0) {
1222 DRM_ERROR("Cannot create DRM class: %d\n", ret);
1223 goto error;
1224 }
1225
1226 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1227
1228 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1229 if (ret < 0)
1230 goto error;
1231
1232 drm_core_init_complete = true;
1233
1234 DRM_DEBUG("Initialized\n");
1235 return 0;
1236
1237 error:
1238 drm_core_exit();
1239 return ret;
1240 }
1241
1242 module_init(drm_core_init);
1243 module_exit(drm_core_exit);
1244
1245 #endif
1246