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