1 /* $NetBSD: drm_drv.c,v 1.24 2022/10/15 15:19:28 riastradh Exp $ */
2
3 /*
4 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
5 *
6 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
7 * All Rights Reserved.
8 *
9 * Author Rickard E. (Rik) Faith <faith@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.24 2022/10/15 15:19:28 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
drm_minor_get_slot(struct drm_device * dev,unsigned int type)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
drm_minor_alloc(struct drm_device * dev,unsigned int type)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
drm_minor_free(struct drm_device * dev,unsigned int type)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
drm_minor_register(struct drm_device * dev,unsigned int type)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
drm_minor_unregister(struct drm_device * dev,unsigned int type)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 */
drm_minor_acquire(unsigned int minor_id)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
drm_minor_release(struct drm_minor * minor)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 */
drm_put_dev(struct drm_device * dev)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 */
drm_dev_enter(struct drm_device * dev,int * idx)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 */
drm_dev_exit(int idx)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 */
drm_dev_unplug(struct drm_device * dev)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 *
drm_fs_inode_new(void)536 drm_fs_inode_new(void)
537 {
538 return NULL;
539 }
540
541 static void
drm_fs_inode_free(void * inode)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
drm_fs_init_fs_context(struct fs_context * fc)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
drm_fs_inode_new(void)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
drm_fs_inode_free(struct inode * inode)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 */
drm_dev_init(struct drm_device * dev,struct drm_driver * driver,struct device * parent)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 #ifdef __NetBSD__
699 mutex_init(&dev->suspend_lock);
700 DRM_INIT_WAITQUEUE(&dev->suspend_cv, "drmsusp");
701 dev->active_ioctls = 0;
702 dev->suspender = NULL;
703 #endif
704
705 dev->sc_monitor_hotplug.smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
706 dev->sc_monitor_hotplug.smpsw_type = PSWITCH_TYPE_HOTKEY;
707 ret = sysmon_pswitch_register(&dev->sc_monitor_hotplug);
708 if (ret)
709 goto err_pswitch;
710
711 dev->anon_inode = drm_fs_inode_new();
712 if (IS_ERR(dev->anon_inode)) {
713 ret = PTR_ERR(dev->anon_inode);
714 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
715 goto err_free;
716 }
717
718 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
719 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
720 if (ret)
721 goto err_minors;
722 }
723
724 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
725 if (ret)
726 goto err_minors;
727
728 ret = drm_legacy_create_map_hash(dev);
729 if (ret)
730 goto err_minors;
731
732 drm_legacy_ctxbitmap_init(dev);
733
734 if (drm_core_check_feature(dev, DRIVER_GEM)) {
735 ret = drm_gem_init(dev);
736 if (ret) {
737 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
738 goto err_ctxbitmap;
739 }
740 }
741
742 ret = drm_dev_set_unique(dev, dev_name(parent));
743 if (ret)
744 goto err_setunique;
745
746 return 0;
747
748 err_setunique:
749 if (drm_core_check_feature(dev, DRIVER_GEM))
750 drm_gem_destroy(dev);
751 err_ctxbitmap:
752 drm_legacy_ctxbitmap_cleanup(dev);
753 drm_legacy_remove_map_hash(dev);
754 err_minors:
755 drm_minor_free(dev, DRM_MINOR_PRIMARY);
756 drm_minor_free(dev, DRM_MINOR_RENDER);
757 drm_fs_inode_free(dev->anon_inode);
758 err_free:
759 #ifdef __NetBSD__
760 sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
761 err_pswitch:
762 #endif
763 #ifndef __NetBSD__ /* XXX drm sysfs */
764 put_device(dev->dev);
765 #endif
766 #ifdef __NetBSD__
767 KASSERT(dev->suspender == NULL);
768 KASSERT(dev->active_ioctls == 0);
769 DRM_DESTROY_WAITQUEUE(&dev->suspend_cv);
770 mutex_destroy(&dev->suspend_lock);
771 #endif
772 mutex_destroy(&dev->master_mutex);
773 mutex_destroy(&dev->clientlist_mutex);
774 mutex_destroy(&dev->filelist_mutex);
775 mutex_destroy(&dev->struct_mutex);
776 spin_lock_destroy(&dev->event_lock);
777 drm_legacy_destroy_members(dev);
778 return ret;
779 }
780 EXPORT_SYMBOL(drm_dev_init);
781
782 #ifndef __NetBSD__
783
devm_drm_dev_init_release(void * data)784 static void devm_drm_dev_init_release(void *data)
785 {
786 drm_dev_put(data);
787 }
788
789 /**
790 * devm_drm_dev_init - Resource managed drm_dev_init()
791 * @parent: Parent device object
792 * @dev: DRM device
793 * @driver: DRM driver
794 *
795 * Managed drm_dev_init(). The DRM device initialized with this function is
796 * automatically put on driver detach using drm_dev_put(). You must supply a
797 * &drm_driver.release callback to control the finalization explicitly.
798 *
799 * RETURNS:
800 * 0 on success, or error code on failure.
801 */
devm_drm_dev_init(struct device * parent,struct drm_device * dev,struct drm_driver * driver)802 int devm_drm_dev_init(struct device *parent,
803 struct drm_device *dev,
804 struct drm_driver *driver)
805 {
806 int ret;
807
808 if (WARN_ON(!driver->release))
809 return -EINVAL;
810
811 ret = drm_dev_init(dev, driver, parent);
812 if (ret)
813 return ret;
814
815 ret = devm_add_action(parent, devm_drm_dev_init_release, dev);
816 if (ret)
817 devm_drm_dev_init_release(dev);
818
819 return ret;
820 }
821 EXPORT_SYMBOL(devm_drm_dev_init);
822
823 #endif
824
825 /**
826 * drm_dev_fini - Finalize a dead DRM device
827 * @dev: DRM device
828 *
829 * Finalize a dead DRM device. This is the converse to drm_dev_init() and
830 * frees up all data allocated by it. All driver private data should be
831 * finalized first. Note that this function does not free the @dev, that is
832 * left to the caller.
833 *
834 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
835 * from a &drm_driver.release callback.
836 */
drm_dev_fini(struct drm_device * dev)837 void drm_dev_fini(struct drm_device *dev)
838 {
839 drm_vblank_cleanup(dev);
840
841 if (drm_core_check_feature(dev, DRIVER_GEM))
842 drm_gem_destroy(dev);
843
844 drm_legacy_ctxbitmap_cleanup(dev);
845 drm_legacy_remove_map_hash(dev);
846 drm_fs_inode_free(dev->anon_inode);
847
848 drm_minor_free(dev, DRM_MINOR_PRIMARY);
849 drm_minor_free(dev, DRM_MINOR_RENDER);
850
851 #ifdef __NetBSD__
852 sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
853 #endif
854
855 #ifndef __NetBSD__ /* XXX drm sysfs */
856 put_device(dev->dev);
857 #endif
858
859 #ifdef __NetBSD__
860 KASSERT(dev->suspender == NULL);
861 KASSERT(dev->active_ioctls == 0);
862 DRM_DESTROY_WAITQUEUE(&dev->suspend_cv);
863 mutex_destroy(&dev->suspend_lock);
864 #endif
865
866 mutex_destroy(&dev->master_mutex);
867 mutex_destroy(&dev->clientlist_mutex);
868 mutex_destroy(&dev->filelist_mutex);
869 mutex_destroy(&dev->struct_mutex);
870 spin_lock_destroy(&dev->event_lock);
871 drm_legacy_destroy_members(dev);
872 kfree(dev->unique);
873 }
874 EXPORT_SYMBOL(drm_dev_fini);
875
876 /**
877 * drm_dev_alloc - Allocate new DRM device
878 * @driver: DRM driver to allocate device for
879 * @parent: Parent device object
880 *
881 * Allocate and initialize a new DRM device. No device registration is done.
882 * Call drm_dev_register() to advertice the device to user space and register it
883 * with other core subsystems. This should be done last in the device
884 * initialization sequence to make sure userspace can't access an inconsistent
885 * state.
886 *
887 * The initial ref-count of the object is 1. Use drm_dev_get() and
888 * drm_dev_put() to take and drop further ref-counts.
889 *
890 * Note that for purely virtual devices @parent can be NULL.
891 *
892 * Drivers that wish to subclass or embed &struct drm_device into their
893 * own struct should look at using drm_dev_init() instead.
894 *
895 * RETURNS:
896 * Pointer to new DRM device, or ERR_PTR on failure.
897 */
drm_dev_alloc(struct drm_driver * driver,struct device * parent)898 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
899 struct device *parent)
900 {
901 struct drm_device *dev;
902 int ret;
903
904 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
905 if (!dev)
906 return ERR_PTR(-ENOMEM);
907
908 ret = drm_dev_init(dev, driver, parent);
909 if (ret) {
910 kfree(dev);
911 return ERR_PTR(ret);
912 }
913
914 return dev;
915 }
916 EXPORT_SYMBOL(drm_dev_alloc);
917
drm_dev_release(struct kref * ref)918 static void drm_dev_release(struct kref *ref)
919 {
920 struct drm_device *dev = container_of(ref, struct drm_device, ref);
921
922 if (dev->driver->release) {
923 dev->driver->release(dev);
924 } else {
925 drm_dev_fini(dev);
926 kfree(dev);
927 }
928 }
929
930 /**
931 * drm_dev_get - Take reference of a DRM device
932 * @dev: device to take reference of or NULL
933 *
934 * This increases the ref-count of @dev by one. You *must* already own a
935 * reference when calling this. Use drm_dev_put() to drop this reference
936 * again.
937 *
938 * This function never fails. However, this function does not provide *any*
939 * guarantee whether the device is alive or running. It only provides a
940 * reference to the object and the memory associated with it.
941 */
drm_dev_get(struct drm_device * dev)942 void drm_dev_get(struct drm_device *dev)
943 {
944 if (dev)
945 kref_get(&dev->ref);
946 }
947 EXPORT_SYMBOL(drm_dev_get);
948
949 /**
950 * drm_dev_put - Drop reference of a DRM device
951 * @dev: device to drop reference of or NULL
952 *
953 * This decreases the ref-count of @dev by one. The device is destroyed if the
954 * ref-count drops to zero.
955 */
drm_dev_put(struct drm_device * dev)956 void drm_dev_put(struct drm_device *dev)
957 {
958 if (dev)
959 kref_put(&dev->ref, drm_dev_release);
960 }
961 EXPORT_SYMBOL(drm_dev_put);
962
create_compat_control_link(struct drm_device * dev)963 static int create_compat_control_link(struct drm_device *dev)
964 {
965 struct drm_minor *minor;
966 char *name;
967 int ret;
968
969 if (!drm_core_check_feature(dev, DRIVER_MODESET))
970 return 0;
971
972 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
973 if (!minor)
974 return 0;
975
976 /*
977 * Some existing userspace out there uses the existing of the controlD*
978 * sysfs files to figure out whether it's a modeset driver. It only does
979 * readdir, hence a symlink is sufficient (and the least confusing
980 * option). Otherwise controlD* is entirely unused.
981 *
982 * Old controlD chardev have been allocated in the range
983 * 64-127.
984 */
985 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
986 if (!name)
987 return -ENOMEM;
988
989 #ifdef __NetBSD__ /* XXX sysfs */
990 ret = 0;
991 #else
992 ret = sysfs_create_link(minor->kdev->kobj.parent,
993 &minor->kdev->kobj,
994 name);
995 #endif
996
997 kfree(name);
998
999 return ret;
1000 }
1001
remove_compat_control_link(struct drm_device * dev)1002 static void remove_compat_control_link(struct drm_device *dev)
1003 {
1004 struct drm_minor *minor;
1005 char *name;
1006
1007 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1008 return;
1009
1010 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
1011 if (!minor)
1012 return;
1013
1014 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
1015 if (!name)
1016 return;
1017
1018 #ifndef __NetBSD__ /* XXX sysfs */
1019 sysfs_remove_link(minor->kdev->kobj.parent, name);
1020 #endif
1021
1022 kfree(name);
1023 }
1024
1025 /**
1026 * drm_dev_register - Register DRM device
1027 * @dev: Device to register
1028 * @flags: Flags passed to the driver's .load() function
1029 *
1030 * Register the DRM device @dev with the system, advertise device to user-space
1031 * and start normal device operation. @dev must be initialized via drm_dev_init()
1032 * previously.
1033 *
1034 * Never call this twice on any device!
1035 *
1036 * NOTE: To ensure backward compatibility with existing drivers method this
1037 * function calls the &drm_driver.load method after registering the device
1038 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
1039 * therefore deprecated, drivers must perform all initialization before calling
1040 * drm_dev_register().
1041 *
1042 * RETURNS:
1043 * 0 on success, negative error code on failure.
1044 */
drm_dev_register(struct drm_device * dev,unsigned long flags)1045 int drm_dev_register(struct drm_device *dev, unsigned long flags)
1046 {
1047 struct drm_driver *driver = dev->driver;
1048 int ret;
1049
1050 #ifndef __NetBSD__
1051 mutex_lock(&drm_global_mutex);
1052 #endif
1053
1054 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
1055 if (ret)
1056 goto err_minors;
1057
1058 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
1059 if (ret)
1060 goto err_minors;
1061
1062 ret = create_compat_control_link(dev);
1063 if (ret)
1064 goto err_minors;
1065
1066 dev->registered = true;
1067
1068 if (dev->driver->load) {
1069 ret = dev->driver->load(dev, flags);
1070 if (ret)
1071 goto err_minors;
1072 }
1073
1074 if (drm_core_check_feature(dev, DRIVER_MODESET))
1075 drm_modeset_register_all(dev);
1076
1077 ret = 0;
1078
1079 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
1080 driver->name, driver->major, driver->minor,
1081 driver->patchlevel, driver->date,
1082 dev->dev ? dev_name(dev->dev) : "virtual device",
1083 dev->primary->index);
1084
1085 goto out_unlock;
1086
1087 err_minors:
1088 remove_compat_control_link(dev);
1089 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1090 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1091 out_unlock:
1092 #ifndef __NetBSD__
1093 mutex_unlock(&drm_global_mutex);
1094 #endif
1095 return ret;
1096 }
1097 EXPORT_SYMBOL(drm_dev_register);
1098
1099 /**
1100 * drm_dev_unregister - Unregister DRM device
1101 * @dev: Device to unregister
1102 *
1103 * Unregister the DRM device from the system. This does the reverse of
1104 * drm_dev_register() but does not deallocate the device. The caller must call
1105 * drm_dev_put() to drop their final reference.
1106 *
1107 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
1108 * which can be called while there are still open users of @dev.
1109 *
1110 * This should be called first in the device teardown code to make sure
1111 * userspace can't access the device instance any more.
1112 */
drm_dev_unregister(struct drm_device * dev)1113 void drm_dev_unregister(struct drm_device *dev)
1114 {
1115 if (drm_core_check_feature(dev, DRIVER_LEGACY))
1116 drm_lastclose(dev);
1117
1118 dev->registered = false;
1119
1120 drm_client_dev_unregister(dev);
1121
1122 if (drm_core_check_feature(dev, DRIVER_MODESET))
1123 drm_modeset_unregister_all(dev);
1124
1125 if (dev->driver->unload)
1126 dev->driver->unload(dev);
1127
1128 #ifndef __NetBSD__ /* Moved to drm_pci. */
1129 if (dev->agp)
1130 drm_pci_agp_destroy(dev);
1131 #endif
1132
1133 drm_legacy_rmmaps(dev);
1134
1135 remove_compat_control_link(dev);
1136 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
1137 drm_minor_unregister(dev, DRM_MINOR_RENDER);
1138 }
1139 EXPORT_SYMBOL(drm_dev_unregister);
1140
1141 /**
1142 * drm_dev_set_unique - Set the unique name of a DRM device
1143 * @dev: device of which to set the unique name
1144 * @name: unique name
1145 *
1146 * Sets the unique name of a DRM device using the specified string. This is
1147 * already done by drm_dev_init(), drivers should only override the default
1148 * unique name for backwards compatibility reasons.
1149 *
1150 * Return: 0 on success or a negative error code on failure.
1151 */
drm_dev_set_unique(struct drm_device * dev,const char * name)1152 int drm_dev_set_unique(struct drm_device *dev, const char *name)
1153 {
1154 kfree(dev->unique);
1155 dev->unique = kstrdup(name, GFP_KERNEL);
1156
1157 return dev->unique ? 0 : -ENOMEM;
1158 }
1159 EXPORT_SYMBOL(drm_dev_set_unique);
1160
1161 #ifndef __NetBSD__
1162
1163 /*
1164 * DRM Core
1165 * The DRM core module initializes all global DRM objects and makes them
1166 * available to drivers. Once setup, drivers can probe their respective
1167 * devices.
1168 * Currently, core management includes:
1169 * - The "DRM-Global" key/value database
1170 * - Global ID management for connectors
1171 * - DRM major number allocation
1172 * - DRM minor management
1173 * - DRM sysfs class
1174 * - DRM debugfs root
1175 *
1176 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1177 * interface registered on a DRM device, you can request minor numbers from DRM
1178 * core. DRM core takes care of major-number management and char-dev
1179 * registration. A stub ->open() callback forwards any open() requests to the
1180 * registered minor.
1181 */
1182
drm_stub_open(struct inode * inode,struct file * filp)1183 static int drm_stub_open(struct inode *inode, struct file *filp)
1184 {
1185 const struct file_operations *new_fops;
1186 struct drm_minor *minor;
1187 int err;
1188
1189 DRM_DEBUG("\n");
1190
1191 mutex_lock(&drm_global_mutex);
1192 minor = drm_minor_acquire(iminor(inode));
1193 if (IS_ERR(minor)) {
1194 err = PTR_ERR(minor);
1195 goto out_unlock;
1196 }
1197
1198 new_fops = fops_get(minor->dev->driver->fops);
1199 if (!new_fops) {
1200 err = -ENODEV;
1201 goto out_release;
1202 }
1203
1204 replace_fops(filp, new_fops);
1205 if (filp->f_op->open)
1206 err = filp->f_op->open(inode, filp);
1207 else
1208 err = 0;
1209
1210 out_release:
1211 drm_minor_release(minor);
1212 out_unlock:
1213 mutex_unlock(&drm_global_mutex);
1214 return err;
1215 }
1216
1217 static const struct file_operations drm_stub_fops = {
1218 .owner = THIS_MODULE,
1219 .open = drm_stub_open,
1220 .llseek = noop_llseek,
1221 };
1222
drm_core_exit(void)1223 static void drm_core_exit(void)
1224 {
1225 unregister_chrdev(DRM_MAJOR, "drm");
1226 debugfs_remove(drm_debugfs_root);
1227 drm_sysfs_destroy();
1228 idr_destroy(&drm_minors_idr);
1229 drm_connector_ida_destroy();
1230 }
1231
drm_core_init(void)1232 static int __init drm_core_init(void)
1233 {
1234 int ret;
1235
1236 drm_connector_ida_init();
1237 idr_init(&drm_minors_idr);
1238
1239 ret = drm_sysfs_init();
1240 if (ret < 0) {
1241 DRM_ERROR("Cannot create DRM class: %d\n", ret);
1242 goto error;
1243 }
1244
1245 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1246
1247 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1248 if (ret < 0)
1249 goto error;
1250
1251 drm_core_init_complete = true;
1252
1253 DRM_DEBUG("Initialized\n");
1254 return 0;
1255
1256 error:
1257 drm_core_exit();
1258 return ret;
1259 }
1260
1261 module_init(drm_core_init);
1262 module_exit(drm_core_exit);
1263
1264 #endif
1265