drm_drv.c revision 1.5 1 1.2 riastrad /* $NetBSD: drm_drv.c,v 1.5 2018/08/27 06:54:08 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.5 2018/08/27 06:54:08 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.2 riastrad minor->kdev = drm_sysfs_minor_alloc(minor);
306 1.2 riastrad if (IS_ERR(minor->kdev)) {
307 1.2 riastrad r = PTR_ERR(minor->kdev);
308 1.2 riastrad goto err_index;
309 1.2 riastrad }
310 1.1 riastrad
311 1.2 riastrad *drm_minor_get_slot(dev, type) = minor;
312 1.2 riastrad return 0;
313 1.2 riastrad
314 1.2 riastrad err_index:
315 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
316 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
317 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
318 1.2 riastrad err_free:
319 1.2 riastrad kfree(minor);
320 1.2 riastrad return r;
321 1.2 riastrad }
322 1.2 riastrad
323 1.2 riastrad static void drm_minor_free(struct drm_device *dev, unsigned int type)
324 1.2 riastrad {
325 1.2 riastrad struct drm_minor **slot, *minor;
326 1.2 riastrad unsigned long flags;
327 1.1 riastrad
328 1.2 riastrad slot = drm_minor_get_slot(dev, type);
329 1.2 riastrad minor = *slot;
330 1.2 riastrad if (!minor)
331 1.2 riastrad return;
332 1.2 riastrad
333 1.5 riastrad #ifndef __NetBSD__
334 1.2 riastrad put_device(minor->kdev);
335 1.5 riastrad #endif
336 1.2 riastrad
337 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
338 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
339 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
340 1.1 riastrad
341 1.2 riastrad kfree(minor);
342 1.2 riastrad *slot = NULL;
343 1.2 riastrad }
344 1.2 riastrad
345 1.2 riastrad static int drm_minor_register(struct drm_device *dev, unsigned int type)
346 1.2 riastrad {
347 1.2 riastrad struct drm_minor *minor;
348 1.2 riastrad unsigned long flags;
349 1.2 riastrad #ifndef __NetBSD__
350 1.2 riastrad int ret;
351 1.2 riastrad #endif
352 1.2 riastrad
353 1.2 riastrad DRM_DEBUG("\n");
354 1.2 riastrad
355 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
356 1.2 riastrad if (!minor)
357 1.2 riastrad return 0;
358 1.2 riastrad
359 1.2 riastrad #ifndef __NetBSD__
360 1.2 riastrad ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
361 1.2 riastrad if (ret) {
362 1.2 riastrad DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
363 1.2 riastrad goto err_debugfs;
364 1.2 riastrad }
365 1.2 riastrad
366 1.2 riastrad ret = device_add(minor->kdev);
367 1.2 riastrad if (ret)
368 1.2 riastrad goto err_debugfs;
369 1.2 riastrad #endif
370 1.2 riastrad
371 1.2 riastrad /* replace NULL with @minor so lookups will succeed from now on */
372 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
373 1.2 riastrad idr_replace(&drm_minors_idr, minor, minor->index);
374 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
375 1.2 riastrad
376 1.2 riastrad DRM_DEBUG("new minor registered %d\n", minor->index);
377 1.2 riastrad return 0;
378 1.2 riastrad
379 1.2 riastrad #ifndef __NetBSD__
380 1.2 riastrad err_debugfs:
381 1.2 riastrad drm_debugfs_cleanup(minor);
382 1.2 riastrad return ret;
383 1.2 riastrad #endif
384 1.2 riastrad }
385 1.2 riastrad
386 1.2 riastrad static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
387 1.2 riastrad {
388 1.2 riastrad struct drm_minor *minor;
389 1.2 riastrad unsigned long flags;
390 1.1 riastrad
391 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
392 1.5 riastrad #ifdef __NetBSD__
393 1.5 riastrad if (!minor)
394 1.5 riastrad #else
395 1.2 riastrad if (!minor || !device_is_registered(minor->kdev))
396 1.5 riastrad #endif
397 1.2 riastrad return;
398 1.2 riastrad
399 1.2 riastrad /* replace @minor with NULL so lookups will fail from now on */
400 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
401 1.2 riastrad idr_replace(&drm_minors_idr, NULL, minor->index);
402 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
403 1.2 riastrad
404 1.5 riastrad #ifndef __NetBSD__
405 1.2 riastrad device_del(minor->kdev);
406 1.2 riastrad dev_set_drvdata(minor->kdev, NULL); /* safety belt */
407 1.2 riastrad drm_debugfs_cleanup(minor);
408 1.5 riastrad #endif
409 1.2 riastrad }
410 1.1 riastrad
411 1.1 riastrad /**
412 1.2 riastrad * drm_minor_acquire - Acquire a DRM minor
413 1.2 riastrad * @minor_id: Minor ID of the DRM-minor
414 1.1 riastrad *
415 1.2 riastrad * Looks up the given minor-ID and returns the respective DRM-minor object. The
416 1.2 riastrad * refence-count of the underlying device is increased so you must release this
417 1.2 riastrad * object with drm_minor_release().
418 1.1 riastrad *
419 1.2 riastrad * As long as you hold this minor, it is guaranteed that the object and the
420 1.2 riastrad * minor->dev pointer will stay valid! However, the device may get unplugged and
421 1.2 riastrad * unregistered while you hold the minor.
422 1.1 riastrad *
423 1.2 riastrad * Returns:
424 1.2 riastrad * Pointer to minor-object with increased device-refcount, or PTR_ERR on
425 1.2 riastrad * failure.
426 1.1 riastrad */
427 1.2 riastrad struct drm_minor *drm_minor_acquire(unsigned int minor_id)
428 1.1 riastrad {
429 1.2 riastrad struct drm_minor *minor;
430 1.2 riastrad unsigned long flags;
431 1.2 riastrad
432 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
433 1.2 riastrad minor = idr_find(&drm_minors_idr, minor_id);
434 1.2 riastrad if (minor)
435 1.2 riastrad drm_dev_ref(minor->dev);
436 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
437 1.2 riastrad
438 1.2 riastrad if (!minor) {
439 1.2 riastrad return ERR_PTR(-ENODEV);
440 1.2 riastrad } else if (drm_device_is_unplugged(minor->dev)) {
441 1.2 riastrad drm_dev_unref(minor->dev);
442 1.2 riastrad return ERR_PTR(-ENODEV);
443 1.2 riastrad }
444 1.2 riastrad
445 1.2 riastrad return minor;
446 1.2 riastrad }
447 1.1 riastrad
448 1.2 riastrad /**
449 1.2 riastrad * drm_minor_release - Release DRM minor
450 1.2 riastrad * @minor: Pointer to DRM minor object
451 1.2 riastrad *
452 1.2 riastrad * Release a minor that was previously acquired via drm_minor_acquire().
453 1.2 riastrad */
454 1.2 riastrad void drm_minor_release(struct drm_minor *minor)
455 1.2 riastrad {
456 1.2 riastrad drm_dev_unref(minor->dev);
457 1.2 riastrad }
458 1.2 riastrad
459 1.2 riastrad /**
460 1.2 riastrad * DOC: driver instance overview
461 1.2 riastrad *
462 1.2 riastrad * A device instance for a drm driver is represented by struct &drm_device. This
463 1.2 riastrad * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
464 1.2 riastrad * callbacks implemented by the driver. The driver then needs to initialize all
465 1.2 riastrad * the various subsystems for the drm device like memory management, vblank
466 1.2 riastrad * handling, modesetting support and intial output configuration plus obviously
467 1.2 riastrad * initialize all the corresponding hardware bits. An important part of this is
468 1.2 riastrad * also calling drm_dev_set_unique() to set the userspace-visible unique name of
469 1.2 riastrad * this device instance. Finally when everything is up and running and ready for
470 1.2 riastrad * userspace the device instance can be published using drm_dev_register().
471 1.2 riastrad *
472 1.2 riastrad * There is also deprecated support for initalizing device instances using
473 1.2 riastrad * bus-specific helpers and the ->load() callback. But due to
474 1.2 riastrad * backwards-compatibility needs the device instance have to be published too
475 1.2 riastrad * early, which requires unpretty global locking to make safe and is therefore
476 1.2 riastrad * only support for existing drivers not yet converted to the new scheme.
477 1.2 riastrad *
478 1.2 riastrad * When cleaning up a device instance everything needs to be done in reverse:
479 1.2 riastrad * First unpublish the device instance with drm_dev_unregister(). Then clean up
480 1.2 riastrad * any other resources allocated at device initialization and drop the driver's
481 1.2 riastrad * reference to &drm_device using drm_dev_unref().
482 1.2 riastrad *
483 1.2 riastrad * Note that the lifetime rules for &drm_device instance has still a lot of
484 1.2 riastrad * historical baggage. Hence use the reference counting provided by
485 1.2 riastrad * drm_dev_ref() and drm_dev_unref() only carefully.
486 1.2 riastrad *
487 1.2 riastrad * Also note that embedding of &drm_device is currently not (yet) supported (but
488 1.2 riastrad * it would be easy to add). Drivers can store driver-private data in the
489 1.2 riastrad * dev_priv field of &drm_device.
490 1.2 riastrad */
491 1.2 riastrad
492 1.2 riastrad /**
493 1.2 riastrad * drm_put_dev - Unregister and release a DRM device
494 1.2 riastrad * @dev: DRM device
495 1.2 riastrad *
496 1.2 riastrad * Called at module unload time or when a PCI device is unplugged.
497 1.2 riastrad *
498 1.2 riastrad * Cleans up all DRM device, calling drm_lastclose().
499 1.2 riastrad *
500 1.2 riastrad * Note: Use of this function is deprecated. It will eventually go away
501 1.2 riastrad * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
502 1.2 riastrad * instead to make sure that the device isn't userspace accessible any more
503 1.2 riastrad * while teardown is in progress, ensuring that userspace can't access an
504 1.2 riastrad * inconsistent state.
505 1.2 riastrad */
506 1.2 riastrad void drm_put_dev(struct drm_device *dev)
507 1.2 riastrad {
508 1.1 riastrad DRM_DEBUG("\n");
509 1.1 riastrad
510 1.2 riastrad if (!dev) {
511 1.2 riastrad DRM_ERROR("cleanup called no dev\n");
512 1.2 riastrad return;
513 1.2 riastrad }
514 1.2 riastrad
515 1.2 riastrad drm_dev_unregister(dev);
516 1.2 riastrad drm_dev_unref(dev);
517 1.2 riastrad }
518 1.2 riastrad EXPORT_SYMBOL(drm_put_dev);
519 1.2 riastrad
520 1.2 riastrad void drm_unplug_dev(struct drm_device *dev)
521 1.2 riastrad {
522 1.2 riastrad /* for a USB device */
523 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
524 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
525 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
526 1.2 riastrad
527 1.2 riastrad mutex_lock(&drm_global_mutex);
528 1.2 riastrad
529 1.2 riastrad drm_device_set_unplugged(dev);
530 1.2 riastrad
531 1.2 riastrad if (dev->open_count == 0) {
532 1.2 riastrad drm_put_dev(dev);
533 1.2 riastrad }
534 1.2 riastrad mutex_unlock(&drm_global_mutex);
535 1.2 riastrad }
536 1.2 riastrad EXPORT_SYMBOL(drm_unplug_dev);
537 1.2 riastrad
538 1.2 riastrad #ifdef __NetBSD__
539 1.2 riastrad
540 1.2 riastrad struct inode;
541 1.2 riastrad
542 1.2 riastrad static struct inode *
543 1.2 riastrad drm_fs_inode_new(void)
544 1.2 riastrad {
545 1.2 riastrad return NULL;
546 1.2 riastrad }
547 1.2 riastrad
548 1.2 riastrad static void
549 1.2 riastrad drm_fs_inode_free(struct inode *inode)
550 1.2 riastrad {
551 1.2 riastrad KASSERT(inode == NULL);
552 1.2 riastrad }
553 1.2 riastrad
554 1.2 riastrad #else
555 1.2 riastrad
556 1.2 riastrad /*
557 1.2 riastrad * DRM internal mount
558 1.2 riastrad * We want to be able to allocate our own "struct address_space" to control
559 1.2 riastrad * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
560 1.2 riastrad * stand-alone address_space objects, so we need an underlying inode. As there
561 1.2 riastrad * is no way to allocate an independent inode easily, we need a fake internal
562 1.2 riastrad * VFS mount-point.
563 1.2 riastrad *
564 1.2 riastrad * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
565 1.2 riastrad * frees it again. You are allowed to use iget() and iput() to get references to
566 1.2 riastrad * the inode. But each drm_fs_inode_new() call must be paired with exactly one
567 1.2 riastrad * drm_fs_inode_free() call (which does not have to be the last iput()).
568 1.2 riastrad * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
569 1.2 riastrad * between multiple inode-users. You could, technically, call
570 1.2 riastrad * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
571 1.2 riastrad * iput(), but this way you'd end up with a new vfsmount for each inode.
572 1.2 riastrad */
573 1.2 riastrad
574 1.2 riastrad static int drm_fs_cnt;
575 1.2 riastrad static struct vfsmount *drm_fs_mnt;
576 1.1 riastrad
577 1.2 riastrad static const struct dentry_operations drm_fs_dops = {
578 1.2 riastrad .d_dname = simple_dname,
579 1.2 riastrad };
580 1.2 riastrad
581 1.2 riastrad static const struct super_operations drm_fs_sops = {
582 1.2 riastrad .statfs = simple_statfs,
583 1.2 riastrad };
584 1.2 riastrad
585 1.2 riastrad static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
586 1.2 riastrad const char *dev_name, void *data)
587 1.2 riastrad {
588 1.2 riastrad return mount_pseudo(fs_type,
589 1.2 riastrad "drm:",
590 1.2 riastrad &drm_fs_sops,
591 1.2 riastrad &drm_fs_dops,
592 1.2 riastrad 0x010203ff);
593 1.2 riastrad }
594 1.2 riastrad
595 1.2 riastrad static struct file_system_type drm_fs_type = {
596 1.2 riastrad .name = "drm",
597 1.2 riastrad .owner = THIS_MODULE,
598 1.2 riastrad .mount = drm_fs_mount,
599 1.2 riastrad .kill_sb = kill_anon_super,
600 1.2 riastrad };
601 1.2 riastrad
602 1.2 riastrad static struct inode *drm_fs_inode_new(void)
603 1.2 riastrad {
604 1.2 riastrad struct inode *inode;
605 1.2 riastrad int r;
606 1.2 riastrad
607 1.2 riastrad r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
608 1.2 riastrad if (r < 0) {
609 1.2 riastrad DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
610 1.2 riastrad return ERR_PTR(r);
611 1.2 riastrad }
612 1.2 riastrad
613 1.2 riastrad inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
614 1.2 riastrad if (IS_ERR(inode))
615 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
616 1.2 riastrad
617 1.2 riastrad return inode;
618 1.2 riastrad }
619 1.2 riastrad
620 1.2 riastrad static void drm_fs_inode_free(struct inode *inode)
621 1.2 riastrad {
622 1.2 riastrad if (inode) {
623 1.2 riastrad iput(inode);
624 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
625 1.2 riastrad }
626 1.2 riastrad }
627 1.2 riastrad
628 1.2 riastrad #endif
629 1.1 riastrad
630 1.2 riastrad /**
631 1.2 riastrad * drm_dev_alloc - Allocate new DRM device
632 1.2 riastrad * @driver: DRM driver to allocate device for
633 1.2 riastrad * @parent: Parent device object
634 1.2 riastrad *
635 1.2 riastrad * Allocate and initialize a new DRM device. No device registration is done.
636 1.2 riastrad * Call drm_dev_register() to advertice the device to user space and register it
637 1.2 riastrad * with other core subsystems. This should be done last in the device
638 1.2 riastrad * initialization sequence to make sure userspace can't access an inconsistent
639 1.2 riastrad * state.
640 1.2 riastrad *
641 1.2 riastrad * The initial ref-count of the object is 1. Use drm_dev_ref() and
642 1.2 riastrad * drm_dev_unref() to take and drop further ref-counts.
643 1.2 riastrad *
644 1.2 riastrad * Note that for purely virtual devices @parent can be NULL.
645 1.2 riastrad *
646 1.2 riastrad * RETURNS:
647 1.2 riastrad * Pointer to new DRM device, or NULL if out of memory.
648 1.2 riastrad */
649 1.2 riastrad struct drm_device *drm_dev_alloc(struct drm_driver *driver,
650 1.2 riastrad struct device *parent)
651 1.2 riastrad {
652 1.2 riastrad struct drm_device *dev;
653 1.2 riastrad int ret;
654 1.2 riastrad
655 1.2 riastrad dev = kzalloc(sizeof(*dev), GFP_KERNEL);
656 1.2 riastrad if (!dev)
657 1.2 riastrad return NULL;
658 1.2 riastrad
659 1.2 riastrad kref_init(&dev->ref);
660 1.2 riastrad dev->dev = parent;
661 1.2 riastrad dev->driver = driver;
662 1.2 riastrad
663 1.2 riastrad INIT_LIST_HEAD(&dev->filelist);
664 1.2 riastrad INIT_LIST_HEAD(&dev->ctxlist);
665 1.2 riastrad INIT_LIST_HEAD(&dev->vmalist);
666 1.2 riastrad INIT_LIST_HEAD(&dev->maplist);
667 1.2 riastrad INIT_LIST_HEAD(&dev->vblank_event_list);
668 1.2 riastrad
669 1.2 riastrad spin_lock_init(&dev->buf_lock);
670 1.2 riastrad spin_lock_init(&dev->event_lock);
671 1.2 riastrad #ifdef __NetBSD__
672 1.2 riastrad linux_mutex_init(&dev->struct_mutex);
673 1.2 riastrad linux_mutex_init(&dev->ctxlist_mutex);
674 1.2 riastrad linux_mutex_init(&dev->master_mutex);
675 1.2 riastrad #else
676 1.2 riastrad mutex_init(&dev->struct_mutex);
677 1.2 riastrad mutex_init(&dev->ctxlist_mutex);
678 1.2 riastrad mutex_init(&dev->master_mutex);
679 1.2 riastrad #endif
680 1.1 riastrad
681 1.2 riastrad dev->anon_inode = drm_fs_inode_new();
682 1.2 riastrad if (IS_ERR(dev->anon_inode)) {
683 1.2 riastrad ret = PTR_ERR(dev->anon_inode);
684 1.2 riastrad DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
685 1.2 riastrad goto err_free;
686 1.2 riastrad }
687 1.1 riastrad
688 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) {
689 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
690 1.2 riastrad if (ret)
691 1.2 riastrad goto err_minors;
692 1.1 riastrad
693 1.2 riastrad WARN_ON(driver->suspend || driver->resume);
694 1.1 riastrad }
695 1.2 riastrad
696 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_RENDER)) {
697 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
698 1.2 riastrad if (ret)
699 1.2 riastrad goto err_minors;
700 1.1 riastrad }
701 1.1 riastrad
702 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
703 1.2 riastrad if (ret)
704 1.2 riastrad goto err_minors;
705 1.2 riastrad
706 1.2 riastrad if (drm_ht_create(&dev->map_hash, 12))
707 1.2 riastrad goto err_minors;
708 1.2 riastrad
709 1.2 riastrad drm_legacy_ctxbitmap_init(dev);
710 1.2 riastrad
711 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) {
712 1.2 riastrad ret = drm_gem_init(dev);
713 1.2 riastrad if (ret) {
714 1.2 riastrad DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
715 1.2 riastrad goto err_ctxbitmap;
716 1.2 riastrad }
717 1.1 riastrad }
718 1.1 riastrad
719 1.2 riastrad return dev;
720 1.2 riastrad
721 1.2 riastrad err_ctxbitmap:
722 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
723 1.2 riastrad drm_ht_remove(&dev->map_hash);
724 1.2 riastrad err_minors:
725 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
726 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
727 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
728 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
729 1.2 riastrad err_free:
730 1.2 riastrad #ifdef __NetBSD__
731 1.2 riastrad linux_mutex_destroy(&dev->struct_mutex);
732 1.2 riastrad linux_mutex_destroy(&dev->ctxlist_mutex);
733 1.2 riastrad linux_mutex_destroy(&dev->master_mutex);
734 1.2 riastrad spin_lock_destroy(&dev->event_lock);
735 1.2 riastrad #else
736 1.2 riastrad mutex_destroy(&dev->master_mutex);
737 1.2 riastrad #endif
738 1.2 riastrad kfree(dev);
739 1.2 riastrad return NULL;
740 1.2 riastrad }
741 1.2 riastrad EXPORT_SYMBOL(drm_dev_alloc);
742 1.2 riastrad
743 1.2 riastrad static void drm_dev_release(struct kref *ref)
744 1.2 riastrad {
745 1.2 riastrad struct drm_device *dev = container_of(ref, struct drm_device, ref);
746 1.2 riastrad
747 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM))
748 1.2 riastrad drm_gem_destroy(dev);
749 1.2 riastrad
750 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
751 1.2 riastrad drm_ht_remove(&dev->map_hash);
752 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
753 1.2 riastrad
754 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
755 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
756 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
757 1.2 riastrad
758 1.2 riastrad #ifdef __NetBSD__
759 1.2 riastrad linux_mutex_destroy(&dev->struct_mutex);
760 1.2 riastrad linux_mutex_destroy(&dev->ctxlist_mutex);
761 1.2 riastrad linux_mutex_destroy(&dev->master_mutex);
762 1.2 riastrad spin_lock_destroy(&dev->event_lock);
763 1.2 riastrad #else
764 1.2 riastrad mutex_destroy(&dev->master_mutex);
765 1.2 riastrad #endif
766 1.2 riastrad kfree(dev->unique);
767 1.2 riastrad kfree(dev);
768 1.2 riastrad }
769 1.2 riastrad
770 1.2 riastrad /**
771 1.2 riastrad * drm_dev_ref - Take reference of a DRM device
772 1.2 riastrad * @dev: device to take reference of or NULL
773 1.2 riastrad *
774 1.2 riastrad * This increases the ref-count of @dev by one. You *must* already own a
775 1.2 riastrad * reference when calling this. Use drm_dev_unref() to drop this reference
776 1.2 riastrad * again.
777 1.2 riastrad *
778 1.2 riastrad * This function never fails. However, this function does not provide *any*
779 1.2 riastrad * guarantee whether the device is alive or running. It only provides a
780 1.2 riastrad * reference to the object and the memory associated with it.
781 1.2 riastrad */
782 1.2 riastrad void drm_dev_ref(struct drm_device *dev)
783 1.2 riastrad {
784 1.2 riastrad if (dev)
785 1.2 riastrad kref_get(&dev->ref);
786 1.2 riastrad }
787 1.2 riastrad EXPORT_SYMBOL(drm_dev_ref);
788 1.2 riastrad
789 1.2 riastrad /**
790 1.2 riastrad * drm_dev_unref - Drop reference of a DRM device
791 1.2 riastrad * @dev: device to drop reference of or NULL
792 1.2 riastrad *
793 1.2 riastrad * This decreases the ref-count of @dev by one. The device is destroyed if the
794 1.2 riastrad * ref-count drops to zero.
795 1.2 riastrad */
796 1.2 riastrad void drm_dev_unref(struct drm_device *dev)
797 1.2 riastrad {
798 1.2 riastrad if (dev)
799 1.2 riastrad kref_put(&dev->ref, drm_dev_release);
800 1.2 riastrad }
801 1.2 riastrad EXPORT_SYMBOL(drm_dev_unref);
802 1.2 riastrad
803 1.2 riastrad /**
804 1.2 riastrad * drm_dev_register - Register DRM device
805 1.2 riastrad * @dev: Device to register
806 1.2 riastrad * @flags: Flags passed to the driver's .load() function
807 1.2 riastrad *
808 1.2 riastrad * Register the DRM device @dev with the system, advertise device to user-space
809 1.2 riastrad * and start normal device operation. @dev must be allocated via drm_dev_alloc()
810 1.2 riastrad * previously.
811 1.2 riastrad *
812 1.2 riastrad * Never call this twice on any device!
813 1.2 riastrad *
814 1.2 riastrad * NOTE: To ensure backward compatibility with existing drivers method this
815 1.2 riastrad * function calls the ->load() method after registering the device nodes,
816 1.2 riastrad * creating race conditions. Usage of the ->load() methods is therefore
817 1.2 riastrad * deprecated, drivers must perform all initialization before calling
818 1.2 riastrad * drm_dev_register().
819 1.2 riastrad *
820 1.2 riastrad * RETURNS:
821 1.2 riastrad * 0 on success, negative error code on failure.
822 1.2 riastrad */
823 1.2 riastrad int drm_dev_register(struct drm_device *dev, unsigned long flags)
824 1.2 riastrad {
825 1.2 riastrad int ret;
826 1.2 riastrad
827 1.2 riastrad #ifndef __NetBSD__
828 1.2 riastrad mutex_lock(&drm_global_mutex);
829 1.2 riastrad #endif
830 1.2 riastrad
831 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
832 1.2 riastrad if (ret)
833 1.2 riastrad goto err_minors;
834 1.2 riastrad
835 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_RENDER);
836 1.2 riastrad if (ret)
837 1.2 riastrad goto err_minors;
838 1.2 riastrad
839 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
840 1.2 riastrad if (ret)
841 1.2 riastrad goto err_minors;
842 1.2 riastrad
843 1.2 riastrad if (dev->driver->load) {
844 1.2 riastrad ret = dev->driver->load(dev, flags);
845 1.2 riastrad if (ret)
846 1.2 riastrad goto err_minors;
847 1.2 riastrad }
848 1.2 riastrad
849 1.2 riastrad ret = 0;
850 1.2 riastrad goto out_unlock;
851 1.2 riastrad
852 1.2 riastrad err_minors:
853 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
854 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
855 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
856 1.2 riastrad out_unlock:
857 1.2 riastrad #ifndef __NetBSD__
858 1.2 riastrad mutex_unlock(&drm_global_mutex);
859 1.2 riastrad #endif
860 1.2 riastrad return ret;
861 1.2 riastrad }
862 1.2 riastrad EXPORT_SYMBOL(drm_dev_register);
863 1.2 riastrad
864 1.2 riastrad /**
865 1.2 riastrad * drm_dev_unregister - Unregister DRM device
866 1.2 riastrad * @dev: Device to unregister
867 1.2 riastrad *
868 1.2 riastrad * Unregister the DRM device from the system. This does the reverse of
869 1.2 riastrad * drm_dev_register() but does not deallocate the device. The caller must call
870 1.2 riastrad * drm_dev_unref() to drop their final reference.
871 1.2 riastrad *
872 1.2 riastrad * This should be called first in the device teardown code to make sure
873 1.2 riastrad * userspace can't access the device instance any more.
874 1.2 riastrad */
875 1.2 riastrad void drm_dev_unregister(struct drm_device *dev)
876 1.2 riastrad {
877 1.2 riastrad struct drm_map_list *r_list, *list_temp;
878 1.2 riastrad
879 1.2 riastrad drm_lastclose(dev);
880 1.2 riastrad
881 1.2 riastrad if (dev->driver->unload)
882 1.2 riastrad dev->driver->unload(dev);
883 1.2 riastrad
884 1.2 riastrad #ifndef __NetBSD__ /* Moved to drm_pci. */
885 1.2 riastrad if (dev->agp)
886 1.2 riastrad drm_pci_agp_destroy(dev);
887 1.2 riastrad #endif
888 1.2 riastrad
889 1.2 riastrad drm_vblank_cleanup(dev);
890 1.1 riastrad
891 1.2 riastrad list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
892 1.2 riastrad drm_legacy_rmmap(dev, r_list->map);
893 1.2 riastrad
894 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
895 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
896 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
897 1.2 riastrad }
898 1.2 riastrad EXPORT_SYMBOL(drm_dev_unregister);
899 1.2 riastrad
900 1.2 riastrad /**
901 1.2 riastrad * drm_dev_set_unique - Set the unique name of a DRM device
902 1.2 riastrad * @dev: device of which to set the unique name
903 1.2 riastrad * @fmt: format string for unique name
904 1.2 riastrad *
905 1.2 riastrad * Sets the unique name of a DRM device using the specified format string and
906 1.2 riastrad * a variable list of arguments. Drivers can use this at driver probe time if
907 1.2 riastrad * the unique name of the devices they drive is static.
908 1.2 riastrad *
909 1.2 riastrad * Return: 0 on success or a negative error code on failure.
910 1.2 riastrad */
911 1.2 riastrad int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
912 1.2 riastrad {
913 1.2 riastrad va_list ap;
914 1.2 riastrad
915 1.2 riastrad kfree(dev->unique);
916 1.2 riastrad
917 1.2 riastrad va_start(ap, fmt);
918 1.2 riastrad dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
919 1.2 riastrad va_end(ap);
920 1.2 riastrad
921 1.2 riastrad return dev->unique ? 0 : -ENOMEM;
922 1.2 riastrad }
923 1.2 riastrad EXPORT_SYMBOL(drm_dev_set_unique);
924 1.2 riastrad
925 1.5 riastrad #ifndef __NetBSD__
926 1.5 riastrad
927 1.2 riastrad /*
928 1.2 riastrad * DRM Core
929 1.2 riastrad * The DRM core module initializes all global DRM objects and makes them
930 1.2 riastrad * available to drivers. Once setup, drivers can probe their respective
931 1.2 riastrad * devices.
932 1.2 riastrad * Currently, core management includes:
933 1.2 riastrad * - The "DRM-Global" key/value database
934 1.2 riastrad * - Global ID management for connectors
935 1.2 riastrad * - DRM major number allocation
936 1.2 riastrad * - DRM minor management
937 1.2 riastrad * - DRM sysfs class
938 1.2 riastrad * - DRM debugfs root
939 1.2 riastrad *
940 1.2 riastrad * Furthermore, the DRM core provides dynamic char-dev lookups. For each
941 1.2 riastrad * interface registered on a DRM device, you can request minor numbers from DRM
942 1.2 riastrad * core. DRM core takes care of major-number management and char-dev
943 1.2 riastrad * registration. A stub ->open() callback forwards any open() requests to the
944 1.2 riastrad * registered minor.
945 1.2 riastrad */
946 1.2 riastrad
947 1.2 riastrad static int drm_stub_open(struct inode *inode, struct file *filp)
948 1.2 riastrad {
949 1.2 riastrad const struct file_operations *new_fops;
950 1.2 riastrad struct drm_minor *minor;
951 1.2 riastrad int err;
952 1.2 riastrad
953 1.2 riastrad DRM_DEBUG("\n");
954 1.2 riastrad
955 1.2 riastrad mutex_lock(&drm_global_mutex);
956 1.2 riastrad minor = drm_minor_acquire(iminor(inode));
957 1.2 riastrad if (IS_ERR(minor)) {
958 1.2 riastrad err = PTR_ERR(minor);
959 1.2 riastrad goto out_unlock;
960 1.2 riastrad }
961 1.2 riastrad
962 1.2 riastrad new_fops = fops_get(minor->dev->driver->fops);
963 1.2 riastrad if (!new_fops) {
964 1.2 riastrad err = -ENODEV;
965 1.2 riastrad goto out_release;
966 1.2 riastrad }
967 1.1 riastrad
968 1.2 riastrad replace_fops(filp, new_fops);
969 1.2 riastrad if (filp->f_op->open)
970 1.2 riastrad err = filp->f_op->open(inode, filp);
971 1.2 riastrad else
972 1.2 riastrad err = 0;
973 1.2 riastrad
974 1.2 riastrad out_release:
975 1.2 riastrad drm_minor_release(minor);
976 1.2 riastrad out_unlock:
977 1.2 riastrad mutex_unlock(&drm_global_mutex);
978 1.2 riastrad return err;
979 1.1 riastrad }
980 1.1 riastrad
981 1.1 riastrad static const struct file_operations drm_stub_fops = {
982 1.1 riastrad .owner = THIS_MODULE,
983 1.1 riastrad .open = drm_stub_open,
984 1.1 riastrad .llseek = noop_llseek,
985 1.1 riastrad };
986 1.1 riastrad
987 1.1 riastrad static int __init drm_core_init(void)
988 1.1 riastrad {
989 1.1 riastrad int ret = -ENOMEM;
990 1.1 riastrad
991 1.1 riastrad drm_global_init();
992 1.2 riastrad drm_connector_ida_init();
993 1.1 riastrad idr_init(&drm_minors_idr);
994 1.1 riastrad
995 1.1 riastrad if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
996 1.1 riastrad goto err_p1;
997 1.1 riastrad
998 1.2 riastrad ret = drm_sysfs_init();
999 1.2 riastrad if (ret < 0) {
1000 1.1 riastrad printk(KERN_ERR "DRM: Error creating drm class.\n");
1001 1.1 riastrad goto err_p2;
1002 1.1 riastrad }
1003 1.1 riastrad
1004 1.1 riastrad drm_debugfs_root = debugfs_create_dir("dri", NULL);
1005 1.1 riastrad if (!drm_debugfs_root) {
1006 1.1 riastrad DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
1007 1.1 riastrad ret = -1;
1008 1.1 riastrad goto err_p3;
1009 1.1 riastrad }
1010 1.1 riastrad
1011 1.1 riastrad DRM_INFO("Initialized %s %d.%d.%d %s\n",
1012 1.1 riastrad CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1013 1.1 riastrad return 0;
1014 1.1 riastrad err_p3:
1015 1.1 riastrad drm_sysfs_destroy();
1016 1.1 riastrad err_p2:
1017 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1018 1.1 riastrad
1019 1.1 riastrad idr_destroy(&drm_minors_idr);
1020 1.1 riastrad err_p1:
1021 1.1 riastrad return ret;
1022 1.1 riastrad }
1023 1.1 riastrad
1024 1.1 riastrad static void __exit drm_core_exit(void)
1025 1.1 riastrad {
1026 1.1 riastrad debugfs_remove(drm_debugfs_root);
1027 1.1 riastrad drm_sysfs_destroy();
1028 1.1 riastrad
1029 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1030 1.1 riastrad
1031 1.2 riastrad drm_connector_ida_destroy();
1032 1.1 riastrad idr_destroy(&drm_minors_idr);
1033 1.1 riastrad }
1034 1.1 riastrad
1035 1.1 riastrad module_init(drm_core_init);
1036 1.1 riastrad module_exit(drm_core_exit);
1037 1.5 riastrad
1038 1.5 riastrad #endif
1039