drm_drv.c revision 1.14 1 1.14 maya /* $NetBSD: drm_drv.c,v 1.14 2020/04/19 17:19:13 maya 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.14 maya __KERNEL_RCSID(0, "$NetBSD: drm_drv.c,v 1.14 2020/04/19 17:19:13 maya 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.1 riastrad #include <linux/slab.h>
40 1.1 riastrad #include <drm/drmP.h>
41 1.1 riastrad #include <drm/drm_core.h>
42 1.2 riastrad #include "drm_legacy.h"
43 1.2 riastrad #include "drm_internal.h"
44 1.2 riastrad
45 1.11 riastrad #include <linux/nbsd-namespace.h>
46 1.11 riastrad
47 1.10 martin unsigned int drm_debug = 0; /* bitmask of DRM_UT_x */
48 1.2 riastrad EXPORT_SYMBOL(drm_debug);
49 1.2 riastrad
50 1.2 riastrad MODULE_AUTHOR(CORE_AUTHOR);
51 1.2 riastrad MODULE_DESCRIPTION(CORE_DESC);
52 1.2 riastrad MODULE_LICENSE("GPL and additional rights");
53 1.2 riastrad MODULE_PARM_DESC(debug, "Enable debug output");
54 1.2 riastrad MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
55 1.2 riastrad MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
56 1.2 riastrad MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
57 1.2 riastrad
58 1.2 riastrad module_param_named(debug, drm_debug, int, 0600);
59 1.2 riastrad
60 1.2 riastrad #ifdef __NetBSD__
61 1.5 riastrad spinlock_t drm_minor_lock;
62 1.5 riastrad struct idr drm_minors_idr;
63 1.2 riastrad #else
64 1.2 riastrad static DEFINE_SPINLOCK(drm_minor_lock);
65 1.5 riastrad static struct idr drm_minors_idr;
66 1.2 riastrad #endif
67 1.2 riastrad
68 1.2 riastrad #ifndef __NetBSD__
69 1.2 riastrad static struct dentry *drm_debugfs_root;
70 1.2 riastrad #endif
71 1.2 riastrad
72 1.8 riastrad #ifdef __NetBSD__
73 1.8 riastrad void
74 1.8 riastrad drm_err(const char *file, int line, const char *func, const char *format, ...)
75 1.2 riastrad {
76 1.2 riastrad va_list args;
77 1.2 riastrad
78 1.2 riastrad va_start(args, format);
79 1.8 riastrad printf(KERN_ERR "[" DRM_NAME ":(%s:%d)%s] *ERROR* ", file, line, func);
80 1.2 riastrad vprintf(format, args);
81 1.2 riastrad va_end(args);
82 1.8 riastrad }
83 1.2 riastrad #else
84 1.8 riastrad void drm_err(const char *format, ...)
85 1.8 riastrad {
86 1.2 riastrad struct va_format vaf;
87 1.2 riastrad va_list args;
88 1.2 riastrad
89 1.2 riastrad va_start(args, format);
90 1.2 riastrad
91 1.2 riastrad vaf.fmt = format;
92 1.2 riastrad vaf.va = &args;
93 1.2 riastrad
94 1.2 riastrad printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
95 1.2 riastrad __builtin_return_address(0), &vaf);
96 1.2 riastrad
97 1.2 riastrad va_end(args);
98 1.8 riastrad }
99 1.2 riastrad #endif
100 1.2 riastrad EXPORT_SYMBOL(drm_err);
101 1.2 riastrad
102 1.2 riastrad void drm_ut_debug_printk(const char *function_name, const char *format, ...)
103 1.2 riastrad {
104 1.2 riastrad #ifdef __NetBSD__
105 1.2 riastrad va_list args;
106 1.2 riastrad
107 1.2 riastrad va_start(args, format);
108 1.2 riastrad printf("DRM debug in %s: ", function_name);
109 1.2 riastrad vprintf(format, args);
110 1.2 riastrad va_end(args);
111 1.2 riastrad #else
112 1.2 riastrad struct va_format vaf;
113 1.2 riastrad va_list args;
114 1.2 riastrad
115 1.2 riastrad va_start(args, format);
116 1.2 riastrad vaf.fmt = format;
117 1.2 riastrad vaf.va = &args;
118 1.2 riastrad
119 1.2 riastrad printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
120 1.2 riastrad
121 1.2 riastrad va_end(args);
122 1.2 riastrad #endif
123 1.2 riastrad }
124 1.2 riastrad EXPORT_SYMBOL(drm_ut_debug_printk);
125 1.2 riastrad
126 1.2 riastrad struct drm_master *drm_master_create(struct drm_minor *minor)
127 1.2 riastrad {
128 1.2 riastrad struct drm_master *master;
129 1.2 riastrad
130 1.2 riastrad master = kzalloc(sizeof(*master), GFP_KERNEL);
131 1.2 riastrad if (!master)
132 1.2 riastrad return NULL;
133 1.2 riastrad
134 1.2 riastrad kref_init(&master->refcount);
135 1.2 riastrad spin_lock_init(&master->lock.spinlock);
136 1.2 riastrad #ifdef __NetBSD__
137 1.2 riastrad DRM_INIT_WAITQUEUE(&master->lock.lock_queue, "drmlockq");
138 1.2 riastrad #else
139 1.2 riastrad init_waitqueue_head(&master->lock.lock_queue);
140 1.2 riastrad #endif
141 1.2 riastrad idr_init(&master->magic_map);
142 1.2 riastrad master->minor = minor;
143 1.2 riastrad
144 1.2 riastrad return master;
145 1.2 riastrad }
146 1.2 riastrad
147 1.2 riastrad struct drm_master *drm_master_get(struct drm_master *master)
148 1.2 riastrad {
149 1.2 riastrad kref_get(&master->refcount);
150 1.2 riastrad return master;
151 1.2 riastrad }
152 1.2 riastrad EXPORT_SYMBOL(drm_master_get);
153 1.2 riastrad
154 1.2 riastrad static void drm_master_destroy(struct kref *kref)
155 1.2 riastrad {
156 1.2 riastrad struct drm_master *master = container_of(kref, struct drm_master, refcount);
157 1.2 riastrad struct drm_device *dev = master->minor->dev;
158 1.2 riastrad struct drm_map_list *r_list, *list_temp;
159 1.2 riastrad
160 1.2 riastrad mutex_lock(&dev->struct_mutex);
161 1.2 riastrad if (dev->driver->master_destroy)
162 1.2 riastrad dev->driver->master_destroy(dev, master);
163 1.2 riastrad
164 1.2 riastrad list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
165 1.2 riastrad if (r_list->master == master) {
166 1.2 riastrad drm_legacy_rmmap_locked(dev, r_list->map);
167 1.2 riastrad r_list = NULL;
168 1.2 riastrad }
169 1.2 riastrad }
170 1.2 riastrad mutex_unlock(&dev->struct_mutex);
171 1.2 riastrad
172 1.2 riastrad idr_destroy(&master->magic_map);
173 1.2 riastrad #ifdef __NetBSD__
174 1.2 riastrad DRM_DESTROY_WAITQUEUE(&master->lock.lock_queue);
175 1.2 riastrad spin_lock_destroy(&master->lock.spinlock);
176 1.2 riastrad #endif
177 1.2 riastrad kfree(master->unique);
178 1.2 riastrad kfree(master);
179 1.2 riastrad }
180 1.2 riastrad
181 1.2 riastrad void drm_master_put(struct drm_master **master)
182 1.2 riastrad {
183 1.2 riastrad kref_put(&(*master)->refcount, drm_master_destroy);
184 1.2 riastrad *master = NULL;
185 1.2 riastrad }
186 1.2 riastrad EXPORT_SYMBOL(drm_master_put);
187 1.2 riastrad
188 1.2 riastrad int drm_setmaster_ioctl(struct drm_device *dev, void *data,
189 1.2 riastrad struct drm_file *file_priv)
190 1.2 riastrad {
191 1.2 riastrad int ret = 0;
192 1.2 riastrad
193 1.2 riastrad mutex_lock(&dev->master_mutex);
194 1.2 riastrad if (file_priv->is_master)
195 1.2 riastrad goto out_unlock;
196 1.2 riastrad
197 1.2 riastrad if (file_priv->minor->master) {
198 1.2 riastrad ret = -EINVAL;
199 1.2 riastrad goto out_unlock;
200 1.2 riastrad }
201 1.2 riastrad
202 1.2 riastrad if (!file_priv->master) {
203 1.2 riastrad ret = -EINVAL;
204 1.2 riastrad goto out_unlock;
205 1.2 riastrad }
206 1.2 riastrad
207 1.2 riastrad if (!file_priv->allowed_master) {
208 1.2 riastrad ret = drm_new_set_master(dev, file_priv);
209 1.2 riastrad goto out_unlock;
210 1.2 riastrad }
211 1.2 riastrad
212 1.2 riastrad file_priv->minor->master = drm_master_get(file_priv->master);
213 1.2 riastrad file_priv->is_master = 1;
214 1.2 riastrad if (dev->driver->master_set) {
215 1.2 riastrad ret = dev->driver->master_set(dev, file_priv, false);
216 1.2 riastrad if (unlikely(ret != 0)) {
217 1.2 riastrad file_priv->is_master = 0;
218 1.2 riastrad drm_master_put(&file_priv->minor->master);
219 1.2 riastrad }
220 1.2 riastrad }
221 1.2 riastrad
222 1.2 riastrad out_unlock:
223 1.2 riastrad mutex_unlock(&dev->master_mutex);
224 1.2 riastrad return ret;
225 1.2 riastrad }
226 1.2 riastrad
227 1.2 riastrad int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
228 1.2 riastrad struct drm_file *file_priv)
229 1.2 riastrad {
230 1.2 riastrad int ret = -EINVAL;
231 1.2 riastrad
232 1.2 riastrad mutex_lock(&dev->master_mutex);
233 1.2 riastrad if (!file_priv->is_master)
234 1.2 riastrad goto out_unlock;
235 1.2 riastrad
236 1.2 riastrad if (!file_priv->minor->master)
237 1.2 riastrad goto out_unlock;
238 1.2 riastrad
239 1.2 riastrad ret = 0;
240 1.2 riastrad if (dev->driver->master_drop)
241 1.2 riastrad dev->driver->master_drop(dev, file_priv, false);
242 1.2 riastrad drm_master_put(&file_priv->minor->master);
243 1.2 riastrad file_priv->is_master = 0;
244 1.2 riastrad
245 1.2 riastrad out_unlock:
246 1.2 riastrad mutex_unlock(&dev->master_mutex);
247 1.2 riastrad return ret;
248 1.2 riastrad }
249 1.2 riastrad
250 1.2 riastrad /*
251 1.2 riastrad * DRM Minors
252 1.2 riastrad * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
253 1.2 riastrad * of them is represented by a drm_minor object. Depending on the capabilities
254 1.2 riastrad * of the device-driver, different interfaces are registered.
255 1.2 riastrad *
256 1.2 riastrad * Minors can be accessed via dev->$minor_name. This pointer is either
257 1.2 riastrad * NULL or a valid drm_minor pointer and stays valid as long as the device is
258 1.2 riastrad * valid. This means, DRM minors have the same life-time as the underlying
259 1.2 riastrad * device. However, this doesn't mean that the minor is active. Minors are
260 1.2 riastrad * registered and unregistered dynamically according to device-state.
261 1.2 riastrad */
262 1.1 riastrad
263 1.2 riastrad static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
264 1.2 riastrad unsigned int type)
265 1.2 riastrad {
266 1.2 riastrad switch (type) {
267 1.2 riastrad case DRM_MINOR_LEGACY:
268 1.2 riastrad return &dev->primary;
269 1.2 riastrad case DRM_MINOR_RENDER:
270 1.2 riastrad return &dev->render;
271 1.2 riastrad case DRM_MINOR_CONTROL:
272 1.2 riastrad return &dev->control;
273 1.2 riastrad default:
274 1.2 riastrad return NULL;
275 1.2 riastrad }
276 1.2 riastrad }
277 1.2 riastrad
278 1.2 riastrad static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
279 1.2 riastrad {
280 1.2 riastrad struct drm_minor *minor;
281 1.2 riastrad unsigned long flags;
282 1.2 riastrad int r;
283 1.2 riastrad
284 1.2 riastrad minor = kzalloc(sizeof(*minor), GFP_KERNEL);
285 1.2 riastrad if (!minor)
286 1.2 riastrad return -ENOMEM;
287 1.2 riastrad
288 1.2 riastrad minor->type = type;
289 1.2 riastrad minor->dev = dev;
290 1.2 riastrad
291 1.2 riastrad idr_preload(GFP_KERNEL);
292 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
293 1.2 riastrad r = idr_alloc(&drm_minors_idr,
294 1.2 riastrad NULL,
295 1.2 riastrad 64 * type,
296 1.2 riastrad 64 * (type + 1),
297 1.2 riastrad GFP_NOWAIT);
298 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
299 1.2 riastrad idr_preload_end();
300 1.2 riastrad
301 1.2 riastrad if (r < 0)
302 1.2 riastrad goto err_free;
303 1.2 riastrad
304 1.2 riastrad minor->index = r;
305 1.2 riastrad
306 1.6 riastrad #ifndef __NetBSD__ /* XXX drm sysfs */
307 1.2 riastrad minor->kdev = drm_sysfs_minor_alloc(minor);
308 1.2 riastrad if (IS_ERR(minor->kdev)) {
309 1.2 riastrad r = PTR_ERR(minor->kdev);
310 1.2 riastrad goto err_index;
311 1.2 riastrad }
312 1.6 riastrad #endif
313 1.1 riastrad
314 1.2 riastrad *drm_minor_get_slot(dev, type) = minor;
315 1.2 riastrad return 0;
316 1.2 riastrad
317 1.6 riastrad err_index: __unused
318 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
319 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
320 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
321 1.2 riastrad err_free:
322 1.2 riastrad kfree(minor);
323 1.2 riastrad return r;
324 1.2 riastrad }
325 1.2 riastrad
326 1.2 riastrad static void drm_minor_free(struct drm_device *dev, unsigned int type)
327 1.2 riastrad {
328 1.2 riastrad struct drm_minor **slot, *minor;
329 1.2 riastrad unsigned long flags;
330 1.1 riastrad
331 1.2 riastrad slot = drm_minor_get_slot(dev, type);
332 1.2 riastrad minor = *slot;
333 1.2 riastrad if (!minor)
334 1.2 riastrad return;
335 1.2 riastrad
336 1.6 riastrad #ifndef __NetBSD__ /* XXX drm sysfs */
337 1.2 riastrad put_device(minor->kdev);
338 1.5 riastrad #endif
339 1.2 riastrad
340 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
341 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
342 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
343 1.1 riastrad
344 1.2 riastrad kfree(minor);
345 1.2 riastrad *slot = NULL;
346 1.2 riastrad }
347 1.2 riastrad
348 1.2 riastrad static int drm_minor_register(struct drm_device *dev, unsigned int type)
349 1.2 riastrad {
350 1.2 riastrad struct drm_minor *minor;
351 1.2 riastrad unsigned long flags;
352 1.2 riastrad #ifndef __NetBSD__
353 1.2 riastrad int ret;
354 1.2 riastrad #endif
355 1.2 riastrad
356 1.2 riastrad DRM_DEBUG("\n");
357 1.2 riastrad
358 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
359 1.2 riastrad if (!minor)
360 1.2 riastrad return 0;
361 1.2 riastrad
362 1.2 riastrad #ifndef __NetBSD__
363 1.2 riastrad ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
364 1.2 riastrad if (ret) {
365 1.2 riastrad DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
366 1.2 riastrad goto err_debugfs;
367 1.2 riastrad }
368 1.2 riastrad
369 1.2 riastrad ret = device_add(minor->kdev);
370 1.2 riastrad if (ret)
371 1.2 riastrad goto err_debugfs;
372 1.2 riastrad #endif
373 1.2 riastrad
374 1.2 riastrad /* replace NULL with @minor so lookups will succeed from now on */
375 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
376 1.2 riastrad idr_replace(&drm_minors_idr, minor, minor->index);
377 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
378 1.2 riastrad
379 1.2 riastrad DRM_DEBUG("new minor registered %d\n", minor->index);
380 1.2 riastrad return 0;
381 1.2 riastrad
382 1.2 riastrad #ifndef __NetBSD__
383 1.2 riastrad err_debugfs:
384 1.2 riastrad drm_debugfs_cleanup(minor);
385 1.2 riastrad return ret;
386 1.2 riastrad #endif
387 1.2 riastrad }
388 1.2 riastrad
389 1.2 riastrad static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
390 1.2 riastrad {
391 1.2 riastrad struct drm_minor *minor;
392 1.2 riastrad unsigned long flags;
393 1.1 riastrad
394 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
395 1.5 riastrad #ifdef __NetBSD__
396 1.5 riastrad if (!minor)
397 1.5 riastrad #else
398 1.2 riastrad if (!minor || !device_is_registered(minor->kdev))
399 1.5 riastrad #endif
400 1.2 riastrad return;
401 1.2 riastrad
402 1.2 riastrad /* replace @minor with NULL so lookups will fail from now on */
403 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
404 1.2 riastrad idr_replace(&drm_minors_idr, NULL, minor->index);
405 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
406 1.2 riastrad
407 1.5 riastrad #ifndef __NetBSD__
408 1.2 riastrad device_del(minor->kdev);
409 1.2 riastrad dev_set_drvdata(minor->kdev, NULL); /* safety belt */
410 1.2 riastrad drm_debugfs_cleanup(minor);
411 1.5 riastrad #endif
412 1.2 riastrad }
413 1.1 riastrad
414 1.1 riastrad /**
415 1.2 riastrad * drm_minor_acquire - Acquire a DRM minor
416 1.2 riastrad * @minor_id: Minor ID of the DRM-minor
417 1.1 riastrad *
418 1.2 riastrad * Looks up the given minor-ID and returns the respective DRM-minor object. The
419 1.2 riastrad * refence-count of the underlying device is increased so you must release this
420 1.2 riastrad * object with drm_minor_release().
421 1.1 riastrad *
422 1.2 riastrad * As long as you hold this minor, it is guaranteed that the object and the
423 1.2 riastrad * minor->dev pointer will stay valid! However, the device may get unplugged and
424 1.2 riastrad * unregistered while you hold the minor.
425 1.1 riastrad *
426 1.2 riastrad * Returns:
427 1.2 riastrad * Pointer to minor-object with increased device-refcount, or PTR_ERR on
428 1.2 riastrad * failure.
429 1.1 riastrad */
430 1.2 riastrad struct drm_minor *drm_minor_acquire(unsigned int minor_id)
431 1.1 riastrad {
432 1.2 riastrad struct drm_minor *minor;
433 1.2 riastrad unsigned long flags;
434 1.2 riastrad
435 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
436 1.2 riastrad minor = idr_find(&drm_minors_idr, minor_id);
437 1.2 riastrad if (minor)
438 1.2 riastrad drm_dev_ref(minor->dev);
439 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
440 1.2 riastrad
441 1.2 riastrad if (!minor) {
442 1.2 riastrad return ERR_PTR(-ENODEV);
443 1.2 riastrad } else if (drm_device_is_unplugged(minor->dev)) {
444 1.2 riastrad drm_dev_unref(minor->dev);
445 1.2 riastrad return ERR_PTR(-ENODEV);
446 1.2 riastrad }
447 1.2 riastrad
448 1.2 riastrad return minor;
449 1.2 riastrad }
450 1.1 riastrad
451 1.2 riastrad /**
452 1.2 riastrad * drm_minor_release - Release DRM minor
453 1.2 riastrad * @minor: Pointer to DRM minor object
454 1.2 riastrad *
455 1.2 riastrad * Release a minor that was previously acquired via drm_minor_acquire().
456 1.2 riastrad */
457 1.2 riastrad void drm_minor_release(struct drm_minor *minor)
458 1.2 riastrad {
459 1.2 riastrad drm_dev_unref(minor->dev);
460 1.2 riastrad }
461 1.2 riastrad
462 1.2 riastrad /**
463 1.2 riastrad * DOC: driver instance overview
464 1.2 riastrad *
465 1.2 riastrad * A device instance for a drm driver is represented by struct &drm_device. This
466 1.2 riastrad * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
467 1.2 riastrad * callbacks implemented by the driver. The driver then needs to initialize all
468 1.2 riastrad * the various subsystems for the drm device like memory management, vblank
469 1.2 riastrad * handling, modesetting support and intial output configuration plus obviously
470 1.2 riastrad * initialize all the corresponding hardware bits. An important part of this is
471 1.2 riastrad * also calling drm_dev_set_unique() to set the userspace-visible unique name of
472 1.2 riastrad * this device instance. Finally when everything is up and running and ready for
473 1.2 riastrad * userspace the device instance can be published using drm_dev_register().
474 1.2 riastrad *
475 1.2 riastrad * There is also deprecated support for initalizing device instances using
476 1.2 riastrad * bus-specific helpers and the ->load() callback. But due to
477 1.2 riastrad * backwards-compatibility needs the device instance have to be published too
478 1.2 riastrad * early, which requires unpretty global locking to make safe and is therefore
479 1.2 riastrad * only support for existing drivers not yet converted to the new scheme.
480 1.2 riastrad *
481 1.2 riastrad * When cleaning up a device instance everything needs to be done in reverse:
482 1.2 riastrad * First unpublish the device instance with drm_dev_unregister(). Then clean up
483 1.2 riastrad * any other resources allocated at device initialization and drop the driver's
484 1.2 riastrad * reference to &drm_device using drm_dev_unref().
485 1.2 riastrad *
486 1.2 riastrad * Note that the lifetime rules for &drm_device instance has still a lot of
487 1.2 riastrad * historical baggage. Hence use the reference counting provided by
488 1.2 riastrad * drm_dev_ref() and drm_dev_unref() only carefully.
489 1.2 riastrad *
490 1.2 riastrad * Also note that embedding of &drm_device is currently not (yet) supported (but
491 1.2 riastrad * it would be easy to add). Drivers can store driver-private data in the
492 1.2 riastrad * dev_priv field of &drm_device.
493 1.2 riastrad */
494 1.2 riastrad
495 1.2 riastrad /**
496 1.2 riastrad * drm_put_dev - Unregister and release a DRM device
497 1.2 riastrad * @dev: DRM device
498 1.2 riastrad *
499 1.2 riastrad * Called at module unload time or when a PCI device is unplugged.
500 1.2 riastrad *
501 1.2 riastrad * Cleans up all DRM device, calling drm_lastclose().
502 1.2 riastrad *
503 1.2 riastrad * Note: Use of this function is deprecated. It will eventually go away
504 1.2 riastrad * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
505 1.2 riastrad * instead to make sure that the device isn't userspace accessible any more
506 1.2 riastrad * while teardown is in progress, ensuring that userspace can't access an
507 1.2 riastrad * inconsistent state.
508 1.2 riastrad */
509 1.2 riastrad void drm_put_dev(struct drm_device *dev)
510 1.2 riastrad {
511 1.1 riastrad DRM_DEBUG("\n");
512 1.1 riastrad
513 1.2 riastrad if (!dev) {
514 1.2 riastrad DRM_ERROR("cleanup called no dev\n");
515 1.2 riastrad return;
516 1.2 riastrad }
517 1.2 riastrad
518 1.2 riastrad drm_dev_unregister(dev);
519 1.2 riastrad drm_dev_unref(dev);
520 1.2 riastrad }
521 1.2 riastrad EXPORT_SYMBOL(drm_put_dev);
522 1.2 riastrad
523 1.2 riastrad void drm_unplug_dev(struct drm_device *dev)
524 1.2 riastrad {
525 1.2 riastrad /* for a USB device */
526 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
527 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
528 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
529 1.2 riastrad
530 1.2 riastrad mutex_lock(&drm_global_mutex);
531 1.2 riastrad
532 1.2 riastrad drm_device_set_unplugged(dev);
533 1.2 riastrad
534 1.2 riastrad if (dev->open_count == 0) {
535 1.2 riastrad drm_put_dev(dev);
536 1.2 riastrad }
537 1.2 riastrad mutex_unlock(&drm_global_mutex);
538 1.2 riastrad }
539 1.2 riastrad EXPORT_SYMBOL(drm_unplug_dev);
540 1.2 riastrad
541 1.2 riastrad #ifdef __NetBSD__
542 1.2 riastrad
543 1.13 riastrad static void *
544 1.2 riastrad drm_fs_inode_new(void)
545 1.2 riastrad {
546 1.2 riastrad return NULL;
547 1.2 riastrad }
548 1.2 riastrad
549 1.2 riastrad static void
550 1.13 riastrad drm_fs_inode_free(void *inode)
551 1.2 riastrad {
552 1.2 riastrad KASSERT(inode == NULL);
553 1.2 riastrad }
554 1.2 riastrad
555 1.2 riastrad #else
556 1.2 riastrad
557 1.2 riastrad /*
558 1.2 riastrad * DRM internal mount
559 1.2 riastrad * We want to be able to allocate our own "struct address_space" to control
560 1.2 riastrad * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
561 1.2 riastrad * stand-alone address_space objects, so we need an underlying inode. As there
562 1.2 riastrad * is no way to allocate an independent inode easily, we need a fake internal
563 1.2 riastrad * VFS mount-point.
564 1.2 riastrad *
565 1.2 riastrad * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
566 1.2 riastrad * frees it again. You are allowed to use iget() and iput() to get references to
567 1.2 riastrad * the inode. But each drm_fs_inode_new() call must be paired with exactly one
568 1.2 riastrad * drm_fs_inode_free() call (which does not have to be the last iput()).
569 1.2 riastrad * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
570 1.2 riastrad * between multiple inode-users. You could, technically, call
571 1.2 riastrad * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
572 1.2 riastrad * iput(), but this way you'd end up with a new vfsmount for each inode.
573 1.2 riastrad */
574 1.2 riastrad
575 1.2 riastrad static int drm_fs_cnt;
576 1.2 riastrad static struct vfsmount *drm_fs_mnt;
577 1.1 riastrad
578 1.2 riastrad static const struct dentry_operations drm_fs_dops = {
579 1.2 riastrad .d_dname = simple_dname,
580 1.2 riastrad };
581 1.2 riastrad
582 1.2 riastrad static const struct super_operations drm_fs_sops = {
583 1.2 riastrad .statfs = simple_statfs,
584 1.2 riastrad };
585 1.2 riastrad
586 1.2 riastrad static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
587 1.2 riastrad const char *dev_name, void *data)
588 1.2 riastrad {
589 1.2 riastrad return mount_pseudo(fs_type,
590 1.2 riastrad "drm:",
591 1.2 riastrad &drm_fs_sops,
592 1.2 riastrad &drm_fs_dops,
593 1.2 riastrad 0x010203ff);
594 1.2 riastrad }
595 1.2 riastrad
596 1.2 riastrad static struct file_system_type drm_fs_type = {
597 1.2 riastrad .name = "drm",
598 1.2 riastrad .owner = THIS_MODULE,
599 1.2 riastrad .mount = drm_fs_mount,
600 1.2 riastrad .kill_sb = kill_anon_super,
601 1.2 riastrad };
602 1.2 riastrad
603 1.2 riastrad static struct inode *drm_fs_inode_new(void)
604 1.2 riastrad {
605 1.2 riastrad struct inode *inode;
606 1.2 riastrad int r;
607 1.2 riastrad
608 1.2 riastrad r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
609 1.2 riastrad if (r < 0) {
610 1.2 riastrad DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
611 1.2 riastrad return ERR_PTR(r);
612 1.2 riastrad }
613 1.2 riastrad
614 1.2 riastrad inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
615 1.2 riastrad if (IS_ERR(inode))
616 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
617 1.2 riastrad
618 1.2 riastrad return inode;
619 1.2 riastrad }
620 1.2 riastrad
621 1.2 riastrad static void drm_fs_inode_free(struct inode *inode)
622 1.2 riastrad {
623 1.2 riastrad if (inode) {
624 1.2 riastrad iput(inode);
625 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
626 1.2 riastrad }
627 1.2 riastrad }
628 1.2 riastrad
629 1.2 riastrad #endif
630 1.1 riastrad
631 1.2 riastrad /**
632 1.2 riastrad * drm_dev_alloc - Allocate new DRM device
633 1.2 riastrad * @driver: DRM driver to allocate device for
634 1.2 riastrad * @parent: Parent device object
635 1.2 riastrad *
636 1.2 riastrad * Allocate and initialize a new DRM device. No device registration is done.
637 1.2 riastrad * Call drm_dev_register() to advertice the device to user space and register it
638 1.2 riastrad * with other core subsystems. This should be done last in the device
639 1.2 riastrad * initialization sequence to make sure userspace can't access an inconsistent
640 1.2 riastrad * state.
641 1.2 riastrad *
642 1.2 riastrad * The initial ref-count of the object is 1. Use drm_dev_ref() and
643 1.2 riastrad * drm_dev_unref() to take and drop further ref-counts.
644 1.2 riastrad *
645 1.2 riastrad * Note that for purely virtual devices @parent can be NULL.
646 1.2 riastrad *
647 1.2 riastrad * RETURNS:
648 1.2 riastrad * Pointer to new DRM device, or NULL if out of memory.
649 1.2 riastrad */
650 1.2 riastrad struct drm_device *drm_dev_alloc(struct drm_driver *driver,
651 1.2 riastrad struct device *parent)
652 1.2 riastrad {
653 1.2 riastrad struct drm_device *dev;
654 1.2 riastrad int ret;
655 1.2 riastrad
656 1.2 riastrad dev = kzalloc(sizeof(*dev), GFP_KERNEL);
657 1.2 riastrad if (!dev)
658 1.2 riastrad return NULL;
659 1.2 riastrad
660 1.2 riastrad kref_init(&dev->ref);
661 1.2 riastrad dev->dev = parent;
662 1.2 riastrad dev->driver = driver;
663 1.14 maya #ifdef __NetBSD__
664 1.14 maya dev->sc_monitor_hotplug.smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
665 1.14 maya dev->sc_monitor_hotplug.smpsw_type = PSWITCH_TYPE_HOTKEY;
666 1.14 maya
667 1.14 maya ret = sysmon_pswitch_register(&dev->sc_monitor_hotplug);
668 1.14 maya if (ret)
669 1.14 maya goto err_pswitch;
670 1.14 maya #endif
671 1.2 riastrad
672 1.2 riastrad INIT_LIST_HEAD(&dev->filelist);
673 1.2 riastrad INIT_LIST_HEAD(&dev->ctxlist);
674 1.2 riastrad INIT_LIST_HEAD(&dev->vmalist);
675 1.2 riastrad INIT_LIST_HEAD(&dev->maplist);
676 1.2 riastrad INIT_LIST_HEAD(&dev->vblank_event_list);
677 1.2 riastrad
678 1.2 riastrad spin_lock_init(&dev->buf_lock);
679 1.2 riastrad spin_lock_init(&dev->event_lock);
680 1.2 riastrad mutex_init(&dev->struct_mutex);
681 1.2 riastrad mutex_init(&dev->ctxlist_mutex);
682 1.2 riastrad mutex_init(&dev->master_mutex);
683 1.1 riastrad
684 1.2 riastrad dev->anon_inode = drm_fs_inode_new();
685 1.2 riastrad if (IS_ERR(dev->anon_inode)) {
686 1.2 riastrad ret = PTR_ERR(dev->anon_inode);
687 1.2 riastrad DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
688 1.2 riastrad goto err_free;
689 1.2 riastrad }
690 1.1 riastrad
691 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) {
692 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
693 1.2 riastrad if (ret)
694 1.2 riastrad goto err_minors;
695 1.1 riastrad
696 1.2 riastrad WARN_ON(driver->suspend || driver->resume);
697 1.1 riastrad }
698 1.2 riastrad
699 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_RENDER)) {
700 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
701 1.2 riastrad if (ret)
702 1.2 riastrad goto err_minors;
703 1.1 riastrad }
704 1.1 riastrad
705 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
706 1.2 riastrad if (ret)
707 1.2 riastrad goto err_minors;
708 1.2 riastrad
709 1.2 riastrad if (drm_ht_create(&dev->map_hash, 12))
710 1.2 riastrad goto err_minors;
711 1.2 riastrad
712 1.2 riastrad drm_legacy_ctxbitmap_init(dev);
713 1.2 riastrad
714 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) {
715 1.2 riastrad ret = drm_gem_init(dev);
716 1.2 riastrad if (ret) {
717 1.2 riastrad DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
718 1.2 riastrad goto err_ctxbitmap;
719 1.2 riastrad }
720 1.1 riastrad }
721 1.1 riastrad
722 1.2 riastrad return dev;
723 1.2 riastrad
724 1.2 riastrad err_ctxbitmap:
725 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
726 1.2 riastrad drm_ht_remove(&dev->map_hash);
727 1.2 riastrad err_minors:
728 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
729 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
730 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
731 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
732 1.2 riastrad err_free:
733 1.2 riastrad spin_lock_destroy(&dev->event_lock);
734 1.9 riastrad spin_lock_destroy(&dev->buf_lock);
735 1.2 riastrad mutex_destroy(&dev->master_mutex);
736 1.9 riastrad mutex_destroy(&dev->ctxlist_mutex);
737 1.9 riastrad mutex_destroy(&dev->struct_mutex);
738 1.14 maya #ifdef __NetBSD__
739 1.14 maya err_pswitch:
740 1.14 maya sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
741 1.14 maya #endif
742 1.2 riastrad kfree(dev);
743 1.2 riastrad return NULL;
744 1.2 riastrad }
745 1.2 riastrad EXPORT_SYMBOL(drm_dev_alloc);
746 1.2 riastrad
747 1.2 riastrad static void drm_dev_release(struct kref *ref)
748 1.2 riastrad {
749 1.2 riastrad struct drm_device *dev = container_of(ref, struct drm_device, ref);
750 1.2 riastrad
751 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM))
752 1.2 riastrad drm_gem_destroy(dev);
753 1.2 riastrad
754 1.14 maya #ifdef __NetBSD__
755 1.14 maya sysmon_pswitch_unregister(&dev->sc_monitor_hotplug);
756 1.14 maya #endif
757 1.14 maya
758 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
759 1.2 riastrad drm_ht_remove(&dev->map_hash);
760 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
761 1.2 riastrad
762 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
763 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
764 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
765 1.2 riastrad
766 1.2 riastrad spin_lock_destroy(&dev->event_lock);
767 1.9 riastrad spin_lock_destroy(&dev->buf_lock);
768 1.2 riastrad mutex_destroy(&dev->master_mutex);
769 1.9 riastrad mutex_destroy(&dev->ctxlist_mutex);
770 1.9 riastrad mutex_destroy(&dev->struct_mutex);
771 1.2 riastrad kfree(dev->unique);
772 1.2 riastrad kfree(dev);
773 1.2 riastrad }
774 1.2 riastrad
775 1.2 riastrad /**
776 1.2 riastrad * drm_dev_ref - Take reference of a DRM device
777 1.2 riastrad * @dev: device to take reference of or NULL
778 1.2 riastrad *
779 1.2 riastrad * This increases the ref-count of @dev by one. You *must* already own a
780 1.2 riastrad * reference when calling this. Use drm_dev_unref() to drop this reference
781 1.2 riastrad * again.
782 1.2 riastrad *
783 1.2 riastrad * This function never fails. However, this function does not provide *any*
784 1.2 riastrad * guarantee whether the device is alive or running. It only provides a
785 1.2 riastrad * reference to the object and the memory associated with it.
786 1.2 riastrad */
787 1.2 riastrad void drm_dev_ref(struct drm_device *dev)
788 1.2 riastrad {
789 1.2 riastrad if (dev)
790 1.2 riastrad kref_get(&dev->ref);
791 1.2 riastrad }
792 1.2 riastrad EXPORT_SYMBOL(drm_dev_ref);
793 1.2 riastrad
794 1.2 riastrad /**
795 1.2 riastrad * drm_dev_unref - Drop reference of a DRM device
796 1.2 riastrad * @dev: device to drop reference of or NULL
797 1.2 riastrad *
798 1.2 riastrad * This decreases the ref-count of @dev by one. The device is destroyed if the
799 1.2 riastrad * ref-count drops to zero.
800 1.2 riastrad */
801 1.2 riastrad void drm_dev_unref(struct drm_device *dev)
802 1.2 riastrad {
803 1.2 riastrad if (dev)
804 1.2 riastrad kref_put(&dev->ref, drm_dev_release);
805 1.2 riastrad }
806 1.2 riastrad EXPORT_SYMBOL(drm_dev_unref);
807 1.2 riastrad
808 1.2 riastrad /**
809 1.2 riastrad * drm_dev_register - Register DRM device
810 1.2 riastrad * @dev: Device to register
811 1.2 riastrad * @flags: Flags passed to the driver's .load() function
812 1.2 riastrad *
813 1.2 riastrad * Register the DRM device @dev with the system, advertise device to user-space
814 1.2 riastrad * and start normal device operation. @dev must be allocated via drm_dev_alloc()
815 1.2 riastrad * previously.
816 1.2 riastrad *
817 1.2 riastrad * Never call this twice on any device!
818 1.2 riastrad *
819 1.2 riastrad * NOTE: To ensure backward compatibility with existing drivers method this
820 1.2 riastrad * function calls the ->load() method after registering the device nodes,
821 1.2 riastrad * creating race conditions. Usage of the ->load() methods is therefore
822 1.2 riastrad * deprecated, drivers must perform all initialization before calling
823 1.2 riastrad * drm_dev_register().
824 1.2 riastrad *
825 1.2 riastrad * RETURNS:
826 1.2 riastrad * 0 on success, negative error code on failure.
827 1.2 riastrad */
828 1.2 riastrad int drm_dev_register(struct drm_device *dev, unsigned long flags)
829 1.2 riastrad {
830 1.2 riastrad int ret;
831 1.2 riastrad
832 1.2 riastrad #ifndef __NetBSD__
833 1.2 riastrad mutex_lock(&drm_global_mutex);
834 1.2 riastrad #endif
835 1.2 riastrad
836 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
837 1.2 riastrad if (ret)
838 1.2 riastrad goto err_minors;
839 1.2 riastrad
840 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_RENDER);
841 1.2 riastrad if (ret)
842 1.2 riastrad goto err_minors;
843 1.2 riastrad
844 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
845 1.2 riastrad if (ret)
846 1.2 riastrad goto err_minors;
847 1.2 riastrad
848 1.2 riastrad if (dev->driver->load) {
849 1.2 riastrad ret = dev->driver->load(dev, flags);
850 1.2 riastrad if (ret)
851 1.2 riastrad goto err_minors;
852 1.2 riastrad }
853 1.2 riastrad
854 1.2 riastrad ret = 0;
855 1.2 riastrad goto out_unlock;
856 1.2 riastrad
857 1.2 riastrad err_minors:
858 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
859 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
860 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
861 1.2 riastrad out_unlock:
862 1.2 riastrad #ifndef __NetBSD__
863 1.2 riastrad mutex_unlock(&drm_global_mutex);
864 1.2 riastrad #endif
865 1.2 riastrad return ret;
866 1.2 riastrad }
867 1.2 riastrad EXPORT_SYMBOL(drm_dev_register);
868 1.2 riastrad
869 1.2 riastrad /**
870 1.2 riastrad * drm_dev_unregister - Unregister DRM device
871 1.2 riastrad * @dev: Device to unregister
872 1.2 riastrad *
873 1.2 riastrad * Unregister the DRM device from the system. This does the reverse of
874 1.2 riastrad * drm_dev_register() but does not deallocate the device. The caller must call
875 1.2 riastrad * drm_dev_unref() to drop their final reference.
876 1.2 riastrad *
877 1.2 riastrad * This should be called first in the device teardown code to make sure
878 1.2 riastrad * userspace can't access the device instance any more.
879 1.2 riastrad */
880 1.2 riastrad void drm_dev_unregister(struct drm_device *dev)
881 1.2 riastrad {
882 1.2 riastrad struct drm_map_list *r_list, *list_temp;
883 1.2 riastrad
884 1.2 riastrad drm_lastclose(dev);
885 1.2 riastrad
886 1.2 riastrad if (dev->driver->unload)
887 1.2 riastrad dev->driver->unload(dev);
888 1.2 riastrad
889 1.2 riastrad #ifndef __NetBSD__ /* Moved to drm_pci. */
890 1.2 riastrad if (dev->agp)
891 1.2 riastrad drm_pci_agp_destroy(dev);
892 1.2 riastrad #endif
893 1.2 riastrad
894 1.2 riastrad drm_vblank_cleanup(dev);
895 1.1 riastrad
896 1.2 riastrad list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
897 1.2 riastrad drm_legacy_rmmap(dev, r_list->map);
898 1.2 riastrad
899 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
900 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
901 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
902 1.2 riastrad }
903 1.2 riastrad EXPORT_SYMBOL(drm_dev_unregister);
904 1.2 riastrad
905 1.2 riastrad /**
906 1.2 riastrad * drm_dev_set_unique - Set the unique name of a DRM device
907 1.2 riastrad * @dev: device of which to set the unique name
908 1.2 riastrad * @fmt: format string for unique name
909 1.2 riastrad *
910 1.2 riastrad * Sets the unique name of a DRM device using the specified format string and
911 1.2 riastrad * a variable list of arguments. Drivers can use this at driver probe time if
912 1.2 riastrad * the unique name of the devices they drive is static.
913 1.2 riastrad *
914 1.2 riastrad * Return: 0 on success or a negative error code on failure.
915 1.2 riastrad */
916 1.2 riastrad int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
917 1.2 riastrad {
918 1.2 riastrad va_list ap;
919 1.2 riastrad
920 1.2 riastrad kfree(dev->unique);
921 1.2 riastrad
922 1.2 riastrad va_start(ap, fmt);
923 1.2 riastrad dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
924 1.2 riastrad va_end(ap);
925 1.2 riastrad
926 1.2 riastrad return dev->unique ? 0 : -ENOMEM;
927 1.2 riastrad }
928 1.2 riastrad EXPORT_SYMBOL(drm_dev_set_unique);
929 1.2 riastrad
930 1.5 riastrad #ifndef __NetBSD__
931 1.5 riastrad
932 1.2 riastrad /*
933 1.2 riastrad * DRM Core
934 1.2 riastrad * The DRM core module initializes all global DRM objects and makes them
935 1.2 riastrad * available to drivers. Once setup, drivers can probe their respective
936 1.2 riastrad * devices.
937 1.2 riastrad * Currently, core management includes:
938 1.2 riastrad * - The "DRM-Global" key/value database
939 1.2 riastrad * - Global ID management for connectors
940 1.2 riastrad * - DRM major number allocation
941 1.2 riastrad * - DRM minor management
942 1.2 riastrad * - DRM sysfs class
943 1.2 riastrad * - DRM debugfs root
944 1.2 riastrad *
945 1.2 riastrad * Furthermore, the DRM core provides dynamic char-dev lookups. For each
946 1.2 riastrad * interface registered on a DRM device, you can request minor numbers from DRM
947 1.2 riastrad * core. DRM core takes care of major-number management and char-dev
948 1.2 riastrad * registration. A stub ->open() callback forwards any open() requests to the
949 1.2 riastrad * registered minor.
950 1.2 riastrad */
951 1.2 riastrad
952 1.2 riastrad static int drm_stub_open(struct inode *inode, struct file *filp)
953 1.2 riastrad {
954 1.2 riastrad const struct file_operations *new_fops;
955 1.2 riastrad struct drm_minor *minor;
956 1.2 riastrad int err;
957 1.2 riastrad
958 1.2 riastrad DRM_DEBUG("\n");
959 1.2 riastrad
960 1.2 riastrad mutex_lock(&drm_global_mutex);
961 1.2 riastrad minor = drm_minor_acquire(iminor(inode));
962 1.2 riastrad if (IS_ERR(minor)) {
963 1.2 riastrad err = PTR_ERR(minor);
964 1.2 riastrad goto out_unlock;
965 1.2 riastrad }
966 1.2 riastrad
967 1.2 riastrad new_fops = fops_get(minor->dev->driver->fops);
968 1.2 riastrad if (!new_fops) {
969 1.2 riastrad err = -ENODEV;
970 1.2 riastrad goto out_release;
971 1.2 riastrad }
972 1.1 riastrad
973 1.2 riastrad replace_fops(filp, new_fops);
974 1.2 riastrad if (filp->f_op->open)
975 1.2 riastrad err = filp->f_op->open(inode, filp);
976 1.2 riastrad else
977 1.2 riastrad err = 0;
978 1.2 riastrad
979 1.2 riastrad out_release:
980 1.2 riastrad drm_minor_release(minor);
981 1.2 riastrad out_unlock:
982 1.2 riastrad mutex_unlock(&drm_global_mutex);
983 1.2 riastrad return err;
984 1.1 riastrad }
985 1.1 riastrad
986 1.1 riastrad static const struct file_operations drm_stub_fops = {
987 1.1 riastrad .owner = THIS_MODULE,
988 1.1 riastrad .open = drm_stub_open,
989 1.1 riastrad .llseek = noop_llseek,
990 1.1 riastrad };
991 1.1 riastrad
992 1.1 riastrad static int __init drm_core_init(void)
993 1.1 riastrad {
994 1.1 riastrad int ret = -ENOMEM;
995 1.1 riastrad
996 1.1 riastrad drm_global_init();
997 1.2 riastrad drm_connector_ida_init();
998 1.1 riastrad idr_init(&drm_minors_idr);
999 1.1 riastrad
1000 1.1 riastrad if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
1001 1.1 riastrad goto err_p1;
1002 1.1 riastrad
1003 1.2 riastrad ret = drm_sysfs_init();
1004 1.2 riastrad if (ret < 0) {
1005 1.1 riastrad printk(KERN_ERR "DRM: Error creating drm class.\n");
1006 1.1 riastrad goto err_p2;
1007 1.1 riastrad }
1008 1.1 riastrad
1009 1.1 riastrad drm_debugfs_root = debugfs_create_dir("dri", NULL);
1010 1.1 riastrad if (!drm_debugfs_root) {
1011 1.1 riastrad DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
1012 1.1 riastrad ret = -1;
1013 1.1 riastrad goto err_p3;
1014 1.1 riastrad }
1015 1.1 riastrad
1016 1.1 riastrad DRM_INFO("Initialized %s %d.%d.%d %s\n",
1017 1.1 riastrad CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1018 1.1 riastrad return 0;
1019 1.1 riastrad err_p3:
1020 1.1 riastrad drm_sysfs_destroy();
1021 1.1 riastrad err_p2:
1022 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1023 1.1 riastrad
1024 1.1 riastrad idr_destroy(&drm_minors_idr);
1025 1.1 riastrad err_p1:
1026 1.1 riastrad return ret;
1027 1.1 riastrad }
1028 1.1 riastrad
1029 1.1 riastrad static void __exit drm_core_exit(void)
1030 1.1 riastrad {
1031 1.1 riastrad debugfs_remove(drm_debugfs_root);
1032 1.1 riastrad drm_sysfs_destroy();
1033 1.1 riastrad
1034 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1035 1.1 riastrad
1036 1.2 riastrad drm_connector_ida_destroy();
1037 1.1 riastrad idr_destroy(&drm_minors_idr);
1038 1.1 riastrad }
1039 1.1 riastrad
1040 1.1 riastrad module_init(drm_core_init);
1041 1.1 riastrad module_exit(drm_core_exit);
1042 1.5 riastrad
1043 1.5 riastrad #endif
1044