drm: Define struct page and use it everywhere
[dragonfly.git] / sys / dev / drm / drm_crtc.c
blobe2bd8f4fb6848fc01f1707b2f2b76ed6c57cb096
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/export.h>
35 #include <drm/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <uapi_drm/drm_fourcc.h>
39 #include <linux/slab.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
43 #include "drm_crtc_internal.h"
44 #include "drm_internal.h"
46 static struct drm_framebuffer *
47 internal_framebuffer_create(struct drm_device *dev,
48 const struct drm_mode_fb_cmd2 *r,
49 struct drm_file *file_priv);
51 /* Avoid boilerplate. I'm tired of typing. */
52 #define DRM_ENUM_NAME_FN(fnname, list) \
53 const char *fnname(int val) \
54 { \
55 int i; \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
59 } \
60 return "(unknown)"; \
64 * Global properties
66 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
68 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
73 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
76 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
82 * Optional properties
84 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
85 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
91 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
98 * Non-global properties, but "required" for certain connectors.
100 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
114 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
117 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
135 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
138 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
144 struct drm_conn_prop_enum_list {
145 int type;
146 const char *name;
147 struct ida ida;
148 int count;
152 * Connector and encoder types.
154 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
155 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
156 { DRM_MODE_CONNECTOR_VGA, "VGA" },
157 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
158 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
159 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
160 { DRM_MODE_CONNECTOR_Composite, "Composite" },
161 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
162 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
163 { DRM_MODE_CONNECTOR_Component, "Component" },
164 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
165 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
166 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
167 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
168 { DRM_MODE_CONNECTOR_TV, "TV" },
169 { DRM_MODE_CONNECTOR_eDP, "eDP" },
170 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
171 { DRM_MODE_CONNECTOR_DSI, "DSI" },
172 { DRM_MODE_CONNECTOR_DPI, "DPI" },
175 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
176 { DRM_MODE_ENCODER_NONE, "None" },
177 { DRM_MODE_ENCODER_DAC, "DAC" },
178 { DRM_MODE_ENCODER_TMDS, "TMDS" },
179 { DRM_MODE_ENCODER_LVDS, "LVDS" },
180 { DRM_MODE_ENCODER_TVDAC, "TV" },
181 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
182 { DRM_MODE_ENCODER_DSI, "DSI" },
183 { DRM_MODE_ENCODER_DPMST, "DP MST" },
184 { DRM_MODE_ENCODER_DPI, "DPI" },
187 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
188 { SubPixelUnknown, "Unknown" },
189 { SubPixelHorizontalRGB, "Horizontal RGB" },
190 { SubPixelHorizontalBGR, "Horizontal BGR" },
191 { SubPixelVerticalRGB, "Vertical RGB" },
192 { SubPixelVerticalBGR, "Vertical BGR" },
193 { SubPixelNone, "None" },
196 void drm_connector_ida_init(void)
198 int i;
200 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
201 ida_init(&drm_connector_enum_list[i].ida);
204 void drm_connector_ida_destroy(void)
206 int i;
208 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
209 ida_destroy(&drm_connector_enum_list[i].ida);
213 * drm_get_connector_status_name - return a string for connector status
214 * @status: connector status to compute name of
216 * In contrast to the other drm_get_*_name functions this one here returns a
217 * const pointer and hence is threadsafe.
219 const char *drm_get_connector_status_name(enum drm_connector_status status)
221 if (status == connector_status_connected)
222 return "connected";
223 else if (status == connector_status_disconnected)
224 return "disconnected";
225 else
226 return "unknown";
228 EXPORT_SYMBOL(drm_get_connector_status_name);
231 * drm_get_subpixel_order_name - return a string for a given subpixel enum
232 * @order: enum of subpixel_order
234 * Note you could abuse this and return something out of bounds, but that
235 * would be a caller error. No unscrubbed user data should make it here.
237 const char *drm_get_subpixel_order_name(enum subpixel_order order)
239 return drm_subpixel_enum_list[order].name;
241 EXPORT_SYMBOL(drm_get_subpixel_order_name);
243 static char printable_char(int c)
245 return isascii(c) && isprint(c) ? c : '?';
249 * drm_get_format_name - return a string for drm fourcc format
250 * @format: format to compute name of
252 * Note that the buffer used by this function is globally shared and owned by
253 * the function itself.
255 * FIXME: This isn't really multithreading safe.
257 const char *drm_get_format_name(uint32_t format)
259 static char buf[32];
261 ksnprintf(buf, sizeof(buf),
262 "%c%c%c%c %s-endian (0x%08x)",
263 printable_char(format & 0xff),
264 printable_char((format >> 8) & 0xff),
265 printable_char((format >> 16) & 0xff),
266 printable_char((format >> 24) & 0x7f),
267 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
268 format);
270 return buf;
272 EXPORT_SYMBOL(drm_get_format_name);
275 * Internal function to assign a slot in the object idr and optionally
276 * register the object into the idr.
278 static int drm_mode_object_get_reg(struct drm_device *dev,
279 struct drm_mode_object *obj,
280 uint32_t obj_type,
281 bool register_obj,
282 void (*obj_free_cb)(struct kref *kref))
284 int ret;
286 mutex_lock(&dev->mode_config.idr_mutex);
287 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
288 if (ret >= 0) {
290 * Set up the object linking under the protection of the idr
291 * lock so that other users can't see inconsistent state.
293 obj->id = ret;
294 obj->type = obj_type;
295 if (obj_free_cb) {
296 obj->free_cb = obj_free_cb;
297 kref_init(&obj->refcount);
300 mutex_unlock(&dev->mode_config.idr_mutex);
302 return ret < 0 ? ret : 0;
306 * drm_mode_object_get - allocate a new modeset identifier
307 * @dev: DRM device
308 * @obj: object pointer, used to generate unique ID
309 * @obj_type: object type
311 * Create a unique identifier based on @ptr in @dev's identifier space. Used
312 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
313 * modeset identifiers are _not_ reference counted. Hence don't use this for
314 * reference counted modeset objects like framebuffers.
316 * Returns:
317 * Zero on success, error code on failure.
319 int drm_mode_object_get(struct drm_device *dev,
320 struct drm_mode_object *obj, uint32_t obj_type)
322 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
325 static void drm_mode_object_register(struct drm_device *dev,
326 struct drm_mode_object *obj)
328 mutex_lock(&dev->mode_config.idr_mutex);
329 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
330 mutex_unlock(&dev->mode_config.idr_mutex);
334 * drm_mode_object_unregister - free a modeset identifer
335 * @dev: DRM device
336 * @object: object to free
338 * Free @id from @dev's unique identifier pool.
339 * This function can be called multiple times, and guards against
340 * multiple removals.
341 * These modeset identifiers are _not_ reference counted. Hence don't use this
342 * for reference counted modeset objects like framebuffers.
344 void drm_mode_object_unregister(struct drm_device *dev,
345 struct drm_mode_object *object)
347 mutex_lock(&dev->mode_config.idr_mutex);
348 if (object->id) {
349 idr_remove(&dev->mode_config.crtc_idr, object->id);
350 object->id = 0;
352 mutex_unlock(&dev->mode_config.idr_mutex);
355 static struct drm_mode_object *_object_find(struct drm_device *dev,
356 uint32_t id, uint32_t type)
358 struct drm_mode_object *obj = NULL;
360 mutex_lock(&dev->mode_config.idr_mutex);
361 obj = idr_find(&dev->mode_config.crtc_idr, id);
362 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
363 obj = NULL;
364 if (obj && obj->id != id)
365 obj = NULL;
367 if (obj && obj->free_cb) {
368 if (!kref_get_unless_zero(&obj->refcount))
369 obj = NULL;
371 mutex_unlock(&dev->mode_config.idr_mutex);
373 return obj;
377 * drm_mode_object_find - look up a drm object with static lifetime
378 * @dev: drm device
379 * @id: id of the mode object
380 * @type: type of the mode object
382 * This function is used to look up a modeset object. It will acquire a
383 * reference for reference counted objects. This reference must be dropped again
384 * by callind drm_mode_object_unreference().
386 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
387 uint32_t id, uint32_t type)
389 struct drm_mode_object *obj = NULL;
391 obj = _object_find(dev, id, type);
392 return obj;
394 EXPORT_SYMBOL(drm_mode_object_find);
397 * drm_mode_object_unreference - decr the object refcnt
398 * @obj: mode_object
400 * This functions decrements the object's refcount if it is a refcounted modeset
401 * object. It is a no-op on any other object. This is used to drop references
402 * acquired with drm_mode_object_reference().
404 void drm_mode_object_unreference(struct drm_mode_object *obj)
406 if (obj->free_cb) {
407 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
408 kref_put(&obj->refcount, obj->free_cb);
411 EXPORT_SYMBOL(drm_mode_object_unreference);
414 * drm_mode_object_reference - incr the object refcnt
415 * @obj: mode_object
417 * This functions increments the object's refcount if it is a refcounted modeset
418 * object. It is a no-op on any other object. References should be dropped again
419 * by calling drm_mode_object_unreference().
421 void drm_mode_object_reference(struct drm_mode_object *obj)
423 if (obj->free_cb) {
424 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
425 kref_get(&obj->refcount);
428 EXPORT_SYMBOL(drm_mode_object_reference);
430 static void drm_framebuffer_free(struct kref *kref)
432 struct drm_framebuffer *fb =
433 container_of(kref, struct drm_framebuffer, base.refcount);
434 struct drm_device *dev = fb->dev;
437 * The lookup idr holds a weak reference, which has not necessarily been
438 * removed at this point. Check for that.
440 drm_mode_object_unregister(dev, &fb->base);
442 fb->funcs->destroy(fb);
446 * drm_framebuffer_init - initialize a framebuffer
447 * @dev: DRM device
448 * @fb: framebuffer to be initialized
449 * @funcs: ... with these functions
451 * Allocates an ID for the framebuffer's parent mode object, sets its mode
452 * functions & device file and adds it to the master fd list.
454 * IMPORTANT:
455 * This functions publishes the fb and makes it available for concurrent access
456 * by other users. Which means by this point the fb _must_ be fully set up -
457 * since all the fb attributes are invariant over its lifetime, no further
458 * locking but only correct reference counting is required.
460 * Returns:
461 * Zero on success, error code on failure.
463 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
464 const struct drm_framebuffer_funcs *funcs)
466 int ret;
468 INIT_LIST_HEAD(&fb->filp_head);
469 fb->dev = dev;
470 fb->funcs = funcs;
472 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
473 false, drm_framebuffer_free);
474 if (ret)
475 goto out;
477 mutex_lock(&dev->mode_config.fb_lock);
478 dev->mode_config.num_fb++;
479 list_add(&fb->head, &dev->mode_config.fb_list);
480 mutex_unlock(&dev->mode_config.fb_lock);
482 drm_mode_object_register(dev, &fb->base);
483 out:
484 return ret;
486 EXPORT_SYMBOL(drm_framebuffer_init);
489 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
490 * @dev: drm device
491 * @id: id of the fb object
493 * If successful, this grabs an additional reference to the framebuffer -
494 * callers need to make sure to eventually unreference the returned framebuffer
495 * again, using @drm_framebuffer_unreference.
497 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
498 uint32_t id)
500 struct drm_mode_object *obj;
501 struct drm_framebuffer *fb = NULL;
503 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
504 if (obj)
505 fb = obj_to_fb(obj);
506 return fb;
508 EXPORT_SYMBOL(drm_framebuffer_lookup);
511 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
512 * @fb: fb to unregister
514 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
515 * those used for fbdev. Note that the caller must hold a reference of it's own,
516 * i.e. the object may not be destroyed through this call (since it'll lead to a
517 * locking inversion).
519 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
521 struct drm_device *dev;
523 if (!fb)
524 return;
526 dev = fb->dev;
528 /* Mark fb as reaped and drop idr ref. */
529 drm_mode_object_unregister(dev, &fb->base);
531 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
534 * drm_framebuffer_cleanup - remove a framebuffer object
535 * @fb: framebuffer to remove
537 * Cleanup framebuffer. This function is intended to be used from the drivers
538 * ->destroy callback. It can also be used to clean up driver private
539 * framebuffers embedded into a larger structure.
541 * Note that this function does not remove the fb from active usuage - if it is
542 * still used anywhere, hilarity can ensue since userspace could call getfb on
543 * the id and get back -EINVAL. Obviously no concern at driver unload time.
545 * Also, the framebuffer will not be removed from the lookup idr - for
546 * user-created framebuffers this will happen in in the rmfb ioctl. For
547 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
548 * drm_framebuffer_unregister_private.
550 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
552 struct drm_device *dev = fb->dev;
554 mutex_lock(&dev->mode_config.fb_lock);
555 list_del(&fb->head);
556 dev->mode_config.num_fb--;
557 mutex_unlock(&dev->mode_config.fb_lock);
559 EXPORT_SYMBOL(drm_framebuffer_cleanup);
562 * drm_framebuffer_remove - remove and unreference a framebuffer object
563 * @fb: framebuffer to remove
565 * Scans all the CRTCs and planes in @dev's mode_config. If they're
566 * using @fb, removes it, setting it to NULL. Then drops the reference to the
567 * passed-in framebuffer. Might take the modeset locks.
569 * Note that this function optimizes the cleanup away if the caller holds the
570 * last reference to the framebuffer. It is also guaranteed to not take the
571 * modeset locks in this case.
573 void drm_framebuffer_remove(struct drm_framebuffer *fb)
575 struct drm_device *dev;
576 struct drm_crtc *crtc;
577 struct drm_plane *plane;
578 struct drm_mode_set set;
579 int ret;
581 if (!fb)
582 return;
584 dev = fb->dev;
586 WARN_ON(!list_empty(&fb->filp_head));
589 * drm ABI mandates that we remove any deleted framebuffers from active
590 * useage. But since most sane clients only remove framebuffers they no
591 * longer need, try to optimize this away.
593 * Since we're holding a reference ourselves, observing a refcount of 1
594 * means that we're the last holder and can skip it. Also, the refcount
595 * can never increase from 1 again, so we don't need any barriers or
596 * locks.
598 * Note that userspace could try to race with use and instate a new
599 * usage _after_ we've cleared all current ones. End result will be an
600 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
601 * in this manner.
603 if (drm_framebuffer_read_refcount(fb) > 1) {
604 drm_modeset_lock_all(dev);
605 /* remove from any CRTC */
606 drm_for_each_crtc(crtc, dev) {
607 if (crtc->primary->fb == fb) {
608 /* should turn off the crtc */
609 memset(&set, 0, sizeof(struct drm_mode_set));
610 set.crtc = crtc;
611 set.fb = NULL;
612 ret = drm_mode_set_config_internal(&set);
613 if (ret)
614 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
618 drm_for_each_plane(plane, dev) {
619 if (plane->fb == fb)
620 drm_plane_force_disable(plane);
622 drm_modeset_unlock_all(dev);
625 drm_framebuffer_unreference(fb);
627 EXPORT_SYMBOL(drm_framebuffer_remove);
629 DEFINE_WW_CLASS(crtc_ww_class);
631 static unsigned int drm_num_crtcs(struct drm_device *dev)
633 unsigned int num = 0;
634 struct drm_crtc *tmp;
636 drm_for_each_crtc(tmp, dev) {
637 num++;
640 return num;
644 * drm_crtc_init_with_planes - Initialise a new CRTC object with
645 * specified primary and cursor planes.
646 * @dev: DRM device
647 * @crtc: CRTC object to init
648 * @primary: Primary plane for CRTC
649 * @cursor: Cursor plane for CRTC
650 * @funcs: callbacks for the new CRTC
651 * @name: printf style format string for the CRTC name, or NULL for default name
653 * Inits a new object created as base part of a driver crtc object.
655 * Returns:
656 * Zero on success, error code on failure.
658 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
659 struct drm_plane *primary,
660 struct drm_plane *cursor,
661 const struct drm_crtc_funcs *funcs,
662 const char *name, ...)
664 struct drm_mode_config *config = &dev->mode_config;
665 int ret;
667 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
668 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
670 crtc->dev = dev;
671 crtc->funcs = funcs;
673 drm_modeset_lock_init(&crtc->mutex);
674 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
675 if (ret)
676 return ret;
678 if (name) {
679 __va_list ap;
681 __va_start(ap, name);
682 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
683 __va_end(ap);
684 } else {
685 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
686 drm_num_crtcs(dev));
688 if (!crtc->name) {
689 drm_mode_object_unregister(dev, &crtc->base);
690 return -ENOMEM;
693 crtc->base.properties = &crtc->properties;
695 list_add_tail(&crtc->head, &config->crtc_list);
696 config->num_crtc++;
698 crtc->primary = primary;
699 crtc->cursor = cursor;
700 if (primary)
701 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
702 if (cursor)
703 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
705 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
706 drm_object_attach_property(&crtc->base, config->prop_active, 0);
707 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
710 return 0;
712 EXPORT_SYMBOL(drm_crtc_init_with_planes);
715 * drm_crtc_cleanup - Clean up the core crtc usage
716 * @crtc: CRTC to cleanup
718 * This function cleans up @crtc and removes it from the DRM mode setting
719 * core. Note that the function does *not* free the crtc structure itself,
720 * this is the responsibility of the caller.
722 void drm_crtc_cleanup(struct drm_crtc *crtc)
724 struct drm_device *dev = crtc->dev;
726 kfree(crtc->gamma_store);
727 crtc->gamma_store = NULL;
729 drm_modeset_lock_fini(&crtc->mutex);
731 drm_mode_object_unregister(dev, &crtc->base);
732 list_del(&crtc->head);
733 dev->mode_config.num_crtc--;
735 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
736 if (crtc->state && crtc->funcs->atomic_destroy_state)
737 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
739 kfree(crtc->name);
741 memset(crtc, 0, sizeof(*crtc));
743 EXPORT_SYMBOL(drm_crtc_cleanup);
746 * drm_crtc_index - find the index of a registered CRTC
747 * @crtc: CRTC to find index for
749 * Given a registered CRTC, return the index of that CRTC within a DRM
750 * device's list of CRTCs.
752 unsigned int drm_crtc_index(struct drm_crtc *crtc)
754 unsigned int index = 0;
755 struct drm_crtc *tmp;
757 drm_for_each_crtc(tmp, crtc->dev) {
758 if (tmp == crtc)
759 return index;
761 index++;
764 BUG();
766 EXPORT_SYMBOL(drm_crtc_index);
769 * drm_mode_remove - remove and free a mode
770 * @connector: connector list to modify
771 * @mode: mode to remove
773 * Remove @mode from @connector's mode list, then free it.
775 static void drm_mode_remove(struct drm_connector *connector,
776 struct drm_display_mode *mode)
778 list_del(&mode->head);
779 drm_mode_destroy(connector->dev, mode);
783 * drm_display_info_set_bus_formats - set the supported bus formats
784 * @info: display info to store bus formats in
785 * @formats: array containing the supported bus formats
786 * @num_formats: the number of entries in the fmts array
788 * Store the supported bus formats in display info structure.
789 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
790 * a full list of available formats.
792 int drm_display_info_set_bus_formats(struct drm_display_info *info,
793 const u32 *formats,
794 unsigned int num_formats)
796 u32 *fmts = NULL;
798 if (!formats && num_formats)
799 return -EINVAL;
801 if (formats && num_formats) {
802 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
803 GFP_KERNEL);
804 if (!fmts)
805 return -ENOMEM;
808 kfree(info->bus_formats);
809 info->bus_formats = fmts;
810 info->num_bus_formats = num_formats;
812 return 0;
814 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
817 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
818 * @connector: connector to quwery
820 * The kernel supports per-connector configration of its consoles through
821 * use of the video= parameter. This function parses that option and
822 * extracts the user's specified mode (or enable/disable status) for a
823 * particular connector. This is typically only used during the early fbdev
824 * setup.
826 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
828 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
829 char *option = NULL;
831 if (fb_get_options(connector->name, &option))
832 return;
834 if (!drm_mode_parse_command_line_for_connector(option,
835 connector,
836 mode))
837 return;
839 if (mode->force) {
840 const char *s;
842 switch (mode->force) {
843 case DRM_FORCE_OFF:
844 s = "OFF";
845 break;
846 case DRM_FORCE_ON_DIGITAL:
847 s = "ON - dig";
848 break;
849 default:
850 case DRM_FORCE_ON:
851 s = "ON";
852 break;
855 DRM_INFO("forcing %s connector %s\n", connector->name, s);
856 connector->force = mode->force;
859 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
860 connector->name,
861 mode->xres, mode->yres,
862 mode->refresh_specified ? mode->refresh : 60,
863 mode->rb ? " reduced blanking" : "",
864 mode->margins ? " with margins" : "",
865 mode->interlace ? " interlaced" : "");
868 static void drm_connector_free(struct kref *kref)
870 struct drm_connector *connector =
871 container_of(kref, struct drm_connector, base.refcount);
872 struct drm_device *dev = connector->dev;
874 drm_mode_object_unregister(dev, &connector->base);
875 connector->funcs->destroy(connector);
879 * drm_connector_init - Init a preallocated connector
880 * @dev: DRM device
881 * @connector: the connector to init
882 * @funcs: callbacks for this connector
883 * @connector_type: user visible type of the connector
885 * Initialises a preallocated connector. Connectors should be
886 * subclassed as part of driver connector objects.
888 * Returns:
889 * Zero on success, error code on failure.
891 int drm_connector_init(struct drm_device *dev,
892 struct drm_connector *connector,
893 const struct drm_connector_funcs *funcs,
894 int connector_type)
896 struct drm_mode_config *config = &dev->mode_config;
897 int ret;
899 drm_modeset_lock_all(dev);
901 ret = drm_mode_object_get_reg(dev, &connector->base,
902 DRM_MODE_OBJECT_CONNECTOR,
903 true, drm_connector_free);
904 if (ret)
905 goto out_unlock;
907 connector->base.properties = &connector->properties;
908 connector->dev = dev;
909 connector->funcs = funcs;
911 connector->connector_type = connector_type;
912 connector->connector_type_id =
913 ++drm_connector_enum_list[connector_type].count; /* TODO */
914 if (connector->connector_type_id < 0) {
915 ret = connector->connector_type_id;
916 goto out_put;
918 connector->name =
919 kasprintf(GFP_KERNEL, "%s-%d",
920 drm_connector_enum_list[connector_type].name,
921 connector->connector_type_id);
922 if (!connector->name) {
923 ret = -ENOMEM;
924 goto out_put;
927 INIT_LIST_HEAD(&connector->probed_modes);
928 INIT_LIST_HEAD(&connector->modes);
929 connector->edid_blob_ptr = NULL;
930 connector->status = connector_status_unknown;
932 drm_connector_get_cmdline_mode(connector);
934 /* We should add connectors at the end to avoid upsetting the connector
935 * index too much. */
936 list_add_tail(&connector->head, &config->connector_list);
937 config->num_connector++;
939 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
940 drm_object_attach_property(&connector->base,
941 config->edid_property,
944 drm_object_attach_property(&connector->base,
945 config->dpms_property, 0);
947 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
948 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
951 connector->debugfs_entry = NULL;
953 out_put:
954 if (ret)
955 drm_mode_object_unregister(dev, &connector->base);
957 out_unlock:
958 drm_modeset_unlock_all(dev);
960 return ret;
962 EXPORT_SYMBOL(drm_connector_init);
965 * drm_connector_cleanup - cleans up an initialised connector
966 * @connector: connector to cleanup
968 * Cleans up the connector but doesn't free the object.
970 void drm_connector_cleanup(struct drm_connector *connector)
972 struct drm_device *dev = connector->dev;
973 struct drm_display_mode *mode, *t;
975 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
976 drm_mode_remove(connector, mode);
978 list_for_each_entry_safe(mode, t, &connector->modes, head)
979 drm_mode_remove(connector, mode);
981 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
982 connector->connector_type_id);
984 ida_remove(&dev->mode_config.connector_ida,
985 connector->connector_id);
987 kfree(connector->display_info.bus_formats);
988 drm_mode_object_unregister(dev, &connector->base);
989 kfree(connector->name);
990 connector->name = NULL;
991 list_del(&connector->head);
992 dev->mode_config.num_connector--;
994 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
995 if (connector->state && connector->funcs->atomic_destroy_state)
996 connector->funcs->atomic_destroy_state(connector,
997 connector->state);
999 memset(connector, 0, sizeof(*connector));
1001 EXPORT_SYMBOL(drm_connector_cleanup);
1004 * drm_connector_index - find the index of a registered connector
1005 * @connector: connector to find index for
1007 * Given a registered connector, return the index of that connector within a DRM
1008 * device's list of connectors.
1010 unsigned int drm_connector_index(struct drm_connector *connector)
1012 unsigned int index = 0;
1013 struct drm_connector *tmp;
1014 struct drm_mode_config *config = &connector->dev->mode_config;
1016 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1018 drm_for_each_connector(tmp, connector->dev) {
1019 if (tmp == connector)
1020 return index;
1022 index++;
1025 BUG();
1027 EXPORT_SYMBOL(drm_connector_index);
1030 * drm_connector_register - register a connector
1031 * @connector: the connector to register
1033 * Register userspace interfaces for a connector
1035 * Returns:
1036 * Zero on success, error code on failure.
1038 int drm_connector_register(struct drm_connector *connector)
1040 int ret;
1042 ret = drm_sysfs_connector_add(connector);
1043 if (ret)
1044 return ret;
1046 ret = drm_debugfs_connector_add(connector);
1047 if (ret) {
1048 drm_sysfs_connector_remove(connector);
1049 return ret;
1052 drm_mode_object_register(connector->dev, &connector->base);
1054 return 0;
1056 EXPORT_SYMBOL(drm_connector_register);
1059 * drm_connector_unregister - unregister a connector
1060 * @connector: the connector to unregister
1062 * Unregister userspace interfaces for a connector
1064 void drm_connector_unregister(struct drm_connector *connector)
1066 drm_sysfs_connector_remove(connector);
1067 #if 0
1068 drm_debugfs_connector_remove(connector);
1069 #endif
1071 EXPORT_SYMBOL(drm_connector_unregister);
1074 * drm_connector_register_all - register all connectors
1075 * @dev: drm device
1077 * This function registers all connectors in sysfs and other places so that
1078 * userspace can start to access them. Drivers can call it after calling
1079 * drm_dev_register() to complete the device registration, if they don't call
1080 * drm_connector_register() on each connector individually.
1082 * When a device is unplugged and should be removed from userspace access,
1083 * call drm_connector_unregister_all(), which is the inverse of this
1084 * function.
1086 * Returns:
1087 * Zero on success, error code on failure.
1089 int drm_connector_register_all(struct drm_device *dev)
1091 struct drm_connector *connector;
1092 int ret;
1094 mutex_lock(&dev->mode_config.mutex);
1096 drm_for_each_connector(connector, dev) {
1097 ret = drm_connector_register(connector);
1098 if (ret)
1099 goto err;
1102 mutex_unlock(&dev->mode_config.mutex);
1104 return 0;
1106 err:
1107 mutex_unlock(&dev->mode_config.mutex);
1108 drm_connector_unregister_all(dev);
1109 return ret;
1111 EXPORT_SYMBOL(drm_connector_register_all);
1114 * drm_connector_unregister_all - unregister connector userspace interfaces
1115 * @dev: drm device
1117 * This functions unregisters all connectors from sysfs and other places so
1118 * that userspace can no longer access them. Drivers should call this as the
1119 * first step tearing down the device instace, or when the underlying
1120 * physical device disappeared (e.g. USB unplug), right before calling
1121 * drm_dev_unregister().
1123 void drm_connector_unregister_all(struct drm_device *dev)
1125 struct drm_connector *connector;
1127 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1128 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1129 drm_connector_unregister(connector);
1131 EXPORT_SYMBOL(drm_connector_unregister_all);
1134 * drm_encoder_init - Init a preallocated encoder
1135 * @dev: drm device
1136 * @encoder: the encoder to init
1137 * @funcs: callbacks for this encoder
1138 * @encoder_type: user visible type of the encoder
1139 * @name: printf style format string for the encoder name, or NULL for default name
1141 * Initialises a preallocated encoder. Encoder should be
1142 * subclassed as part of driver encoder objects.
1144 * Returns:
1145 * Zero on success, error code on failure.
1147 int drm_encoder_init(struct drm_device *dev,
1148 struct drm_encoder *encoder,
1149 const struct drm_encoder_funcs *funcs,
1150 int encoder_type, const char *name, ...)
1152 int ret;
1154 drm_modeset_lock_all(dev);
1156 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1157 if (ret)
1158 goto out_unlock;
1160 encoder->dev = dev;
1161 encoder->encoder_type = encoder_type;
1162 encoder->funcs = funcs;
1163 if (name) {
1164 __va_list ap;
1166 __va_start(ap, name);
1167 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1168 __va_end(ap);
1169 } else {
1170 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1171 drm_encoder_enum_list[encoder_type].name,
1172 encoder->base.id);
1174 if (!encoder->name) {
1175 ret = -ENOMEM;
1176 goto out_put;
1179 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1180 dev->mode_config.num_encoder++;
1182 out_put:
1183 if (ret)
1184 drm_mode_object_unregister(dev, &encoder->base);
1186 out_unlock:
1187 drm_modeset_unlock_all(dev);
1189 return ret;
1191 EXPORT_SYMBOL(drm_encoder_init);
1194 * drm_encoder_index - find the index of a registered encoder
1195 * @encoder: encoder to find index for
1197 * Given a registered encoder, return the index of that encoder within a DRM
1198 * device's list of encoders.
1200 unsigned int drm_encoder_index(struct drm_encoder *encoder)
1202 unsigned int index = 0;
1203 struct drm_encoder *tmp;
1205 drm_for_each_encoder(tmp, encoder->dev) {
1206 if (tmp == encoder)
1207 return index;
1209 index++;
1212 BUG();
1214 EXPORT_SYMBOL(drm_encoder_index);
1217 * drm_encoder_cleanup - cleans up an initialised encoder
1218 * @encoder: encoder to cleanup
1220 * Cleans up the encoder but doesn't free the object.
1222 void drm_encoder_cleanup(struct drm_encoder *encoder)
1224 struct drm_device *dev = encoder->dev;
1226 drm_modeset_lock_all(dev);
1227 drm_mode_object_unregister(dev, &encoder->base);
1228 kfree(encoder->name);
1229 list_del(&encoder->head);
1230 dev->mode_config.num_encoder--;
1231 drm_modeset_unlock_all(dev);
1233 memset(encoder, 0, sizeof(*encoder));
1235 EXPORT_SYMBOL(drm_encoder_cleanup);
1237 static unsigned int drm_num_planes(struct drm_device *dev)
1239 unsigned int num = 0;
1240 struct drm_plane *tmp;
1242 drm_for_each_plane(tmp, dev) {
1243 num++;
1246 return num;
1250 * drm_universal_plane_init - Initialize a new universal plane object
1251 * @dev: DRM device
1252 * @plane: plane object to init
1253 * @possible_crtcs: bitmask of possible CRTCs
1254 * @funcs: callbacks for the new plane
1255 * @formats: array of supported formats (%DRM_FORMAT_*)
1256 * @format_count: number of elements in @formats
1257 * @type: type of plane (overlay, primary, cursor)
1258 * @name: printf style format string for the plane name, or NULL for default name
1260 * Initializes a plane object of type @type.
1262 * Returns:
1263 * Zero on success, error code on failure.
1265 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1266 unsigned long possible_crtcs,
1267 const struct drm_plane_funcs *funcs,
1268 const uint32_t *formats, unsigned int format_count,
1269 enum drm_plane_type type,
1270 const char *name, ...)
1272 struct drm_mode_config *config = &dev->mode_config;
1273 int ret;
1275 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1276 if (ret)
1277 return ret;
1279 drm_modeset_lock_init(&plane->mutex);
1281 plane->base.properties = &plane->properties;
1282 plane->dev = dev;
1283 plane->funcs = funcs;
1284 plane->format_types = kmalloc(format_count * sizeof(uint32_t),
1285 M_DRM, M_WAITOK);
1286 if (!plane->format_types) {
1287 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1288 drm_mode_object_unregister(dev, &plane->base);
1289 return -ENOMEM;
1292 if (name) {
1293 __va_list ap;
1295 __va_start(ap, name);
1296 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1297 __va_end(ap);
1298 } else {
1299 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1300 drm_num_planes(dev));
1302 if (!plane->name) {
1303 kfree(plane->format_types);
1304 drm_mode_object_unregister(dev, &plane->base);
1305 return -ENOMEM;
1308 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1309 plane->format_count = format_count;
1310 plane->possible_crtcs = possible_crtcs;
1311 plane->type = type;
1313 list_add_tail(&plane->head, &config->plane_list);
1314 config->num_total_plane++;
1315 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1316 config->num_overlay_plane++;
1318 drm_object_attach_property(&plane->base,
1319 config->plane_type_property,
1320 plane->type);
1322 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1323 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1324 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1325 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1326 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1327 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1328 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1329 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1330 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1331 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1332 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1335 return 0;
1337 EXPORT_SYMBOL(drm_universal_plane_init);
1340 * drm_plane_init - Initialize a legacy plane
1341 * @dev: DRM device
1342 * @plane: plane object to init
1343 * @possible_crtcs: bitmask of possible CRTCs
1344 * @funcs: callbacks for the new plane
1345 * @formats: array of supported formats (%DRM_FORMAT_*)
1346 * @format_count: number of elements in @formats
1347 * @is_primary: plane type (primary vs overlay)
1349 * Legacy API to initialize a DRM plane.
1351 * New drivers should call drm_universal_plane_init() instead.
1353 * Returns:
1354 * Zero on success, error code on failure.
1356 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1357 unsigned long possible_crtcs,
1358 const struct drm_plane_funcs *funcs,
1359 const uint32_t *formats, unsigned int format_count,
1360 bool is_primary)
1362 enum drm_plane_type type;
1364 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1365 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1366 formats, format_count, type, NULL);
1368 EXPORT_SYMBOL(drm_plane_init);
1371 * drm_plane_cleanup - Clean up the core plane usage
1372 * @plane: plane to cleanup
1374 * This function cleans up @plane and removes it from the DRM mode setting
1375 * core. Note that the function does *not* free the plane structure itself,
1376 * this is the responsibility of the caller.
1378 void drm_plane_cleanup(struct drm_plane *plane)
1380 struct drm_device *dev = plane->dev;
1382 drm_modeset_lock_all(dev);
1383 kfree(plane->format_types);
1384 drm_mode_object_unregister(dev, &plane->base);
1386 BUG_ON(list_empty(&plane->head));
1388 list_del(&plane->head);
1389 dev->mode_config.num_total_plane--;
1390 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1391 dev->mode_config.num_overlay_plane--;
1392 drm_modeset_unlock_all(dev);
1394 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1395 if (plane->state && plane->funcs->atomic_destroy_state)
1396 plane->funcs->atomic_destroy_state(plane, plane->state);
1398 kfree(plane->name);
1400 memset(plane, 0, sizeof(*plane));
1402 EXPORT_SYMBOL(drm_plane_cleanup);
1405 * drm_plane_index - find the index of a registered plane
1406 * @plane: plane to find index for
1408 * Given a registered plane, return the index of that CRTC within a DRM
1409 * device's list of planes.
1411 unsigned int drm_plane_index(struct drm_plane *plane)
1413 unsigned int index = 0;
1414 struct drm_plane *tmp;
1416 drm_for_each_plane(tmp, plane->dev) {
1417 if (tmp == plane)
1418 return index;
1420 index++;
1423 BUG();
1425 EXPORT_SYMBOL(drm_plane_index);
1428 * drm_plane_from_index - find the registered plane at an index
1429 * @dev: DRM device
1430 * @idx: index of registered plane to find for
1432 * Given a plane index, return the registered plane from DRM device's
1433 * list of planes with matching index.
1435 struct drm_plane *
1436 drm_plane_from_index(struct drm_device *dev, int idx)
1438 struct drm_plane *plane;
1439 unsigned int i = 0;
1441 drm_for_each_plane(plane, dev) {
1442 if (i == idx)
1443 return plane;
1444 i++;
1446 return NULL;
1448 EXPORT_SYMBOL(drm_plane_from_index);
1451 * drm_plane_force_disable - Forcibly disable a plane
1452 * @plane: plane to disable
1454 * Forces the plane to be disabled.
1456 * Used when the plane's current framebuffer is destroyed,
1457 * and when restoring fbdev mode.
1459 void drm_plane_force_disable(struct drm_plane *plane)
1461 int ret;
1463 if (!plane->fb)
1464 return;
1466 plane->old_fb = plane->fb;
1467 ret = plane->funcs->disable_plane(plane);
1468 if (ret) {
1469 DRM_ERROR("failed to disable plane with busy fb\n");
1470 plane->old_fb = NULL;
1471 return;
1473 /* disconnect the plane from the fb and crtc: */
1474 drm_framebuffer_unreference(plane->old_fb);
1475 plane->old_fb = NULL;
1476 plane->fb = NULL;
1477 plane->crtc = NULL;
1479 EXPORT_SYMBOL(drm_plane_force_disable);
1481 static int drm_mode_create_standard_properties(struct drm_device *dev)
1483 struct drm_property *prop;
1486 * Standard properties (apply to all connectors)
1488 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1489 DRM_MODE_PROP_IMMUTABLE,
1490 "EDID", 0);
1491 if (!prop)
1492 return -ENOMEM;
1493 dev->mode_config.edid_property = prop;
1495 prop = drm_property_create_enum(dev, 0,
1496 "DPMS", drm_dpms_enum_list,
1497 ARRAY_SIZE(drm_dpms_enum_list));
1498 if (!prop)
1499 return -ENOMEM;
1500 dev->mode_config.dpms_property = prop;
1502 prop = drm_property_create(dev,
1503 DRM_MODE_PROP_BLOB |
1504 DRM_MODE_PROP_IMMUTABLE,
1505 "PATH", 0);
1506 if (!prop)
1507 return -ENOMEM;
1508 dev->mode_config.path_property = prop;
1510 prop = drm_property_create(dev,
1511 DRM_MODE_PROP_BLOB |
1512 DRM_MODE_PROP_IMMUTABLE,
1513 "TILE", 0);
1514 if (!prop)
1515 return -ENOMEM;
1516 dev->mode_config.tile_property = prop;
1518 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1519 "type", drm_plane_type_enum_list,
1520 ARRAY_SIZE(drm_plane_type_enum_list));
1521 if (!prop)
1522 return -ENOMEM;
1523 dev->mode_config.plane_type_property = prop;
1525 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1526 "SRC_X", 0, UINT_MAX);
1527 if (!prop)
1528 return -ENOMEM;
1529 dev->mode_config.prop_src_x = prop;
1531 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1532 "SRC_Y", 0, UINT_MAX);
1533 if (!prop)
1534 return -ENOMEM;
1535 dev->mode_config.prop_src_y = prop;
1537 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1538 "SRC_W", 0, UINT_MAX);
1539 if (!prop)
1540 return -ENOMEM;
1541 dev->mode_config.prop_src_w = prop;
1543 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1544 "SRC_H", 0, UINT_MAX);
1545 if (!prop)
1546 return -ENOMEM;
1547 dev->mode_config.prop_src_h = prop;
1549 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1550 "CRTC_X", INT_MIN, INT_MAX);
1551 if (!prop)
1552 return -ENOMEM;
1553 dev->mode_config.prop_crtc_x = prop;
1555 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1556 "CRTC_Y", INT_MIN, INT_MAX);
1557 if (!prop)
1558 return -ENOMEM;
1559 dev->mode_config.prop_crtc_y = prop;
1561 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1562 "CRTC_W", 0, INT_MAX);
1563 if (!prop)
1564 return -ENOMEM;
1565 dev->mode_config.prop_crtc_w = prop;
1567 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1568 "CRTC_H", 0, INT_MAX);
1569 if (!prop)
1570 return -ENOMEM;
1571 dev->mode_config.prop_crtc_h = prop;
1573 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1574 "FB_ID", DRM_MODE_OBJECT_FB);
1575 if (!prop)
1576 return -ENOMEM;
1577 dev->mode_config.prop_fb_id = prop;
1579 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1580 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1581 if (!prop)
1582 return -ENOMEM;
1583 dev->mode_config.prop_crtc_id = prop;
1585 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1586 "ACTIVE");
1587 if (!prop)
1588 return -ENOMEM;
1589 dev->mode_config.prop_active = prop;
1591 prop = drm_property_create(dev,
1592 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1593 "MODE_ID", 0);
1594 if (!prop)
1595 return -ENOMEM;
1596 dev->mode_config.prop_mode_id = prop;
1598 prop = drm_property_create(dev,
1599 DRM_MODE_PROP_BLOB,
1600 "DEGAMMA_LUT", 0);
1601 if (!prop)
1602 return -ENOMEM;
1603 dev->mode_config.degamma_lut_property = prop;
1605 prop = drm_property_create_range(dev,
1606 DRM_MODE_PROP_IMMUTABLE,
1607 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1608 if (!prop)
1609 return -ENOMEM;
1610 dev->mode_config.degamma_lut_size_property = prop;
1612 prop = drm_property_create(dev,
1613 DRM_MODE_PROP_BLOB,
1614 "CTM", 0);
1615 if (!prop)
1616 return -ENOMEM;
1617 dev->mode_config.ctm_property = prop;
1619 prop = drm_property_create(dev,
1620 DRM_MODE_PROP_BLOB,
1621 "GAMMA_LUT", 0);
1622 if (!prop)
1623 return -ENOMEM;
1624 dev->mode_config.gamma_lut_property = prop;
1626 prop = drm_property_create_range(dev,
1627 DRM_MODE_PROP_IMMUTABLE,
1628 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1629 if (!prop)
1630 return -ENOMEM;
1631 dev->mode_config.gamma_lut_size_property = prop;
1633 return 0;
1637 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1638 * @dev: DRM device
1640 * Called by a driver the first time a DVI-I connector is made.
1642 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1644 struct drm_property *dvi_i_selector;
1645 struct drm_property *dvi_i_subconnector;
1647 if (dev->mode_config.dvi_i_select_subconnector_property)
1648 return 0;
1650 dvi_i_selector =
1651 drm_property_create_enum(dev, 0,
1652 "select subconnector",
1653 drm_dvi_i_select_enum_list,
1654 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1655 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1657 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1658 "subconnector",
1659 drm_dvi_i_subconnector_enum_list,
1660 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1661 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1663 return 0;
1665 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1668 * drm_create_tv_properties - create TV specific connector properties
1669 * @dev: DRM device
1670 * @num_modes: number of different TV formats (modes) supported
1671 * @modes: array of pointers to strings containing name of each format
1673 * Called by a driver's TV initialization routine, this function creates
1674 * the TV specific connector properties for a given device. Caller is
1675 * responsible for allocating a list of format names and passing them to
1676 * this routine.
1678 int drm_mode_create_tv_properties(struct drm_device *dev,
1679 unsigned int num_modes,
1680 const char * const modes[])
1682 struct drm_property *tv_selector;
1683 struct drm_property *tv_subconnector;
1684 unsigned int i;
1686 if (dev->mode_config.tv_select_subconnector_property)
1687 return 0;
1690 * Basic connector properties
1692 tv_selector = drm_property_create_enum(dev, 0,
1693 "select subconnector",
1694 drm_tv_select_enum_list,
1695 ARRAY_SIZE(drm_tv_select_enum_list));
1696 if (!tv_selector)
1697 goto nomem;
1699 dev->mode_config.tv_select_subconnector_property = tv_selector;
1701 tv_subconnector =
1702 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1703 "subconnector",
1704 drm_tv_subconnector_enum_list,
1705 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1706 if (!tv_subconnector)
1707 goto nomem;
1708 dev->mode_config.tv_subconnector_property = tv_subconnector;
1711 * Other, TV specific properties: margins & TV modes.
1713 dev->mode_config.tv_left_margin_property =
1714 drm_property_create_range(dev, 0, "left margin", 0, 100);
1715 if (!dev->mode_config.tv_left_margin_property)
1716 goto nomem;
1718 dev->mode_config.tv_right_margin_property =
1719 drm_property_create_range(dev, 0, "right margin", 0, 100);
1720 if (!dev->mode_config.tv_right_margin_property)
1721 goto nomem;
1723 dev->mode_config.tv_top_margin_property =
1724 drm_property_create_range(dev, 0, "top margin", 0, 100);
1725 if (!dev->mode_config.tv_top_margin_property)
1726 goto nomem;
1728 dev->mode_config.tv_bottom_margin_property =
1729 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1730 if (!dev->mode_config.tv_bottom_margin_property)
1731 goto nomem;
1733 dev->mode_config.tv_mode_property =
1734 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1735 "mode", num_modes);
1736 if (!dev->mode_config.tv_mode_property)
1737 goto nomem;
1739 for (i = 0; i < num_modes; i++)
1740 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1741 i, modes[i]);
1743 dev->mode_config.tv_brightness_property =
1744 drm_property_create_range(dev, 0, "brightness", 0, 100);
1745 if (!dev->mode_config.tv_brightness_property)
1746 goto nomem;
1748 dev->mode_config.tv_contrast_property =
1749 drm_property_create_range(dev, 0, "contrast", 0, 100);
1750 if (!dev->mode_config.tv_contrast_property)
1751 goto nomem;
1753 dev->mode_config.tv_flicker_reduction_property =
1754 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1755 if (!dev->mode_config.tv_flicker_reduction_property)
1756 goto nomem;
1758 dev->mode_config.tv_overscan_property =
1759 drm_property_create_range(dev, 0, "overscan", 0, 100);
1760 if (!dev->mode_config.tv_overscan_property)
1761 goto nomem;
1763 dev->mode_config.tv_saturation_property =
1764 drm_property_create_range(dev, 0, "saturation", 0, 100);
1765 if (!dev->mode_config.tv_saturation_property)
1766 goto nomem;
1768 dev->mode_config.tv_hue_property =
1769 drm_property_create_range(dev, 0, "hue", 0, 100);
1770 if (!dev->mode_config.tv_hue_property)
1771 goto nomem;
1773 return 0;
1774 nomem:
1775 return -ENOMEM;
1777 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1780 * drm_mode_create_scaling_mode_property - create scaling mode property
1781 * @dev: DRM device
1783 * Called by a driver the first time it's needed, must be attached to desired
1784 * connectors.
1786 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1788 struct drm_property *scaling_mode;
1790 if (dev->mode_config.scaling_mode_property)
1791 return 0;
1793 scaling_mode =
1794 drm_property_create_enum(dev, 0, "scaling mode",
1795 drm_scaling_mode_enum_list,
1796 ARRAY_SIZE(drm_scaling_mode_enum_list));
1798 dev->mode_config.scaling_mode_property = scaling_mode;
1800 return 0;
1802 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1805 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1806 * @dev: DRM device
1808 * Called by a driver the first time it's needed, must be attached to desired
1809 * connectors.
1811 * Returns:
1812 * Zero on success, negative errno on failure.
1814 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1816 if (dev->mode_config.aspect_ratio_property)
1817 return 0;
1819 dev->mode_config.aspect_ratio_property =
1820 drm_property_create_enum(dev, 0, "aspect ratio",
1821 drm_aspect_ratio_enum_list,
1822 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1824 if (dev->mode_config.aspect_ratio_property == NULL)
1825 return -ENOMEM;
1827 return 0;
1829 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1832 * drm_mode_create_dirty_property - create dirty property
1833 * @dev: DRM device
1835 * Called by a driver the first time it's needed, must be attached to desired
1836 * connectors.
1838 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1840 struct drm_property *dirty_info;
1842 if (dev->mode_config.dirty_info_property)
1843 return 0;
1845 dirty_info =
1846 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1847 "dirty",
1848 drm_dirty_info_enum_list,
1849 ARRAY_SIZE(drm_dirty_info_enum_list));
1850 dev->mode_config.dirty_info_property = dirty_info;
1852 return 0;
1854 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1857 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1858 * @dev: DRM device
1860 * Create the the suggested x/y offset property for connectors.
1862 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1864 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1865 return 0;
1867 dev->mode_config.suggested_x_property =
1868 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1870 dev->mode_config.suggested_y_property =
1871 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1873 if (dev->mode_config.suggested_x_property == NULL ||
1874 dev->mode_config.suggested_y_property == NULL)
1875 return -ENOMEM;
1876 return 0;
1878 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1881 * drm_mode_getresources - get graphics configuration
1882 * @dev: drm device for the ioctl
1883 * @data: data pointer for the ioctl
1884 * @file_priv: drm file for the ioctl call
1886 * Construct a set of configuration description structures and return
1887 * them to the user, including CRTC, connector and framebuffer configuration.
1889 * Called by the user via ioctl.
1891 * Returns:
1892 * Zero on success, negative errno on failure.
1894 int drm_mode_getresources(struct drm_device *dev, void *data,
1895 struct drm_file *file_priv)
1897 struct drm_mode_card_res *card_res = data;
1898 struct list_head *lh;
1899 struct drm_framebuffer *fb;
1900 struct drm_connector *connector;
1901 struct drm_crtc *crtc;
1902 struct drm_encoder *encoder;
1903 int ret = 0;
1904 int connector_count = 0;
1905 int crtc_count = 0;
1906 int fb_count = 0;
1907 int encoder_count = 0;
1908 int copied = 0;
1909 uint32_t __user *fb_id;
1910 uint32_t __user *crtc_id;
1911 uint32_t __user *connector_id;
1912 uint32_t __user *encoder_id;
1914 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1915 return -EINVAL;
1918 mutex_lock(&file_priv->fbs_lock);
1920 * For the non-control nodes we need to limit the list of resources
1921 * by IDs in the group list for this node
1923 list_for_each(lh, &file_priv->fbs)
1924 fb_count++;
1926 /* handle this in 4 parts */
1927 /* FBs */
1928 if (card_res->count_fbs >= fb_count) {
1929 copied = 0;
1930 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1931 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1932 if (put_user(fb->base.id, fb_id + copied)) {
1933 mutex_unlock(&file_priv->fbs_lock);
1934 return -EFAULT;
1936 copied++;
1939 card_res->count_fbs = fb_count;
1940 mutex_unlock(&file_priv->fbs_lock);
1942 /* mode_config.mutex protects the connector list against e.g. DP MST
1943 * connector hot-adding. CRTC/Plane lists are invariant. */
1944 mutex_lock(&dev->mode_config.mutex);
1945 drm_for_each_crtc(crtc, dev)
1946 crtc_count++;
1948 drm_for_each_connector(connector, dev)
1949 connector_count++;
1951 drm_for_each_encoder(encoder, dev)
1952 encoder_count++;
1954 card_res->max_height = dev->mode_config.max_height;
1955 card_res->min_height = dev->mode_config.min_height;
1956 card_res->max_width = dev->mode_config.max_width;
1957 card_res->min_width = dev->mode_config.min_width;
1959 /* CRTCs */
1960 if (card_res->count_crtcs >= crtc_count) {
1961 copied = 0;
1962 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1963 drm_for_each_crtc(crtc, dev) {
1964 if (put_user(crtc->base.id, crtc_id + copied)) {
1965 ret = -EFAULT;
1966 goto out;
1968 copied++;
1971 card_res->count_crtcs = crtc_count;
1973 /* Encoders */
1974 if (card_res->count_encoders >= encoder_count) {
1975 copied = 0;
1976 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1977 drm_for_each_encoder(encoder, dev) {
1978 if (put_user(encoder->base.id, encoder_id +
1979 copied)) {
1980 ret = -EFAULT;
1981 goto out;
1983 copied++;
1986 card_res->count_encoders = encoder_count;
1988 /* Connectors */
1989 if (card_res->count_connectors >= connector_count) {
1990 copied = 0;
1991 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1992 drm_for_each_connector(connector, dev) {
1993 if (put_user(connector->base.id,
1994 connector_id + copied)) {
1995 ret = -EFAULT;
1996 goto out;
1998 copied++;
2001 card_res->count_connectors = connector_count;
2003 out:
2004 mutex_unlock(&dev->mode_config.mutex);
2005 return ret;
2009 * drm_mode_getcrtc - get CRTC configuration
2010 * @dev: drm device for the ioctl
2011 * @data: data pointer for the ioctl
2012 * @file_priv: drm file for the ioctl call
2014 * Construct a CRTC configuration structure to return to the user.
2016 * Called by the user via ioctl.
2018 * Returns:
2019 * Zero on success, negative errno on failure.
2021 int drm_mode_getcrtc(struct drm_device *dev,
2022 void *data, struct drm_file *file_priv)
2024 struct drm_mode_crtc *crtc_resp = data;
2025 struct drm_crtc *crtc;
2027 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2028 return -EINVAL;
2030 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2031 if (!crtc)
2032 return -ENOENT;
2034 drm_modeset_lock_crtc(crtc, crtc->primary);
2035 crtc_resp->gamma_size = crtc->gamma_size;
2036 if (crtc->primary->fb)
2037 crtc_resp->fb_id = crtc->primary->fb->base.id;
2038 else
2039 crtc_resp->fb_id = 0;
2041 if (crtc->state) {
2042 crtc_resp->x = crtc->primary->state->src_x >> 16;
2043 crtc_resp->y = crtc->primary->state->src_y >> 16;
2044 if (crtc->state->enable) {
2045 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2046 crtc_resp->mode_valid = 1;
2048 } else {
2049 crtc_resp->mode_valid = 0;
2051 } else {
2052 crtc_resp->x = crtc->x;
2053 crtc_resp->y = crtc->y;
2054 if (crtc->enabled) {
2055 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2056 crtc_resp->mode_valid = 1;
2058 } else {
2059 crtc_resp->mode_valid = 0;
2062 drm_modeset_unlock_crtc(crtc);
2064 return 0;
2067 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2068 const struct drm_file *file_priv)
2071 * If user-space hasn't configured the driver to expose the stereo 3D
2072 * modes, don't expose them.
2074 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2075 return false;
2077 return true;
2080 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2082 /* For atomic drivers only state objects are synchronously updated and
2083 * protected by modeset locks, so check those first. */
2084 if (connector->state)
2085 return connector->state->best_encoder;
2086 return connector->encoder;
2089 /* helper for getconnector and getproperties ioctls */
2090 static int get_properties(struct drm_mode_object *obj, bool atomic,
2091 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2092 uint32_t *arg_count_props)
2094 int props_count;
2095 int i, ret, copied;
2097 props_count = obj->properties->count;
2098 if (!atomic)
2099 props_count -= obj->properties->atomic_count;
2101 if ((*arg_count_props >= props_count) && props_count) {
2102 for (i = 0, copied = 0; copied < props_count; i++) {
2103 struct drm_property *prop = obj->properties->properties[i];
2104 uint64_t val;
2106 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2107 continue;
2109 ret = drm_object_property_get_value(obj, prop, &val);
2110 if (ret)
2111 return ret;
2113 if (put_user(prop->base.id, prop_ptr + copied))
2114 return -EFAULT;
2116 if (put_user(val, prop_values + copied))
2117 return -EFAULT;
2119 copied++;
2122 *arg_count_props = props_count;
2124 return 0;
2128 * drm_mode_getconnector - get connector configuration
2129 * @dev: drm device for the ioctl
2130 * @data: data pointer for the ioctl
2131 * @file_priv: drm file for the ioctl call
2133 * Construct a connector configuration structure to return to the user.
2135 * Called by the user via ioctl.
2137 * Returns:
2138 * Zero on success, negative errno on failure.
2140 int drm_mode_getconnector(struct drm_device *dev, void *data,
2141 struct drm_file *file_priv)
2143 struct drm_mode_get_connector *out_resp = data;
2144 struct drm_connector *connector;
2145 struct drm_encoder *encoder;
2146 struct drm_display_mode *mode;
2147 int mode_count = 0;
2148 int encoders_count = 0;
2149 int ret = 0;
2150 int copied = 0;
2151 int i;
2152 struct drm_mode_modeinfo u_mode;
2153 struct drm_mode_modeinfo __user *mode_ptr;
2154 uint32_t __user *encoder_ptr;
2156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2157 return -EINVAL;
2159 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2161 mutex_lock(&dev->mode_config.mutex);
2163 connector = drm_connector_lookup(dev, out_resp->connector_id);
2164 if (!connector) {
2165 ret = -ENOENT;
2166 goto out_unlock;
2169 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2170 if (connector->encoder_ids[i] != 0)
2171 encoders_count++;
2173 if (out_resp->count_modes == 0) {
2174 connector->funcs->fill_modes(connector,
2175 dev->mode_config.max_width,
2176 dev->mode_config.max_height);
2179 /* delayed so we get modes regardless of pre-fill_modes state */
2180 list_for_each_entry(mode, &connector->modes, head)
2181 if (drm_mode_expose_to_userspace(mode, file_priv))
2182 mode_count++;
2184 out_resp->connector_id = connector->base.id;
2185 out_resp->connector_type = connector->connector_type;
2186 out_resp->connector_type_id = connector->connector_type_id;
2187 out_resp->mm_width = connector->display_info.width_mm;
2188 out_resp->mm_height = connector->display_info.height_mm;
2189 out_resp->subpixel = connector->display_info.subpixel_order;
2190 out_resp->connection = connector->status;
2192 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2193 encoder = drm_connector_get_encoder(connector);
2194 if (encoder)
2195 out_resp->encoder_id = encoder->base.id;
2196 else
2197 out_resp->encoder_id = 0;
2200 * This ioctl is called twice, once to determine how much space is
2201 * needed, and the 2nd time to fill it.
2203 if ((out_resp->count_modes >= mode_count) && mode_count) {
2204 copied = 0;
2205 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2206 list_for_each_entry(mode, &connector->modes, head) {
2207 if (!drm_mode_expose_to_userspace(mode, file_priv))
2208 continue;
2210 drm_mode_convert_to_umode(&u_mode, mode);
2211 if (copy_to_user(mode_ptr + copied,
2212 &u_mode, sizeof(u_mode))) {
2213 ret = -EFAULT;
2214 goto out;
2216 copied++;
2219 out_resp->count_modes = mode_count;
2221 ret = get_properties(&connector->base, file_priv->atomic,
2222 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2223 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2224 &out_resp->count_props);
2225 if (ret)
2226 goto out;
2228 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2229 copied = 0;
2230 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2231 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2232 if (connector->encoder_ids[i] != 0) {
2233 if (put_user(connector->encoder_ids[i],
2234 encoder_ptr + copied)) {
2235 ret = -EFAULT;
2236 goto out;
2238 copied++;
2242 out_resp->count_encoders = encoders_count;
2244 out:
2245 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2247 drm_connector_unreference(connector);
2248 out_unlock:
2249 mutex_unlock(&dev->mode_config.mutex);
2251 return ret;
2254 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2256 struct drm_connector *connector;
2257 struct drm_device *dev = encoder->dev;
2258 bool uses_atomic = false;
2260 /* For atomic drivers only state objects are synchronously updated and
2261 * protected by modeset locks, so check those first. */
2262 drm_for_each_connector(connector, dev) {
2263 if (!connector->state)
2264 continue;
2266 uses_atomic = true;
2268 if (connector->state->best_encoder != encoder)
2269 continue;
2271 return connector->state->crtc;
2274 /* Don't return stale data (e.g. pending async disable). */
2275 if (uses_atomic)
2276 return NULL;
2278 return encoder->crtc;
2282 * drm_mode_getencoder - get encoder configuration
2283 * @dev: drm device for the ioctl
2284 * @data: data pointer for the ioctl
2285 * @file_priv: drm file for the ioctl call
2287 * Construct a encoder configuration structure to return to the user.
2289 * Called by the user via ioctl.
2291 * Returns:
2292 * Zero on success, negative errno on failure.
2294 int drm_mode_getencoder(struct drm_device *dev, void *data,
2295 struct drm_file *file_priv)
2297 struct drm_mode_get_encoder *enc_resp = data;
2298 struct drm_encoder *encoder;
2299 struct drm_crtc *crtc;
2301 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2302 return -EINVAL;
2304 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2305 if (!encoder)
2306 return -ENOENT;
2308 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2309 crtc = drm_encoder_get_crtc(encoder);
2310 if (crtc)
2311 enc_resp->crtc_id = crtc->base.id;
2312 else
2313 enc_resp->crtc_id = 0;
2314 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2316 enc_resp->encoder_type = encoder->encoder_type;
2317 enc_resp->encoder_id = encoder->base.id;
2318 enc_resp->possible_crtcs = encoder->possible_crtcs;
2319 enc_resp->possible_clones = encoder->possible_clones;
2321 return 0;
2325 * drm_mode_getplane_res - enumerate all plane resources
2326 * @dev: DRM device
2327 * @data: ioctl data
2328 * @file_priv: DRM file info
2330 * Construct a list of plane ids to return to the user.
2332 * Called by the user via ioctl.
2334 * Returns:
2335 * Zero on success, negative errno on failure.
2337 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2338 struct drm_file *file_priv)
2340 struct drm_mode_get_plane_res *plane_resp = data;
2341 struct drm_mode_config *config;
2342 struct drm_plane *plane;
2343 uint32_t __user *plane_ptr;
2344 int copied = 0;
2345 unsigned num_planes;
2347 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2348 return -EINVAL;
2350 config = &dev->mode_config;
2352 if (file_priv->universal_planes)
2353 num_planes = config->num_total_plane;
2354 else
2355 num_planes = config->num_overlay_plane;
2358 * This ioctl is called twice, once to determine how much space is
2359 * needed, and the 2nd time to fill it.
2361 if (num_planes &&
2362 (plane_resp->count_planes >= num_planes)) {
2363 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2365 /* Plane lists are invariant, no locking needed. */
2366 drm_for_each_plane(plane, dev) {
2368 * Unless userspace set the 'universal planes'
2369 * capability bit, only advertise overlays.
2371 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2372 !file_priv->universal_planes)
2373 continue;
2375 if (put_user(plane->base.id, plane_ptr + copied))
2376 return -EFAULT;
2377 copied++;
2380 plane_resp->count_planes = num_planes;
2382 return 0;
2386 * drm_mode_getplane - get plane configuration
2387 * @dev: DRM device
2388 * @data: ioctl data
2389 * @file_priv: DRM file info
2391 * Construct a plane configuration structure to return to the user.
2393 * Called by the user via ioctl.
2395 * Returns:
2396 * Zero on success, negative errno on failure.
2398 int drm_mode_getplane(struct drm_device *dev, void *data,
2399 struct drm_file *file_priv)
2401 struct drm_mode_get_plane *plane_resp = data;
2402 struct drm_plane *plane;
2403 uint32_t __user *format_ptr;
2405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2406 return -EINVAL;
2408 plane = drm_plane_find(dev, plane_resp->plane_id);
2409 if (!plane)
2410 return -ENOENT;
2412 drm_modeset_lock(&plane->mutex, NULL);
2413 if (plane->crtc)
2414 plane_resp->crtc_id = plane->crtc->base.id;
2415 else
2416 plane_resp->crtc_id = 0;
2418 if (plane->fb)
2419 plane_resp->fb_id = plane->fb->base.id;
2420 else
2421 plane_resp->fb_id = 0;
2422 drm_modeset_unlock(&plane->mutex);
2424 plane_resp->plane_id = plane->base.id;
2425 plane_resp->possible_crtcs = plane->possible_crtcs;
2426 plane_resp->gamma_size = 0;
2429 * This ioctl is called twice, once to determine how much space is
2430 * needed, and the 2nd time to fill it.
2432 if (plane->format_count &&
2433 (plane_resp->count_format_types >= plane->format_count)) {
2434 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2435 if (copy_to_user(format_ptr,
2436 plane->format_types,
2437 sizeof(uint32_t) * plane->format_count)) {
2438 return -EFAULT;
2441 plane_resp->count_format_types = plane->format_count;
2443 return 0;
2447 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2448 * @plane: plane to check for format support
2449 * @format: the pixel format
2451 * Returns:
2452 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2453 * otherwise.
2455 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2457 unsigned int i;
2459 for (i = 0; i < plane->format_count; i++) {
2460 if (format == plane->format_types[i])
2461 return 0;
2464 return -EINVAL;
2467 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2468 uint32_t src_w, uint32_t src_h,
2469 const struct drm_framebuffer *fb)
2471 unsigned int fb_width, fb_height;
2473 fb_width = fb->width << 16;
2474 fb_height = fb->height << 16;
2476 /* Make sure source coordinates are inside the fb. */
2477 if (src_w > fb_width ||
2478 src_x > fb_width - src_w ||
2479 src_h > fb_height ||
2480 src_y > fb_height - src_h) {
2481 DRM_DEBUG_KMS("Invalid source coordinates "
2482 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2483 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2484 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2485 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2486 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2487 return -ENOSPC;
2490 return 0;
2494 * setplane_internal - setplane handler for internal callers
2496 * Note that we assume an extra reference has already been taken on fb. If the
2497 * update fails, this reference will be dropped before return; if it succeeds,
2498 * the previous framebuffer (if any) will be unreferenced instead.
2500 * src_{x,y,w,h} are provided in 16.16 fixed point format
2502 static int __setplane_internal(struct drm_plane *plane,
2503 struct drm_crtc *crtc,
2504 struct drm_framebuffer *fb,
2505 int32_t crtc_x, int32_t crtc_y,
2506 uint32_t crtc_w, uint32_t crtc_h,
2507 /* src_{x,y,w,h} values are 16.16 fixed point */
2508 uint32_t src_x, uint32_t src_y,
2509 uint32_t src_w, uint32_t src_h)
2511 int ret = 0;
2513 /* No fb means shut it down */
2514 if (!fb) {
2515 plane->old_fb = plane->fb;
2516 ret = plane->funcs->disable_plane(plane);
2517 if (!ret) {
2518 plane->crtc = NULL;
2519 plane->fb = NULL;
2520 } else {
2521 plane->old_fb = NULL;
2523 goto out;
2526 /* Check whether this plane is usable on this CRTC */
2527 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2528 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2529 ret = -EINVAL;
2530 goto out;
2533 /* Check whether this plane supports the fb pixel format. */
2534 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2535 if (ret) {
2536 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2537 drm_get_format_name(fb->pixel_format));
2538 goto out;
2541 /* Give drivers some help against integer overflows */
2542 if (crtc_w > INT_MAX ||
2543 crtc_x > INT_MAX - (int32_t) crtc_w ||
2544 crtc_h > INT_MAX ||
2545 crtc_y > INT_MAX - (int32_t) crtc_h) {
2546 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2547 crtc_w, crtc_h, crtc_x, crtc_y);
2548 ret = -ERANGE;
2549 goto out;
2552 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2553 if (ret)
2554 goto out;
2556 plane->old_fb = plane->fb;
2557 ret = plane->funcs->update_plane(plane, crtc, fb,
2558 crtc_x, crtc_y, crtc_w, crtc_h,
2559 src_x, src_y, src_w, src_h);
2560 if (!ret) {
2561 plane->crtc = crtc;
2562 plane->fb = fb;
2563 fb = NULL;
2564 } else {
2565 plane->old_fb = NULL;
2568 out:
2569 if (fb)
2570 drm_framebuffer_unreference(fb);
2571 if (plane->old_fb)
2572 drm_framebuffer_unreference(plane->old_fb);
2573 plane->old_fb = NULL;
2575 return ret;
2578 static int setplane_internal(struct drm_plane *plane,
2579 struct drm_crtc *crtc,
2580 struct drm_framebuffer *fb,
2581 int32_t crtc_x, int32_t crtc_y,
2582 uint32_t crtc_w, uint32_t crtc_h,
2583 /* src_{x,y,w,h} values are 16.16 fixed point */
2584 uint32_t src_x, uint32_t src_y,
2585 uint32_t src_w, uint32_t src_h)
2587 int ret;
2589 drm_modeset_lock_all(plane->dev);
2590 ret = __setplane_internal(plane, crtc, fb,
2591 crtc_x, crtc_y, crtc_w, crtc_h,
2592 src_x, src_y, src_w, src_h);
2593 drm_modeset_unlock_all(plane->dev);
2595 return ret;
2599 * drm_mode_setplane - configure a plane's configuration
2600 * @dev: DRM device
2601 * @data: ioctl data*
2602 * @file_priv: DRM file info
2604 * Set plane configuration, including placement, fb, scaling, and other factors.
2605 * Or pass a NULL fb to disable (planes may be disabled without providing a
2606 * valid crtc).
2608 * Returns:
2609 * Zero on success, negative errno on failure.
2611 int drm_mode_setplane(struct drm_device *dev, void *data,
2612 struct drm_file *file_priv)
2614 struct drm_mode_set_plane *plane_req = data;
2615 struct drm_plane *plane;
2616 struct drm_crtc *crtc = NULL;
2617 struct drm_framebuffer *fb = NULL;
2619 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2620 return -EINVAL;
2623 * First, find the plane, crtc, and fb objects. If not available,
2624 * we don't bother to call the driver.
2626 plane = drm_plane_find(dev, plane_req->plane_id);
2627 if (!plane) {
2628 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2629 plane_req->plane_id);
2630 return -ENOENT;
2633 if (plane_req->fb_id) {
2634 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2635 if (!fb) {
2636 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2637 plane_req->fb_id);
2638 return -ENOENT;
2641 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2642 if (!crtc) {
2643 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2644 plane_req->crtc_id);
2645 return -ENOENT;
2650 * setplane_internal will take care of deref'ing either the old or new
2651 * framebuffer depending on success.
2653 return setplane_internal(plane, crtc, fb,
2654 plane_req->crtc_x, plane_req->crtc_y,
2655 plane_req->crtc_w, plane_req->crtc_h,
2656 plane_req->src_x, plane_req->src_y,
2657 plane_req->src_w, plane_req->src_h);
2661 * drm_mode_set_config_internal - helper to call ->set_config
2662 * @set: modeset config to set
2664 * This is a little helper to wrap internal calls to the ->set_config driver
2665 * interface. The only thing it adds is correct refcounting dance.
2667 * Returns:
2668 * Zero on success, negative errno on failure.
2670 int drm_mode_set_config_internal(struct drm_mode_set *set)
2672 struct drm_crtc *crtc = set->crtc;
2673 struct drm_framebuffer *fb;
2674 struct drm_crtc *tmp;
2675 int ret;
2678 * NOTE: ->set_config can also disable other crtcs (if we steal all
2679 * connectors from it), hence we need to refcount the fbs across all
2680 * crtcs. Atomic modeset will have saner semantics ...
2682 drm_for_each_crtc(tmp, crtc->dev)
2683 tmp->primary->old_fb = tmp->primary->fb;
2685 fb = set->fb;
2687 ret = crtc->funcs->set_config(set);
2688 if (ret == 0) {
2689 crtc->primary->crtc = crtc;
2690 crtc->primary->fb = fb;
2693 drm_for_each_crtc(tmp, crtc->dev) {
2694 if (tmp->primary->fb)
2695 drm_framebuffer_reference(tmp->primary->fb);
2696 if (tmp->primary->old_fb)
2697 drm_framebuffer_unreference(tmp->primary->old_fb);
2698 tmp->primary->old_fb = NULL;
2701 return ret;
2703 EXPORT_SYMBOL(drm_mode_set_config_internal);
2706 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2707 * @mode: mode to query
2708 * @hdisplay: hdisplay value to fill in
2709 * @vdisplay: vdisplay value to fill in
2711 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2712 * the appropriate layout.
2714 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2715 int *hdisplay, int *vdisplay)
2717 struct drm_display_mode adjusted;
2719 drm_mode_copy(&adjusted, mode);
2720 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2721 *hdisplay = adjusted.crtc_hdisplay;
2722 *vdisplay = adjusted.crtc_vdisplay;
2724 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2727 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2728 * CRTC viewport
2729 * @crtc: CRTC that framebuffer will be displayed on
2730 * @x: x panning
2731 * @y: y panning
2732 * @mode: mode that framebuffer will be displayed under
2733 * @fb: framebuffer to check size of
2735 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2736 int x, int y,
2737 const struct drm_display_mode *mode,
2738 const struct drm_framebuffer *fb)
2741 int hdisplay, vdisplay;
2743 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2745 if (crtc->state &&
2746 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2747 BIT(DRM_ROTATE_270)))
2748 swap(hdisplay, vdisplay);
2750 return check_src_coords(x << 16, y << 16,
2751 hdisplay << 16, vdisplay << 16, fb);
2753 EXPORT_SYMBOL(drm_crtc_check_viewport);
2756 * drm_mode_setcrtc - set CRTC configuration
2757 * @dev: drm device for the ioctl
2758 * @data: data pointer for the ioctl
2759 * @file_priv: drm file for the ioctl call
2761 * Build a new CRTC configuration based on user request.
2763 * Called by the user via ioctl.
2765 * Returns:
2766 * Zero on success, negative errno on failure.
2768 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2769 struct drm_file *file_priv)
2771 struct drm_mode_config *config = &dev->mode_config;
2772 struct drm_mode_crtc *crtc_req = data;
2773 struct drm_crtc *crtc;
2774 struct drm_connector **connector_set = NULL, *connector;
2775 struct drm_framebuffer *fb = NULL;
2776 struct drm_display_mode *mode = NULL;
2777 struct drm_mode_set set;
2778 uint32_t __user *set_connectors_ptr;
2779 int ret;
2780 int i;
2782 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2783 return -EINVAL;
2786 * Universal plane src offsets are only 16.16, prevent havoc for
2787 * drivers using universal plane code internally.
2789 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2790 return -ERANGE;
2792 drm_modeset_lock_all(dev);
2793 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2794 if (!crtc) {
2795 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2796 ret = -ENOENT;
2797 goto out;
2799 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2801 if (crtc_req->mode_valid) {
2802 /* If we have a mode we need a framebuffer. */
2803 /* If we pass -1, set the mode with the currently bound fb */
2804 if (crtc_req->fb_id == -1) {
2805 if (!crtc->primary->fb) {
2806 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2807 ret = -EINVAL;
2808 goto out;
2810 fb = crtc->primary->fb;
2811 /* Make refcounting symmetric with the lookup path. */
2812 drm_framebuffer_reference(fb);
2813 } else {
2814 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2815 if (!fb) {
2816 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2817 crtc_req->fb_id);
2818 ret = -ENOENT;
2819 goto out;
2823 mode = drm_mode_create(dev);
2824 if (!mode) {
2825 ret = -ENOMEM;
2826 goto out;
2829 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2830 if (ret) {
2831 DRM_DEBUG_KMS("Invalid mode\n");
2832 goto out;
2836 * Check whether the primary plane supports the fb pixel format.
2837 * Drivers not implementing the universal planes API use a
2838 * default formats list provided by the DRM core which doesn't
2839 * match real hardware capabilities. Skip the check in that
2840 * case.
2842 if (!crtc->primary->format_default) {
2843 ret = drm_plane_check_pixel_format(crtc->primary,
2844 fb->pixel_format);
2845 if (ret) {
2846 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2847 drm_get_format_name(fb->pixel_format));
2848 goto out;
2852 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2853 mode, fb);
2854 if (ret)
2855 goto out;
2859 if (crtc_req->count_connectors == 0 && mode) {
2860 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2861 ret = -EINVAL;
2862 goto out;
2865 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2866 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2867 crtc_req->count_connectors);
2868 ret = -EINVAL;
2869 goto out;
2872 if (crtc_req->count_connectors > 0) {
2873 u32 out_id;
2875 /* Avoid unbounded kernel memory allocation */
2876 if (crtc_req->count_connectors > config->num_connector) {
2877 ret = -EINVAL;
2878 goto out;
2881 connector_set = kmalloc(crtc_req->count_connectors *
2882 sizeof(struct drm_connector *),
2883 M_DRM, M_WAITOK);
2884 if (!connector_set) {
2885 ret = -ENOMEM;
2886 goto out;
2889 for (i = 0; i < crtc_req->count_connectors; i++) {
2890 connector_set[i] = NULL;
2891 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2892 if (get_user(out_id, &set_connectors_ptr[i])) {
2893 ret = -EFAULT;
2894 goto out;
2897 connector = drm_connector_lookup(dev, out_id);
2898 if (!connector) {
2899 DRM_DEBUG_KMS("Connector id %d unknown\n",
2900 out_id);
2901 ret = -ENOENT;
2902 goto out;
2904 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2905 connector->base.id,
2906 connector->name);
2908 connector_set[i] = connector;
2912 set.crtc = crtc;
2913 set.x = crtc_req->x;
2914 set.y = crtc_req->y;
2915 set.mode = mode;
2916 set.connectors = connector_set;
2917 set.num_connectors = crtc_req->count_connectors;
2918 set.fb = fb;
2919 ret = drm_mode_set_config_internal(&set);
2921 out:
2922 if (fb)
2923 drm_framebuffer_unreference(fb);
2925 if (connector_set) {
2926 for (i = 0; i < crtc_req->count_connectors; i++) {
2927 if (connector_set[i])
2928 drm_connector_unreference(connector_set[i]);
2931 kfree(connector_set);
2932 drm_mode_destroy(dev, mode);
2933 drm_modeset_unlock_all(dev);
2934 return ret;
2938 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2939 * universal plane handler call
2940 * @crtc: crtc to update cursor for
2941 * @req: data pointer for the ioctl
2942 * @file_priv: drm file for the ioctl call
2944 * Legacy cursor ioctl's work directly with driver buffer handles. To
2945 * translate legacy ioctl calls into universal plane handler calls, we need to
2946 * wrap the native buffer handle in a drm_framebuffer.
2948 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2949 * buffer with a pitch of 4*width; the universal plane interface should be used
2950 * directly in cases where the hardware can support other buffer settings and
2951 * userspace wants to make use of these capabilities.
2953 * Returns:
2954 * Zero on success, negative errno on failure.
2956 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2957 struct drm_mode_cursor2 *req,
2958 struct drm_file *file_priv)
2960 struct drm_device *dev = crtc->dev;
2961 struct drm_framebuffer *fb = NULL;
2962 struct drm_mode_fb_cmd2 fbreq = {
2963 .width = req->width,
2964 .height = req->height,
2965 .pixel_format = DRM_FORMAT_ARGB8888,
2966 .pitches = { req->width * 4 },
2967 .handles = { req->handle },
2969 int32_t crtc_x, crtc_y;
2970 uint32_t crtc_w = 0, crtc_h = 0;
2971 uint32_t src_w = 0, src_h = 0;
2972 int ret = 0;
2974 BUG_ON(!crtc->cursor);
2975 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2978 * Obtain fb we'll be using (either new or existing) and take an extra
2979 * reference to it if fb != null. setplane will take care of dropping
2980 * the reference if the plane update fails.
2982 if (req->flags & DRM_MODE_CURSOR_BO) {
2983 if (req->handle) {
2984 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2985 if (IS_ERR(fb)) {
2986 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2987 return PTR_ERR(fb);
2989 } else {
2990 fb = NULL;
2992 } else {
2993 fb = crtc->cursor->fb;
2994 if (fb)
2995 drm_framebuffer_reference(fb);
2998 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2999 crtc_x = req->x;
3000 crtc_y = req->y;
3001 } else {
3002 crtc_x = crtc->cursor_x;
3003 crtc_y = crtc->cursor_y;
3006 if (fb) {
3007 crtc_w = fb->width;
3008 crtc_h = fb->height;
3009 src_w = fb->width << 16;
3010 src_h = fb->height << 16;
3014 * setplane_internal will take care of deref'ing either the old or new
3015 * framebuffer depending on success.
3017 ret = __setplane_internal(crtc->cursor, crtc, fb,
3018 crtc_x, crtc_y, crtc_w, crtc_h,
3019 0, 0, src_w, src_h);
3021 /* Update successful; save new cursor position, if necessary */
3022 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3023 crtc->cursor_x = req->x;
3024 crtc->cursor_y = req->y;
3027 return ret;
3030 static int drm_mode_cursor_common(struct drm_device *dev,
3031 struct drm_mode_cursor2 *req,
3032 struct drm_file *file_priv)
3034 struct drm_crtc *crtc;
3035 int ret = 0;
3037 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3038 return -EINVAL;
3040 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3041 return -EINVAL;
3043 crtc = drm_crtc_find(dev, req->crtc_id);
3044 if (!crtc) {
3045 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3046 return -ENOENT;
3050 * If this crtc has a universal cursor plane, call that plane's update
3051 * handler rather than using legacy cursor handlers.
3053 drm_modeset_lock_crtc(crtc, crtc->cursor);
3054 if (crtc->cursor) {
3055 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3056 goto out;
3059 if (req->flags & DRM_MODE_CURSOR_BO) {
3060 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3061 ret = -ENXIO;
3062 goto out;
3064 /* Turns off the cursor if handle is 0 */
3065 if (crtc->funcs->cursor_set2)
3066 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3067 req->width, req->height, req->hot_x, req->hot_y);
3068 else
3069 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3070 req->width, req->height);
3073 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3074 if (crtc->funcs->cursor_move) {
3075 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3076 } else {
3077 ret = -EFAULT;
3078 goto out;
3081 out:
3082 drm_modeset_unlock_crtc(crtc);
3084 return ret;
3090 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3091 * @dev: drm device for the ioctl
3092 * @data: data pointer for the ioctl
3093 * @file_priv: drm file for the ioctl call
3095 * Set the cursor configuration based on user request.
3097 * Called by the user via ioctl.
3099 * Returns:
3100 * Zero on success, negative errno on failure.
3102 int drm_mode_cursor_ioctl(struct drm_device *dev,
3103 void *data, struct drm_file *file_priv)
3105 struct drm_mode_cursor *req = data;
3106 struct drm_mode_cursor2 new_req;
3108 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3109 new_req.hot_x = new_req.hot_y = 0;
3111 return drm_mode_cursor_common(dev, &new_req, file_priv);
3115 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3116 * @dev: drm device for the ioctl
3117 * @data: data pointer for the ioctl
3118 * @file_priv: drm file for the ioctl call
3120 * Set the cursor configuration based on user request. This implements the 2nd
3121 * version of the cursor ioctl, which allows userspace to additionally specify
3122 * the hotspot of the pointer.
3124 * Called by the user via ioctl.
3126 * Returns:
3127 * Zero on success, negative errno on failure.
3129 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3130 void *data, struct drm_file *file_priv)
3132 struct drm_mode_cursor2 *req = data;
3134 return drm_mode_cursor_common(dev, req, file_priv);
3138 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3139 * @bpp: bits per pixels
3140 * @depth: bit depth per pixel
3142 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3143 * Useful in fbdev emulation code, since that deals in those values.
3145 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3147 uint32_t fmt;
3149 switch (bpp) {
3150 case 8:
3151 fmt = DRM_FORMAT_C8;
3152 break;
3153 case 16:
3154 if (depth == 15)
3155 fmt = DRM_FORMAT_XRGB1555;
3156 else
3157 fmt = DRM_FORMAT_RGB565;
3158 break;
3159 case 24:
3160 fmt = DRM_FORMAT_RGB888;
3161 break;
3162 case 32:
3163 if (depth == 24)
3164 fmt = DRM_FORMAT_XRGB8888;
3165 else if (depth == 30)
3166 fmt = DRM_FORMAT_XRGB2101010;
3167 else
3168 fmt = DRM_FORMAT_ARGB8888;
3169 break;
3170 default:
3171 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3172 fmt = DRM_FORMAT_XRGB8888;
3173 break;
3176 return fmt;
3178 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3181 * drm_mode_addfb - add an FB to the graphics configuration
3182 * @dev: drm device for the ioctl
3183 * @data: data pointer for the ioctl
3184 * @file_priv: drm file for the ioctl call
3186 * Add a new FB to the specified CRTC, given a user request. This is the
3187 * original addfb ioctl which only supported RGB formats.
3189 * Called by the user via ioctl.
3191 * Returns:
3192 * Zero on success, negative errno on failure.
3194 int drm_mode_addfb(struct drm_device *dev,
3195 void *data, struct drm_file *file_priv)
3197 struct drm_mode_fb_cmd *or = data;
3198 struct drm_mode_fb_cmd2 r = {};
3199 int ret;
3201 /* convert to new format and call new ioctl */
3202 r.fb_id = or->fb_id;
3203 r.width = or->width;
3204 r.height = or->height;
3205 r.pitches[0] = or->pitch;
3206 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3207 r.handles[0] = or->handle;
3209 ret = drm_mode_addfb2(dev, &r, file_priv);
3210 if (ret)
3211 return ret;
3213 or->fb_id = r.fb_id;
3215 return 0;
3218 static int format_check(const struct drm_mode_fb_cmd2 *r)
3220 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3222 switch (format) {
3223 case DRM_FORMAT_C8:
3224 case DRM_FORMAT_RGB332:
3225 case DRM_FORMAT_BGR233:
3226 case DRM_FORMAT_XRGB4444:
3227 case DRM_FORMAT_XBGR4444:
3228 case DRM_FORMAT_RGBX4444:
3229 case DRM_FORMAT_BGRX4444:
3230 case DRM_FORMAT_ARGB4444:
3231 case DRM_FORMAT_ABGR4444:
3232 case DRM_FORMAT_RGBA4444:
3233 case DRM_FORMAT_BGRA4444:
3234 case DRM_FORMAT_XRGB1555:
3235 case DRM_FORMAT_XBGR1555:
3236 case DRM_FORMAT_RGBX5551:
3237 case DRM_FORMAT_BGRX5551:
3238 case DRM_FORMAT_ARGB1555:
3239 case DRM_FORMAT_ABGR1555:
3240 case DRM_FORMAT_RGBA5551:
3241 case DRM_FORMAT_BGRA5551:
3242 case DRM_FORMAT_RGB565:
3243 case DRM_FORMAT_BGR565:
3244 case DRM_FORMAT_RGB888:
3245 case DRM_FORMAT_BGR888:
3246 case DRM_FORMAT_XRGB8888:
3247 case DRM_FORMAT_XBGR8888:
3248 case DRM_FORMAT_RGBX8888:
3249 case DRM_FORMAT_BGRX8888:
3250 case DRM_FORMAT_ARGB8888:
3251 case DRM_FORMAT_ABGR8888:
3252 case DRM_FORMAT_RGBA8888:
3253 case DRM_FORMAT_BGRA8888:
3254 case DRM_FORMAT_XRGB2101010:
3255 case DRM_FORMAT_XBGR2101010:
3256 case DRM_FORMAT_RGBX1010102:
3257 case DRM_FORMAT_BGRX1010102:
3258 case DRM_FORMAT_ARGB2101010:
3259 case DRM_FORMAT_ABGR2101010:
3260 case DRM_FORMAT_RGBA1010102:
3261 case DRM_FORMAT_BGRA1010102:
3262 case DRM_FORMAT_YUYV:
3263 case DRM_FORMAT_YVYU:
3264 case DRM_FORMAT_UYVY:
3265 case DRM_FORMAT_VYUY:
3266 case DRM_FORMAT_AYUV:
3267 case DRM_FORMAT_NV12:
3268 case DRM_FORMAT_NV21:
3269 case DRM_FORMAT_NV16:
3270 case DRM_FORMAT_NV61:
3271 case DRM_FORMAT_NV24:
3272 case DRM_FORMAT_NV42:
3273 case DRM_FORMAT_YUV410:
3274 case DRM_FORMAT_YVU410:
3275 case DRM_FORMAT_YUV411:
3276 case DRM_FORMAT_YVU411:
3277 case DRM_FORMAT_YUV420:
3278 case DRM_FORMAT_YVU420:
3279 case DRM_FORMAT_YUV422:
3280 case DRM_FORMAT_YVU422:
3281 case DRM_FORMAT_YUV444:
3282 case DRM_FORMAT_YVU444:
3283 return 0;
3284 default:
3285 DRM_DEBUG_KMS("invalid pixel format %s\n",
3286 drm_get_format_name(r->pixel_format));
3287 return -EINVAL;
3291 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3293 int ret, hsub, vsub, num_planes, i;
3295 ret = format_check(r);
3296 if (ret) {
3297 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3298 drm_get_format_name(r->pixel_format));
3299 return ret;
3302 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3303 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3304 num_planes = drm_format_num_planes(r->pixel_format);
3306 if (r->width == 0 || r->width % hsub) {
3307 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3308 return -EINVAL;
3311 if (r->height == 0 || r->height % vsub) {
3312 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3313 return -EINVAL;
3316 for (i = 0; i < num_planes; i++) {
3317 unsigned int width = r->width / (i != 0 ? hsub : 1);
3318 unsigned int height = r->height / (i != 0 ? vsub : 1);
3319 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3321 if (!r->handles[i]) {
3322 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3323 return -EINVAL;
3326 if ((uint64_t) width * cpp > UINT_MAX)
3327 return -ERANGE;
3329 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3330 return -ERANGE;
3332 if (r->pitches[i] < width * cpp) {
3333 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3334 return -EINVAL;
3337 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3338 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3339 r->modifier[i], i);
3340 return -EINVAL;
3343 /* modifier specific checks: */
3344 switch (r->modifier[i]) {
3345 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3346 /* NOTE: the pitch restriction may be lifted later if it turns
3347 * out that no hw has this restriction:
3349 if (r->pixel_format != DRM_FORMAT_NV12 ||
3350 width % 128 || height % 32 ||
3351 r->pitches[i] % 128) {
3352 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3353 return -EINVAL;
3355 break;
3357 default:
3358 break;
3362 for (i = num_planes; i < 4; i++) {
3363 if (r->modifier[i]) {
3364 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3365 return -EINVAL;
3368 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3369 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3370 continue;
3372 if (r->handles[i]) {
3373 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3374 return -EINVAL;
3377 if (r->pitches[i]) {
3378 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3379 return -EINVAL;
3382 if (r->offsets[i]) {
3383 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3384 return -EINVAL;
3388 return 0;
3391 static struct drm_framebuffer *
3392 internal_framebuffer_create(struct drm_device *dev,
3393 const struct drm_mode_fb_cmd2 *r,
3394 struct drm_file *file_priv)
3396 struct drm_mode_config *config = &dev->mode_config;
3397 struct drm_framebuffer *fb;
3398 int ret;
3400 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3401 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3402 return ERR_PTR(-EINVAL);
3405 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3406 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3407 r->width, config->min_width, config->max_width);
3408 return ERR_PTR(-EINVAL);
3410 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3411 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3412 r->height, config->min_height, config->max_height);
3413 return ERR_PTR(-EINVAL);
3416 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3417 !dev->mode_config.allow_fb_modifiers) {
3418 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3419 return ERR_PTR(-EINVAL);
3422 ret = framebuffer_check(r);
3423 if (ret)
3424 return ERR_PTR(ret);
3426 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3427 if (IS_ERR(fb)) {
3428 DRM_DEBUG_KMS("could not create framebuffer\n");
3429 return fb;
3432 return fb;
3436 * drm_mode_addfb2 - add an FB to the graphics configuration
3437 * @dev: drm device for the ioctl
3438 * @data: data pointer for the ioctl
3439 * @file_priv: drm file for the ioctl call
3441 * Add a new FB to the specified CRTC, given a user request with format. This is
3442 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3443 * and uses fourcc codes as pixel format specifiers.
3445 * Called by the user via ioctl.
3447 * Returns:
3448 * Zero on success, negative errno on failure.
3450 int drm_mode_addfb2(struct drm_device *dev,
3451 void *data, struct drm_file *file_priv)
3453 struct drm_mode_fb_cmd2 *r = data;
3454 struct drm_framebuffer *fb;
3456 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3457 return -EINVAL;
3459 fb = internal_framebuffer_create(dev, r, file_priv);
3460 if (IS_ERR(fb))
3461 return PTR_ERR(fb);
3463 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3464 r->fb_id = fb->base.id;
3466 /* Transfer ownership to the filp for reaping on close */
3467 mutex_lock(&file_priv->fbs_lock);
3468 list_add(&fb->filp_head, &file_priv->fbs);
3469 mutex_unlock(&file_priv->fbs_lock);
3471 return 0;
3474 struct drm_mode_rmfb_work {
3475 struct work_struct work;
3476 struct list_head fbs;
3479 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3481 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3483 while (!list_empty(&arg->fbs)) {
3484 struct drm_framebuffer *fb =
3485 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3487 list_del_init(&fb->filp_head);
3488 drm_framebuffer_remove(fb);
3493 * drm_mode_rmfb - remove an FB from the configuration
3494 * @dev: drm device for the ioctl
3495 * @data: data pointer for the ioctl
3496 * @file_priv: drm file for the ioctl call
3498 * Remove the FB specified by the user.
3500 * Called by the user via ioctl.
3502 * Returns:
3503 * Zero on success, negative errno on failure.
3505 int drm_mode_rmfb(struct drm_device *dev,
3506 void *data, struct drm_file *file_priv)
3508 struct drm_framebuffer *fb = NULL;
3509 struct drm_framebuffer *fbl = NULL;
3510 uint32_t *id = data;
3511 int found = 0;
3513 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3514 return -EINVAL;
3516 fb = drm_framebuffer_lookup(dev, *id);
3517 if (!fb)
3518 return -ENOENT;
3520 mutex_lock(&file_priv->fbs_lock);
3521 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3522 if (fb == fbl)
3523 found = 1;
3524 if (!found) {
3525 mutex_unlock(&file_priv->fbs_lock);
3526 goto fail_unref;
3529 list_del_init(&fb->filp_head);
3530 mutex_unlock(&file_priv->fbs_lock);
3532 /* drop the reference we picked up in framebuffer lookup */
3533 drm_framebuffer_unreference(fb);
3536 * we now own the reference that was stored in the fbs list
3538 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3539 * so run this in a separate stack as there's no way to correctly
3540 * handle this after the fb is already removed from the lookup table.
3542 if (drm_framebuffer_read_refcount(fb) > 1) {
3543 struct drm_mode_rmfb_work arg;
3545 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3546 INIT_LIST_HEAD(&arg.fbs);
3547 list_add_tail(&fb->filp_head, &arg.fbs);
3549 schedule_work(&arg.work);
3550 flush_work(&arg.work);
3551 destroy_work_on_stack(&arg.work);
3552 } else
3553 drm_framebuffer_unreference(fb);
3555 return 0;
3557 fail_unref:
3558 drm_framebuffer_unreference(fb);
3559 return -ENOENT;
3563 * drm_mode_getfb - get FB info
3564 * @dev: drm device for the ioctl
3565 * @data: data pointer for the ioctl
3566 * @file_priv: drm file for the ioctl call
3568 * Lookup the FB given its ID and return info about it.
3570 * Called by the user via ioctl.
3572 * Returns:
3573 * Zero on success, negative errno on failure.
3575 int drm_mode_getfb(struct drm_device *dev,
3576 void *data, struct drm_file *file_priv)
3578 struct drm_mode_fb_cmd *r = data;
3579 struct drm_framebuffer *fb;
3580 int ret;
3582 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3583 return -EINVAL;
3585 fb = drm_framebuffer_lookup(dev, r->fb_id);
3586 if (!fb)
3587 return -ENOENT;
3589 r->height = fb->height;
3590 r->width = fb->width;
3591 r->depth = fb->depth;
3592 r->bpp = fb->bits_per_pixel;
3593 r->pitch = fb->pitches[0];
3594 if (fb->funcs->create_handle) {
3595 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
3596 } else {
3597 ret = -ENODEV;
3600 drm_framebuffer_unreference(fb);
3602 return ret;
3606 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3607 * @dev: drm device for the ioctl
3608 * @data: data pointer for the ioctl
3609 * @file_priv: drm file for the ioctl call
3611 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3612 * rectangle list. Generic userspace which does frontbuffer rendering must call
3613 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3614 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3616 * Modesetting drivers which always update the frontbuffer do not need to
3617 * implement the corresponding ->dirty framebuffer callback.
3619 * Called by the user via ioctl.
3621 * Returns:
3622 * Zero on success, negative errno on failure.
3624 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3625 void *data, struct drm_file *file_priv)
3627 struct drm_clip_rect __user *clips_ptr;
3628 struct drm_clip_rect *clips = NULL;
3629 struct drm_mode_fb_dirty_cmd *r = data;
3630 struct drm_framebuffer *fb;
3631 unsigned flags;
3632 int num_clips;
3633 int ret;
3635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3636 return -EINVAL;
3638 fb = drm_framebuffer_lookup(dev, r->fb_id);
3639 if (!fb)
3640 return -ENOENT;
3642 num_clips = r->num_clips;
3643 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3645 if (!num_clips != !clips_ptr) {
3646 ret = -EINVAL;
3647 goto out_err1;
3650 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3652 /* If userspace annotates copy, clips must come in pairs */
3653 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3654 ret = -EINVAL;
3655 goto out_err1;
3658 if (num_clips && clips_ptr) {
3659 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3660 ret = -EINVAL;
3661 goto out_err1;
3663 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3664 if (!clips) {
3665 ret = -ENOMEM;
3666 goto out_err1;
3669 ret = copy_from_user(clips, clips_ptr,
3670 num_clips * sizeof(*clips));
3671 if (ret) {
3672 ret = -EFAULT;
3673 goto out_err2;
3677 if (fb->funcs->dirty) {
3678 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3679 clips, num_clips);
3680 } else {
3681 ret = -ENOSYS;
3684 out_err2:
3685 kfree(clips);
3686 out_err1:
3687 drm_framebuffer_unreference(fb);
3689 return ret;
3693 * drm_fb_release - remove and free the FBs on this file
3694 * @priv: drm file for the ioctl
3696 * Destroy all the FBs associated with @filp.
3698 * Called by the user via ioctl.
3700 * Returns:
3701 * Zero on success, negative errno on failure.
3703 void drm_fb_release(struct drm_file *priv)
3705 struct drm_framebuffer *fb, *tfb;
3706 struct drm_mode_rmfb_work arg;
3708 INIT_LIST_HEAD(&arg.fbs);
3711 * When the file gets released that means no one else can access the fb
3712 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3713 * avoid upsetting lockdep since the universal cursor code adds a
3714 * framebuffer while holding mutex locks.
3716 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3717 * locks is impossible here since no one else but this function can get
3718 * at it any more.
3720 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3721 if (drm_framebuffer_read_refcount(fb) > 1) {
3722 list_move_tail(&fb->filp_head, &arg.fbs);
3723 } else {
3724 list_del_init(&fb->filp_head);
3726 /* This drops the fpriv->fbs reference. */
3727 drm_framebuffer_unreference(fb);
3731 if (!list_empty(&arg.fbs)) {
3732 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3734 schedule_work(&arg.work);
3735 flush_work(&arg.work);
3736 destroy_work_on_stack(&arg.work);
3741 * drm_property_create - create a new property type
3742 * @dev: drm device
3743 * @flags: flags specifying the property type
3744 * @name: name of the property
3745 * @num_values: number of pre-defined values
3747 * This creates a new generic drm property which can then be attached to a drm
3748 * object with drm_object_attach_property. The returned property object must be
3749 * freed with drm_property_destroy.
3751 * Note that the DRM core keeps a per-device list of properties and that, if
3752 * drm_mode_config_cleanup() is called, it will destroy all properties created
3753 * by the driver.
3755 * Returns:
3756 * A pointer to the newly created property on success, NULL on failure.
3758 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3759 const char *name, int num_values)
3761 struct drm_property *property = NULL;
3762 int ret;
3764 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3765 if (!property)
3766 return NULL;
3768 property->dev = dev;
3770 if (num_values) {
3771 property->values = kcalloc(num_values, sizeof(uint64_t),
3772 GFP_KERNEL);
3773 if (!property->values)
3774 goto fail;
3777 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3778 if (ret)
3779 goto fail;
3781 property->flags = flags;
3782 property->num_values = num_values;
3783 INIT_LIST_HEAD(&property->enum_list);
3785 if (name) {
3786 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3787 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3790 list_add_tail(&property->head, &dev->mode_config.property_list);
3792 WARN_ON(!drm_property_type_valid(property));
3794 return property;
3795 fail:
3796 kfree(property->values);
3797 kfree(property);
3798 return NULL;
3800 EXPORT_SYMBOL(drm_property_create);
3803 * drm_property_create_enum - create a new enumeration property type
3804 * @dev: drm device
3805 * @flags: flags specifying the property type
3806 * @name: name of the property
3807 * @props: enumeration lists with property values
3808 * @num_values: number of pre-defined values
3810 * This creates a new generic drm property which can then be attached to a drm
3811 * object with drm_object_attach_property. The returned property object must be
3812 * freed with drm_property_destroy.
3814 * Userspace is only allowed to set one of the predefined values for enumeration
3815 * properties.
3817 * Returns:
3818 * A pointer to the newly created property on success, NULL on failure.
3820 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3821 const char *name,
3822 const struct drm_prop_enum_list *props,
3823 int num_values)
3825 struct drm_property *property;
3826 int i, ret;
3828 flags |= DRM_MODE_PROP_ENUM;
3830 property = drm_property_create(dev, flags, name, num_values);
3831 if (!property)
3832 return NULL;
3834 for (i = 0; i < num_values; i++) {
3835 ret = drm_property_add_enum(property, i,
3836 props[i].type,
3837 props[i].name);
3838 if (ret) {
3839 drm_property_destroy(dev, property);
3840 return NULL;
3844 return property;
3846 EXPORT_SYMBOL(drm_property_create_enum);
3849 * drm_property_create_bitmask - create a new bitmask property type
3850 * @dev: drm device
3851 * @flags: flags specifying the property type
3852 * @name: name of the property
3853 * @props: enumeration lists with property bitflags
3854 * @num_props: size of the @props array
3855 * @supported_bits: bitmask of all supported enumeration values
3857 * This creates a new bitmask drm property which can then be attached to a drm
3858 * object with drm_object_attach_property. The returned property object must be
3859 * freed with drm_property_destroy.
3861 * Compared to plain enumeration properties userspace is allowed to set any
3862 * or'ed together combination of the predefined property bitflag values
3864 * Returns:
3865 * A pointer to the newly created property on success, NULL on failure.
3867 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3868 int flags, const char *name,
3869 const struct drm_prop_enum_list *props,
3870 int num_props,
3871 uint64_t supported_bits)
3873 struct drm_property *property;
3874 int i, ret, index = 0;
3875 int num_values = hweight64(supported_bits);
3877 flags |= DRM_MODE_PROP_BITMASK;
3879 property = drm_property_create(dev, flags, name, num_values);
3880 if (!property)
3881 return NULL;
3882 for (i = 0; i < num_props; i++) {
3883 if (!(supported_bits & (1ULL << props[i].type)))
3884 continue;
3886 if (WARN_ON(index >= num_values)) {
3887 drm_property_destroy(dev, property);
3888 return NULL;
3891 ret = drm_property_add_enum(property, index++,
3892 props[i].type,
3893 props[i].name);
3894 if (ret) {
3895 drm_property_destroy(dev, property);
3896 return NULL;
3900 return property;
3902 EXPORT_SYMBOL(drm_property_create_bitmask);
3904 static struct drm_property *property_create_range(struct drm_device *dev,
3905 int flags, const char *name,
3906 uint64_t min, uint64_t max)
3908 struct drm_property *property;
3910 property = drm_property_create(dev, flags, name, 2);
3911 if (!property)
3912 return NULL;
3914 property->values[0] = min;
3915 property->values[1] = max;
3917 return property;
3921 * drm_property_create_range - create a new unsigned ranged property type
3922 * @dev: drm device
3923 * @flags: flags specifying the property type
3924 * @name: name of the property
3925 * @min: minimum value of the property
3926 * @max: maximum value of the property
3928 * This creates a new generic drm property which can then be attached to a drm
3929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3932 * Userspace is allowed to set any unsigned integer value in the (min, max)
3933 * range inclusive.
3935 * Returns:
3936 * A pointer to the newly created property on success, NULL on failure.
3938 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3939 const char *name,
3940 uint64_t min, uint64_t max)
3942 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3943 name, min, max);
3945 EXPORT_SYMBOL(drm_property_create_range);
3948 * drm_property_create_signed_range - create a new signed ranged property type
3949 * @dev: drm device
3950 * @flags: flags specifying the property type
3951 * @name: name of the property
3952 * @min: minimum value of the property
3953 * @max: maximum value of the property
3955 * This creates a new generic drm property which can then be attached to a drm
3956 * object with drm_object_attach_property. The returned property object must be
3957 * freed with drm_property_destroy.
3959 * Userspace is allowed to set any signed integer value in the (min, max)
3960 * range inclusive.
3962 * Returns:
3963 * A pointer to the newly created property on success, NULL on failure.
3965 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3966 int flags, const char *name,
3967 int64_t min, int64_t max)
3969 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3970 name, I642U64(min), I642U64(max));
3972 EXPORT_SYMBOL(drm_property_create_signed_range);
3975 * drm_property_create_object - create a new object property type
3976 * @dev: drm device
3977 * @flags: flags specifying the property type
3978 * @name: name of the property
3979 * @type: object type from DRM_MODE_OBJECT_* defines
3981 * This creates a new generic drm property which can then be attached to a drm
3982 * object with drm_object_attach_property. The returned property object must be
3983 * freed with drm_property_destroy.
3985 * Userspace is only allowed to set this to any property value of the given
3986 * @type. Only useful for atomic properties, which is enforced.
3988 * Returns:
3989 * A pointer to the newly created property on success, NULL on failure.
3991 struct drm_property *drm_property_create_object(struct drm_device *dev,
3992 int flags, const char *name, uint32_t type)
3994 struct drm_property *property;
3996 flags |= DRM_MODE_PROP_OBJECT;
3998 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3999 return NULL;
4001 property = drm_property_create(dev, flags, name, 1);
4002 if (!property)
4003 return NULL;
4005 property->values[0] = type;
4007 return property;
4009 EXPORT_SYMBOL(drm_property_create_object);
4012 * drm_property_create_bool - create a new boolean property type
4013 * @dev: drm device
4014 * @flags: flags specifying the property type
4015 * @name: name of the property
4017 * This creates a new generic drm property which can then be attached to a drm
4018 * object with drm_object_attach_property. The returned property object must be
4019 * freed with drm_property_destroy.
4021 * This is implemented as a ranged property with only {0, 1} as valid values.
4023 * Returns:
4024 * A pointer to the newly created property on success, NULL on failure.
4026 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
4027 const char *name)
4029 return drm_property_create_range(dev, flags, name, 0, 1);
4031 EXPORT_SYMBOL(drm_property_create_bool);
4034 * drm_property_add_enum - add a possible value to an enumeration property
4035 * @property: enumeration property to change
4036 * @index: index of the new enumeration
4037 * @value: value of the new enumeration
4038 * @name: symbolic name of the new enumeration
4040 * This functions adds enumerations to a property.
4042 * It's use is deprecated, drivers should use one of the more specific helpers
4043 * to directly create the property with all enumerations already attached.
4045 * Returns:
4046 * Zero on success, error code on failure.
4048 int drm_property_add_enum(struct drm_property *property, int index,
4049 uint64_t value, const char *name)
4051 struct drm_property_enum *prop_enum;
4053 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4054 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
4055 return -EINVAL;
4058 * Bitmask enum properties have the additional constraint of values
4059 * from 0 to 63
4061 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4062 (value > 63))
4063 return -EINVAL;
4065 if (!list_empty(&property->enum_list)) {
4066 list_for_each_entry(prop_enum, &property->enum_list, head) {
4067 if (prop_enum->value == value) {
4068 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4069 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4070 return 0;
4075 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4076 if (!prop_enum)
4077 return -ENOMEM;
4079 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4080 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4081 prop_enum->value = value;
4083 property->values[index] = value;
4084 list_add_tail(&prop_enum->head, &property->enum_list);
4085 return 0;
4087 EXPORT_SYMBOL(drm_property_add_enum);
4090 * drm_property_destroy - destroy a drm property
4091 * @dev: drm device
4092 * @property: property to destry
4094 * This function frees a property including any attached resources like
4095 * enumeration values.
4097 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4099 struct drm_property_enum *prop_enum, *pt;
4101 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4102 list_del(&prop_enum->head);
4103 kfree(prop_enum);
4106 if (property->num_values)
4107 kfree(property->values);
4108 drm_mode_object_unregister(dev, &property->base);
4109 list_del(&property->head);
4110 kfree(property);
4112 EXPORT_SYMBOL(drm_property_destroy);
4115 * drm_object_attach_property - attach a property to a modeset object
4116 * @obj: drm modeset object
4117 * @property: property to attach
4118 * @init_val: initial value of the property
4120 * This attaches the given property to the modeset object with the given initial
4121 * value. Currently this function cannot fail since the properties are stored in
4122 * a statically sized array.
4124 void drm_object_attach_property(struct drm_mode_object *obj,
4125 struct drm_property *property,
4126 uint64_t init_val)
4128 int count = obj->properties->count;
4130 if (count == DRM_OBJECT_MAX_PROPERTY) {
4131 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4132 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4133 "you see this message on the same object type.\n",
4134 obj->type);
4135 return;
4138 obj->properties->properties[count] = property;
4139 obj->properties->values[count] = init_val;
4140 obj->properties->count++;
4141 if (property->flags & DRM_MODE_PROP_ATOMIC)
4142 obj->properties->atomic_count++;
4144 EXPORT_SYMBOL(drm_object_attach_property);
4147 * drm_object_property_set_value - set the value of a property
4148 * @obj: drm mode object to set property value for
4149 * @property: property to set
4150 * @val: value the property should be set to
4152 * This functions sets a given property on a given object. This function only
4153 * changes the software state of the property, it does not call into the
4154 * driver's ->set_property callback.
4156 * Returns:
4157 * Zero on success, error code on failure.
4159 int drm_object_property_set_value(struct drm_mode_object *obj,
4160 struct drm_property *property, uint64_t val)
4162 int i;
4164 for (i = 0; i < obj->properties->count; i++) {
4165 if (obj->properties->properties[i] == property) {
4166 obj->properties->values[i] = val;
4167 return 0;
4171 return -EINVAL;
4173 EXPORT_SYMBOL(drm_object_property_set_value);
4176 * drm_object_property_get_value - retrieve the value of a property
4177 * @obj: drm mode object to get property value from
4178 * @property: property to retrieve
4179 * @val: storage for the property value
4181 * This function retrieves the softare state of the given property for the given
4182 * property. Since there is no driver callback to retrieve the current property
4183 * value this might be out of sync with the hardware, depending upon the driver
4184 * and property.
4186 * Returns:
4187 * Zero on success, error code on failure.
4189 int drm_object_property_get_value(struct drm_mode_object *obj,
4190 struct drm_property *property, uint64_t *val)
4192 int i;
4194 /* read-only properties bypass atomic mechanism and still store
4195 * their value in obj->properties->values[].. mostly to avoid
4196 * having to deal w/ EDID and similar props in atomic paths:
4198 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4199 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4200 return drm_atomic_get_property(obj, property, val);
4202 for (i = 0; i < obj->properties->count; i++) {
4203 if (obj->properties->properties[i] == property) {
4204 *val = obj->properties->values[i];
4205 return 0;
4209 return -EINVAL;
4211 EXPORT_SYMBOL(drm_object_property_get_value);
4214 * drm_mode_getproperty_ioctl - get the property metadata
4215 * @dev: DRM device
4216 * @data: ioctl data
4217 * @file_priv: DRM file info
4219 * This function retrieves the metadata for a given property, like the different
4220 * possible values for an enum property or the limits for a range property.
4222 * Blob properties are special
4224 * Called by the user via ioctl.
4226 * Returns:
4227 * Zero on success, negative errno on failure.
4229 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4230 void *data, struct drm_file *file_priv)
4232 struct drm_mode_get_property *out_resp = data;
4233 struct drm_property *property;
4234 int enum_count = 0;
4235 int value_count = 0;
4236 int ret = 0, i;
4237 int copied;
4238 struct drm_property_enum *prop_enum;
4239 struct drm_mode_property_enum __user *enum_ptr;
4240 uint64_t __user *values_ptr;
4242 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4243 return -EINVAL;
4245 drm_modeset_lock_all(dev);
4246 property = drm_property_find(dev, out_resp->prop_id);
4247 if (!property) {
4248 ret = -ENOENT;
4249 goto done;
4252 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4253 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4254 list_for_each_entry(prop_enum, &property->enum_list, head)
4255 enum_count++;
4258 value_count = property->num_values;
4260 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4261 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4262 out_resp->flags = property->flags;
4264 if ((out_resp->count_values >= value_count) && value_count) {
4265 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4266 for (i = 0; i < value_count; i++) {
4267 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4268 ret = -EFAULT;
4269 goto done;
4273 out_resp->count_values = value_count;
4275 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4276 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4277 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4278 copied = 0;
4279 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4280 list_for_each_entry(prop_enum, &property->enum_list, head) {
4282 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4283 ret = -EFAULT;
4284 goto done;
4287 if (copy_to_user(&enum_ptr[copied].name,
4288 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4289 ret = -EFAULT;
4290 goto done;
4292 copied++;
4295 out_resp->count_enum_blobs = enum_count;
4299 * NOTE: The idea seems to have been to use this to read all the blob
4300 * property values. But nothing ever added them to the corresponding
4301 * list, userspace always used the special-purpose get_blob ioctl to
4302 * read the value for a blob property. It also doesn't make a lot of
4303 * sense to return values here when everything else is just metadata for
4304 * the property itself.
4306 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4307 out_resp->count_enum_blobs = 0;
4308 done:
4309 drm_modeset_unlock_all(dev);
4310 return ret;
4313 static void drm_property_free_blob(struct kref *kref)
4315 struct drm_property_blob *blob =
4316 container_of(kref, struct drm_property_blob, base.refcount);
4318 mutex_lock(&blob->dev->mode_config.blob_lock);
4319 list_del(&blob->head_global);
4320 mutex_unlock(&blob->dev->mode_config.blob_lock);
4322 drm_mode_object_unregister(blob->dev, &blob->base);
4324 kfree(blob);
4328 * drm_property_create_blob - Create new blob property
4330 * Creates a new blob property for a specified DRM device, optionally
4331 * copying data.
4333 * @dev: DRM device to create property for
4334 * @length: Length to allocate for blob data
4335 * @data: If specified, copies data into blob
4337 * Returns:
4338 * New blob property with a single reference on success, or an ERR_PTR
4339 * value on failure.
4341 struct drm_property_blob *
4342 drm_property_create_blob(struct drm_device *dev, size_t length,
4343 const void *data)
4345 struct drm_property_blob *blob;
4346 int ret;
4348 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4349 return ERR_PTR(-EINVAL);
4351 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4352 if (!blob)
4353 return ERR_PTR(-ENOMEM);
4355 /* This must be explicitly initialised, so we can safely call list_del
4356 * on it in the removal handler, even if it isn't in a file list. */
4357 INIT_LIST_HEAD(&blob->head_file);
4358 blob->length = length;
4359 blob->dev = dev;
4361 if (data)
4362 memcpy(blob->data, data, length);
4364 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4365 true, drm_property_free_blob);
4366 if (ret) {
4367 kfree(blob);
4368 return ERR_PTR(-EINVAL);
4371 mutex_lock(&dev->mode_config.blob_lock);
4372 list_add_tail(&blob->head_global,
4373 &dev->mode_config.property_blob_list);
4374 mutex_unlock(&dev->mode_config.blob_lock);
4376 return blob;
4378 EXPORT_SYMBOL(drm_property_create_blob);
4381 * drm_property_unreference_blob - Unreference a blob property
4383 * Drop a reference on a blob property. May free the object.
4385 * @blob: Pointer to blob property
4387 void drm_property_unreference_blob(struct drm_property_blob *blob)
4389 if (!blob)
4390 return;
4392 drm_mode_object_unreference(&blob->base);
4394 EXPORT_SYMBOL(drm_property_unreference_blob);
4397 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4398 * @dev: DRM device
4399 * @file_priv: destroy all blobs owned by this file handle
4401 void drm_property_destroy_user_blobs(struct drm_device *dev,
4402 struct drm_file *file_priv)
4404 struct drm_property_blob *blob, *bt;
4407 * When the file gets released that means no one else can access the
4408 * blob list any more, so no need to grab dev->blob_lock.
4410 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4411 list_del_init(&blob->head_file);
4412 drm_property_unreference_blob(blob);
4417 * drm_property_reference_blob - Take a reference on an existing property
4419 * Take a new reference on an existing blob property.
4421 * @blob: Pointer to blob property
4423 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4425 drm_mode_object_reference(&blob->base);
4426 return blob;
4428 EXPORT_SYMBOL(drm_property_reference_blob);
4431 * drm_property_lookup_blob - look up a blob property and take a reference
4432 * @dev: drm device
4433 * @id: id of the blob property
4435 * If successful, this takes an additional reference to the blob property.
4436 * callers need to make sure to eventually unreference the returned property
4437 * again, using @drm_property_unreference_blob.
4439 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4440 uint32_t id)
4442 struct drm_mode_object *obj;
4443 struct drm_property_blob *blob = NULL;
4445 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4446 if (obj)
4447 blob = obj_to_blob(obj);
4448 return blob;
4450 EXPORT_SYMBOL(drm_property_lookup_blob);
4453 * drm_property_replace_global_blob - atomically replace existing blob property
4454 * @dev: drm device
4455 * @replace: location of blob property pointer to be replaced
4456 * @length: length of data for new blob, or 0 for no data
4457 * @data: content for new blob, or NULL for no data
4458 * @obj_holds_id: optional object for property holding blob ID
4459 * @prop_holds_id: optional property holding blob ID
4460 * @return 0 on success or error on failure
4462 * This function will atomically replace a global property in the blob list,
4463 * optionally updating a property which holds the ID of that property. It is
4464 * guaranteed to be atomic: no caller will be allowed to see intermediate
4465 * results, and either the entire operation will succeed and clean up the
4466 * previous property, or it will fail and the state will be unchanged.
4468 * If length is 0 or data is NULL, no new blob will be created, and the holding
4469 * property, if specified, will be set to 0.
4471 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4472 * by holding the relevant modesetting object lock for its parent.
4474 * For example, a drm_connector has a 'PATH' property, which contains the ID
4475 * of a blob property with the value of the MST path information. Calling this
4476 * function with replace pointing to the connector's path_blob_ptr, length and
4477 * data set for the new path information, obj_holds_id set to the connector's
4478 * base object, and prop_holds_id set to the path property name, will perform
4479 * a completely atomic update. The access to path_blob_ptr is protected by the
4480 * caller holding a lock on the connector.
4482 static int drm_property_replace_global_blob(struct drm_device *dev,
4483 struct drm_property_blob **replace,
4484 size_t length,
4485 const void *data,
4486 struct drm_mode_object *obj_holds_id,
4487 struct drm_property *prop_holds_id)
4489 struct drm_property_blob *new_blob = NULL;
4490 struct drm_property_blob *old_blob = NULL;
4491 int ret;
4493 WARN_ON(replace == NULL);
4495 old_blob = *replace;
4497 if (length && data) {
4498 new_blob = drm_property_create_blob(dev, length, data);
4499 if (IS_ERR(new_blob))
4500 return PTR_ERR(new_blob);
4503 /* This does not need to be synchronised with blob_lock, as the
4504 * get_properties ioctl locks all modesetting objects, and
4505 * obj_holds_id must be locked before calling here, so we cannot
4506 * have its value out of sync with the list membership modified
4507 * below under blob_lock. */
4508 if (obj_holds_id) {
4509 ret = drm_object_property_set_value(obj_holds_id,
4510 prop_holds_id,
4511 new_blob ?
4512 new_blob->base.id : 0);
4513 if (ret != 0)
4514 goto err_created;
4517 drm_property_unreference_blob(old_blob);
4518 *replace = new_blob;
4520 return 0;
4522 err_created:
4523 drm_property_unreference_blob(new_blob);
4524 return ret;
4528 * drm_mode_getblob_ioctl - get the contents of a blob property value
4529 * @dev: DRM device
4530 * @data: ioctl data
4531 * @file_priv: DRM file info
4533 * This function retrieves the contents of a blob property. The value stored in
4534 * an object's blob property is just a normal modeset object id.
4536 * Called by the user via ioctl.
4538 * Returns:
4539 * Zero on success, negative errno on failure.
4541 int drm_mode_getblob_ioctl(struct drm_device *dev,
4542 void *data, struct drm_file *file_priv)
4544 struct drm_mode_get_blob *out_resp = data;
4545 struct drm_property_blob *blob;
4546 int ret = 0;
4547 void __user *blob_ptr;
4549 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4550 return -EINVAL;
4552 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4553 if (!blob)
4554 return -ENOENT;
4556 if (out_resp->length == blob->length) {
4557 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4558 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4559 ret = -EFAULT;
4560 goto unref;
4563 out_resp->length = blob->length;
4564 unref:
4565 drm_property_unreference_blob(blob);
4567 return ret;
4571 * drm_mode_createblob_ioctl - create a new blob property
4572 * @dev: DRM device
4573 * @data: ioctl data
4574 * @file_priv: DRM file info
4576 * This function creates a new blob property with user-defined values. In order
4577 * to give us sensible validation and checking when creating, rather than at
4578 * every potential use, we also require a type to be provided upfront.
4580 * Called by the user via ioctl.
4582 * Returns:
4583 * Zero on success, negative errno on failure.
4585 int drm_mode_createblob_ioctl(struct drm_device *dev,
4586 void *data, struct drm_file *file_priv)
4588 struct drm_mode_create_blob *out_resp = data;
4589 struct drm_property_blob *blob;
4590 void __user *blob_ptr;
4591 int ret = 0;
4593 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4594 return -EINVAL;
4596 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4597 if (IS_ERR(blob))
4598 return PTR_ERR(blob);
4600 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4601 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4602 ret = -EFAULT;
4603 goto out_blob;
4606 /* Dropping the lock between create_blob and our access here is safe
4607 * as only the same file_priv can remove the blob; at this point, it is
4608 * not associated with any file_priv. */
4609 mutex_lock(&dev->mode_config.blob_lock);
4610 out_resp->blob_id = blob->base.id;
4611 list_add_tail(&blob->head_file, &file_priv->blobs);
4612 mutex_unlock(&dev->mode_config.blob_lock);
4614 return 0;
4616 out_blob:
4617 drm_property_unreference_blob(blob);
4618 return ret;
4622 * drm_mode_destroyblob_ioctl - destroy a user blob property
4623 * @dev: DRM device
4624 * @data: ioctl data
4625 * @file_priv: DRM file info
4627 * Destroy an existing user-defined blob property.
4629 * Called by the user via ioctl.
4631 * Returns:
4632 * Zero on success, negative errno on failure.
4634 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4635 void *data, struct drm_file *file_priv)
4637 struct drm_mode_destroy_blob *out_resp = data;
4638 struct drm_property_blob *blob = NULL, *bt;
4639 bool found = false;
4640 int ret = 0;
4642 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4643 return -EINVAL;
4645 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4646 if (!blob)
4647 return -ENOENT;
4649 mutex_lock(&dev->mode_config.blob_lock);
4650 /* Ensure the property was actually created by this user. */
4651 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4652 if (bt == blob) {
4653 found = true;
4654 break;
4658 if (!found) {
4659 ret = -EPERM;
4660 goto err;
4663 /* We must drop head_file here, because we may not be the last
4664 * reference on the blob. */
4665 list_del_init(&blob->head_file);
4666 mutex_unlock(&dev->mode_config.blob_lock);
4668 /* One reference from lookup, and one from the filp. */
4669 drm_property_unreference_blob(blob);
4670 drm_property_unreference_blob(blob);
4672 return 0;
4674 err:
4675 mutex_unlock(&dev->mode_config.blob_lock);
4676 drm_property_unreference_blob(blob);
4678 return ret;
4682 * drm_mode_connector_set_path_property - set tile property on connector
4683 * @connector: connector to set property on.
4684 * @path: path to use for property; must not be NULL.
4686 * This creates a property to expose to userspace to specify a
4687 * connector path. This is mainly used for DisplayPort MST where
4688 * connectors have a topology and we want to allow userspace to give
4689 * them more meaningful names.
4691 * Returns:
4692 * Zero on success, negative errno on failure.
4694 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4695 const char *path)
4697 struct drm_device *dev = connector->dev;
4698 int ret;
4700 ret = drm_property_replace_global_blob(dev,
4701 &connector->path_blob_ptr,
4702 strlen(path) + 1,
4703 path,
4704 &connector->base,
4705 dev->mode_config.path_property);
4706 return ret;
4708 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4711 * drm_mode_connector_set_tile_property - set tile property on connector
4712 * @connector: connector to set property on.
4714 * This looks up the tile information for a connector, and creates a
4715 * property for userspace to parse if it exists. The property is of
4716 * the form of 8 integers using ':' as a separator.
4718 * Returns:
4719 * Zero on success, errno on failure.
4721 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4723 struct drm_device *dev = connector->dev;
4724 char tile[256];
4725 int ret;
4727 if (!connector->has_tile) {
4728 ret = drm_property_replace_global_blob(dev,
4729 &connector->tile_blob_ptr,
4731 NULL,
4732 &connector->base,
4733 dev->mode_config.tile_property);
4734 return ret;
4737 ksnprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4738 connector->tile_group->id, connector->tile_is_single_monitor,
4739 connector->num_h_tile, connector->num_v_tile,
4740 connector->tile_h_loc, connector->tile_v_loc,
4741 connector->tile_h_size, connector->tile_v_size);
4743 ret = drm_property_replace_global_blob(dev,
4744 &connector->tile_blob_ptr,
4745 strlen(tile) + 1,
4746 tile,
4747 &connector->base,
4748 dev->mode_config.tile_property);
4749 return ret;
4751 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4754 * drm_mode_connector_update_edid_property - update the edid property of a connector
4755 * @connector: drm connector
4756 * @edid: new value of the edid property
4758 * This function creates a new blob modeset object and assigns its id to the
4759 * connector's edid property.
4761 * Returns:
4762 * Zero on success, negative errno on failure.
4764 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4765 const struct edid *edid)
4767 struct drm_device *dev = connector->dev;
4768 size_t size = 0;
4769 int ret;
4771 /* ignore requests to set edid when overridden */
4772 if (connector->override_edid)
4773 return 0;
4775 if (edid)
4776 size = EDID_LENGTH * (1 + edid->extensions);
4778 ret = drm_property_replace_global_blob(dev,
4779 &connector->edid_blob_ptr,
4780 size,
4781 edid,
4782 &connector->base,
4783 dev->mode_config.edid_property);
4784 return ret;
4786 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4788 /* Some properties could refer to dynamic refcnt'd objects, or things that
4789 * need special locking to handle lifetime issues (ie. to ensure the prop
4790 * value doesn't become invalid part way through the property update due to
4791 * race). The value returned by reference via 'obj' should be passed back
4792 * to drm_property_change_valid_put() after the property is set (and the
4793 * object to which the property is attached has a chance to take it's own
4794 * reference).
4796 bool drm_property_change_valid_get(struct drm_property *property,
4797 uint64_t value, struct drm_mode_object **ref)
4799 int i;
4801 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4802 return false;
4804 *ref = NULL;
4806 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4807 if (value < property->values[0] || value > property->values[1])
4808 return false;
4809 return true;
4810 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4811 int64_t svalue = U642I64(value);
4813 if (svalue < U642I64(property->values[0]) ||
4814 svalue > U642I64(property->values[1]))
4815 return false;
4816 return true;
4817 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4818 uint64_t valid_mask = 0;
4820 for (i = 0; i < property->num_values; i++)
4821 valid_mask |= (1ULL << property->values[i]);
4822 return !(value & ~valid_mask);
4823 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4824 struct drm_property_blob *blob;
4826 if (value == 0)
4827 return true;
4829 blob = drm_property_lookup_blob(property->dev, value);
4830 if (blob) {
4831 *ref = &blob->base;
4832 return true;
4833 } else {
4834 return false;
4836 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4837 /* a zero value for an object property translates to null: */
4838 if (value == 0)
4839 return true;
4841 *ref = _object_find(property->dev, value, property->values[0]);
4842 return *ref != NULL;
4845 for (i = 0; i < property->num_values; i++)
4846 if (property->values[i] == value)
4847 return true;
4848 return false;
4851 void drm_property_change_valid_put(struct drm_property *property,
4852 struct drm_mode_object *ref)
4854 if (!ref)
4855 return;
4857 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4858 drm_mode_object_unreference(ref);
4859 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4860 drm_property_unreference_blob(obj_to_blob(ref));
4864 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4865 * @dev: DRM device
4866 * @data: ioctl data
4867 * @file_priv: DRM file info
4869 * This function sets the current value for a connectors's property. It also
4870 * calls into a driver's ->set_property callback to update the hardware state
4872 * Called by the user via ioctl.
4874 * Returns:
4875 * Zero on success, negative errno on failure.
4877 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4878 void *data, struct drm_file *file_priv)
4880 struct drm_mode_connector_set_property *conn_set_prop = data;
4881 struct drm_mode_obj_set_property obj_set_prop = {
4882 .value = conn_set_prop->value,
4883 .prop_id = conn_set_prop->prop_id,
4884 .obj_id = conn_set_prop->connector_id,
4885 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4888 /* It does all the locking and checking we need */
4889 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4892 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4893 struct drm_property *property,
4894 uint64_t value)
4896 int ret = -EINVAL;
4897 struct drm_connector *connector = obj_to_connector(obj);
4899 /* Do DPMS ourselves */
4900 if (property == connector->dev->mode_config.dpms_property) {
4901 ret = (*connector->funcs->dpms)(connector, (int)value);
4902 } else if (connector->funcs->set_property)
4903 ret = connector->funcs->set_property(connector, property, value);
4905 /* store the property value if successful */
4906 if (!ret)
4907 drm_object_property_set_value(&connector->base, property, value);
4908 return ret;
4911 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4912 struct drm_property *property,
4913 uint64_t value)
4915 int ret = -EINVAL;
4916 struct drm_crtc *crtc = obj_to_crtc(obj);
4918 if (crtc->funcs->set_property)
4919 ret = crtc->funcs->set_property(crtc, property, value);
4920 if (!ret)
4921 drm_object_property_set_value(obj, property, value);
4923 return ret;
4927 * drm_mode_plane_set_obj_prop - set the value of a property
4928 * @plane: drm plane object to set property value for
4929 * @property: property to set
4930 * @value: value the property should be set to
4932 * This functions sets a given property on a given plane object. This function
4933 * calls the driver's ->set_property callback and changes the software state of
4934 * the property if the callback succeeds.
4936 * Returns:
4937 * Zero on success, error code on failure.
4939 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4940 struct drm_property *property,
4941 uint64_t value)
4943 int ret = -EINVAL;
4944 struct drm_mode_object *obj = &plane->base;
4946 if (plane->funcs->set_property)
4947 ret = plane->funcs->set_property(plane, property, value);
4948 if (!ret)
4949 drm_object_property_set_value(obj, property, value);
4951 return ret;
4953 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4956 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4957 * @dev: DRM device
4958 * @data: ioctl data
4959 * @file_priv: DRM file info
4961 * This function retrieves the current value for an object's property. Compared
4962 * to the connector specific ioctl this one is extended to also work on crtc and
4963 * plane objects.
4965 * Called by the user via ioctl.
4967 * Returns:
4968 * Zero on success, negative errno on failure.
4970 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4971 struct drm_file *file_priv)
4973 struct drm_mode_obj_get_properties *arg = data;
4974 struct drm_mode_object *obj;
4975 int ret = 0;
4977 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4978 return -EINVAL;
4980 drm_modeset_lock_all(dev);
4982 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4983 if (!obj) {
4984 ret = -ENOENT;
4985 goto out;
4987 if (!obj->properties) {
4988 ret = -EINVAL;
4989 goto out_unref;
4992 ret = get_properties(obj, file_priv->atomic,
4993 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4994 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4995 &arg->count_props);
4997 out_unref:
4998 drm_mode_object_unreference(obj);
4999 out:
5000 drm_modeset_unlock_all(dev);
5001 return ret;
5005 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5006 * @dev: DRM device
5007 * @data: ioctl data
5008 * @file_priv: DRM file info
5010 * This function sets the current value for an object's property. It also calls
5011 * into a driver's ->set_property callback to update the hardware state.
5012 * Compared to the connector specific ioctl this one is extended to also work on
5013 * crtc and plane objects.
5015 * Called by the user via ioctl.
5017 * Returns:
5018 * Zero on success, negative errno on failure.
5020 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5021 struct drm_file *file_priv)
5023 struct drm_mode_obj_set_property *arg = data;
5024 struct drm_mode_object *arg_obj;
5025 struct drm_mode_object *prop_obj;
5026 struct drm_property *property;
5027 int i, ret = -EINVAL;
5028 struct drm_mode_object *ref;
5030 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5031 return -EINVAL;
5033 drm_modeset_lock_all(dev);
5035 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5036 if (!arg_obj) {
5037 ret = -ENOENT;
5038 goto out;
5040 if (!arg_obj->properties)
5041 goto out_unref;
5043 for (i = 0; i < arg_obj->properties->count; i++)
5044 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5045 break;
5047 if (i == arg_obj->properties->count)
5048 goto out_unref;
5050 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5051 DRM_MODE_OBJECT_PROPERTY);
5052 if (!prop_obj) {
5053 ret = -ENOENT;
5054 goto out_unref;
5056 property = obj_to_property(prop_obj);
5058 if (!drm_property_change_valid_get(property, arg->value, &ref))
5059 goto out_unref;
5061 switch (arg_obj->type) {
5062 case DRM_MODE_OBJECT_CONNECTOR:
5063 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5064 arg->value);
5065 break;
5066 case DRM_MODE_OBJECT_CRTC:
5067 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5068 break;
5069 case DRM_MODE_OBJECT_PLANE:
5070 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5071 property, arg->value);
5072 break;
5075 drm_property_change_valid_put(property, ref);
5077 out_unref:
5078 drm_mode_object_unreference(arg_obj);
5079 out:
5080 drm_modeset_unlock_all(dev);
5081 return ret;
5085 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5086 * @connector: connector to attach
5087 * @encoder: encoder to attach @connector to
5089 * This function links up a connector to an encoder. Note that the routing
5090 * restrictions between encoders and crtcs are exposed to userspace through the
5091 * possible_clones and possible_crtcs bitmasks.
5093 * Returns:
5094 * Zero on success, negative errno on failure.
5096 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5097 struct drm_encoder *encoder)
5099 int i;
5102 * In the past, drivers have attempted to model the static association
5103 * of connector to encoder in simple connector/encoder devices using a
5104 * direct assignment of connector->encoder = encoder. This connection
5105 * is a logical one and the responsibility of the core, so drivers are
5106 * expected not to mess with this.
5108 * Note that the error return should've been enough here, but a large
5109 * majority of drivers ignores the return value, so add in a big WARN
5110 * to get people's attention.
5112 if (WARN_ON(connector->encoder))
5113 return -EINVAL;
5115 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5116 if (connector->encoder_ids[i] == 0) {
5117 connector->encoder_ids[i] = encoder->base.id;
5118 return 0;
5121 return -ENOMEM;
5123 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5126 * drm_mode_crtc_set_gamma_size - set the gamma table size
5127 * @crtc: CRTC to set the gamma table size for
5128 * @gamma_size: size of the gamma table
5130 * Drivers which support gamma tables should set this to the supported gamma
5131 * table size when initializing the CRTC. Currently the drm core only supports a
5132 * fixed gamma table size.
5134 * Returns:
5135 * Zero on success, negative errno on failure.
5137 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5138 int gamma_size)
5140 crtc->gamma_size = gamma_size;
5142 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5143 GFP_KERNEL);
5144 if (!crtc->gamma_store) {
5145 crtc->gamma_size = 0;
5146 return -ENOMEM;
5149 return 0;
5151 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5154 * drm_mode_gamma_set_ioctl - set the gamma table
5155 * @dev: DRM device
5156 * @data: ioctl data
5157 * @file_priv: DRM file info
5159 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5160 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5162 * Called by the user via ioctl.
5164 * Returns:
5165 * Zero on success, negative errno on failure.
5167 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5168 void *data, struct drm_file *file_priv)
5170 struct drm_mode_crtc_lut *crtc_lut = data;
5171 struct drm_crtc *crtc;
5172 void *r_base, *g_base, *b_base;
5173 int size;
5174 int ret = 0;
5176 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5177 return -EINVAL;
5179 drm_modeset_lock_all(dev);
5180 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5181 if (!crtc) {
5182 ret = -ENOENT;
5183 goto out;
5186 if (crtc->funcs->gamma_set == NULL) {
5187 ret = -ENOSYS;
5188 goto out;
5191 /* memcpy into gamma store */
5192 if (crtc_lut->gamma_size != crtc->gamma_size) {
5193 ret = -EINVAL;
5194 goto out;
5197 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5198 r_base = crtc->gamma_store;
5199 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5200 ret = -EFAULT;
5201 goto out;
5204 g_base = (char *)r_base + size;
5205 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5206 ret = -EFAULT;
5207 goto out;
5210 b_base = (char *)g_base + size;
5211 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5212 ret = -EFAULT;
5213 goto out;
5216 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5218 out:
5219 drm_modeset_unlock_all(dev);
5220 return ret;
5225 * drm_mode_gamma_get_ioctl - get the gamma table
5226 * @dev: DRM device
5227 * @data: ioctl data
5228 * @file_priv: DRM file info
5230 * Copy the current gamma table into the storage provided. This also provides
5231 * the gamma table size the driver expects, which can be used to size the
5232 * allocated storage.
5234 * Called by the user via ioctl.
5236 * Returns:
5237 * Zero on success, negative errno on failure.
5239 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5240 void *data, struct drm_file *file_priv)
5242 struct drm_mode_crtc_lut *crtc_lut = data;
5243 struct drm_crtc *crtc;
5244 void *r_base, *g_base, *b_base;
5245 int size;
5246 int ret = 0;
5248 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5249 return -EINVAL;
5251 drm_modeset_lock_all(dev);
5252 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5253 if (!crtc) {
5254 ret = -ENOENT;
5255 goto out;
5258 /* memcpy into gamma store */
5259 if (crtc_lut->gamma_size != crtc->gamma_size) {
5260 ret = -EINVAL;
5261 goto out;
5264 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5265 r_base = crtc->gamma_store;
5266 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5267 ret = -EFAULT;
5268 goto out;
5271 g_base = (char *)r_base + size;
5272 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5273 ret = -EFAULT;
5274 goto out;
5277 b_base = (char *)g_base + size;
5278 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5279 ret = -EFAULT;
5280 goto out;
5282 out:
5283 drm_modeset_unlock_all(dev);
5284 return ret;
5288 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5289 * @dev: DRM device
5290 * @data: ioctl data
5291 * @file_priv: DRM file info
5293 * This schedules an asynchronous update on a given CRTC, called page flip.
5294 * Optionally a drm event is generated to signal the completion of the event.
5295 * Generic drivers cannot assume that a pageflip with changed framebuffer
5296 * properties (including driver specific metadata like tiling layout) will work,
5297 * but some drivers support e.g. pixel format changes through the pageflip
5298 * ioctl.
5300 * Called by the user via ioctl.
5302 * Returns:
5303 * Zero on success, negative errno on failure.
5305 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5306 void *data, struct drm_file *file_priv)
5308 struct drm_mode_crtc_page_flip *page_flip = data;
5309 struct drm_crtc *crtc;
5310 struct drm_framebuffer *fb = NULL;
5311 struct drm_pending_vblank_event *e = NULL;
5312 int ret = -EINVAL;
5314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5315 return -EINVAL;
5317 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5318 page_flip->reserved != 0)
5319 return -EINVAL;
5321 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5322 return -EINVAL;
5324 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5325 if (!crtc)
5326 return -ENOENT;
5328 drm_modeset_lock_crtc(crtc, crtc->primary);
5329 if (crtc->primary->fb == NULL) {
5330 /* The framebuffer is currently unbound, presumably
5331 * due to a hotplug event, that userspace has not
5332 * yet discovered.
5334 ret = -EBUSY;
5335 goto out;
5338 if (crtc->funcs->page_flip == NULL)
5339 goto out;
5341 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5342 if (!fb) {
5343 ret = -ENOENT;
5344 goto out;
5347 if (crtc->state) {
5348 const struct drm_plane_state *state = crtc->primary->state;
5350 ret = check_src_coords(state->src_x, state->src_y,
5351 state->src_w, state->src_h, fb);
5352 } else {
5353 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5355 if (ret)
5356 goto out;
5358 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5359 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5360 ret = -EINVAL;
5361 goto out;
5364 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5365 e = kzalloc(sizeof *e, GFP_KERNEL);
5366 if (!e) {
5367 ret = -ENOMEM;
5368 goto out;
5370 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5371 e->event.base.length = sizeof(e->event);
5372 e->event.user_data = page_flip->user_data;
5373 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5374 if (ret) {
5375 kfree(e);
5376 goto out;
5380 crtc->primary->old_fb = crtc->primary->fb;
5381 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5382 if (ret) {
5383 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5384 drm_event_cancel_free(dev, &e->base);
5385 /* Keep the old fb, don't unref it. */
5386 crtc->primary->old_fb = NULL;
5387 } else {
5388 crtc->primary->fb = fb;
5389 /* Unref only the old framebuffer. */
5390 fb = NULL;
5393 out:
5394 if (fb)
5395 drm_framebuffer_unreference(fb);
5396 if (crtc->primary->old_fb)
5397 drm_framebuffer_unreference(crtc->primary->old_fb);
5398 crtc->primary->old_fb = NULL;
5399 drm_modeset_unlock_crtc(crtc);
5401 return ret;
5405 * drm_mode_config_reset - call ->reset callbacks
5406 * @dev: drm device
5408 * This functions calls all the crtc's, encoder's and connector's ->reset
5409 * callback. Drivers can use this in e.g. their driver load or resume code to
5410 * reset hardware and software state.
5412 void drm_mode_config_reset(struct drm_device *dev)
5414 struct drm_crtc *crtc;
5415 struct drm_plane *plane;
5416 struct drm_encoder *encoder;
5417 struct drm_connector *connector;
5419 drm_for_each_plane(plane, dev)
5420 if (plane->funcs->reset)
5421 plane->funcs->reset(plane);
5423 drm_for_each_crtc(crtc, dev)
5424 if (crtc->funcs->reset)
5425 crtc->funcs->reset(crtc);
5427 drm_for_each_encoder(encoder, dev)
5428 if (encoder->funcs->reset)
5429 encoder->funcs->reset(encoder);
5431 mutex_lock(&dev->mode_config.mutex);
5432 drm_for_each_connector(connector, dev)
5433 if (connector->funcs->reset)
5434 connector->funcs->reset(connector);
5435 mutex_unlock(&dev->mode_config.mutex);
5437 EXPORT_SYMBOL(drm_mode_config_reset);
5440 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5441 * @dev: DRM device
5442 * @data: ioctl data
5443 * @file_priv: DRM file info
5445 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5446 * TTM or something else entirely) and returns the resulting buffer handle. This
5447 * handle can then be wrapped up into a framebuffer modeset object.
5449 * Note that userspace is not allowed to use such objects for render
5450 * acceleration - drivers must create their own private ioctls for such a use
5451 * case.
5453 * Called by the user via ioctl.
5455 * Returns:
5456 * Zero on success, negative errno on failure.
5458 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5459 void *data, struct drm_file *file_priv)
5461 struct drm_mode_create_dumb *args = data;
5462 u32 cpp, stride, size;
5464 if (!dev->driver->dumb_create)
5465 return -ENOSYS;
5466 if (!args->width || !args->height || !args->bpp)
5467 return -EINVAL;
5469 /* overflow checks for 32bit size calculations */
5470 /* NOTE: DIV_ROUND_UP() can overflow */
5471 cpp = DIV_ROUND_UP(args->bpp, 8);
5472 if (!cpp || cpp > 0xffffffffU / args->width)
5473 return -EINVAL;
5474 stride = cpp * args->width;
5475 if (args->height > 0xffffffffU / stride)
5476 return -EINVAL;
5478 /* test for wrap-around */
5479 size = args->height * stride;
5480 if (PAGE_ALIGN(size) == 0)
5481 return -EINVAL;
5484 * handle, pitch and size are output parameters. Zero them out to
5485 * prevent drivers from accidentally using uninitialized data. Since
5486 * not all existing userspace is clearing these fields properly we
5487 * cannot reject IOCTL with garbage in them.
5489 args->handle = 0;
5490 args->pitch = 0;
5491 args->size = 0;
5493 return dev->driver->dumb_create(file_priv, dev, args);
5497 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5498 * @dev: DRM device
5499 * @data: ioctl data
5500 * @file_priv: DRM file info
5502 * Allocate an offset in the drm device node's address space to be able to
5503 * memory map a dumb buffer.
5505 * Called by the user via ioctl.
5507 * Returns:
5508 * Zero on success, negative errno on failure.
5510 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5511 void *data, struct drm_file *file_priv)
5513 struct drm_mode_map_dumb *args = data;
5515 /* call driver ioctl to get mmap offset */
5516 if (!dev->driver->dumb_map_offset)
5517 return -ENOSYS;
5519 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, (uint64_t *)&args->offset);
5523 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5524 * @dev: DRM device
5525 * @data: ioctl data
5526 * @file_priv: DRM file info
5528 * This destroys the userspace handle for the given dumb backing storage buffer.
5529 * Since buffer objects must be reference counted in the kernel a buffer object
5530 * won't be immediately freed if a framebuffer modeset object still uses it.
5532 * Called by the user via ioctl.
5534 * Returns:
5535 * Zero on success, negative errno on failure.
5537 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5538 void *data, struct drm_file *file_priv)
5540 struct drm_mode_destroy_dumb *args = data;
5542 if (!dev->driver->dumb_destroy)
5543 return -ENOSYS;
5545 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5549 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5550 * @format: pixel format (DRM_FORMAT_*)
5551 * @depth: storage for the depth value
5552 * @bpp: storage for the bpp value
5554 * This only supports RGB formats here for compat with code that doesn't use
5555 * pixel formats directly yet.
5557 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5558 int *bpp)
5560 switch (format) {
5561 case DRM_FORMAT_C8:
5562 case DRM_FORMAT_RGB332:
5563 case DRM_FORMAT_BGR233:
5564 *depth = 8;
5565 *bpp = 8;
5566 break;
5567 case DRM_FORMAT_XRGB1555:
5568 case DRM_FORMAT_XBGR1555:
5569 case DRM_FORMAT_RGBX5551:
5570 case DRM_FORMAT_BGRX5551:
5571 case DRM_FORMAT_ARGB1555:
5572 case DRM_FORMAT_ABGR1555:
5573 case DRM_FORMAT_RGBA5551:
5574 case DRM_FORMAT_BGRA5551:
5575 *depth = 15;
5576 *bpp = 16;
5577 break;
5578 case DRM_FORMAT_RGB565:
5579 case DRM_FORMAT_BGR565:
5580 *depth = 16;
5581 *bpp = 16;
5582 break;
5583 case DRM_FORMAT_RGB888:
5584 case DRM_FORMAT_BGR888:
5585 *depth = 24;
5586 *bpp = 24;
5587 break;
5588 case DRM_FORMAT_XRGB8888:
5589 case DRM_FORMAT_XBGR8888:
5590 case DRM_FORMAT_RGBX8888:
5591 case DRM_FORMAT_BGRX8888:
5592 *depth = 24;
5593 *bpp = 32;
5594 break;
5595 case DRM_FORMAT_XRGB2101010:
5596 case DRM_FORMAT_XBGR2101010:
5597 case DRM_FORMAT_RGBX1010102:
5598 case DRM_FORMAT_BGRX1010102:
5599 case DRM_FORMAT_ARGB2101010:
5600 case DRM_FORMAT_ABGR2101010:
5601 case DRM_FORMAT_RGBA1010102:
5602 case DRM_FORMAT_BGRA1010102:
5603 *depth = 30;
5604 *bpp = 32;
5605 break;
5606 case DRM_FORMAT_ARGB8888:
5607 case DRM_FORMAT_ABGR8888:
5608 case DRM_FORMAT_RGBA8888:
5609 case DRM_FORMAT_BGRA8888:
5610 *depth = 32;
5611 *bpp = 32;
5612 break;
5613 default:
5614 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5615 drm_get_format_name(format));
5616 *depth = 0;
5617 *bpp = 0;
5618 break;
5621 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5624 * drm_format_num_planes - get the number of planes for format
5625 * @format: pixel format (DRM_FORMAT_*)
5627 * Returns:
5628 * The number of planes used by the specified pixel format.
5630 int drm_format_num_planes(uint32_t format)
5632 switch (format) {
5633 case DRM_FORMAT_YUV410:
5634 case DRM_FORMAT_YVU410:
5635 case DRM_FORMAT_YUV411:
5636 case DRM_FORMAT_YVU411:
5637 case DRM_FORMAT_YUV420:
5638 case DRM_FORMAT_YVU420:
5639 case DRM_FORMAT_YUV422:
5640 case DRM_FORMAT_YVU422:
5641 case DRM_FORMAT_YUV444:
5642 case DRM_FORMAT_YVU444:
5643 return 3;
5644 case DRM_FORMAT_NV12:
5645 case DRM_FORMAT_NV21:
5646 case DRM_FORMAT_NV16:
5647 case DRM_FORMAT_NV61:
5648 case DRM_FORMAT_NV24:
5649 case DRM_FORMAT_NV42:
5650 return 2;
5651 default:
5652 return 1;
5655 EXPORT_SYMBOL(drm_format_num_planes);
5658 * drm_format_plane_cpp - determine the bytes per pixel value
5659 * @format: pixel format (DRM_FORMAT_*)
5660 * @plane: plane index
5662 * Returns:
5663 * The bytes per pixel value for the specified plane.
5665 int drm_format_plane_cpp(uint32_t format, int plane)
5667 unsigned int depth;
5668 int bpp;
5670 if (plane >= drm_format_num_planes(format))
5671 return 0;
5673 switch (format) {
5674 case DRM_FORMAT_YUYV:
5675 case DRM_FORMAT_YVYU:
5676 case DRM_FORMAT_UYVY:
5677 case DRM_FORMAT_VYUY:
5678 return 2;
5679 case DRM_FORMAT_NV12:
5680 case DRM_FORMAT_NV21:
5681 case DRM_FORMAT_NV16:
5682 case DRM_FORMAT_NV61:
5683 case DRM_FORMAT_NV24:
5684 case DRM_FORMAT_NV42:
5685 return plane ? 2 : 1;
5686 case DRM_FORMAT_YUV410:
5687 case DRM_FORMAT_YVU410:
5688 case DRM_FORMAT_YUV411:
5689 case DRM_FORMAT_YVU411:
5690 case DRM_FORMAT_YUV420:
5691 case DRM_FORMAT_YVU420:
5692 case DRM_FORMAT_YUV422:
5693 case DRM_FORMAT_YVU422:
5694 case DRM_FORMAT_YUV444:
5695 case DRM_FORMAT_YVU444:
5696 return 1;
5697 default:
5698 drm_fb_get_bpp_depth(format, &depth, &bpp);
5699 return bpp >> 3;
5702 EXPORT_SYMBOL(drm_format_plane_cpp);
5705 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5706 * @format: pixel format (DRM_FORMAT_*)
5708 * Returns:
5709 * The horizontal chroma subsampling factor for the
5710 * specified pixel format.
5712 int drm_format_horz_chroma_subsampling(uint32_t format)
5714 switch (format) {
5715 case DRM_FORMAT_YUV411:
5716 case DRM_FORMAT_YVU411:
5717 case DRM_FORMAT_YUV410:
5718 case DRM_FORMAT_YVU410:
5719 return 4;
5720 case DRM_FORMAT_YUYV:
5721 case DRM_FORMAT_YVYU:
5722 case DRM_FORMAT_UYVY:
5723 case DRM_FORMAT_VYUY:
5724 case DRM_FORMAT_NV12:
5725 case DRM_FORMAT_NV21:
5726 case DRM_FORMAT_NV16:
5727 case DRM_FORMAT_NV61:
5728 case DRM_FORMAT_YUV422:
5729 case DRM_FORMAT_YVU422:
5730 case DRM_FORMAT_YUV420:
5731 case DRM_FORMAT_YVU420:
5732 return 2;
5733 default:
5734 return 1;
5737 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5740 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5741 * @format: pixel format (DRM_FORMAT_*)
5743 * Returns:
5744 * The vertical chroma subsampling factor for the
5745 * specified pixel format.
5747 int drm_format_vert_chroma_subsampling(uint32_t format)
5749 switch (format) {
5750 case DRM_FORMAT_YUV410:
5751 case DRM_FORMAT_YVU410:
5752 return 4;
5753 case DRM_FORMAT_YUV420:
5754 case DRM_FORMAT_YVU420:
5755 case DRM_FORMAT_NV12:
5756 case DRM_FORMAT_NV21:
5757 return 2;
5758 default:
5759 return 1;
5762 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5765 * drm_format_plane_width - width of the plane given the first plane
5766 * @width: width of the first plane
5767 * @format: pixel format
5768 * @plane: plane index
5770 * Returns:
5771 * The width of @plane, given that the width of the first plane is @width.
5773 int drm_format_plane_width(int width, uint32_t format, int plane)
5775 if (plane >= drm_format_num_planes(format))
5776 return 0;
5778 if (plane == 0)
5779 return width;
5781 return width / drm_format_horz_chroma_subsampling(format);
5783 EXPORT_SYMBOL(drm_format_plane_width);
5786 * drm_format_plane_height - height of the plane given the first plane
5787 * @height: height of the first plane
5788 * @format: pixel format
5789 * @plane: plane index
5791 * Returns:
5792 * The height of @plane, given that the height of the first plane is @height.
5794 int drm_format_plane_height(int height, uint32_t format, int plane)
5796 if (plane >= drm_format_num_planes(format))
5797 return 0;
5799 if (plane == 0)
5800 return height;
5802 return height / drm_format_vert_chroma_subsampling(format);
5804 EXPORT_SYMBOL(drm_format_plane_height);
5807 * drm_rotation_simplify() - Try to simplify the rotation
5808 * @rotation: Rotation to be simplified
5809 * @supported_rotations: Supported rotations
5811 * Attempt to simplify the rotation to a form that is supported.
5812 * Eg. if the hardware supports everything except DRM_REFLECT_X
5813 * one could call this function like this:
5815 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5816 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5817 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5819 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5820 * transforms the hardware supports, this function may not
5821 * be able to produce a supported transform, so the caller should
5822 * check the result afterwards.
5824 unsigned int drm_rotation_simplify(unsigned int rotation,
5825 unsigned int supported_rotations)
5827 if (rotation & ~supported_rotations) {
5828 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5829 rotation = (rotation & DRM_REFLECT_MASK) |
5830 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5833 return rotation;
5835 EXPORT_SYMBOL(drm_rotation_simplify);
5838 * drm_mode_config_init - initialize DRM mode_configuration structure
5839 * @dev: DRM device
5841 * Initialize @dev's mode_config structure, used for tracking the graphics
5842 * configuration of @dev.
5844 * Since this initializes the modeset locks, no locking is possible. Which is no
5845 * problem, since this should happen single threaded at init time. It is the
5846 * driver's problem to ensure this guarantee.
5849 void drm_mode_config_init(struct drm_device *dev)
5851 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
5852 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5853 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
5854 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
5855 lockinit(&dev->mode_config.blob_lock, "drmcbl", 0, LK_CANRECURSE);
5856 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5857 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5858 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5859 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5860 INIT_LIST_HEAD(&dev->mode_config.property_list);
5861 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5862 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5863 idr_init(&dev->mode_config.crtc_idr);
5864 idr_init(&dev->mode_config.tile_idr);
5865 ida_init(&dev->mode_config.connector_ida);
5867 drm_modeset_lock_all(dev);
5868 drm_mode_create_standard_properties(dev);
5869 drm_modeset_unlock_all(dev);
5871 /* Just to be sure */
5872 dev->mode_config.num_fb = 0;
5873 dev->mode_config.num_connector = 0;
5874 dev->mode_config.num_crtc = 0;
5875 dev->mode_config.num_encoder = 0;
5876 dev->mode_config.num_overlay_plane = 0;
5877 dev->mode_config.num_total_plane = 0;
5879 EXPORT_SYMBOL(drm_mode_config_init);
5882 * drm_mode_config_cleanup - free up DRM mode_config info
5883 * @dev: DRM device
5885 * Free up all the connectors and CRTCs associated with this DRM device, then
5886 * free up the framebuffers and associated buffer objects.
5888 * Note that since this /should/ happen single-threaded at driver/device
5889 * teardown time, no locking is required. It's the driver's job to ensure that
5890 * this guarantee actually holds true.
5892 * FIXME: cleanup any dangling user buffer objects too
5894 void drm_mode_config_cleanup(struct drm_device *dev)
5896 struct drm_connector *connector, *ot;
5897 struct drm_crtc *crtc, *ct;
5898 struct drm_encoder *encoder, *enct;
5899 struct drm_framebuffer *fb, *fbt;
5900 struct drm_property *property, *pt;
5901 struct drm_property_blob *blob, *bt;
5902 struct drm_plane *plane, *plt;
5904 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5905 head) {
5906 encoder->funcs->destroy(encoder);
5909 list_for_each_entry_safe(connector, ot,
5910 &dev->mode_config.connector_list, head) {
5911 connector->funcs->destroy(connector);
5914 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5915 head) {
5916 drm_property_destroy(dev, property);
5919 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5920 head) {
5921 plane->funcs->destroy(plane);
5924 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5925 crtc->funcs->destroy(crtc);
5928 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5929 head_global) {
5930 drm_property_unreference_blob(blob);
5934 * Single-threaded teardown context, so it's not required to grab the
5935 * fb_lock to protect against concurrent fb_list access. Contrary, it
5936 * would actually deadlock with the drm_framebuffer_cleanup function.
5938 * Also, if there are any framebuffers left, that's a driver leak now,
5939 * so politely WARN about this.
5941 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5942 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5943 drm_framebuffer_free(&fb->base.refcount);
5946 ida_destroy(&dev->mode_config.connector_ida);
5947 idr_destroy(&dev->mode_config.tile_idr);
5948 idr_destroy(&dev->mode_config.crtc_idr);
5949 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5951 EXPORT_SYMBOL(drm_mode_config_cleanup);
5953 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5954 unsigned int supported_rotations)
5956 static const struct drm_prop_enum_list props[] = {
5957 { DRM_ROTATE_0, "rotate-0" },
5958 { DRM_ROTATE_90, "rotate-90" },
5959 { DRM_ROTATE_180, "rotate-180" },
5960 { DRM_ROTATE_270, "rotate-270" },
5961 { DRM_REFLECT_X, "reflect-x" },
5962 { DRM_REFLECT_Y, "reflect-y" },
5965 return drm_property_create_bitmask(dev, 0, "rotation",
5966 props, ARRAY_SIZE(props),
5967 supported_rotations);
5969 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5972 * DOC: Tile group
5974 * Tile groups are used to represent tiled monitors with a unique
5975 * integer identifier. Tiled monitors using DisplayID v1.3 have
5976 * a unique 8-byte handle, we store this in a tile group, so we
5977 * have a common identifier for all tiles in a monitor group.
5979 static void drm_tile_group_free(struct kref *kref)
5981 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5982 struct drm_device *dev = tg->dev;
5983 mutex_lock(&dev->mode_config.idr_mutex);
5984 idr_remove(&dev->mode_config.tile_idr, tg->id);
5985 mutex_unlock(&dev->mode_config.idr_mutex);
5986 kfree(tg);
5990 * drm_mode_put_tile_group - drop a reference to a tile group.
5991 * @dev: DRM device
5992 * @tg: tile group to drop reference to.
5994 * drop reference to tile group and free if 0.
5996 void drm_mode_put_tile_group(struct drm_device *dev,
5997 struct drm_tile_group *tg)
5999 kref_put(&tg->refcount, drm_tile_group_free);
6003 * drm_mode_get_tile_group - get a reference to an existing tile group
6004 * @dev: DRM device
6005 * @topology: 8-bytes unique per monitor.
6007 * Use the unique bytes to get a reference to an existing tile group.
6009 * RETURNS:
6010 * tile group or NULL if not found.
6012 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6013 char topology[8])
6015 #if 0
6016 struct drm_tile_group *tg;
6017 int id;
6018 mutex_lock(&dev->mode_config.idr_mutex);
6019 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6020 if (!memcmp(tg->group_data, topology, 8)) {
6021 if (!kref_get_unless_zero(&tg->refcount))
6022 tg = NULL;
6023 mutex_unlock(&dev->mode_config.idr_mutex);
6024 return tg;
6027 mutex_unlock(&dev->mode_config.idr_mutex);
6028 #endif
6029 return NULL;
6031 EXPORT_SYMBOL(drm_mode_get_tile_group);
6034 * drm_mode_create_tile_group - create a tile group from a displayid description
6035 * @dev: DRM device
6036 * @topology: 8-bytes unique per monitor.
6038 * Create a tile group for the unique monitor, and get a unique
6039 * identifier for the tile group.
6041 * RETURNS:
6042 * new tile group or error.
6044 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6045 char topology[8])
6047 struct drm_tile_group *tg;
6048 int ret;
6050 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6051 if (!tg)
6052 return ERR_PTR(-ENOMEM);
6054 kref_init(&tg->refcount);
6055 memcpy(tg->group_data, topology, 8);
6056 tg->dev = dev;
6058 mutex_lock(&dev->mode_config.idr_mutex);
6059 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6060 if (ret >= 0) {
6061 tg->id = ret;
6062 } else {
6063 kfree(tg);
6064 tg = ERR_PTR(ret);
6067 mutex_unlock(&dev->mode_config.idr_mutex);
6068 return tg;
6070 EXPORT_SYMBOL(drm_mode_create_tile_group);