drm/radeon: add a dpm quirk for all R7 370 parts
[linux/fpc-iii.git] / drivers / gpu / drm / drm_crtc.c
blob0dd2c26c55ab8fcda55d4bd990799bcb04ad578a
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 /**
45 * drm_modeset_lock_all - take all modeset locks
46 * @dev: drm device
48 * This function takes all modeset locks, suitable where a more fine-grained
49 * scheme isn't (yet) implemented. Locks must be dropped with
50 * drm_modeset_unlock_all.
52 void drm_modeset_lock_all(struct drm_device *dev)
54 struct drm_mode_config *config = &dev->mode_config;
55 struct drm_modeset_acquire_ctx *ctx;
56 int ret;
58 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
59 if (WARN_ON(!ctx))
60 return;
62 mutex_lock(&config->mutex);
64 drm_modeset_acquire_init(ctx, 0);
66 retry:
67 ret = drm_modeset_lock(&config->connection_mutex, ctx);
68 if (ret)
69 goto fail;
70 ret = drm_modeset_lock_all_crtcs(dev, ctx);
71 if (ret)
72 goto fail;
74 WARN_ON(config->acquire_ctx);
76 /* now we hold the locks, so now that it is safe, stash the
77 * ctx for drm_modeset_unlock_all():
79 config->acquire_ctx = ctx;
81 drm_warn_on_modeset_not_all_locked(dev);
83 return;
85 fail:
86 if (ret == -EDEADLK) {
87 drm_modeset_backoff(ctx);
88 goto retry;
91 EXPORT_SYMBOL(drm_modeset_lock_all);
93 /**
94 * drm_modeset_unlock_all - drop all modeset locks
95 * @dev: device
97 * This function drop all modeset locks taken by drm_modeset_lock_all.
99 void drm_modeset_unlock_all(struct drm_device *dev)
101 struct drm_mode_config *config = &dev->mode_config;
102 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
104 if (WARN_ON(!ctx))
105 return;
107 config->acquire_ctx = NULL;
108 drm_modeset_drop_locks(ctx);
109 drm_modeset_acquire_fini(ctx);
111 kfree(ctx);
113 mutex_unlock(&dev->mode_config.mutex);
115 EXPORT_SYMBOL(drm_modeset_unlock_all);
118 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
119 * @dev: device
121 * Useful as a debug assert.
123 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
125 struct drm_crtc *crtc;
127 /* Locking is currently fubar in the panic handler. */
128 if (oops_in_progress)
129 return;
131 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
132 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
134 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
135 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
137 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
139 /* Avoid boilerplate. I'm tired of typing. */
140 #define DRM_ENUM_NAME_FN(fnname, list) \
141 const char *fnname(int val) \
143 int i; \
144 for (i = 0; i < ARRAY_SIZE(list); i++) { \
145 if (list[i].type == val) \
146 return list[i].name; \
148 return "(unknown)"; \
152 * Global properties
154 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
155 { { DRM_MODE_DPMS_ON, "On" },
156 { DRM_MODE_DPMS_STANDBY, "Standby" },
157 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
158 { DRM_MODE_DPMS_OFF, "Off" }
161 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
163 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
165 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
166 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
167 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
171 * Optional properties
173 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
175 { DRM_MODE_SCALE_NONE, "None" },
176 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
177 { DRM_MODE_SCALE_CENTER, "Center" },
178 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
182 * Non-global properties, but "required" for certain connectors.
184 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
186 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
187 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
188 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
191 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
193 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
195 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
196 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
197 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
200 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
201 drm_dvi_i_subconnector_enum_list)
203 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
205 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
207 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
208 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
209 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
212 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
214 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
216 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
217 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
219 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
220 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
223 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
224 drm_tv_subconnector_enum_list)
226 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
227 { DRM_MODE_DIRTY_OFF, "Off" },
228 { DRM_MODE_DIRTY_ON, "On" },
229 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
232 struct drm_conn_prop_enum_list {
233 int type;
234 const char *name;
235 struct ida ida;
239 * Connector and encoder types.
241 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
242 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
243 { DRM_MODE_CONNECTOR_VGA, "VGA" },
244 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
245 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
246 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
247 { DRM_MODE_CONNECTOR_Composite, "Composite" },
248 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
249 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
250 { DRM_MODE_CONNECTOR_Component, "Component" },
251 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
252 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
253 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
254 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
255 { DRM_MODE_CONNECTOR_TV, "TV" },
256 { DRM_MODE_CONNECTOR_eDP, "eDP" },
257 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
258 { DRM_MODE_CONNECTOR_DSI, "DSI" },
261 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
262 { { DRM_MODE_ENCODER_NONE, "None" },
263 { DRM_MODE_ENCODER_DAC, "DAC" },
264 { DRM_MODE_ENCODER_TMDS, "TMDS" },
265 { DRM_MODE_ENCODER_LVDS, "LVDS" },
266 { DRM_MODE_ENCODER_TVDAC, "TV" },
267 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
268 { DRM_MODE_ENCODER_DSI, "DSI" },
269 { DRM_MODE_ENCODER_DPMST, "DP MST" },
272 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
274 { SubPixelUnknown, "Unknown" },
275 { SubPixelHorizontalRGB, "Horizontal RGB" },
276 { SubPixelHorizontalBGR, "Horizontal BGR" },
277 { SubPixelVerticalRGB, "Vertical RGB" },
278 { SubPixelVerticalBGR, "Vertical BGR" },
279 { SubPixelNone, "None" },
282 void drm_connector_ida_init(void)
284 int i;
286 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
287 ida_init(&drm_connector_enum_list[i].ida);
290 void drm_connector_ida_destroy(void)
292 int i;
294 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
295 ida_destroy(&drm_connector_enum_list[i].ida);
299 * drm_get_connector_status_name - return a string for connector status
300 * @status: connector status to compute name of
302 * In contrast to the other drm_get_*_name functions this one here returns a
303 * const pointer and hence is threadsafe.
305 const char *drm_get_connector_status_name(enum drm_connector_status status)
307 if (status == connector_status_connected)
308 return "connected";
309 else if (status == connector_status_disconnected)
310 return "disconnected";
311 else
312 return "unknown";
314 EXPORT_SYMBOL(drm_get_connector_status_name);
317 * drm_get_subpixel_order_name - return a string for a given subpixel enum
318 * @order: enum of subpixel_order
320 * Note you could abuse this and return something out of bounds, but that
321 * would be a caller error. No unscrubbed user data should make it here.
323 const char *drm_get_subpixel_order_name(enum subpixel_order order)
325 return drm_subpixel_enum_list[order].name;
327 EXPORT_SYMBOL(drm_get_subpixel_order_name);
329 static char printable_char(int c)
331 return isascii(c) && isprint(c) ? c : '?';
335 * drm_get_format_name - return a string for drm fourcc format
336 * @format: format to compute name of
338 * Note that the buffer used by this function is globally shared and owned by
339 * the function itself.
341 * FIXME: This isn't really multithreading safe.
343 const char *drm_get_format_name(uint32_t format)
345 static char buf[32];
347 snprintf(buf, sizeof(buf),
348 "%c%c%c%c %s-endian (0x%08x)",
349 printable_char(format & 0xff),
350 printable_char((format >> 8) & 0xff),
351 printable_char((format >> 16) & 0xff),
352 printable_char((format >> 24) & 0x7f),
353 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
354 format);
356 return buf;
358 EXPORT_SYMBOL(drm_get_format_name);
361 * drm_mode_object_get - allocate a new modeset identifier
362 * @dev: DRM device
363 * @obj: object pointer, used to generate unique ID
364 * @obj_type: object type
366 * Create a unique identifier based on @ptr in @dev's identifier space. Used
367 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368 * modeset identifiers are _not_ reference counted. Hence don't use this for
369 * reference counted modeset objects like framebuffers.
371 * Returns:
372 * Zero on success, error code on failure.
374 int drm_mode_object_get(struct drm_device *dev,
375 struct drm_mode_object *obj, uint32_t obj_type)
377 int ret;
379 mutex_lock(&dev->mode_config.idr_mutex);
380 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
381 if (ret >= 0) {
383 * Set up the object linking under the protection of the idr
384 * lock so that other users can't see inconsistent state.
386 obj->id = ret;
387 obj->type = obj_type;
389 mutex_unlock(&dev->mode_config.idr_mutex);
391 return ret < 0 ? ret : 0;
395 * drm_mode_object_put - free a modeset identifer
396 * @dev: DRM device
397 * @object: object to free
399 * Free @id from @dev's unique identifier pool. Note that despite the _get
400 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
401 * for reference counted modeset objects like framebuffers.
403 void drm_mode_object_put(struct drm_device *dev,
404 struct drm_mode_object *object)
406 mutex_lock(&dev->mode_config.idr_mutex);
407 idr_remove(&dev->mode_config.crtc_idr, object->id);
408 mutex_unlock(&dev->mode_config.idr_mutex);
411 static struct drm_mode_object *_object_find(struct drm_device *dev,
412 uint32_t id, uint32_t type)
414 struct drm_mode_object *obj = NULL;
416 mutex_lock(&dev->mode_config.idr_mutex);
417 obj = idr_find(&dev->mode_config.crtc_idr, id);
418 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
419 (obj->id != id))
420 obj = NULL;
421 mutex_unlock(&dev->mode_config.idr_mutex);
423 return obj;
427 * drm_mode_object_find - look up a drm object with static lifetime
428 * @dev: drm device
429 * @id: id of the mode object
430 * @type: type of the mode object
432 * Note that framebuffers cannot be looked up with this functions - since those
433 * are reference counted, they need special treatment. Even with
434 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
435 * rather than WARN_ON()).
437 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
438 uint32_t id, uint32_t type)
440 struct drm_mode_object *obj = NULL;
442 /* Framebuffers are reference counted and need their own lookup
443 * function.*/
444 WARN_ON(type == DRM_MODE_OBJECT_FB);
445 obj = _object_find(dev, id, type);
446 /* don't leak out unref'd fb's */
447 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
448 obj = NULL;
449 return obj;
451 EXPORT_SYMBOL(drm_mode_object_find);
454 * drm_framebuffer_init - initialize a framebuffer
455 * @dev: DRM device
456 * @fb: framebuffer to be initialized
457 * @funcs: ... with these functions
459 * Allocates an ID for the framebuffer's parent mode object, sets its mode
460 * functions & device file and adds it to the master fd list.
462 * IMPORTANT:
463 * This functions publishes the fb and makes it available for concurrent access
464 * by other users. Which means by this point the fb _must_ be fully set up -
465 * since all the fb attributes are invariant over its lifetime, no further
466 * locking but only correct reference counting is required.
468 * Returns:
469 * Zero on success, error code on failure.
471 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
472 const struct drm_framebuffer_funcs *funcs)
474 int ret;
476 mutex_lock(&dev->mode_config.fb_lock);
477 kref_init(&fb->refcount);
478 INIT_LIST_HEAD(&fb->filp_head);
479 fb->dev = dev;
480 fb->funcs = funcs;
482 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
483 if (ret)
484 goto out;
486 /* Grab the idr reference. */
487 drm_framebuffer_reference(fb);
489 dev->mode_config.num_fb++;
490 list_add(&fb->head, &dev->mode_config.fb_list);
491 out:
492 mutex_unlock(&dev->mode_config.fb_lock);
494 return ret;
496 EXPORT_SYMBOL(drm_framebuffer_init);
498 static void drm_framebuffer_free(struct kref *kref)
500 struct drm_framebuffer *fb =
501 container_of(kref, struct drm_framebuffer, refcount);
502 fb->funcs->destroy(fb);
505 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
506 uint32_t id)
508 struct drm_mode_object *obj = NULL;
509 struct drm_framebuffer *fb;
511 mutex_lock(&dev->mode_config.idr_mutex);
512 obj = idr_find(&dev->mode_config.crtc_idr, id);
513 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
514 fb = NULL;
515 else
516 fb = obj_to_fb(obj);
517 mutex_unlock(&dev->mode_config.idr_mutex);
519 return fb;
523 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
524 * @dev: drm device
525 * @id: id of the fb object
527 * If successful, this grabs an additional reference to the framebuffer -
528 * callers need to make sure to eventually unreference the returned framebuffer
529 * again, using @drm_framebuffer_unreference.
531 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
532 uint32_t id)
534 struct drm_framebuffer *fb;
536 mutex_lock(&dev->mode_config.fb_lock);
537 fb = __drm_framebuffer_lookup(dev, id);
538 if (fb)
539 drm_framebuffer_reference(fb);
540 mutex_unlock(&dev->mode_config.fb_lock);
542 return fb;
544 EXPORT_SYMBOL(drm_framebuffer_lookup);
547 * drm_framebuffer_unreference - unref a framebuffer
548 * @fb: framebuffer to unref
550 * This functions decrements the fb's refcount and frees it if it drops to zero.
552 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
554 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
555 kref_put(&fb->refcount, drm_framebuffer_free);
557 EXPORT_SYMBOL(drm_framebuffer_unreference);
560 * drm_framebuffer_reference - incr the fb refcnt
561 * @fb: framebuffer
563 * This functions increments the fb's refcount.
565 void drm_framebuffer_reference(struct drm_framebuffer *fb)
567 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
568 kref_get(&fb->refcount);
570 EXPORT_SYMBOL(drm_framebuffer_reference);
572 static void drm_framebuffer_free_bug(struct kref *kref)
574 BUG();
577 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
579 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
580 kref_put(&fb->refcount, drm_framebuffer_free_bug);
583 /* dev->mode_config.fb_lock must be held! */
584 static void __drm_framebuffer_unregister(struct drm_device *dev,
585 struct drm_framebuffer *fb)
587 mutex_lock(&dev->mode_config.idr_mutex);
588 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
589 mutex_unlock(&dev->mode_config.idr_mutex);
591 fb->base.id = 0;
593 __drm_framebuffer_unreference(fb);
597 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
598 * @fb: fb to unregister
600 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
601 * those used for fbdev. Note that the caller must hold a reference of it's own,
602 * i.e. the object may not be destroyed through this call (since it'll lead to a
603 * locking inversion).
605 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
607 struct drm_device *dev = fb->dev;
609 mutex_lock(&dev->mode_config.fb_lock);
610 /* Mark fb as reaped and drop idr ref. */
611 __drm_framebuffer_unregister(dev, fb);
612 mutex_unlock(&dev->mode_config.fb_lock);
614 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
617 * drm_framebuffer_cleanup - remove a framebuffer object
618 * @fb: framebuffer to remove
620 * Cleanup framebuffer. This function is intended to be used from the drivers
621 * ->destroy callback. It can also be used to clean up driver private
622 * framebuffers embedded into a larger structure.
624 * Note that this function does not remove the fb from active usuage - if it is
625 * still used anywhere, hilarity can ensue since userspace could call getfb on
626 * the id and get back -EINVAL. Obviously no concern at driver unload time.
628 * Also, the framebuffer will not be removed from the lookup idr - for
629 * user-created framebuffers this will happen in in the rmfb ioctl. For
630 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
631 * drm_framebuffer_unregister_private.
633 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
635 struct drm_device *dev = fb->dev;
637 mutex_lock(&dev->mode_config.fb_lock);
638 list_del(&fb->head);
639 dev->mode_config.num_fb--;
640 mutex_unlock(&dev->mode_config.fb_lock);
642 EXPORT_SYMBOL(drm_framebuffer_cleanup);
645 * drm_framebuffer_remove - remove and unreference a framebuffer object
646 * @fb: framebuffer to remove
648 * Scans all the CRTCs and planes in @dev's mode_config. If they're
649 * using @fb, removes it, setting it to NULL. Then drops the reference to the
650 * passed-in framebuffer. Might take the modeset locks.
652 * Note that this function optimizes the cleanup away if the caller holds the
653 * last reference to the framebuffer. It is also guaranteed to not take the
654 * modeset locks in this case.
656 void drm_framebuffer_remove(struct drm_framebuffer *fb)
658 struct drm_device *dev = fb->dev;
659 struct drm_crtc *crtc;
660 struct drm_plane *plane;
661 struct drm_mode_set set;
662 int ret;
664 WARN_ON(!list_empty(&fb->filp_head));
667 * drm ABI mandates that we remove any deleted framebuffers from active
668 * useage. But since most sane clients only remove framebuffers they no
669 * longer need, try to optimize this away.
671 * Since we're holding a reference ourselves, observing a refcount of 1
672 * means that we're the last holder and can skip it. Also, the refcount
673 * can never increase from 1 again, so we don't need any barriers or
674 * locks.
676 * Note that userspace could try to race with use and instate a new
677 * usage _after_ we've cleared all current ones. End result will be an
678 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
679 * in this manner.
681 if (atomic_read(&fb->refcount.refcount) > 1) {
682 drm_modeset_lock_all(dev);
683 /* remove from any CRTC */
684 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
685 if (crtc->primary->fb == fb) {
686 /* should turn off the crtc */
687 memset(&set, 0, sizeof(struct drm_mode_set));
688 set.crtc = crtc;
689 set.fb = NULL;
690 ret = drm_mode_set_config_internal(&set);
691 if (ret)
692 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
696 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
697 if (plane->fb == fb)
698 drm_plane_force_disable(plane);
700 drm_modeset_unlock_all(dev);
703 drm_framebuffer_unreference(fb);
705 EXPORT_SYMBOL(drm_framebuffer_remove);
707 DEFINE_WW_CLASS(crtc_ww_class);
710 * drm_crtc_init_with_planes - Initialise a new CRTC object with
711 * specified primary and cursor planes.
712 * @dev: DRM device
713 * @crtc: CRTC object to init
714 * @primary: Primary plane for CRTC
715 * @cursor: Cursor plane for CRTC
716 * @funcs: callbacks for the new CRTC
718 * Inits a new object created as base part of a driver crtc object.
720 * Returns:
721 * Zero on success, error code on failure.
723 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
724 struct drm_plane *primary,
725 void *cursor,
726 const struct drm_crtc_funcs *funcs)
728 struct drm_mode_config *config = &dev->mode_config;
729 int ret;
731 crtc->dev = dev;
732 crtc->funcs = funcs;
733 crtc->invert_dimensions = false;
735 drm_modeset_lock_all(dev);
736 drm_modeset_lock_init(&crtc->mutex);
737 /* dropped by _unlock_all(): */
738 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
740 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
741 if (ret)
742 goto out;
744 crtc->base.properties = &crtc->properties;
746 list_add_tail(&crtc->head, &config->crtc_list);
747 config->num_crtc++;
749 crtc->primary = primary;
750 if (primary)
751 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
753 out:
754 drm_modeset_unlock_all(dev);
756 return ret;
758 EXPORT_SYMBOL(drm_crtc_init_with_planes);
761 * drm_crtc_cleanup - Clean up the core crtc usage
762 * @crtc: CRTC to cleanup
764 * This function cleans up @crtc and removes it from the DRM mode setting
765 * core. Note that the function does *not* free the crtc structure itself,
766 * this is the responsibility of the caller.
768 void drm_crtc_cleanup(struct drm_crtc *crtc)
770 struct drm_device *dev = crtc->dev;
772 kfree(crtc->gamma_store);
773 crtc->gamma_store = NULL;
775 drm_modeset_lock_fini(&crtc->mutex);
777 drm_mode_object_put(dev, &crtc->base);
778 list_del(&crtc->head);
779 dev->mode_config.num_crtc--;
781 EXPORT_SYMBOL(drm_crtc_cleanup);
784 * drm_crtc_index - find the index of a registered CRTC
785 * @crtc: CRTC to find index for
787 * Given a registered CRTC, return the index of that CRTC within a DRM
788 * device's list of CRTCs.
790 unsigned int drm_crtc_index(struct drm_crtc *crtc)
792 unsigned int index = 0;
793 struct drm_crtc *tmp;
795 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
796 if (tmp == crtc)
797 return index;
799 index++;
802 BUG();
804 EXPORT_SYMBOL(drm_crtc_index);
807 * drm_mode_remove - remove and free a mode
808 * @connector: connector list to modify
809 * @mode: mode to remove
811 * Remove @mode from @connector's mode list, then free it.
813 static void drm_mode_remove(struct drm_connector *connector,
814 struct drm_display_mode *mode)
816 list_del(&mode->head);
817 drm_mode_destroy(connector->dev, mode);
821 * drm_connector_init - Init a preallocated connector
822 * @dev: DRM device
823 * @connector: the connector to init
824 * @funcs: callbacks for this connector
825 * @connector_type: user visible type of the connector
827 * Initialises a preallocated connector. Connectors should be
828 * subclassed as part of driver connector objects.
830 * Returns:
831 * Zero on success, error code on failure.
833 int drm_connector_init(struct drm_device *dev,
834 struct drm_connector *connector,
835 const struct drm_connector_funcs *funcs,
836 int connector_type)
838 int ret;
839 struct ida *connector_ida =
840 &drm_connector_enum_list[connector_type].ida;
842 drm_modeset_lock_all(dev);
844 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
845 if (ret)
846 goto out_unlock;
848 connector->base.properties = &connector->properties;
849 connector->dev = dev;
850 connector->funcs = funcs;
851 connector->connector_type = connector_type;
852 connector->connector_type_id =
853 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
854 if (connector->connector_type_id < 0) {
855 ret = connector->connector_type_id;
856 goto out_put;
858 connector->name =
859 kasprintf(GFP_KERNEL, "%s-%d",
860 drm_connector_enum_list[connector_type].name,
861 connector->connector_type_id);
862 if (!connector->name) {
863 ret = -ENOMEM;
864 goto out_put;
867 INIT_LIST_HEAD(&connector->probed_modes);
868 INIT_LIST_HEAD(&connector->modes);
869 connector->edid_blob_ptr = NULL;
870 connector->status = connector_status_unknown;
872 list_add_tail(&connector->head, &dev->mode_config.connector_list);
873 dev->mode_config.num_connector++;
875 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
876 drm_object_attach_property(&connector->base,
877 dev->mode_config.edid_property,
880 drm_object_attach_property(&connector->base,
881 dev->mode_config.dpms_property, 0);
883 out_put:
884 if (ret)
885 drm_mode_object_put(dev, &connector->base);
887 out_unlock:
888 drm_modeset_unlock_all(dev);
890 return ret;
892 EXPORT_SYMBOL(drm_connector_init);
895 * drm_connector_cleanup - cleans up an initialised connector
896 * @connector: connector to cleanup
898 * Cleans up the connector but doesn't free the object.
900 void drm_connector_cleanup(struct drm_connector *connector)
902 struct drm_device *dev = connector->dev;
903 struct drm_display_mode *mode, *t;
905 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
906 drm_mode_remove(connector, mode);
908 list_for_each_entry_safe(mode, t, &connector->modes, head)
909 drm_mode_remove(connector, mode);
911 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
912 connector->connector_type_id);
914 drm_mode_object_put(dev, &connector->base);
915 kfree(connector->name);
916 connector->name = NULL;
917 list_del(&connector->head);
918 dev->mode_config.num_connector--;
920 EXPORT_SYMBOL(drm_connector_cleanup);
923 * drm_connector_unplug_all - unregister connector userspace interfaces
924 * @dev: drm device
926 * This function unregisters all connector userspace interfaces in sysfs. Should
927 * be call when the device is disconnected, e.g. from an usb driver's
928 * ->disconnect callback.
930 void drm_connector_unplug_all(struct drm_device *dev)
932 struct drm_connector *connector;
934 /* taking the mode config mutex ends up in a clash with sysfs */
935 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
936 drm_sysfs_connector_remove(connector);
939 EXPORT_SYMBOL(drm_connector_unplug_all);
942 * drm_bridge_init - initialize a drm transcoder/bridge
943 * @dev: drm device
944 * @bridge: transcoder/bridge to set up
945 * @funcs: bridge function table
947 * Initialises a preallocated bridge. Bridges should be
948 * subclassed as part of driver connector objects.
950 * Returns:
951 * Zero on success, error code on failure.
953 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
954 const struct drm_bridge_funcs *funcs)
956 int ret;
958 drm_modeset_lock_all(dev);
960 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
961 if (ret)
962 goto out;
964 bridge->dev = dev;
965 bridge->funcs = funcs;
967 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
968 dev->mode_config.num_bridge++;
970 out:
971 drm_modeset_unlock_all(dev);
972 return ret;
974 EXPORT_SYMBOL(drm_bridge_init);
977 * drm_bridge_cleanup - cleans up an initialised bridge
978 * @bridge: bridge to cleanup
980 * Cleans up the bridge but doesn't free the object.
982 void drm_bridge_cleanup(struct drm_bridge *bridge)
984 struct drm_device *dev = bridge->dev;
986 drm_modeset_lock_all(dev);
987 drm_mode_object_put(dev, &bridge->base);
988 list_del(&bridge->head);
989 dev->mode_config.num_bridge--;
990 drm_modeset_unlock_all(dev);
992 EXPORT_SYMBOL(drm_bridge_cleanup);
995 * drm_encoder_init - Init a preallocated encoder
996 * @dev: drm device
997 * @encoder: the encoder to init
998 * @funcs: callbacks for this encoder
999 * @encoder_type: user visible type of the encoder
1001 * Initialises a preallocated encoder. Encoder should be
1002 * subclassed as part of driver encoder objects.
1004 * Returns:
1005 * Zero on success, error code on failure.
1007 int drm_encoder_init(struct drm_device *dev,
1008 struct drm_encoder *encoder,
1009 const struct drm_encoder_funcs *funcs,
1010 int encoder_type)
1012 int ret;
1014 drm_modeset_lock_all(dev);
1016 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1017 if (ret)
1018 goto out_unlock;
1020 encoder->dev = dev;
1021 encoder->encoder_type = encoder_type;
1022 encoder->funcs = funcs;
1023 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1024 drm_encoder_enum_list[encoder_type].name,
1025 encoder->base.id);
1026 if (!encoder->name) {
1027 ret = -ENOMEM;
1028 goto out_put;
1031 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1032 dev->mode_config.num_encoder++;
1034 out_put:
1035 if (ret)
1036 drm_mode_object_put(dev, &encoder->base);
1038 out_unlock:
1039 drm_modeset_unlock_all(dev);
1041 return ret;
1043 EXPORT_SYMBOL(drm_encoder_init);
1046 * drm_encoder_cleanup - cleans up an initialised encoder
1047 * @encoder: encoder to cleanup
1049 * Cleans up the encoder but doesn't free the object.
1051 void drm_encoder_cleanup(struct drm_encoder *encoder)
1053 struct drm_device *dev = encoder->dev;
1054 drm_modeset_lock_all(dev);
1055 drm_mode_object_put(dev, &encoder->base);
1056 kfree(encoder->name);
1057 encoder->name = NULL;
1058 list_del(&encoder->head);
1059 dev->mode_config.num_encoder--;
1060 drm_modeset_unlock_all(dev);
1062 EXPORT_SYMBOL(drm_encoder_cleanup);
1065 * drm_universal_plane_init - Initialize a new universal plane object
1066 * @dev: DRM device
1067 * @plane: plane object to init
1068 * @possible_crtcs: bitmask of possible CRTCs
1069 * @funcs: callbacks for the new plane
1070 * @formats: array of supported formats (%DRM_FORMAT_*)
1071 * @format_count: number of elements in @formats
1072 * @type: type of plane (overlay, primary, cursor)
1074 * Initializes a plane object of type @type.
1076 * Returns:
1077 * Zero on success, error code on failure.
1079 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1080 unsigned long possible_crtcs,
1081 const struct drm_plane_funcs *funcs,
1082 const uint32_t *formats, uint32_t format_count,
1083 enum drm_plane_type type)
1085 int ret;
1087 drm_modeset_lock_all(dev);
1089 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1090 if (ret)
1091 goto out;
1093 plane->base.properties = &plane->properties;
1094 plane->dev = dev;
1095 plane->funcs = funcs;
1096 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1097 GFP_KERNEL);
1098 if (!plane->format_types) {
1099 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1100 drm_mode_object_put(dev, &plane->base);
1101 ret = -ENOMEM;
1102 goto out;
1105 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1106 plane->format_count = format_count;
1107 plane->possible_crtcs = possible_crtcs;
1108 plane->type = type;
1110 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1111 dev->mode_config.num_total_plane++;
1112 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1113 dev->mode_config.num_overlay_plane++;
1115 drm_object_attach_property(&plane->base,
1116 dev->mode_config.plane_type_property,
1117 plane->type);
1119 out:
1120 drm_modeset_unlock_all(dev);
1122 return ret;
1124 EXPORT_SYMBOL(drm_universal_plane_init);
1127 * drm_plane_init - Initialize a legacy plane
1128 * @dev: DRM device
1129 * @plane: plane object to init
1130 * @possible_crtcs: bitmask of possible CRTCs
1131 * @funcs: callbacks for the new plane
1132 * @formats: array of supported formats (%DRM_FORMAT_*)
1133 * @format_count: number of elements in @formats
1134 * @is_primary: plane type (primary vs overlay)
1136 * Legacy API to initialize a DRM plane.
1138 * New drivers should call drm_universal_plane_init() instead.
1140 * Returns:
1141 * Zero on success, error code on failure.
1143 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1144 unsigned long possible_crtcs,
1145 const struct drm_plane_funcs *funcs,
1146 const uint32_t *formats, uint32_t format_count,
1147 bool is_primary)
1149 enum drm_plane_type type;
1151 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1152 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1153 formats, format_count, type);
1155 EXPORT_SYMBOL(drm_plane_init);
1158 * drm_plane_cleanup - Clean up the core plane usage
1159 * @plane: plane to cleanup
1161 * This function cleans up @plane and removes it from the DRM mode setting
1162 * core. Note that the function does *not* free the plane structure itself,
1163 * this is the responsibility of the caller.
1165 void drm_plane_cleanup(struct drm_plane *plane)
1167 struct drm_device *dev = plane->dev;
1169 drm_modeset_lock_all(dev);
1170 kfree(plane->format_types);
1171 drm_mode_object_put(dev, &plane->base);
1173 BUG_ON(list_empty(&plane->head));
1175 list_del(&plane->head);
1176 dev->mode_config.num_total_plane--;
1177 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1178 dev->mode_config.num_overlay_plane--;
1179 drm_modeset_unlock_all(dev);
1181 EXPORT_SYMBOL(drm_plane_cleanup);
1184 * drm_plane_force_disable - Forcibly disable a plane
1185 * @plane: plane to disable
1187 * Forces the plane to be disabled.
1189 * Used when the plane's current framebuffer is destroyed,
1190 * and when restoring fbdev mode.
1192 void drm_plane_force_disable(struct drm_plane *plane)
1194 struct drm_framebuffer *old_fb = plane->fb;
1195 int ret;
1197 if (!old_fb)
1198 return;
1200 ret = plane->funcs->disable_plane(plane);
1201 if (ret) {
1202 DRM_ERROR("failed to disable plane with busy fb\n");
1203 return;
1205 /* disconnect the plane from the fb and crtc: */
1206 __drm_framebuffer_unreference(old_fb);
1207 plane->fb = NULL;
1208 plane->crtc = NULL;
1210 EXPORT_SYMBOL(drm_plane_force_disable);
1212 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1214 struct drm_property *edid;
1215 struct drm_property *dpms;
1218 * Standard properties (apply to all connectors)
1220 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1221 DRM_MODE_PROP_IMMUTABLE,
1222 "EDID", 0);
1223 dev->mode_config.edid_property = edid;
1225 dpms = drm_property_create_enum(dev, 0,
1226 "DPMS", drm_dpms_enum_list,
1227 ARRAY_SIZE(drm_dpms_enum_list));
1228 dev->mode_config.dpms_property = dpms;
1230 return 0;
1233 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1235 struct drm_property *type;
1238 * Standard properties (apply to all planes)
1240 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1241 "type", drm_plane_type_enum_list,
1242 ARRAY_SIZE(drm_plane_type_enum_list));
1243 dev->mode_config.plane_type_property = type;
1245 return 0;
1249 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1250 * @dev: DRM device
1252 * Called by a driver the first time a DVI-I connector is made.
1254 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1256 struct drm_property *dvi_i_selector;
1257 struct drm_property *dvi_i_subconnector;
1259 if (dev->mode_config.dvi_i_select_subconnector_property)
1260 return 0;
1262 dvi_i_selector =
1263 drm_property_create_enum(dev, 0,
1264 "select subconnector",
1265 drm_dvi_i_select_enum_list,
1266 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1267 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1269 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1270 "subconnector",
1271 drm_dvi_i_subconnector_enum_list,
1272 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1273 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1275 return 0;
1277 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1280 * drm_create_tv_properties - create TV specific connector properties
1281 * @dev: DRM device
1282 * @num_modes: number of different TV formats (modes) supported
1283 * @modes: array of pointers to strings containing name of each format
1285 * Called by a driver's TV initialization routine, this function creates
1286 * the TV specific connector properties for a given device. Caller is
1287 * responsible for allocating a list of format names and passing them to
1288 * this routine.
1290 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1291 char *modes[])
1293 struct drm_property *tv_selector;
1294 struct drm_property *tv_subconnector;
1295 int i;
1297 if (dev->mode_config.tv_select_subconnector_property)
1298 return 0;
1301 * Basic connector properties
1303 tv_selector = drm_property_create_enum(dev, 0,
1304 "select subconnector",
1305 drm_tv_select_enum_list,
1306 ARRAY_SIZE(drm_tv_select_enum_list));
1307 dev->mode_config.tv_select_subconnector_property = tv_selector;
1309 tv_subconnector =
1310 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1311 "subconnector",
1312 drm_tv_subconnector_enum_list,
1313 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1314 dev->mode_config.tv_subconnector_property = tv_subconnector;
1317 * Other, TV specific properties: margins & TV modes.
1319 dev->mode_config.tv_left_margin_property =
1320 drm_property_create_range(dev, 0, "left margin", 0, 100);
1322 dev->mode_config.tv_right_margin_property =
1323 drm_property_create_range(dev, 0, "right margin", 0, 100);
1325 dev->mode_config.tv_top_margin_property =
1326 drm_property_create_range(dev, 0, "top margin", 0, 100);
1328 dev->mode_config.tv_bottom_margin_property =
1329 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1331 dev->mode_config.tv_mode_property =
1332 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1333 "mode", num_modes);
1334 for (i = 0; i < num_modes; i++)
1335 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1336 i, modes[i]);
1338 dev->mode_config.tv_brightness_property =
1339 drm_property_create_range(dev, 0, "brightness", 0, 100);
1341 dev->mode_config.tv_contrast_property =
1342 drm_property_create_range(dev, 0, "contrast", 0, 100);
1344 dev->mode_config.tv_flicker_reduction_property =
1345 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1347 dev->mode_config.tv_overscan_property =
1348 drm_property_create_range(dev, 0, "overscan", 0, 100);
1350 dev->mode_config.tv_saturation_property =
1351 drm_property_create_range(dev, 0, "saturation", 0, 100);
1353 dev->mode_config.tv_hue_property =
1354 drm_property_create_range(dev, 0, "hue", 0, 100);
1356 return 0;
1358 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1361 * drm_mode_create_scaling_mode_property - create scaling mode property
1362 * @dev: DRM device
1364 * Called by a driver the first time it's needed, must be attached to desired
1365 * connectors.
1367 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1369 struct drm_property *scaling_mode;
1371 if (dev->mode_config.scaling_mode_property)
1372 return 0;
1374 scaling_mode =
1375 drm_property_create_enum(dev, 0, "scaling mode",
1376 drm_scaling_mode_enum_list,
1377 ARRAY_SIZE(drm_scaling_mode_enum_list));
1379 dev->mode_config.scaling_mode_property = scaling_mode;
1381 return 0;
1383 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1386 * drm_mode_create_dirty_property - create dirty property
1387 * @dev: DRM device
1389 * Called by a driver the first time it's needed, must be attached to desired
1390 * connectors.
1392 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1394 struct drm_property *dirty_info;
1396 if (dev->mode_config.dirty_info_property)
1397 return 0;
1399 dirty_info =
1400 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1401 "dirty",
1402 drm_dirty_info_enum_list,
1403 ARRAY_SIZE(drm_dirty_info_enum_list));
1404 dev->mode_config.dirty_info_property = dirty_info;
1406 return 0;
1408 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1410 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1412 uint32_t total_objects = 0;
1414 total_objects += dev->mode_config.num_crtc;
1415 total_objects += dev->mode_config.num_connector;
1416 total_objects += dev->mode_config.num_encoder;
1417 total_objects += dev->mode_config.num_bridge;
1419 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1420 if (!group->id_list)
1421 return -ENOMEM;
1423 group->num_crtcs = 0;
1424 group->num_connectors = 0;
1425 group->num_encoders = 0;
1426 group->num_bridges = 0;
1427 return 0;
1430 void drm_mode_group_destroy(struct drm_mode_group *group)
1432 kfree(group->id_list);
1433 group->id_list = NULL;
1437 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1438 * the drm core's responsibility to set up mode control groups.
1440 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1441 struct drm_mode_group *group)
1443 struct drm_crtc *crtc;
1444 struct drm_encoder *encoder;
1445 struct drm_connector *connector;
1446 struct drm_bridge *bridge;
1447 int ret;
1449 if ((ret = drm_mode_group_init(dev, group)))
1450 return ret;
1452 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1453 group->id_list[group->num_crtcs++] = crtc->base.id;
1455 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1456 group->id_list[group->num_crtcs + group->num_encoders++] =
1457 encoder->base.id;
1459 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1460 group->id_list[group->num_crtcs + group->num_encoders +
1461 group->num_connectors++] = connector->base.id;
1463 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1464 group->id_list[group->num_crtcs + group->num_encoders +
1465 group->num_connectors + group->num_bridges++] =
1466 bridge->base.id;
1468 return 0;
1470 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1473 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1474 * @out: drm_mode_modeinfo struct to return to the user
1475 * @in: drm_display_mode to use
1477 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1478 * the user.
1480 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1481 const struct drm_display_mode *in)
1483 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1484 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1485 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1486 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1487 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1488 "timing values too large for mode info\n");
1490 out->clock = in->clock;
1491 out->hdisplay = in->hdisplay;
1492 out->hsync_start = in->hsync_start;
1493 out->hsync_end = in->hsync_end;
1494 out->htotal = in->htotal;
1495 out->hskew = in->hskew;
1496 out->vdisplay = in->vdisplay;
1497 out->vsync_start = in->vsync_start;
1498 out->vsync_end = in->vsync_end;
1499 out->vtotal = in->vtotal;
1500 out->vscan = in->vscan;
1501 out->vrefresh = in->vrefresh;
1502 out->flags = in->flags;
1503 out->type = in->type;
1504 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1505 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1509 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1510 * @out: drm_display_mode to return to the user
1511 * @in: drm_mode_modeinfo to use
1513 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1514 * the caller.
1516 * Returns:
1517 * Zero on success, errno on failure.
1519 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1520 const struct drm_mode_modeinfo *in)
1522 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1523 return -ERANGE;
1525 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1526 return -EINVAL;
1528 out->clock = in->clock;
1529 out->hdisplay = in->hdisplay;
1530 out->hsync_start = in->hsync_start;
1531 out->hsync_end = in->hsync_end;
1532 out->htotal = in->htotal;
1533 out->hskew = in->hskew;
1534 out->vdisplay = in->vdisplay;
1535 out->vsync_start = in->vsync_start;
1536 out->vsync_end = in->vsync_end;
1537 out->vtotal = in->vtotal;
1538 out->vscan = in->vscan;
1539 out->vrefresh = in->vrefresh;
1540 out->flags = in->flags;
1541 out->type = in->type;
1542 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1543 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1545 return 0;
1549 * drm_mode_getresources - get graphics configuration
1550 * @dev: drm device for the ioctl
1551 * @data: data pointer for the ioctl
1552 * @file_priv: drm file for the ioctl call
1554 * Construct a set of configuration description structures and return
1555 * them to the user, including CRTC, connector and framebuffer configuration.
1557 * Called by the user via ioctl.
1559 * Returns:
1560 * Zero on success, errno on failure.
1562 int drm_mode_getresources(struct drm_device *dev, void *data,
1563 struct drm_file *file_priv)
1565 struct drm_mode_card_res *card_res = data;
1566 struct list_head *lh;
1567 struct drm_framebuffer *fb;
1568 struct drm_connector *connector;
1569 struct drm_crtc *crtc;
1570 struct drm_encoder *encoder;
1571 int ret = 0;
1572 int connector_count = 0;
1573 int crtc_count = 0;
1574 int fb_count = 0;
1575 int encoder_count = 0;
1576 int copied = 0, i;
1577 uint32_t __user *fb_id;
1578 uint32_t __user *crtc_id;
1579 uint32_t __user *connector_id;
1580 uint32_t __user *encoder_id;
1581 struct drm_mode_group *mode_group;
1583 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1584 return -EINVAL;
1587 mutex_lock(&file_priv->fbs_lock);
1589 * For the non-control nodes we need to limit the list of resources
1590 * by IDs in the group list for this node
1592 list_for_each(lh, &file_priv->fbs)
1593 fb_count++;
1595 /* handle this in 4 parts */
1596 /* FBs */
1597 if (card_res->count_fbs >= fb_count) {
1598 copied = 0;
1599 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1600 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1601 if (put_user(fb->base.id, fb_id + copied)) {
1602 mutex_unlock(&file_priv->fbs_lock);
1603 return -EFAULT;
1605 copied++;
1608 card_res->count_fbs = fb_count;
1609 mutex_unlock(&file_priv->fbs_lock);
1611 drm_modeset_lock_all(dev);
1612 if (!drm_is_primary_client(file_priv)) {
1614 mode_group = NULL;
1615 list_for_each(lh, &dev->mode_config.crtc_list)
1616 crtc_count++;
1618 list_for_each(lh, &dev->mode_config.connector_list)
1619 connector_count++;
1621 list_for_each(lh, &dev->mode_config.encoder_list)
1622 encoder_count++;
1623 } else {
1625 mode_group = &file_priv->master->minor->mode_group;
1626 crtc_count = mode_group->num_crtcs;
1627 connector_count = mode_group->num_connectors;
1628 encoder_count = mode_group->num_encoders;
1631 card_res->max_height = dev->mode_config.max_height;
1632 card_res->min_height = dev->mode_config.min_height;
1633 card_res->max_width = dev->mode_config.max_width;
1634 card_res->min_width = dev->mode_config.min_width;
1636 /* CRTCs */
1637 if (card_res->count_crtcs >= crtc_count) {
1638 copied = 0;
1639 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1640 if (!mode_group) {
1641 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1642 head) {
1643 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1644 if (put_user(crtc->base.id, crtc_id + copied)) {
1645 ret = -EFAULT;
1646 goto out;
1648 copied++;
1650 } else {
1651 for (i = 0; i < mode_group->num_crtcs; i++) {
1652 if (put_user(mode_group->id_list[i],
1653 crtc_id + copied)) {
1654 ret = -EFAULT;
1655 goto out;
1657 copied++;
1661 card_res->count_crtcs = crtc_count;
1663 /* Encoders */
1664 if (card_res->count_encoders >= encoder_count) {
1665 copied = 0;
1666 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1667 if (!mode_group) {
1668 list_for_each_entry(encoder,
1669 &dev->mode_config.encoder_list,
1670 head) {
1671 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1672 encoder->name);
1673 if (put_user(encoder->base.id, encoder_id +
1674 copied)) {
1675 ret = -EFAULT;
1676 goto out;
1678 copied++;
1680 } else {
1681 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1682 if (put_user(mode_group->id_list[i],
1683 encoder_id + copied)) {
1684 ret = -EFAULT;
1685 goto out;
1687 copied++;
1692 card_res->count_encoders = encoder_count;
1694 /* Connectors */
1695 if (card_res->count_connectors >= connector_count) {
1696 copied = 0;
1697 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1698 if (!mode_group) {
1699 list_for_each_entry(connector,
1700 &dev->mode_config.connector_list,
1701 head) {
1702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1703 connector->base.id,
1704 connector->name);
1705 if (put_user(connector->base.id,
1706 connector_id + copied)) {
1707 ret = -EFAULT;
1708 goto out;
1710 copied++;
1712 } else {
1713 int start = mode_group->num_crtcs +
1714 mode_group->num_encoders;
1715 for (i = start; i < start + mode_group->num_connectors; i++) {
1716 if (put_user(mode_group->id_list[i],
1717 connector_id + copied)) {
1718 ret = -EFAULT;
1719 goto out;
1721 copied++;
1725 card_res->count_connectors = connector_count;
1727 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1728 card_res->count_connectors, card_res->count_encoders);
1730 out:
1731 drm_modeset_unlock_all(dev);
1732 return ret;
1736 * drm_mode_getcrtc - get CRTC configuration
1737 * @dev: drm device for the ioctl
1738 * @data: data pointer for the ioctl
1739 * @file_priv: drm file for the ioctl call
1741 * Construct a CRTC configuration structure to return to the user.
1743 * Called by the user via ioctl.
1745 * Returns:
1746 * Zero on success, errno on failure.
1748 int drm_mode_getcrtc(struct drm_device *dev,
1749 void *data, struct drm_file *file_priv)
1751 struct drm_mode_crtc *crtc_resp = data;
1752 struct drm_crtc *crtc;
1753 int ret = 0;
1755 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1756 return -EINVAL;
1758 drm_modeset_lock_all(dev);
1760 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1761 if (!crtc) {
1762 ret = -ENOENT;
1763 goto out;
1766 crtc_resp->x = crtc->x;
1767 crtc_resp->y = crtc->y;
1768 crtc_resp->gamma_size = crtc->gamma_size;
1769 if (crtc->primary->fb)
1770 crtc_resp->fb_id = crtc->primary->fb->base.id;
1771 else
1772 crtc_resp->fb_id = 0;
1774 if (crtc->enabled) {
1776 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1777 crtc_resp->mode_valid = 1;
1779 } else {
1780 crtc_resp->mode_valid = 0;
1783 out:
1784 drm_modeset_unlock_all(dev);
1785 return ret;
1788 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1789 const struct drm_file *file_priv)
1792 * If user-space hasn't configured the driver to expose the stereo 3D
1793 * modes, don't expose them.
1795 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1796 return false;
1798 return true;
1802 * drm_mode_getconnector - get connector configuration
1803 * @dev: drm device for the ioctl
1804 * @data: data pointer for the ioctl
1805 * @file_priv: drm file for the ioctl call
1807 * Construct a connector configuration structure to return to the user.
1809 * Called by the user via ioctl.
1811 * Returns:
1812 * Zero on success, errno on failure.
1814 int drm_mode_getconnector(struct drm_device *dev, void *data,
1815 struct drm_file *file_priv)
1817 struct drm_mode_get_connector *out_resp = data;
1818 struct drm_connector *connector;
1819 struct drm_display_mode *mode;
1820 int mode_count = 0;
1821 int props_count = 0;
1822 int encoders_count = 0;
1823 int ret = 0;
1824 int copied = 0;
1825 int i;
1826 struct drm_mode_modeinfo u_mode;
1827 struct drm_mode_modeinfo __user *mode_ptr;
1828 uint32_t __user *prop_ptr;
1829 uint64_t __user *prop_values;
1830 uint32_t __user *encoder_ptr;
1832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1833 return -EINVAL;
1835 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1837 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1839 mutex_lock(&dev->mode_config.mutex);
1841 connector = drm_connector_find(dev, out_resp->connector_id);
1842 if (!connector) {
1843 ret = -ENOENT;
1844 goto out;
1847 props_count = connector->properties.count;
1849 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1850 if (connector->encoder_ids[i] != 0) {
1851 encoders_count++;
1855 if (out_resp->count_modes == 0) {
1856 connector->funcs->fill_modes(connector,
1857 dev->mode_config.max_width,
1858 dev->mode_config.max_height);
1861 /* delayed so we get modes regardless of pre-fill_modes state */
1862 list_for_each_entry(mode, &connector->modes, head)
1863 if (drm_mode_expose_to_userspace(mode, file_priv))
1864 mode_count++;
1866 out_resp->connector_id = connector->base.id;
1867 out_resp->connector_type = connector->connector_type;
1868 out_resp->connector_type_id = connector->connector_type_id;
1869 out_resp->mm_width = connector->display_info.width_mm;
1870 out_resp->mm_height = connector->display_info.height_mm;
1871 out_resp->subpixel = connector->display_info.subpixel_order;
1872 out_resp->connection = connector->status;
1873 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1874 if (connector->encoder)
1875 out_resp->encoder_id = connector->encoder->base.id;
1876 else
1877 out_resp->encoder_id = 0;
1878 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1881 * This ioctl is called twice, once to determine how much space is
1882 * needed, and the 2nd time to fill it.
1884 if ((out_resp->count_modes >= mode_count) && mode_count) {
1885 copied = 0;
1886 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1887 list_for_each_entry(mode, &connector->modes, head) {
1888 if (!drm_mode_expose_to_userspace(mode, file_priv))
1889 continue;
1891 drm_crtc_convert_to_umode(&u_mode, mode);
1892 if (copy_to_user(mode_ptr + copied,
1893 &u_mode, sizeof(u_mode))) {
1894 ret = -EFAULT;
1895 goto out;
1897 copied++;
1900 out_resp->count_modes = mode_count;
1902 if ((out_resp->count_props >= props_count) && props_count) {
1903 copied = 0;
1904 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1905 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1906 for (i = 0; i < connector->properties.count; i++) {
1907 if (put_user(connector->properties.ids[i],
1908 prop_ptr + copied)) {
1909 ret = -EFAULT;
1910 goto out;
1913 if (put_user(connector->properties.values[i],
1914 prop_values + copied)) {
1915 ret = -EFAULT;
1916 goto out;
1918 copied++;
1921 out_resp->count_props = props_count;
1923 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1924 copied = 0;
1925 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1926 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1927 if (connector->encoder_ids[i] != 0) {
1928 if (put_user(connector->encoder_ids[i],
1929 encoder_ptr + copied)) {
1930 ret = -EFAULT;
1931 goto out;
1933 copied++;
1937 out_resp->count_encoders = encoders_count;
1939 out:
1940 mutex_unlock(&dev->mode_config.mutex);
1942 return ret;
1946 * drm_mode_getencoder - get encoder configuration
1947 * @dev: drm device for the ioctl
1948 * @data: data pointer for the ioctl
1949 * @file_priv: drm file for the ioctl call
1951 * Construct a encoder configuration structure to return to the user.
1953 * Called by the user via ioctl.
1955 * Returns:
1956 * Zero on success, errno on failure.
1958 int drm_mode_getencoder(struct drm_device *dev, void *data,
1959 struct drm_file *file_priv)
1961 struct drm_mode_get_encoder *enc_resp = data;
1962 struct drm_encoder *encoder;
1963 int ret = 0;
1965 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1966 return -EINVAL;
1968 drm_modeset_lock_all(dev);
1969 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1970 if (!encoder) {
1971 ret = -ENOENT;
1972 goto out;
1975 if (encoder->crtc)
1976 enc_resp->crtc_id = encoder->crtc->base.id;
1977 else
1978 enc_resp->crtc_id = 0;
1979 enc_resp->encoder_type = encoder->encoder_type;
1980 enc_resp->encoder_id = encoder->base.id;
1981 enc_resp->possible_crtcs = encoder->possible_crtcs;
1982 enc_resp->possible_clones = encoder->possible_clones;
1984 out:
1985 drm_modeset_unlock_all(dev);
1986 return ret;
1990 * drm_mode_getplane_res - enumerate all plane resources
1991 * @dev: DRM device
1992 * @data: ioctl data
1993 * @file_priv: DRM file info
1995 * Construct a list of plane ids to return to the user.
1997 * Called by the user via ioctl.
1999 * Returns:
2000 * Zero on success, errno on failure.
2002 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2003 struct drm_file *file_priv)
2005 struct drm_mode_get_plane_res *plane_resp = data;
2006 struct drm_mode_config *config;
2007 struct drm_plane *plane;
2008 uint32_t __user *plane_ptr;
2009 int copied = 0, ret = 0;
2010 unsigned num_planes;
2012 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2013 return -EINVAL;
2015 drm_modeset_lock_all(dev);
2016 config = &dev->mode_config;
2018 if (file_priv->universal_planes)
2019 num_planes = config->num_total_plane;
2020 else
2021 num_planes = config->num_overlay_plane;
2024 * This ioctl is called twice, once to determine how much space is
2025 * needed, and the 2nd time to fill it.
2027 if (num_planes &&
2028 (plane_resp->count_planes >= num_planes)) {
2029 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2031 list_for_each_entry(plane, &config->plane_list, head) {
2033 * Unless userspace set the 'universal planes'
2034 * capability bit, only advertise overlays.
2036 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2037 !file_priv->universal_planes)
2038 continue;
2040 if (put_user(plane->base.id, plane_ptr + copied)) {
2041 ret = -EFAULT;
2042 goto out;
2044 copied++;
2047 plane_resp->count_planes = num_planes;
2049 out:
2050 drm_modeset_unlock_all(dev);
2051 return ret;
2055 * drm_mode_getplane - get plane configuration
2056 * @dev: DRM device
2057 * @data: ioctl data
2058 * @file_priv: DRM file info
2060 * Construct a plane configuration structure to return to the user.
2062 * Called by the user via ioctl.
2064 * Returns:
2065 * Zero on success, errno on failure.
2067 int drm_mode_getplane(struct drm_device *dev, void *data,
2068 struct drm_file *file_priv)
2070 struct drm_mode_get_plane *plane_resp = data;
2071 struct drm_plane *plane;
2072 uint32_t __user *format_ptr;
2073 int ret = 0;
2075 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2076 return -EINVAL;
2078 drm_modeset_lock_all(dev);
2079 plane = drm_plane_find(dev, plane_resp->plane_id);
2080 if (!plane) {
2081 ret = -ENOENT;
2082 goto out;
2085 if (plane->crtc)
2086 plane_resp->crtc_id = plane->crtc->base.id;
2087 else
2088 plane_resp->crtc_id = 0;
2090 if (plane->fb)
2091 plane_resp->fb_id = plane->fb->base.id;
2092 else
2093 plane_resp->fb_id = 0;
2095 plane_resp->plane_id = plane->base.id;
2096 plane_resp->possible_crtcs = plane->possible_crtcs;
2097 plane_resp->gamma_size = 0;
2100 * This ioctl is called twice, once to determine how much space is
2101 * needed, and the 2nd time to fill it.
2103 if (plane->format_count &&
2104 (plane_resp->count_format_types >= plane->format_count)) {
2105 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2106 if (copy_to_user(format_ptr,
2107 plane->format_types,
2108 sizeof(uint32_t) * plane->format_count)) {
2109 ret = -EFAULT;
2110 goto out;
2113 plane_resp->count_format_types = plane->format_count;
2115 out:
2116 drm_modeset_unlock_all(dev);
2117 return ret;
2121 * drm_mode_setplane - configure a plane's configuration
2122 * @dev: DRM device
2123 * @data: ioctl data*
2124 * @file_priv: DRM file info
2126 * Set plane configuration, including placement, fb, scaling, and other factors.
2127 * Or pass a NULL fb to disable.
2129 * Returns:
2130 * Zero on success, errno on failure.
2132 int drm_mode_setplane(struct drm_device *dev, void *data,
2133 struct drm_file *file_priv)
2135 struct drm_mode_set_plane *plane_req = data;
2136 struct drm_plane *plane;
2137 struct drm_crtc *crtc;
2138 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2139 int ret = 0;
2140 unsigned int fb_width, fb_height;
2141 int i;
2143 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2144 return -EINVAL;
2147 * First, find the plane, crtc, and fb objects. If not available,
2148 * we don't bother to call the driver.
2150 plane = drm_plane_find(dev, plane_req->plane_id);
2151 if (!plane) {
2152 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2153 plane_req->plane_id);
2154 return -ENOENT;
2157 /* No fb means shut it down */
2158 if (!plane_req->fb_id) {
2159 drm_modeset_lock_all(dev);
2160 old_fb = plane->fb;
2161 ret = plane->funcs->disable_plane(plane);
2162 if (!ret) {
2163 plane->crtc = NULL;
2164 plane->fb = NULL;
2165 } else {
2166 old_fb = NULL;
2168 drm_modeset_unlock_all(dev);
2169 goto out;
2172 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2173 if (!crtc) {
2174 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2175 plane_req->crtc_id);
2176 ret = -ENOENT;
2177 goto out;
2180 /* Check whether this plane is usable on this CRTC */
2181 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2182 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2183 ret = -EINVAL;
2184 goto out;
2187 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2188 if (!fb) {
2189 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2190 plane_req->fb_id);
2191 ret = -ENOENT;
2192 goto out;
2195 /* Check whether this plane supports the fb pixel format. */
2196 for (i = 0; i < plane->format_count; i++)
2197 if (fb->pixel_format == plane->format_types[i])
2198 break;
2199 if (i == plane->format_count) {
2200 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2201 drm_get_format_name(fb->pixel_format));
2202 ret = -EINVAL;
2203 goto out;
2206 fb_width = fb->width << 16;
2207 fb_height = fb->height << 16;
2209 /* Make sure source coordinates are inside the fb. */
2210 if (plane_req->src_w > fb_width ||
2211 plane_req->src_x > fb_width - plane_req->src_w ||
2212 plane_req->src_h > fb_height ||
2213 plane_req->src_y > fb_height - plane_req->src_h) {
2214 DRM_DEBUG_KMS("Invalid source coordinates "
2215 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2216 plane_req->src_w >> 16,
2217 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2218 plane_req->src_h >> 16,
2219 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2220 plane_req->src_x >> 16,
2221 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2222 plane_req->src_y >> 16,
2223 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2224 ret = -ENOSPC;
2225 goto out;
2228 /* Give drivers some help against integer overflows */
2229 if (plane_req->crtc_w > INT_MAX ||
2230 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2231 plane_req->crtc_h > INT_MAX ||
2232 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2233 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2234 plane_req->crtc_w, plane_req->crtc_h,
2235 plane_req->crtc_x, plane_req->crtc_y);
2236 ret = -ERANGE;
2237 goto out;
2240 drm_modeset_lock_all(dev);
2241 old_fb = plane->fb;
2242 ret = plane->funcs->update_plane(plane, crtc, fb,
2243 plane_req->crtc_x, plane_req->crtc_y,
2244 plane_req->crtc_w, plane_req->crtc_h,
2245 plane_req->src_x, plane_req->src_y,
2246 plane_req->src_w, plane_req->src_h);
2247 if (!ret) {
2248 plane->crtc = crtc;
2249 plane->fb = fb;
2250 fb = NULL;
2251 } else {
2252 old_fb = NULL;
2254 drm_modeset_unlock_all(dev);
2256 out:
2257 if (fb)
2258 drm_framebuffer_unreference(fb);
2259 if (old_fb)
2260 drm_framebuffer_unreference(old_fb);
2262 return ret;
2266 * drm_mode_set_config_internal - helper to call ->set_config
2267 * @set: modeset config to set
2269 * This is a little helper to wrap internal calls to the ->set_config driver
2270 * interface. The only thing it adds is correct refcounting dance.
2272 * Returns:
2273 * Zero on success, errno on failure.
2275 int drm_mode_set_config_internal(struct drm_mode_set *set)
2277 struct drm_crtc *crtc = set->crtc;
2278 struct drm_framebuffer *fb;
2279 struct drm_crtc *tmp;
2280 int ret;
2283 * NOTE: ->set_config can also disable other crtcs (if we steal all
2284 * connectors from it), hence we need to refcount the fbs across all
2285 * crtcs. Atomic modeset will have saner semantics ...
2287 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2288 tmp->old_fb = tmp->primary->fb;
2290 fb = set->fb;
2292 ret = crtc->funcs->set_config(set);
2293 if (ret == 0) {
2294 crtc->primary->crtc = crtc;
2295 crtc->primary->fb = fb;
2298 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2299 if (tmp->primary->fb)
2300 drm_framebuffer_reference(tmp->primary->fb);
2301 if (tmp->old_fb)
2302 drm_framebuffer_unreference(tmp->old_fb);
2305 return ret;
2307 EXPORT_SYMBOL(drm_mode_set_config_internal);
2310 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2311 * CRTC viewport
2312 * @crtc: CRTC that framebuffer will be displayed on
2313 * @x: x panning
2314 * @y: y panning
2315 * @mode: mode that framebuffer will be displayed under
2316 * @fb: framebuffer to check size of
2318 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2319 int x, int y,
2320 const struct drm_display_mode *mode,
2321 const struct drm_framebuffer *fb)
2324 int hdisplay, vdisplay;
2326 hdisplay = mode->hdisplay;
2327 vdisplay = mode->vdisplay;
2329 if (drm_mode_is_stereo(mode)) {
2330 struct drm_display_mode adjusted = *mode;
2332 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2333 hdisplay = adjusted.crtc_hdisplay;
2334 vdisplay = adjusted.crtc_vdisplay;
2337 if (crtc->invert_dimensions)
2338 swap(hdisplay, vdisplay);
2340 if (hdisplay > fb->width ||
2341 vdisplay > fb->height ||
2342 x > fb->width - hdisplay ||
2343 y > fb->height - vdisplay) {
2344 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2345 fb->width, fb->height, hdisplay, vdisplay, x, y,
2346 crtc->invert_dimensions ? " (inverted)" : "");
2347 return -ENOSPC;
2350 return 0;
2352 EXPORT_SYMBOL(drm_crtc_check_viewport);
2355 * drm_mode_setcrtc - set CRTC configuration
2356 * @dev: drm device for the ioctl
2357 * @data: data pointer for the ioctl
2358 * @file_priv: drm file for the ioctl call
2360 * Build a new CRTC configuration based on user request.
2362 * Called by the user via ioctl.
2364 * Returns:
2365 * Zero on success, errno on failure.
2367 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2368 struct drm_file *file_priv)
2370 struct drm_mode_config *config = &dev->mode_config;
2371 struct drm_mode_crtc *crtc_req = data;
2372 struct drm_crtc *crtc;
2373 struct drm_connector **connector_set = NULL, *connector;
2374 struct drm_framebuffer *fb = NULL;
2375 struct drm_display_mode *mode = NULL;
2376 struct drm_mode_set set;
2377 uint32_t __user *set_connectors_ptr;
2378 int ret;
2379 int i;
2381 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2382 return -EINVAL;
2385 * Universal plane src offsets are only 16.16, prevent havoc for
2386 * drivers using universal plane code internally.
2388 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2389 return -ERANGE;
2391 drm_modeset_lock_all(dev);
2392 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2393 if (!crtc) {
2394 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2395 ret = -ENOENT;
2396 goto out;
2398 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2400 if (crtc_req->mode_valid) {
2401 /* If we have a mode we need a framebuffer. */
2402 /* If we pass -1, set the mode with the currently bound fb */
2403 if (crtc_req->fb_id == -1) {
2404 if (!crtc->primary->fb) {
2405 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2406 ret = -EINVAL;
2407 goto out;
2409 fb = crtc->primary->fb;
2410 /* Make refcounting symmetric with the lookup path. */
2411 drm_framebuffer_reference(fb);
2412 } else {
2413 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2414 if (!fb) {
2415 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2416 crtc_req->fb_id);
2417 ret = -ENOENT;
2418 goto out;
2422 mode = drm_mode_create(dev);
2423 if (!mode) {
2424 ret = -ENOMEM;
2425 goto out;
2428 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2429 if (ret) {
2430 DRM_DEBUG_KMS("Invalid mode\n");
2431 goto out;
2434 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2436 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2437 mode, fb);
2438 if (ret)
2439 goto out;
2443 if (crtc_req->count_connectors == 0 && mode) {
2444 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2445 ret = -EINVAL;
2446 goto out;
2449 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2450 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2451 crtc_req->count_connectors);
2452 ret = -EINVAL;
2453 goto out;
2456 if (crtc_req->count_connectors > 0) {
2457 u32 out_id;
2459 /* Avoid unbounded kernel memory allocation */
2460 if (crtc_req->count_connectors > config->num_connector) {
2461 ret = -EINVAL;
2462 goto out;
2465 connector_set = kmalloc(crtc_req->count_connectors *
2466 sizeof(struct drm_connector *),
2467 GFP_KERNEL);
2468 if (!connector_set) {
2469 ret = -ENOMEM;
2470 goto out;
2473 for (i = 0; i < crtc_req->count_connectors; i++) {
2474 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2475 if (get_user(out_id, &set_connectors_ptr[i])) {
2476 ret = -EFAULT;
2477 goto out;
2480 connector = drm_connector_find(dev, out_id);
2481 if (!connector) {
2482 DRM_DEBUG_KMS("Connector id %d unknown\n",
2483 out_id);
2484 ret = -ENOENT;
2485 goto out;
2487 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2488 connector->base.id,
2489 connector->name);
2491 connector_set[i] = connector;
2495 set.crtc = crtc;
2496 set.x = crtc_req->x;
2497 set.y = crtc_req->y;
2498 set.mode = mode;
2499 set.connectors = connector_set;
2500 set.num_connectors = crtc_req->count_connectors;
2501 set.fb = fb;
2502 ret = drm_mode_set_config_internal(&set);
2504 out:
2505 if (fb)
2506 drm_framebuffer_unreference(fb);
2508 kfree(connector_set);
2509 drm_mode_destroy(dev, mode);
2510 drm_modeset_unlock_all(dev);
2511 return ret;
2514 static int drm_mode_cursor_common(struct drm_device *dev,
2515 struct drm_mode_cursor2 *req,
2516 struct drm_file *file_priv)
2518 struct drm_crtc *crtc;
2519 int ret = 0;
2521 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2522 return -EINVAL;
2524 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2525 return -EINVAL;
2527 crtc = drm_crtc_find(dev, req->crtc_id);
2528 if (!crtc) {
2529 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2530 return -ENOENT;
2533 drm_modeset_lock(&crtc->mutex, NULL);
2534 if (req->flags & DRM_MODE_CURSOR_BO) {
2535 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2536 ret = -ENXIO;
2537 goto out;
2539 /* Turns off the cursor if handle is 0 */
2540 if (crtc->funcs->cursor_set2)
2541 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2542 req->width, req->height, req->hot_x, req->hot_y);
2543 else
2544 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2545 req->width, req->height);
2548 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2549 if (crtc->funcs->cursor_move) {
2550 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2551 } else {
2552 ret = -EFAULT;
2553 goto out;
2556 out:
2557 drm_modeset_unlock(&crtc->mutex);
2559 return ret;
2565 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2566 * @dev: drm device for the ioctl
2567 * @data: data pointer for the ioctl
2568 * @file_priv: drm file for the ioctl call
2570 * Set the cursor configuration based on user request.
2572 * Called by the user via ioctl.
2574 * Returns:
2575 * Zero on success, errno on failure.
2577 int drm_mode_cursor_ioctl(struct drm_device *dev,
2578 void *data, struct drm_file *file_priv)
2580 struct drm_mode_cursor *req = data;
2581 struct drm_mode_cursor2 new_req;
2583 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2584 new_req.hot_x = new_req.hot_y = 0;
2586 return drm_mode_cursor_common(dev, &new_req, file_priv);
2590 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2591 * @dev: drm device for the ioctl
2592 * @data: data pointer for the ioctl
2593 * @file_priv: drm file for the ioctl call
2595 * Set the cursor configuration based on user request. This implements the 2nd
2596 * version of the cursor ioctl, which allows userspace to additionally specify
2597 * the hotspot of the pointer.
2599 * Called by the user via ioctl.
2601 * Returns:
2602 * Zero on success, errno on failure.
2604 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2605 void *data, struct drm_file *file_priv)
2607 struct drm_mode_cursor2 *req = data;
2608 return drm_mode_cursor_common(dev, req, file_priv);
2612 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2613 * @bpp: bits per pixels
2614 * @depth: bit depth per pixel
2616 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2617 * Useful in fbdev emulation code, since that deals in those values.
2619 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2621 uint32_t fmt;
2623 switch (bpp) {
2624 case 8:
2625 fmt = DRM_FORMAT_C8;
2626 break;
2627 case 16:
2628 if (depth == 15)
2629 fmt = DRM_FORMAT_XRGB1555;
2630 else
2631 fmt = DRM_FORMAT_RGB565;
2632 break;
2633 case 24:
2634 fmt = DRM_FORMAT_RGB888;
2635 break;
2636 case 32:
2637 if (depth == 24)
2638 fmt = DRM_FORMAT_XRGB8888;
2639 else if (depth == 30)
2640 fmt = DRM_FORMAT_XRGB2101010;
2641 else
2642 fmt = DRM_FORMAT_ARGB8888;
2643 break;
2644 default:
2645 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2646 fmt = DRM_FORMAT_XRGB8888;
2647 break;
2650 return fmt;
2652 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2655 * drm_mode_addfb - add an FB to the graphics configuration
2656 * @dev: drm device for the ioctl
2657 * @data: data pointer for the ioctl
2658 * @file_priv: drm file for the ioctl call
2660 * Add a new FB to the specified CRTC, given a user request. This is the
2661 * original addfb ioclt which only supported RGB formats.
2663 * Called by the user via ioctl.
2665 * Returns:
2666 * Zero on success, errno on failure.
2668 int drm_mode_addfb(struct drm_device *dev,
2669 void *data, struct drm_file *file_priv)
2671 struct drm_mode_fb_cmd *or = data;
2672 struct drm_mode_fb_cmd2 r = {};
2673 struct drm_mode_config *config = &dev->mode_config;
2674 struct drm_framebuffer *fb;
2675 int ret = 0;
2677 /* Use new struct with format internally */
2678 r.fb_id = or->fb_id;
2679 r.width = or->width;
2680 r.height = or->height;
2681 r.pitches[0] = or->pitch;
2682 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2683 r.handles[0] = or->handle;
2685 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2686 return -EINVAL;
2688 if ((config->min_width > r.width) || (r.width > config->max_width))
2689 return -EINVAL;
2691 if ((config->min_height > r.height) || (r.height > config->max_height))
2692 return -EINVAL;
2694 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2695 if (IS_ERR(fb)) {
2696 DRM_DEBUG_KMS("could not create framebuffer\n");
2697 return PTR_ERR(fb);
2700 mutex_lock(&file_priv->fbs_lock);
2701 or->fb_id = fb->base.id;
2702 list_add(&fb->filp_head, &file_priv->fbs);
2703 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2704 mutex_unlock(&file_priv->fbs_lock);
2706 return ret;
2709 static int format_check(const struct drm_mode_fb_cmd2 *r)
2711 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2713 switch (format) {
2714 case DRM_FORMAT_C8:
2715 case DRM_FORMAT_RGB332:
2716 case DRM_FORMAT_BGR233:
2717 case DRM_FORMAT_XRGB4444:
2718 case DRM_FORMAT_XBGR4444:
2719 case DRM_FORMAT_RGBX4444:
2720 case DRM_FORMAT_BGRX4444:
2721 case DRM_FORMAT_ARGB4444:
2722 case DRM_FORMAT_ABGR4444:
2723 case DRM_FORMAT_RGBA4444:
2724 case DRM_FORMAT_BGRA4444:
2725 case DRM_FORMAT_XRGB1555:
2726 case DRM_FORMAT_XBGR1555:
2727 case DRM_FORMAT_RGBX5551:
2728 case DRM_FORMAT_BGRX5551:
2729 case DRM_FORMAT_ARGB1555:
2730 case DRM_FORMAT_ABGR1555:
2731 case DRM_FORMAT_RGBA5551:
2732 case DRM_FORMAT_BGRA5551:
2733 case DRM_FORMAT_RGB565:
2734 case DRM_FORMAT_BGR565:
2735 case DRM_FORMAT_RGB888:
2736 case DRM_FORMAT_BGR888:
2737 case DRM_FORMAT_XRGB8888:
2738 case DRM_FORMAT_XBGR8888:
2739 case DRM_FORMAT_RGBX8888:
2740 case DRM_FORMAT_BGRX8888:
2741 case DRM_FORMAT_ARGB8888:
2742 case DRM_FORMAT_ABGR8888:
2743 case DRM_FORMAT_RGBA8888:
2744 case DRM_FORMAT_BGRA8888:
2745 case DRM_FORMAT_XRGB2101010:
2746 case DRM_FORMAT_XBGR2101010:
2747 case DRM_FORMAT_RGBX1010102:
2748 case DRM_FORMAT_BGRX1010102:
2749 case DRM_FORMAT_ARGB2101010:
2750 case DRM_FORMAT_ABGR2101010:
2751 case DRM_FORMAT_RGBA1010102:
2752 case DRM_FORMAT_BGRA1010102:
2753 case DRM_FORMAT_YUYV:
2754 case DRM_FORMAT_YVYU:
2755 case DRM_FORMAT_UYVY:
2756 case DRM_FORMAT_VYUY:
2757 case DRM_FORMAT_AYUV:
2758 case DRM_FORMAT_NV12:
2759 case DRM_FORMAT_NV21:
2760 case DRM_FORMAT_NV16:
2761 case DRM_FORMAT_NV61:
2762 case DRM_FORMAT_NV24:
2763 case DRM_FORMAT_NV42:
2764 case DRM_FORMAT_YUV410:
2765 case DRM_FORMAT_YVU410:
2766 case DRM_FORMAT_YUV411:
2767 case DRM_FORMAT_YVU411:
2768 case DRM_FORMAT_YUV420:
2769 case DRM_FORMAT_YVU420:
2770 case DRM_FORMAT_YUV422:
2771 case DRM_FORMAT_YVU422:
2772 case DRM_FORMAT_YUV444:
2773 case DRM_FORMAT_YVU444:
2774 return 0;
2775 default:
2776 DRM_DEBUG_KMS("invalid pixel format %s\n",
2777 drm_get_format_name(r->pixel_format));
2778 return -EINVAL;
2782 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2784 int ret, hsub, vsub, num_planes, i;
2786 ret = format_check(r);
2787 if (ret) {
2788 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2789 drm_get_format_name(r->pixel_format));
2790 return ret;
2793 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2794 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2795 num_planes = drm_format_num_planes(r->pixel_format);
2797 if (r->width == 0 || r->width % hsub) {
2798 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2799 return -EINVAL;
2802 if (r->height == 0 || r->height % vsub) {
2803 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2804 return -EINVAL;
2807 for (i = 0; i < num_planes; i++) {
2808 unsigned int width = r->width / (i != 0 ? hsub : 1);
2809 unsigned int height = r->height / (i != 0 ? vsub : 1);
2810 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2812 if (!r->handles[i]) {
2813 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2814 return -EINVAL;
2817 if ((uint64_t) width * cpp > UINT_MAX)
2818 return -ERANGE;
2820 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2821 return -ERANGE;
2823 if (r->pitches[i] < width * cpp) {
2824 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2825 return -EINVAL;
2829 return 0;
2833 * drm_mode_addfb2 - add an FB to the graphics configuration
2834 * @dev: drm device for the ioctl
2835 * @data: data pointer for the ioctl
2836 * @file_priv: drm file for the ioctl call
2838 * Add a new FB to the specified CRTC, given a user request with format. This is
2839 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2840 * and uses fourcc codes as pixel format specifiers.
2842 * Called by the user via ioctl.
2844 * Returns:
2845 * Zero on success, errno on failure.
2847 int drm_mode_addfb2(struct drm_device *dev,
2848 void *data, struct drm_file *file_priv)
2850 struct drm_mode_fb_cmd2 *r = data;
2851 struct drm_mode_config *config = &dev->mode_config;
2852 struct drm_framebuffer *fb;
2853 int ret;
2855 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2856 return -EINVAL;
2858 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2859 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2860 return -EINVAL;
2863 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2864 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2865 r->width, config->min_width, config->max_width);
2866 return -EINVAL;
2868 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2869 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2870 r->height, config->min_height, config->max_height);
2871 return -EINVAL;
2874 ret = framebuffer_check(r);
2875 if (ret)
2876 return ret;
2878 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2879 if (IS_ERR(fb)) {
2880 DRM_DEBUG_KMS("could not create framebuffer\n");
2881 return PTR_ERR(fb);
2884 mutex_lock(&file_priv->fbs_lock);
2885 r->fb_id = fb->base.id;
2886 list_add(&fb->filp_head, &file_priv->fbs);
2887 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2888 mutex_unlock(&file_priv->fbs_lock);
2891 return ret;
2895 * drm_mode_rmfb - remove an FB from the configuration
2896 * @dev: drm device for the ioctl
2897 * @data: data pointer for the ioctl
2898 * @file_priv: drm file for the ioctl call
2900 * Remove the FB specified by the user.
2902 * Called by the user via ioctl.
2904 * Returns:
2905 * Zero on success, errno on failure.
2907 int drm_mode_rmfb(struct drm_device *dev,
2908 void *data, struct drm_file *file_priv)
2910 struct drm_framebuffer *fb = NULL;
2911 struct drm_framebuffer *fbl = NULL;
2912 uint32_t *id = data;
2913 int found = 0;
2915 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2916 return -EINVAL;
2918 mutex_lock(&file_priv->fbs_lock);
2919 mutex_lock(&dev->mode_config.fb_lock);
2920 fb = __drm_framebuffer_lookup(dev, *id);
2921 if (!fb)
2922 goto fail_lookup;
2924 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2925 if (fb == fbl)
2926 found = 1;
2927 if (!found)
2928 goto fail_lookup;
2930 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2931 __drm_framebuffer_unregister(dev, fb);
2933 list_del_init(&fb->filp_head);
2934 mutex_unlock(&dev->mode_config.fb_lock);
2935 mutex_unlock(&file_priv->fbs_lock);
2937 drm_framebuffer_remove(fb);
2939 return 0;
2941 fail_lookup:
2942 mutex_unlock(&dev->mode_config.fb_lock);
2943 mutex_unlock(&file_priv->fbs_lock);
2945 return -ENOENT;
2949 * drm_mode_getfb - get FB info
2950 * @dev: drm device for the ioctl
2951 * @data: data pointer for the ioctl
2952 * @file_priv: drm file for the ioctl call
2954 * Lookup the FB given its ID and return info about it.
2956 * Called by the user via ioctl.
2958 * Returns:
2959 * Zero on success, errno on failure.
2961 int drm_mode_getfb(struct drm_device *dev,
2962 void *data, struct drm_file *file_priv)
2964 struct drm_mode_fb_cmd *r = data;
2965 struct drm_framebuffer *fb;
2966 int ret;
2968 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2969 return -EINVAL;
2971 fb = drm_framebuffer_lookup(dev, r->fb_id);
2972 if (!fb)
2973 return -ENOENT;
2975 r->height = fb->height;
2976 r->width = fb->width;
2977 r->depth = fb->depth;
2978 r->bpp = fb->bits_per_pixel;
2979 r->pitch = fb->pitches[0];
2980 if (fb->funcs->create_handle) {
2981 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2982 drm_is_control_client(file_priv)) {
2983 ret = fb->funcs->create_handle(fb, file_priv,
2984 &r->handle);
2985 } else {
2986 /* GET_FB() is an unprivileged ioctl so we must not
2987 * return a buffer-handle to non-master processes! For
2988 * backwards-compatibility reasons, we cannot make
2989 * GET_FB() privileged, so just return an invalid handle
2990 * for non-masters. */
2991 r->handle = 0;
2992 ret = 0;
2994 } else {
2995 ret = -ENODEV;
2998 drm_framebuffer_unreference(fb);
3000 return ret;
3004 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3005 * @dev: drm device for the ioctl
3006 * @data: data pointer for the ioctl
3007 * @file_priv: drm file for the ioctl call
3009 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3010 * rectangle list. Generic userspace which does frontbuffer rendering must call
3011 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3012 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3014 * Modesetting drivers which always update the frontbuffer do not need to
3015 * implement the corresponding ->dirty framebuffer callback.
3017 * Called by the user via ioctl.
3019 * Returns:
3020 * Zero on success, errno on failure.
3022 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3023 void *data, struct drm_file *file_priv)
3025 struct drm_clip_rect __user *clips_ptr;
3026 struct drm_clip_rect *clips = NULL;
3027 struct drm_mode_fb_dirty_cmd *r = data;
3028 struct drm_framebuffer *fb;
3029 unsigned flags;
3030 int num_clips;
3031 int ret;
3033 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3034 return -EINVAL;
3036 fb = drm_framebuffer_lookup(dev, r->fb_id);
3037 if (!fb)
3038 return -ENOENT;
3040 num_clips = r->num_clips;
3041 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3043 if (!num_clips != !clips_ptr) {
3044 ret = -EINVAL;
3045 goto out_err1;
3048 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3050 /* If userspace annotates copy, clips must come in pairs */
3051 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3052 ret = -EINVAL;
3053 goto out_err1;
3056 if (num_clips && clips_ptr) {
3057 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3058 ret = -EINVAL;
3059 goto out_err1;
3061 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3062 if (!clips) {
3063 ret = -ENOMEM;
3064 goto out_err1;
3067 ret = copy_from_user(clips, clips_ptr,
3068 num_clips * sizeof(*clips));
3069 if (ret) {
3070 ret = -EFAULT;
3071 goto out_err2;
3075 if (fb->funcs->dirty) {
3076 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3077 clips, num_clips);
3078 } else {
3079 ret = -ENOSYS;
3082 out_err2:
3083 kfree(clips);
3084 out_err1:
3085 drm_framebuffer_unreference(fb);
3087 return ret;
3092 * drm_fb_release - remove and free the FBs on this file
3093 * @priv: drm file for the ioctl
3095 * Destroy all the FBs associated with @filp.
3097 * Called by the user via ioctl.
3099 * Returns:
3100 * Zero on success, errno on failure.
3102 void drm_fb_release(struct drm_file *priv)
3104 struct drm_device *dev = priv->minor->dev;
3105 struct drm_framebuffer *fb, *tfb;
3107 mutex_lock(&priv->fbs_lock);
3108 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3110 mutex_lock(&dev->mode_config.fb_lock);
3111 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3112 __drm_framebuffer_unregister(dev, fb);
3113 mutex_unlock(&dev->mode_config.fb_lock);
3115 list_del_init(&fb->filp_head);
3117 /* This will also drop the fpriv->fbs reference. */
3118 drm_framebuffer_remove(fb);
3120 mutex_unlock(&priv->fbs_lock);
3124 * drm_property_create - create a new property type
3125 * @dev: drm device
3126 * @flags: flags specifying the property type
3127 * @name: name of the property
3128 * @num_values: number of pre-defined values
3130 * This creates a new generic drm property which can then be attached to a drm
3131 * object with drm_object_attach_property. The returned property object must be
3132 * freed with drm_property_destroy.
3134 * Returns:
3135 * A pointer to the newly created property on success, NULL on failure.
3137 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3138 const char *name, int num_values)
3140 struct drm_property *property = NULL;
3141 int ret;
3143 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3144 if (!property)
3145 return NULL;
3147 property->dev = dev;
3149 if (num_values) {
3150 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3151 if (!property->values)
3152 goto fail;
3155 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3156 if (ret)
3157 goto fail;
3159 property->flags = flags;
3160 property->num_values = num_values;
3161 INIT_LIST_HEAD(&property->enum_blob_list);
3163 if (name) {
3164 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3165 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3168 list_add_tail(&property->head, &dev->mode_config.property_list);
3170 WARN_ON(!drm_property_type_valid(property));
3172 return property;
3173 fail:
3174 kfree(property->values);
3175 kfree(property);
3176 return NULL;
3178 EXPORT_SYMBOL(drm_property_create);
3181 * drm_property_create - create a new enumeration property type
3182 * @dev: drm device
3183 * @flags: flags specifying the property type
3184 * @name: name of the property
3185 * @props: enumeration lists with property values
3186 * @num_values: number of pre-defined values
3188 * This creates a new generic drm property which can then be attached to a drm
3189 * object with drm_object_attach_property. The returned property object must be
3190 * freed with drm_property_destroy.
3192 * Userspace is only allowed to set one of the predefined values for enumeration
3193 * properties.
3195 * Returns:
3196 * A pointer to the newly created property on success, NULL on failure.
3198 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3199 const char *name,
3200 const struct drm_prop_enum_list *props,
3201 int num_values)
3203 struct drm_property *property;
3204 int i, ret;
3206 flags |= DRM_MODE_PROP_ENUM;
3208 property = drm_property_create(dev, flags, name, num_values);
3209 if (!property)
3210 return NULL;
3212 for (i = 0; i < num_values; i++) {
3213 ret = drm_property_add_enum(property, i,
3214 props[i].type,
3215 props[i].name);
3216 if (ret) {
3217 drm_property_destroy(dev, property);
3218 return NULL;
3222 return property;
3224 EXPORT_SYMBOL(drm_property_create_enum);
3227 * drm_property_create - create a new bitmask property type
3228 * @dev: drm device
3229 * @flags: flags specifying the property type
3230 * @name: name of the property
3231 * @props: enumeration lists with property bitflags
3232 * @num_values: number of pre-defined values
3234 * This creates a new generic drm property which can then be attached to a drm
3235 * object with drm_object_attach_property. The returned property object must be
3236 * freed with drm_property_destroy.
3238 * Compared to plain enumeration properties userspace is allowed to set any
3239 * or'ed together combination of the predefined property bitflag values
3241 * Returns:
3242 * A pointer to the newly created property on success, NULL on failure.
3244 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3245 int flags, const char *name,
3246 const struct drm_prop_enum_list *props,
3247 int num_values)
3249 struct drm_property *property;
3250 int i, ret;
3252 flags |= DRM_MODE_PROP_BITMASK;
3254 property = drm_property_create(dev, flags, name, num_values);
3255 if (!property)
3256 return NULL;
3258 for (i = 0; i < num_values; i++) {
3259 ret = drm_property_add_enum(property, i,
3260 props[i].type,
3261 props[i].name);
3262 if (ret) {
3263 drm_property_destroy(dev, property);
3264 return NULL;
3268 return property;
3270 EXPORT_SYMBOL(drm_property_create_bitmask);
3272 static struct drm_property *property_create_range(struct drm_device *dev,
3273 int flags, const char *name,
3274 uint64_t min, uint64_t max)
3276 struct drm_property *property;
3278 property = drm_property_create(dev, flags, name, 2);
3279 if (!property)
3280 return NULL;
3282 property->values[0] = min;
3283 property->values[1] = max;
3285 return property;
3289 * drm_property_create - create a new ranged property type
3290 * @dev: drm device
3291 * @flags: flags specifying the property type
3292 * @name: name of the property
3293 * @min: minimum value of the property
3294 * @max: maximum value of the property
3296 * This creates a new generic drm property which can then be attached to a drm
3297 * object with drm_object_attach_property. The returned property object must be
3298 * freed with drm_property_destroy.
3300 * Userspace is allowed to set any interger value in the (min, max) range
3301 * inclusive.
3303 * Returns:
3304 * A pointer to the newly created property on success, NULL on failure.
3306 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3307 const char *name,
3308 uint64_t min, uint64_t max)
3310 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3311 name, min, max);
3313 EXPORT_SYMBOL(drm_property_create_range);
3315 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3316 int flags, const char *name,
3317 int64_t min, int64_t max)
3319 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3320 name, I642U64(min), I642U64(max));
3322 EXPORT_SYMBOL(drm_property_create_signed_range);
3324 struct drm_property *drm_property_create_object(struct drm_device *dev,
3325 int flags, const char *name, uint32_t type)
3327 struct drm_property *property;
3329 flags |= DRM_MODE_PROP_OBJECT;
3331 property = drm_property_create(dev, flags, name, 1);
3332 if (!property)
3333 return NULL;
3335 property->values[0] = type;
3337 return property;
3339 EXPORT_SYMBOL(drm_property_create_object);
3342 * drm_property_add_enum - add a possible value to an enumeration property
3343 * @property: enumeration property to change
3344 * @index: index of the new enumeration
3345 * @value: value of the new enumeration
3346 * @name: symbolic name of the new enumeration
3348 * This functions adds enumerations to a property.
3350 * It's use is deprecated, drivers should use one of the more specific helpers
3351 * to directly create the property with all enumerations already attached.
3353 * Returns:
3354 * Zero on success, error code on failure.
3356 int drm_property_add_enum(struct drm_property *property, int index,
3357 uint64_t value, const char *name)
3359 struct drm_property_enum *prop_enum;
3361 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3362 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3363 return -EINVAL;
3366 * Bitmask enum properties have the additional constraint of values
3367 * from 0 to 63
3369 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3370 (value > 63))
3371 return -EINVAL;
3373 if (!list_empty(&property->enum_blob_list)) {
3374 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3375 if (prop_enum->value == value) {
3376 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3377 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3378 return 0;
3383 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3384 if (!prop_enum)
3385 return -ENOMEM;
3387 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3388 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3389 prop_enum->value = value;
3391 property->values[index] = value;
3392 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3393 return 0;
3395 EXPORT_SYMBOL(drm_property_add_enum);
3398 * drm_property_destroy - destroy a drm property
3399 * @dev: drm device
3400 * @property: property to destry
3402 * This function frees a property including any attached resources like
3403 * enumeration values.
3405 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3407 struct drm_property_enum *prop_enum, *pt;
3409 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3410 list_del(&prop_enum->head);
3411 kfree(prop_enum);
3414 if (property->num_values)
3415 kfree(property->values);
3416 drm_mode_object_put(dev, &property->base);
3417 list_del(&property->head);
3418 kfree(property);
3420 EXPORT_SYMBOL(drm_property_destroy);
3423 * drm_object_attach_property - attach a property to a modeset object
3424 * @obj: drm modeset object
3425 * @property: property to attach
3426 * @init_val: initial value of the property
3428 * This attaches the given property to the modeset object with the given initial
3429 * value. Currently this function cannot fail since the properties are stored in
3430 * a statically sized array.
3432 void drm_object_attach_property(struct drm_mode_object *obj,
3433 struct drm_property *property,
3434 uint64_t init_val)
3436 int count = obj->properties->count;
3438 if (count == DRM_OBJECT_MAX_PROPERTY) {
3439 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3440 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3441 "you see this message on the same object type.\n",
3442 obj->type);
3443 return;
3446 obj->properties->ids[count] = property->base.id;
3447 obj->properties->values[count] = init_val;
3448 obj->properties->count++;
3450 EXPORT_SYMBOL(drm_object_attach_property);
3453 * drm_object_property_set_value - set the value of a property
3454 * @obj: drm mode object to set property value for
3455 * @property: property to set
3456 * @val: value the property should be set to
3458 * This functions sets a given property on a given object. This function only
3459 * changes the software state of the property, it does not call into the
3460 * driver's ->set_property callback.
3462 * Returns:
3463 * Zero on success, error code on failure.
3465 int drm_object_property_set_value(struct drm_mode_object *obj,
3466 struct drm_property *property, uint64_t val)
3468 int i;
3470 for (i = 0; i < obj->properties->count; i++) {
3471 if (obj->properties->ids[i] == property->base.id) {
3472 obj->properties->values[i] = val;
3473 return 0;
3477 return -EINVAL;
3479 EXPORT_SYMBOL(drm_object_property_set_value);
3482 * drm_object_property_get_value - retrieve the value of a property
3483 * @obj: drm mode object to get property value from
3484 * @property: property to retrieve
3485 * @val: storage for the property value
3487 * This function retrieves the softare state of the given property for the given
3488 * property. Since there is no driver callback to retrieve the current property
3489 * value this might be out of sync with the hardware, depending upon the driver
3490 * and property.
3492 * Returns:
3493 * Zero on success, error code on failure.
3495 int drm_object_property_get_value(struct drm_mode_object *obj,
3496 struct drm_property *property, uint64_t *val)
3498 int i;
3500 for (i = 0; i < obj->properties->count; i++) {
3501 if (obj->properties->ids[i] == property->base.id) {
3502 *val = obj->properties->values[i];
3503 return 0;
3507 return -EINVAL;
3509 EXPORT_SYMBOL(drm_object_property_get_value);
3512 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3513 * @dev: DRM device
3514 * @data: ioctl data
3515 * @file_priv: DRM file info
3517 * This function retrieves the current value for an connectors's property.
3519 * Called by the user via ioctl.
3521 * Returns:
3522 * Zero on success, errno on failure.
3524 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3525 void *data, struct drm_file *file_priv)
3527 struct drm_mode_get_property *out_resp = data;
3528 struct drm_property *property;
3529 int enum_count = 0;
3530 int blob_count = 0;
3531 int value_count = 0;
3532 int ret = 0, i;
3533 int copied;
3534 struct drm_property_enum *prop_enum;
3535 struct drm_mode_property_enum __user *enum_ptr;
3536 struct drm_property_blob *prop_blob;
3537 uint32_t __user *blob_id_ptr;
3538 uint64_t __user *values_ptr;
3539 uint32_t __user *blob_length_ptr;
3541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3542 return -EINVAL;
3544 drm_modeset_lock_all(dev);
3545 property = drm_property_find(dev, out_resp->prop_id);
3546 if (!property) {
3547 ret = -ENOENT;
3548 goto done;
3551 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3552 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3553 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3554 enum_count++;
3555 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3556 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3557 blob_count++;
3560 value_count = property->num_values;
3562 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3563 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3564 out_resp->flags = property->flags;
3566 if ((out_resp->count_values >= value_count) && value_count) {
3567 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3568 for (i = 0; i < value_count; i++) {
3569 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3570 ret = -EFAULT;
3571 goto done;
3575 out_resp->count_values = value_count;
3577 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3578 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3579 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3580 copied = 0;
3581 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3582 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3584 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3585 ret = -EFAULT;
3586 goto done;
3589 if (copy_to_user(&enum_ptr[copied].name,
3590 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3591 ret = -EFAULT;
3592 goto done;
3594 copied++;
3597 out_resp->count_enum_blobs = enum_count;
3600 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3601 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3602 copied = 0;
3603 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3604 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3606 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3607 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3608 ret = -EFAULT;
3609 goto done;
3612 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3613 ret = -EFAULT;
3614 goto done;
3617 copied++;
3620 out_resp->count_enum_blobs = blob_count;
3622 done:
3623 drm_modeset_unlock_all(dev);
3624 return ret;
3627 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3628 void *data)
3630 struct drm_property_blob *blob;
3631 int ret;
3633 if (!length || !data)
3634 return NULL;
3636 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3637 if (!blob)
3638 return NULL;
3640 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3641 if (ret) {
3642 kfree(blob);
3643 return NULL;
3646 blob->length = length;
3648 memcpy(blob->data, data, length);
3650 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3651 return blob;
3654 static void drm_property_destroy_blob(struct drm_device *dev,
3655 struct drm_property_blob *blob)
3657 drm_mode_object_put(dev, &blob->base);
3658 list_del(&blob->head);
3659 kfree(blob);
3663 * drm_mode_getblob_ioctl - get the contents of a blob property value
3664 * @dev: DRM device
3665 * @data: ioctl data
3666 * @file_priv: DRM file info
3668 * This function retrieves the contents of a blob property. The value stored in
3669 * an object's blob property is just a normal modeset object id.
3671 * Called by the user via ioctl.
3673 * Returns:
3674 * Zero on success, errno on failure.
3676 int drm_mode_getblob_ioctl(struct drm_device *dev,
3677 void *data, struct drm_file *file_priv)
3679 struct drm_mode_get_blob *out_resp = data;
3680 struct drm_property_blob *blob;
3681 int ret = 0;
3682 void __user *blob_ptr;
3684 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3685 return -EINVAL;
3687 drm_modeset_lock_all(dev);
3688 blob = drm_property_blob_find(dev, out_resp->blob_id);
3689 if (!blob) {
3690 ret = -ENOENT;
3691 goto done;
3694 if (out_resp->length == blob->length) {
3695 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3696 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3697 ret = -EFAULT;
3698 goto done;
3701 out_resp->length = blob->length;
3703 done:
3704 drm_modeset_unlock_all(dev);
3705 return ret;
3709 * drm_mode_connector_update_edid_property - update the edid property of a connector
3710 * @connector: drm connector
3711 * @edid: new value of the edid property
3713 * This function creates a new blob modeset object and assigns its id to the
3714 * connector's edid property.
3716 * Returns:
3717 * Zero on success, errno on failure.
3719 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3720 struct edid *edid)
3722 struct drm_device *dev = connector->dev;
3723 int ret, size;
3725 if (connector->edid_blob_ptr)
3726 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3728 /* Delete edid, when there is none. */
3729 if (!edid) {
3730 connector->edid_blob_ptr = NULL;
3731 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3732 return ret;
3735 size = EDID_LENGTH * (1 + edid->extensions);
3736 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3737 size, edid);
3738 if (!connector->edid_blob_ptr)
3739 return -EINVAL;
3741 ret = drm_object_property_set_value(&connector->base,
3742 dev->mode_config.edid_property,
3743 connector->edid_blob_ptr->base.id);
3745 return ret;
3747 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3749 static bool drm_property_change_is_valid(struct drm_property *property,
3750 uint64_t value)
3752 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3753 return false;
3755 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3756 if (value < property->values[0] || value > property->values[1])
3757 return false;
3758 return true;
3759 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3760 int64_t svalue = U642I64(value);
3761 if (svalue < U642I64(property->values[0]) ||
3762 svalue > U642I64(property->values[1]))
3763 return false;
3764 return true;
3765 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3766 int i;
3767 uint64_t valid_mask = 0;
3768 for (i = 0; i < property->num_values; i++)
3769 valid_mask |= (1ULL << property->values[i]);
3770 return !(value & ~valid_mask);
3771 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3772 /* Only the driver knows */
3773 return true;
3774 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3775 struct drm_mode_object *obj;
3776 /* a zero value for an object property translates to null: */
3777 if (value == 0)
3778 return true;
3780 * NOTE: use _object_find() directly to bypass restriction on
3781 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3782 * object this could race against object finalization, so it
3783 * simply tells us that the object *was* valid. Which is good
3784 * enough.
3786 obj = _object_find(property->dev, value, property->values[0]);
3787 return obj != NULL;
3788 } else {
3789 int i;
3790 for (i = 0; i < property->num_values; i++)
3791 if (property->values[i] == value)
3792 return true;
3793 return false;
3798 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3799 * @dev: DRM device
3800 * @data: ioctl data
3801 * @file_priv: DRM file info
3803 * This function sets the current value for a connectors's property. It also
3804 * calls into a driver's ->set_property callback to update the hardware state
3806 * Called by the user via ioctl.
3808 * Returns:
3809 * Zero on success, errno on failure.
3811 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3812 void *data, struct drm_file *file_priv)
3814 struct drm_mode_connector_set_property *conn_set_prop = data;
3815 struct drm_mode_obj_set_property obj_set_prop = {
3816 .value = conn_set_prop->value,
3817 .prop_id = conn_set_prop->prop_id,
3818 .obj_id = conn_set_prop->connector_id,
3819 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3822 /* It does all the locking and checking we need */
3823 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3826 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3827 struct drm_property *property,
3828 uint64_t value)
3830 int ret = -EINVAL;
3831 struct drm_connector *connector = obj_to_connector(obj);
3833 /* Do DPMS ourselves */
3834 if (property == connector->dev->mode_config.dpms_property) {
3835 if (connector->funcs->dpms)
3836 (*connector->funcs->dpms)(connector, (int)value);
3837 ret = 0;
3838 } else if (connector->funcs->set_property)
3839 ret = connector->funcs->set_property(connector, property, value);
3841 /* store the property value if successful */
3842 if (!ret)
3843 drm_object_property_set_value(&connector->base, property, value);
3844 return ret;
3847 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3848 struct drm_property *property,
3849 uint64_t value)
3851 int ret = -EINVAL;
3852 struct drm_crtc *crtc = obj_to_crtc(obj);
3854 if (crtc->funcs->set_property)
3855 ret = crtc->funcs->set_property(crtc, property, value);
3856 if (!ret)
3857 drm_object_property_set_value(obj, property, value);
3859 return ret;
3862 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3863 struct drm_property *property,
3864 uint64_t value)
3866 int ret = -EINVAL;
3867 struct drm_plane *plane = obj_to_plane(obj);
3869 if (plane->funcs->set_property)
3870 ret = plane->funcs->set_property(plane, property, value);
3871 if (!ret)
3872 drm_object_property_set_value(obj, property, value);
3874 return ret;
3878 * drm_mode_getproperty_ioctl - get the current value of a object's property
3879 * @dev: DRM device
3880 * @data: ioctl data
3881 * @file_priv: DRM file info
3883 * This function retrieves the current value for an object's property. Compared
3884 * to the connector specific ioctl this one is extended to also work on crtc and
3885 * plane objects.
3887 * Called by the user via ioctl.
3889 * Returns:
3890 * Zero on success, errno on failure.
3892 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3893 struct drm_file *file_priv)
3895 struct drm_mode_obj_get_properties *arg = data;
3896 struct drm_mode_object *obj;
3897 int ret = 0;
3898 int i;
3899 int copied = 0;
3900 int props_count = 0;
3901 uint32_t __user *props_ptr;
3902 uint64_t __user *prop_values_ptr;
3904 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3905 return -EINVAL;
3907 drm_modeset_lock_all(dev);
3909 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3910 if (!obj) {
3911 ret = -ENOENT;
3912 goto out;
3914 if (!obj->properties) {
3915 ret = -EINVAL;
3916 goto out;
3919 props_count = obj->properties->count;
3921 /* This ioctl is called twice, once to determine how much space is
3922 * needed, and the 2nd time to fill it. */
3923 if ((arg->count_props >= props_count) && props_count) {
3924 copied = 0;
3925 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3926 prop_values_ptr = (uint64_t __user *)(unsigned long)
3927 (arg->prop_values_ptr);
3928 for (i = 0; i < props_count; i++) {
3929 if (put_user(obj->properties->ids[i],
3930 props_ptr + copied)) {
3931 ret = -EFAULT;
3932 goto out;
3934 if (put_user(obj->properties->values[i],
3935 prop_values_ptr + copied)) {
3936 ret = -EFAULT;
3937 goto out;
3939 copied++;
3942 arg->count_props = props_count;
3943 out:
3944 drm_modeset_unlock_all(dev);
3945 return ret;
3949 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3950 * @dev: DRM device
3951 * @data: ioctl data
3952 * @file_priv: DRM file info
3954 * This function sets the current value for an object's property. It also calls
3955 * into a driver's ->set_property callback to update the hardware state.
3956 * Compared to the connector specific ioctl this one is extended to also work on
3957 * crtc and plane objects.
3959 * Called by the user via ioctl.
3961 * Returns:
3962 * Zero on success, errno on failure.
3964 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3965 struct drm_file *file_priv)
3967 struct drm_mode_obj_set_property *arg = data;
3968 struct drm_mode_object *arg_obj;
3969 struct drm_mode_object *prop_obj;
3970 struct drm_property *property;
3971 int ret = -EINVAL;
3972 int i;
3974 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3975 return -EINVAL;
3977 drm_modeset_lock_all(dev);
3979 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3980 if (!arg_obj) {
3981 ret = -ENOENT;
3982 goto out;
3984 if (!arg_obj->properties)
3985 goto out;
3987 for (i = 0; i < arg_obj->properties->count; i++)
3988 if (arg_obj->properties->ids[i] == arg->prop_id)
3989 break;
3991 if (i == arg_obj->properties->count)
3992 goto out;
3994 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3995 DRM_MODE_OBJECT_PROPERTY);
3996 if (!prop_obj) {
3997 ret = -ENOENT;
3998 goto out;
4000 property = obj_to_property(prop_obj);
4002 if (!drm_property_change_is_valid(property, arg->value))
4003 goto out;
4005 switch (arg_obj->type) {
4006 case DRM_MODE_OBJECT_CONNECTOR:
4007 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4008 arg->value);
4009 break;
4010 case DRM_MODE_OBJECT_CRTC:
4011 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4012 break;
4013 case DRM_MODE_OBJECT_PLANE:
4014 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4015 break;
4018 out:
4019 drm_modeset_unlock_all(dev);
4020 return ret;
4024 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4025 * @connector: connector to attach
4026 * @encoder: encoder to attach @connector to
4028 * This function links up a connector to an encoder. Note that the routing
4029 * restrictions between encoders and crtcs are exposed to userspace through the
4030 * possible_clones and possible_crtcs bitmasks.
4032 * Returns:
4033 * Zero on success, errno on failure.
4035 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4036 struct drm_encoder *encoder)
4038 int i;
4040 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4041 if (connector->encoder_ids[i] == 0) {
4042 connector->encoder_ids[i] = encoder->base.id;
4043 return 0;
4046 return -ENOMEM;
4048 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4051 * drm_mode_crtc_set_gamma_size - set the gamma table size
4052 * @crtc: CRTC to set the gamma table size for
4053 * @gamma_size: size of the gamma table
4055 * Drivers which support gamma tables should set this to the supported gamma
4056 * table size when initializing the CRTC. Currently the drm core only supports a
4057 * fixed gamma table size.
4059 * Returns:
4060 * Zero on success, errno on failure.
4062 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4063 int gamma_size)
4065 crtc->gamma_size = gamma_size;
4067 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4068 if (!crtc->gamma_store) {
4069 crtc->gamma_size = 0;
4070 return -ENOMEM;
4073 return 0;
4075 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4078 * drm_mode_gamma_set_ioctl - set the gamma table
4079 * @dev: DRM device
4080 * @data: ioctl data
4081 * @file_priv: DRM file info
4083 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4084 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4086 * Called by the user via ioctl.
4088 * Returns:
4089 * Zero on success, errno on failure.
4091 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4092 void *data, struct drm_file *file_priv)
4094 struct drm_mode_crtc_lut *crtc_lut = data;
4095 struct drm_crtc *crtc;
4096 void *r_base, *g_base, *b_base;
4097 int size;
4098 int ret = 0;
4100 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4101 return -EINVAL;
4103 drm_modeset_lock_all(dev);
4104 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4105 if (!crtc) {
4106 ret = -ENOENT;
4107 goto out;
4110 if (crtc->funcs->gamma_set == NULL) {
4111 ret = -ENOSYS;
4112 goto out;
4115 /* memcpy into gamma store */
4116 if (crtc_lut->gamma_size != crtc->gamma_size) {
4117 ret = -EINVAL;
4118 goto out;
4121 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4122 r_base = crtc->gamma_store;
4123 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4124 ret = -EFAULT;
4125 goto out;
4128 g_base = r_base + size;
4129 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4130 ret = -EFAULT;
4131 goto out;
4134 b_base = g_base + size;
4135 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4136 ret = -EFAULT;
4137 goto out;
4140 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4142 out:
4143 drm_modeset_unlock_all(dev);
4144 return ret;
4149 * drm_mode_gamma_get_ioctl - get the gamma table
4150 * @dev: DRM device
4151 * @data: ioctl data
4152 * @file_priv: DRM file info
4154 * Copy the current gamma table into the storage provided. This also provides
4155 * the gamma table size the driver expects, which can be used to size the
4156 * allocated storage.
4158 * Called by the user via ioctl.
4160 * Returns:
4161 * Zero on success, errno on failure.
4163 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4164 void *data, struct drm_file *file_priv)
4166 struct drm_mode_crtc_lut *crtc_lut = data;
4167 struct drm_crtc *crtc;
4168 void *r_base, *g_base, *b_base;
4169 int size;
4170 int ret = 0;
4172 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4173 return -EINVAL;
4175 drm_modeset_lock_all(dev);
4176 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4177 if (!crtc) {
4178 ret = -ENOENT;
4179 goto out;
4182 /* memcpy into gamma store */
4183 if (crtc_lut->gamma_size != crtc->gamma_size) {
4184 ret = -EINVAL;
4185 goto out;
4188 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4189 r_base = crtc->gamma_store;
4190 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4191 ret = -EFAULT;
4192 goto out;
4195 g_base = r_base + size;
4196 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4197 ret = -EFAULT;
4198 goto out;
4201 b_base = g_base + size;
4202 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4203 ret = -EFAULT;
4204 goto out;
4206 out:
4207 drm_modeset_unlock_all(dev);
4208 return ret;
4212 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4213 * @dev: DRM device
4214 * @data: ioctl data
4215 * @file_priv: DRM file info
4217 * This schedules an asynchronous update on a given CRTC, called page flip.
4218 * Optionally a drm event is generated to signal the completion of the event.
4219 * Generic drivers cannot assume that a pageflip with changed framebuffer
4220 * properties (including driver specific metadata like tiling layout) will work,
4221 * but some drivers support e.g. pixel format changes through the pageflip
4222 * ioctl.
4224 * Called by the user via ioctl.
4226 * Returns:
4227 * Zero on success, errno on failure.
4229 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4230 void *data, struct drm_file *file_priv)
4232 struct drm_mode_crtc_page_flip *page_flip = data;
4233 struct drm_crtc *crtc;
4234 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4235 struct drm_pending_vblank_event *e = NULL;
4236 unsigned long flags;
4237 int ret = -EINVAL;
4239 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4240 page_flip->reserved != 0)
4241 return -EINVAL;
4243 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4244 return -EINVAL;
4246 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4247 if (!crtc)
4248 return -ENOENT;
4250 drm_modeset_lock(&crtc->mutex, NULL);
4251 if (crtc->primary->fb == NULL) {
4252 /* The framebuffer is currently unbound, presumably
4253 * due to a hotplug event, that userspace has not
4254 * yet discovered.
4256 ret = -EBUSY;
4257 goto out;
4260 if (crtc->funcs->page_flip == NULL)
4261 goto out;
4263 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4264 if (!fb) {
4265 ret = -ENOENT;
4266 goto out;
4269 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4270 if (ret)
4271 goto out;
4273 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4274 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4275 ret = -EINVAL;
4276 goto out;
4279 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4280 ret = -ENOMEM;
4281 spin_lock_irqsave(&dev->event_lock, flags);
4282 if (file_priv->event_space < sizeof e->event) {
4283 spin_unlock_irqrestore(&dev->event_lock, flags);
4284 goto out;
4286 file_priv->event_space -= sizeof e->event;
4287 spin_unlock_irqrestore(&dev->event_lock, flags);
4289 e = kzalloc(sizeof *e, GFP_KERNEL);
4290 if (e == NULL) {
4291 spin_lock_irqsave(&dev->event_lock, flags);
4292 file_priv->event_space += sizeof e->event;
4293 spin_unlock_irqrestore(&dev->event_lock, flags);
4294 goto out;
4297 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4298 e->event.base.length = sizeof e->event;
4299 e->event.user_data = page_flip->user_data;
4300 e->base.event = &e->event.base;
4301 e->base.file_priv = file_priv;
4302 e->base.destroy =
4303 (void (*) (struct drm_pending_event *)) kfree;
4306 old_fb = crtc->primary->fb;
4307 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4308 if (ret) {
4309 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4310 spin_lock_irqsave(&dev->event_lock, flags);
4311 file_priv->event_space += sizeof e->event;
4312 spin_unlock_irqrestore(&dev->event_lock, flags);
4313 kfree(e);
4315 /* Keep the old fb, don't unref it. */
4316 old_fb = NULL;
4317 } else {
4319 * Warn if the driver hasn't properly updated the crtc->fb
4320 * field to reflect that the new framebuffer is now used.
4321 * Failing to do so will screw with the reference counting
4322 * on framebuffers.
4324 WARN_ON(crtc->primary->fb != fb);
4325 /* Unref only the old framebuffer. */
4326 fb = NULL;
4329 out:
4330 if (fb)
4331 drm_framebuffer_unreference(fb);
4332 if (old_fb)
4333 drm_framebuffer_unreference(old_fb);
4334 drm_modeset_unlock(&crtc->mutex);
4336 return ret;
4340 * drm_mode_config_reset - call ->reset callbacks
4341 * @dev: drm device
4343 * This functions calls all the crtc's, encoder's and connector's ->reset
4344 * callback. Drivers can use this in e.g. their driver load or resume code to
4345 * reset hardware and software state.
4347 void drm_mode_config_reset(struct drm_device *dev)
4349 struct drm_crtc *crtc;
4350 struct drm_encoder *encoder;
4351 struct drm_connector *connector;
4353 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4354 if (crtc->funcs->reset)
4355 crtc->funcs->reset(crtc);
4357 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4358 if (encoder->funcs->reset)
4359 encoder->funcs->reset(encoder);
4361 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4362 connector->status = connector_status_unknown;
4364 if (connector->funcs->reset)
4365 connector->funcs->reset(connector);
4368 EXPORT_SYMBOL(drm_mode_config_reset);
4371 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4372 * @dev: DRM device
4373 * @data: ioctl data
4374 * @file_priv: DRM file info
4376 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4377 * TTM or something else entirely) and returns the resulting buffer handle. This
4378 * handle can then be wrapped up into a framebuffer modeset object.
4380 * Note that userspace is not allowed to use such objects for render
4381 * acceleration - drivers must create their own private ioctls for such a use
4382 * case.
4384 * Called by the user via ioctl.
4386 * Returns:
4387 * Zero on success, errno on failure.
4389 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4390 void *data, struct drm_file *file_priv)
4392 struct drm_mode_create_dumb *args = data;
4393 u32 cpp, stride, size;
4395 if (!dev->driver->dumb_create)
4396 return -ENOSYS;
4397 if (!args->width || !args->height || !args->bpp)
4398 return -EINVAL;
4400 /* overflow checks for 32bit size calculations */
4401 cpp = DIV_ROUND_UP(args->bpp, 8);
4402 if (cpp > 0xffffffffU / args->width)
4403 return -EINVAL;
4404 stride = cpp * args->width;
4405 if (args->height > 0xffffffffU / stride)
4406 return -EINVAL;
4408 /* test for wrap-around */
4409 size = args->height * stride;
4410 if (PAGE_ALIGN(size) == 0)
4411 return -EINVAL;
4413 return dev->driver->dumb_create(file_priv, dev, args);
4417 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4418 * @dev: DRM device
4419 * @data: ioctl data
4420 * @file_priv: DRM file info
4422 * Allocate an offset in the drm device node's address space to be able to
4423 * memory map a dumb buffer.
4425 * Called by the user via ioctl.
4427 * Returns:
4428 * Zero on success, errno on failure.
4430 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4431 void *data, struct drm_file *file_priv)
4433 struct drm_mode_map_dumb *args = data;
4435 /* call driver ioctl to get mmap offset */
4436 if (!dev->driver->dumb_map_offset)
4437 return -ENOSYS;
4439 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4443 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4444 * @dev: DRM device
4445 * @data: ioctl data
4446 * @file_priv: DRM file info
4448 * This destroys the userspace handle for the given dumb backing storage buffer.
4449 * Since buffer objects must be reference counted in the kernel a buffer object
4450 * won't be immediately freed if a framebuffer modeset object still uses it.
4452 * Called by the user via ioctl.
4454 * Returns:
4455 * Zero on success, errno on failure.
4457 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4458 void *data, struct drm_file *file_priv)
4460 struct drm_mode_destroy_dumb *args = data;
4462 if (!dev->driver->dumb_destroy)
4463 return -ENOSYS;
4465 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4469 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4470 * @format: pixel format (DRM_FORMAT_*)
4471 * @depth: storage for the depth value
4472 * @bpp: storage for the bpp value
4474 * This only supports RGB formats here for compat with code that doesn't use
4475 * pixel formats directly yet.
4477 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4478 int *bpp)
4480 switch (format) {
4481 case DRM_FORMAT_C8:
4482 case DRM_FORMAT_RGB332:
4483 case DRM_FORMAT_BGR233:
4484 *depth = 8;
4485 *bpp = 8;
4486 break;
4487 case DRM_FORMAT_XRGB1555:
4488 case DRM_FORMAT_XBGR1555:
4489 case DRM_FORMAT_RGBX5551:
4490 case DRM_FORMAT_BGRX5551:
4491 case DRM_FORMAT_ARGB1555:
4492 case DRM_FORMAT_ABGR1555:
4493 case DRM_FORMAT_RGBA5551:
4494 case DRM_FORMAT_BGRA5551:
4495 *depth = 15;
4496 *bpp = 16;
4497 break;
4498 case DRM_FORMAT_RGB565:
4499 case DRM_FORMAT_BGR565:
4500 *depth = 16;
4501 *bpp = 16;
4502 break;
4503 case DRM_FORMAT_RGB888:
4504 case DRM_FORMAT_BGR888:
4505 *depth = 24;
4506 *bpp = 24;
4507 break;
4508 case DRM_FORMAT_XRGB8888:
4509 case DRM_FORMAT_XBGR8888:
4510 case DRM_FORMAT_RGBX8888:
4511 case DRM_FORMAT_BGRX8888:
4512 *depth = 24;
4513 *bpp = 32;
4514 break;
4515 case DRM_FORMAT_XRGB2101010:
4516 case DRM_FORMAT_XBGR2101010:
4517 case DRM_FORMAT_RGBX1010102:
4518 case DRM_FORMAT_BGRX1010102:
4519 case DRM_FORMAT_ARGB2101010:
4520 case DRM_FORMAT_ABGR2101010:
4521 case DRM_FORMAT_RGBA1010102:
4522 case DRM_FORMAT_BGRA1010102:
4523 *depth = 30;
4524 *bpp = 32;
4525 break;
4526 case DRM_FORMAT_ARGB8888:
4527 case DRM_FORMAT_ABGR8888:
4528 case DRM_FORMAT_RGBA8888:
4529 case DRM_FORMAT_BGRA8888:
4530 *depth = 32;
4531 *bpp = 32;
4532 break;
4533 default:
4534 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4535 drm_get_format_name(format));
4536 *depth = 0;
4537 *bpp = 0;
4538 break;
4541 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4544 * drm_format_num_planes - get the number of planes for format
4545 * @format: pixel format (DRM_FORMAT_*)
4547 * Returns:
4548 * The number of planes used by the specified pixel format.
4550 int drm_format_num_planes(uint32_t format)
4552 switch (format) {
4553 case DRM_FORMAT_YUV410:
4554 case DRM_FORMAT_YVU410:
4555 case DRM_FORMAT_YUV411:
4556 case DRM_FORMAT_YVU411:
4557 case DRM_FORMAT_YUV420:
4558 case DRM_FORMAT_YVU420:
4559 case DRM_FORMAT_YUV422:
4560 case DRM_FORMAT_YVU422:
4561 case DRM_FORMAT_YUV444:
4562 case DRM_FORMAT_YVU444:
4563 return 3;
4564 case DRM_FORMAT_NV12:
4565 case DRM_FORMAT_NV21:
4566 case DRM_FORMAT_NV16:
4567 case DRM_FORMAT_NV61:
4568 case DRM_FORMAT_NV24:
4569 case DRM_FORMAT_NV42:
4570 return 2;
4571 default:
4572 return 1;
4575 EXPORT_SYMBOL(drm_format_num_planes);
4578 * drm_format_plane_cpp - determine the bytes per pixel value
4579 * @format: pixel format (DRM_FORMAT_*)
4580 * @plane: plane index
4582 * Returns:
4583 * The bytes per pixel value for the specified plane.
4585 int drm_format_plane_cpp(uint32_t format, int plane)
4587 unsigned int depth;
4588 int bpp;
4590 if (plane >= drm_format_num_planes(format))
4591 return 0;
4593 switch (format) {
4594 case DRM_FORMAT_YUYV:
4595 case DRM_FORMAT_YVYU:
4596 case DRM_FORMAT_UYVY:
4597 case DRM_FORMAT_VYUY:
4598 return 2;
4599 case DRM_FORMAT_NV12:
4600 case DRM_FORMAT_NV21:
4601 case DRM_FORMAT_NV16:
4602 case DRM_FORMAT_NV61:
4603 case DRM_FORMAT_NV24:
4604 case DRM_FORMAT_NV42:
4605 return plane ? 2 : 1;
4606 case DRM_FORMAT_YUV410:
4607 case DRM_FORMAT_YVU410:
4608 case DRM_FORMAT_YUV411:
4609 case DRM_FORMAT_YVU411:
4610 case DRM_FORMAT_YUV420:
4611 case DRM_FORMAT_YVU420:
4612 case DRM_FORMAT_YUV422:
4613 case DRM_FORMAT_YVU422:
4614 case DRM_FORMAT_YUV444:
4615 case DRM_FORMAT_YVU444:
4616 return 1;
4617 default:
4618 drm_fb_get_bpp_depth(format, &depth, &bpp);
4619 return bpp >> 3;
4622 EXPORT_SYMBOL(drm_format_plane_cpp);
4625 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4626 * @format: pixel format (DRM_FORMAT_*)
4628 * Returns:
4629 * The horizontal chroma subsampling factor for the
4630 * specified pixel format.
4632 int drm_format_horz_chroma_subsampling(uint32_t format)
4634 switch (format) {
4635 case DRM_FORMAT_YUV411:
4636 case DRM_FORMAT_YVU411:
4637 case DRM_FORMAT_YUV410:
4638 case DRM_FORMAT_YVU410:
4639 return 4;
4640 case DRM_FORMAT_YUYV:
4641 case DRM_FORMAT_YVYU:
4642 case DRM_FORMAT_UYVY:
4643 case DRM_FORMAT_VYUY:
4644 case DRM_FORMAT_NV12:
4645 case DRM_FORMAT_NV21:
4646 case DRM_FORMAT_NV16:
4647 case DRM_FORMAT_NV61:
4648 case DRM_FORMAT_YUV422:
4649 case DRM_FORMAT_YVU422:
4650 case DRM_FORMAT_YUV420:
4651 case DRM_FORMAT_YVU420:
4652 return 2;
4653 default:
4654 return 1;
4657 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4660 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4661 * @format: pixel format (DRM_FORMAT_*)
4663 * Returns:
4664 * The vertical chroma subsampling factor for the
4665 * specified pixel format.
4667 int drm_format_vert_chroma_subsampling(uint32_t format)
4669 switch (format) {
4670 case DRM_FORMAT_YUV410:
4671 case DRM_FORMAT_YVU410:
4672 return 4;
4673 case DRM_FORMAT_YUV420:
4674 case DRM_FORMAT_YVU420:
4675 case DRM_FORMAT_NV12:
4676 case DRM_FORMAT_NV21:
4677 return 2;
4678 default:
4679 return 1;
4682 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4685 * drm_mode_config_init - initialize DRM mode_configuration structure
4686 * @dev: DRM device
4688 * Initialize @dev's mode_config structure, used for tracking the graphics
4689 * configuration of @dev.
4691 * Since this initializes the modeset locks, no locking is possible. Which is no
4692 * problem, since this should happen single threaded at init time. It is the
4693 * driver's problem to ensure this guarantee.
4696 void drm_mode_config_init(struct drm_device *dev)
4698 mutex_init(&dev->mode_config.mutex);
4699 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
4700 mutex_init(&dev->mode_config.idr_mutex);
4701 mutex_init(&dev->mode_config.fb_lock);
4702 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4703 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4704 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4705 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4706 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4707 INIT_LIST_HEAD(&dev->mode_config.property_list);
4708 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4709 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4710 idr_init(&dev->mode_config.crtc_idr);
4712 drm_modeset_lock_all(dev);
4713 drm_mode_create_standard_connector_properties(dev);
4714 drm_mode_create_standard_plane_properties(dev);
4715 drm_modeset_unlock_all(dev);
4717 /* Just to be sure */
4718 dev->mode_config.num_fb = 0;
4719 dev->mode_config.num_connector = 0;
4720 dev->mode_config.num_crtc = 0;
4721 dev->mode_config.num_encoder = 0;
4722 dev->mode_config.num_overlay_plane = 0;
4723 dev->mode_config.num_total_plane = 0;
4725 EXPORT_SYMBOL(drm_mode_config_init);
4728 * drm_mode_config_cleanup - free up DRM mode_config info
4729 * @dev: DRM device
4731 * Free up all the connectors and CRTCs associated with this DRM device, then
4732 * free up the framebuffers and associated buffer objects.
4734 * Note that since this /should/ happen single-threaded at driver/device
4735 * teardown time, no locking is required. It's the driver's job to ensure that
4736 * this guarantee actually holds true.
4738 * FIXME: cleanup any dangling user buffer objects too
4740 void drm_mode_config_cleanup(struct drm_device *dev)
4742 struct drm_connector *connector, *ot;
4743 struct drm_crtc *crtc, *ct;
4744 struct drm_encoder *encoder, *enct;
4745 struct drm_bridge *bridge, *brt;
4746 struct drm_framebuffer *fb, *fbt;
4747 struct drm_property *property, *pt;
4748 struct drm_property_blob *blob, *bt;
4749 struct drm_plane *plane, *plt;
4751 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4752 head) {
4753 encoder->funcs->destroy(encoder);
4756 list_for_each_entry_safe(bridge, brt,
4757 &dev->mode_config.bridge_list, head) {
4758 bridge->funcs->destroy(bridge);
4761 list_for_each_entry_safe(connector, ot,
4762 &dev->mode_config.connector_list, head) {
4763 connector->funcs->destroy(connector);
4766 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4767 head) {
4768 drm_property_destroy(dev, property);
4771 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4772 head) {
4773 drm_property_destroy_blob(dev, blob);
4777 * Single-threaded teardown context, so it's not required to grab the
4778 * fb_lock to protect against concurrent fb_list access. Contrary, it
4779 * would actually deadlock with the drm_framebuffer_cleanup function.
4781 * Also, if there are any framebuffers left, that's a driver leak now,
4782 * so politely WARN about this.
4784 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4785 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4786 drm_framebuffer_remove(fb);
4789 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4790 head) {
4791 plane->funcs->destroy(plane);
4794 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4795 crtc->funcs->destroy(crtc);
4798 idr_destroy(&dev->mode_config.crtc_idr);
4799 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
4801 EXPORT_SYMBOL(drm_mode_config_cleanup);