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