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