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