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
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
45 struct drm_mode_fb_cmd2
*r
,
46 struct drm_file
*file_priv
);
49 * drm_modeset_lock_all - take all modeset locks
52 * This function takes all modeset locks, suitable where a more fine-grained
53 * scheme isn't (yet) implemented. Locks must be dropped with
54 * drm_modeset_unlock_all.
56 void drm_modeset_lock_all(struct drm_device
*dev
)
58 struct drm_mode_config
*config
= &dev
->mode_config
;
59 struct drm_modeset_acquire_ctx
*ctx
;
62 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
66 mutex_lock(&config
->mutex
);
68 drm_modeset_acquire_init(ctx
, 0);
71 ret
= drm_modeset_lock(&config
->connection_mutex
, ctx
);
74 ret
= drm_modeset_lock_all_crtcs(dev
, ctx
);
78 WARN_ON(config
->acquire_ctx
);
80 /* now we hold the locks, so now that it is safe, stash the
81 * ctx for drm_modeset_unlock_all():
83 config
->acquire_ctx
= ctx
;
85 drm_warn_on_modeset_not_all_locked(dev
);
90 if (ret
== -EDEADLK
) {
91 drm_modeset_backoff(ctx
);
95 EXPORT_SYMBOL(drm_modeset_lock_all
);
98 * drm_modeset_unlock_all - drop all modeset locks
101 * This function drop all modeset locks taken by drm_modeset_lock_all.
103 void drm_modeset_unlock_all(struct drm_device
*dev
)
105 struct drm_mode_config
*config
= &dev
->mode_config
;
106 struct drm_modeset_acquire_ctx
*ctx
= config
->acquire_ctx
;
111 config
->acquire_ctx
= NULL
;
112 drm_modeset_drop_locks(ctx
);
113 drm_modeset_acquire_fini(ctx
);
117 mutex_unlock(&dev
->mode_config
.mutex
);
119 EXPORT_SYMBOL(drm_modeset_unlock_all
);
122 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
125 * Useful as a debug assert.
127 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
129 struct drm_crtc
*crtc
;
131 /* Locking is currently fubar in the panic handler. */
132 if (oops_in_progress
)
135 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
136 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
138 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
139 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
141 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
143 /* Avoid boilerplate. I'm tired of typing. */
144 #define DRM_ENUM_NAME_FN(fnname, list) \
145 const char *fnname(int val) \
148 for (i = 0; i < ARRAY_SIZE(list); i++) { \
149 if (list[i].type == val) \
150 return list[i].name; \
152 return "(unknown)"; \
158 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
159 { { DRM_MODE_DPMS_ON
, "On" },
160 { DRM_MODE_DPMS_STANDBY
, "Standby" },
161 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
162 { DRM_MODE_DPMS_OFF
, "Off" }
165 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
167 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] =
169 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
170 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
171 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
175 * Optional properties
177 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
179 { DRM_MODE_SCALE_NONE
, "None" },
180 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
181 { DRM_MODE_SCALE_CENTER
, "Center" },
182 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
185 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list
[] = {
186 { DRM_MODE_PICTURE_ASPECT_NONE
, "Automatic" },
187 { DRM_MODE_PICTURE_ASPECT_4_3
, "4:3" },
188 { DRM_MODE_PICTURE_ASPECT_16_9
, "16:9" },
192 * Non-global properties, but "required" for certain connectors.
194 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
196 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
197 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
198 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
201 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
203 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
205 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
207 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
210 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
211 drm_dvi_i_subconnector_enum_list
)
213 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
215 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
216 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
217 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
219 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
222 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
224 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
226 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
227 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
228 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
229 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
230 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
233 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
234 drm_tv_subconnector_enum_list
)
236 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
237 { DRM_MODE_DIRTY_OFF
, "Off" },
238 { DRM_MODE_DIRTY_ON
, "On" },
239 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
242 struct drm_conn_prop_enum_list
{
249 * Connector and encoder types.
251 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
252 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
253 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
254 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
255 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
256 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
257 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
258 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
259 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
260 { DRM_MODE_CONNECTOR_Component
, "Component" },
261 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
262 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
263 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
264 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
265 { DRM_MODE_CONNECTOR_TV
, "TV" },
266 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
267 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
268 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
271 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
272 { { DRM_MODE_ENCODER_NONE
, "None" },
273 { DRM_MODE_ENCODER_DAC
, "DAC" },
274 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
275 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
276 { DRM_MODE_ENCODER_TVDAC
, "TV" },
277 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
278 { DRM_MODE_ENCODER_DSI
, "DSI" },
279 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
282 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
284 { SubPixelUnknown
, "Unknown" },
285 { SubPixelHorizontalRGB
, "Horizontal RGB" },
286 { SubPixelHorizontalBGR
, "Horizontal BGR" },
287 { SubPixelVerticalRGB
, "Vertical RGB" },
288 { SubPixelVerticalBGR
, "Vertical BGR" },
289 { SubPixelNone
, "None" },
292 void drm_connector_ida_init(void)
296 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
297 ida_init(&drm_connector_enum_list
[i
].ida
);
300 void drm_connector_ida_destroy(void)
304 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
305 ida_destroy(&drm_connector_enum_list
[i
].ida
);
309 * drm_get_connector_status_name - return a string for connector status
310 * @status: connector status to compute name of
312 * In contrast to the other drm_get_*_name functions this one here returns a
313 * const pointer and hence is threadsafe.
315 const char *drm_get_connector_status_name(enum drm_connector_status status
)
317 if (status
== connector_status_connected
)
319 else if (status
== connector_status_disconnected
)
320 return "disconnected";
324 EXPORT_SYMBOL(drm_get_connector_status_name
);
327 * drm_get_subpixel_order_name - return a string for a given subpixel enum
328 * @order: enum of subpixel_order
330 * Note you could abuse this and return something out of bounds, but that
331 * would be a caller error. No unscrubbed user data should make it here.
333 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
335 return drm_subpixel_enum_list
[order
].name
;
337 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
339 static char printable_char(int c
)
341 return isascii(c
) && isprint(c
) ? c
: '?';
345 * drm_get_format_name - return a string for drm fourcc format
346 * @format: format to compute name of
348 * Note that the buffer used by this function is globally shared and owned by
349 * the function itself.
351 * FIXME: This isn't really multithreading safe.
353 const char *drm_get_format_name(uint32_t format
)
357 snprintf(buf
, sizeof(buf
),
358 "%c%c%c%c %s-endian (0x%08x)",
359 printable_char(format
& 0xff),
360 printable_char((format
>> 8) & 0xff),
361 printable_char((format
>> 16) & 0xff),
362 printable_char((format
>> 24) & 0x7f),
363 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
368 EXPORT_SYMBOL(drm_get_format_name
);
371 * Internal function to assign a slot in the object idr and optionally
372 * register the object into the idr.
374 static int drm_mode_object_get_reg(struct drm_device
*dev
,
375 struct drm_mode_object
*obj
,
381 mutex_lock(&dev
->mode_config
.idr_mutex
);
382 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
385 * Set up the object linking under the protection of the idr
386 * lock so that other users can't see inconsistent state.
389 obj
->type
= obj_type
;
391 mutex_unlock(&dev
->mode_config
.idr_mutex
);
393 return ret
< 0 ? ret
: 0;
397 * drm_mode_object_get - allocate a new modeset identifier
399 * @obj: object pointer, used to generate unique ID
400 * @obj_type: object type
402 * Create a unique identifier based on @ptr in @dev's identifier space. Used
403 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
404 * modeset identifiers are _not_ reference counted. Hence don't use this for
405 * reference counted modeset objects like framebuffers.
408 * New unique (relative to other objects in @dev) integer identifier for the
411 int drm_mode_object_get(struct drm_device
*dev
,
412 struct drm_mode_object
*obj
, uint32_t obj_type
)
414 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true);
417 static void drm_mode_object_register(struct drm_device
*dev
,
418 struct drm_mode_object
*obj
)
420 mutex_lock(&dev
->mode_config
.idr_mutex
);
421 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
422 mutex_unlock(&dev
->mode_config
.idr_mutex
);
426 * drm_mode_object_put - free a modeset identifer
428 * @object: object to free
430 * Free @id from @dev's unique identifier pool. Note that despite the _get
431 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
432 * for reference counted modeset objects like framebuffers.
434 void drm_mode_object_put(struct drm_device
*dev
,
435 struct drm_mode_object
*object
)
437 mutex_lock(&dev
->mode_config
.idr_mutex
);
438 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
439 mutex_unlock(&dev
->mode_config
.idr_mutex
);
442 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
443 uint32_t id
, uint32_t type
)
445 struct drm_mode_object
*obj
= NULL
;
447 mutex_lock(&dev
->mode_config
.idr_mutex
);
448 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
449 if (!obj
|| (type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
) ||
452 mutex_unlock(&dev
->mode_config
.idr_mutex
);
458 * drm_mode_object_find - look up a drm object with static lifetime
460 * @id: id of the mode object
461 * @type: type of the mode object
463 * Note that framebuffers cannot be looked up with this functions - since those
464 * are reference counted, they need special treatment. Even with
465 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
466 * rather than WARN_ON()).
468 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
469 uint32_t id
, uint32_t type
)
471 struct drm_mode_object
*obj
= NULL
;
473 /* Framebuffers are reference counted and need their own lookup
475 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
476 obj
= _object_find(dev
, id
, type
);
477 /* don't leak out unref'd fb's */
478 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
482 EXPORT_SYMBOL(drm_mode_object_find
);
485 * drm_framebuffer_init - initialize a framebuffer
487 * @fb: framebuffer to be initialized
488 * @funcs: ... with these functions
490 * Allocates an ID for the framebuffer's parent mode object, sets its mode
491 * functions & device file and adds it to the master fd list.
494 * This functions publishes the fb and makes it available for concurrent access
495 * by other users. Which means by this point the fb _must_ be fully set up -
496 * since all the fb attributes are invariant over its lifetime, no further
497 * locking but only correct reference counting is required.
500 * Zero on success, error code on failure.
502 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
503 const struct drm_framebuffer_funcs
*funcs
)
507 mutex_lock(&dev
->mode_config
.fb_lock
);
508 kref_init(&fb
->refcount
);
509 INIT_LIST_HEAD(&fb
->filp_head
);
513 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
517 /* Grab the idr reference. */
518 drm_framebuffer_reference(fb
);
520 dev
->mode_config
.num_fb
++;
521 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
523 mutex_unlock(&dev
->mode_config
.fb_lock
);
527 EXPORT_SYMBOL(drm_framebuffer_init
);
529 static void drm_framebuffer_free(struct kref
*kref
)
531 struct drm_framebuffer
*fb
=
532 container_of(kref
, struct drm_framebuffer
, refcount
);
533 fb
->funcs
->destroy(fb
);
536 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
539 struct drm_mode_object
*obj
= NULL
;
540 struct drm_framebuffer
*fb
;
542 mutex_lock(&dev
->mode_config
.idr_mutex
);
543 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
544 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
548 mutex_unlock(&dev
->mode_config
.idr_mutex
);
554 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
556 * @id: id of the fb object
558 * If successful, this grabs an additional reference to the framebuffer -
559 * callers need to make sure to eventually unreference the returned framebuffer
560 * again, using @drm_framebuffer_unreference.
562 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
565 struct drm_framebuffer
*fb
;
567 mutex_lock(&dev
->mode_config
.fb_lock
);
568 fb
= __drm_framebuffer_lookup(dev
, id
);
570 drm_framebuffer_reference(fb
);
571 mutex_unlock(&dev
->mode_config
.fb_lock
);
575 EXPORT_SYMBOL(drm_framebuffer_lookup
);
578 * drm_framebuffer_unreference - unref a framebuffer
579 * @fb: framebuffer to unref
581 * This functions decrements the fb's refcount and frees it if it drops to zero.
583 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
585 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
586 kref_put(&fb
->refcount
, drm_framebuffer_free
);
588 EXPORT_SYMBOL(drm_framebuffer_unreference
);
591 * drm_framebuffer_reference - incr the fb refcnt
594 * This functions increments the fb's refcount.
596 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
598 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
599 kref_get(&fb
->refcount
);
601 EXPORT_SYMBOL(drm_framebuffer_reference
);
603 static void drm_framebuffer_free_bug(struct kref
*kref
)
608 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
610 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
611 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
614 /* dev->mode_config.fb_lock must be held! */
615 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
616 struct drm_framebuffer
*fb
)
618 mutex_lock(&dev
->mode_config
.idr_mutex
);
619 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
620 mutex_unlock(&dev
->mode_config
.idr_mutex
);
624 __drm_framebuffer_unreference(fb
);
628 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
629 * @fb: fb to unregister
631 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
632 * those used for fbdev. Note that the caller must hold a reference of it's own,
633 * i.e. the object may not be destroyed through this call (since it'll lead to a
634 * locking inversion).
636 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
638 struct drm_device
*dev
= fb
->dev
;
640 mutex_lock(&dev
->mode_config
.fb_lock
);
641 /* Mark fb as reaped and drop idr ref. */
642 __drm_framebuffer_unregister(dev
, fb
);
643 mutex_unlock(&dev
->mode_config
.fb_lock
);
645 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
648 * drm_framebuffer_cleanup - remove a framebuffer object
649 * @fb: framebuffer to remove
651 * Cleanup framebuffer. This function is intended to be used from the drivers
652 * ->destroy callback. It can also be used to clean up driver private
653 * framebuffers embedded into a larger structure.
655 * Note that this function does not remove the fb from active usuage - if it is
656 * still used anywhere, hilarity can ensue since userspace could call getfb on
657 * the id and get back -EINVAL. Obviously no concern at driver unload time.
659 * Also, the framebuffer will not be removed from the lookup idr - for
660 * user-created framebuffers this will happen in in the rmfb ioctl. For
661 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
662 * drm_framebuffer_unregister_private.
664 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
666 struct drm_device
*dev
= fb
->dev
;
668 mutex_lock(&dev
->mode_config
.fb_lock
);
670 dev
->mode_config
.num_fb
--;
671 mutex_unlock(&dev
->mode_config
.fb_lock
);
673 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
676 * drm_framebuffer_remove - remove and unreference a framebuffer object
677 * @fb: framebuffer to remove
679 * Scans all the CRTCs and planes in @dev's mode_config. If they're
680 * using @fb, removes it, setting it to NULL. Then drops the reference to the
681 * passed-in framebuffer. Might take the modeset locks.
683 * Note that this function optimizes the cleanup away if the caller holds the
684 * last reference to the framebuffer. It is also guaranteed to not take the
685 * modeset locks in this case.
687 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
689 struct drm_device
*dev
= fb
->dev
;
690 struct drm_crtc
*crtc
;
691 struct drm_plane
*plane
;
692 struct drm_mode_set set
;
695 WARN_ON(!list_empty(&fb
->filp_head
));
698 * drm ABI mandates that we remove any deleted framebuffers from active
699 * useage. But since most sane clients only remove framebuffers they no
700 * longer need, try to optimize this away.
702 * Since we're holding a reference ourselves, observing a refcount of 1
703 * means that we're the last holder and can skip it. Also, the refcount
704 * can never increase from 1 again, so we don't need any barriers or
707 * Note that userspace could try to race with use and instate a new
708 * usage _after_ we've cleared all current ones. End result will be an
709 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
712 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
713 drm_modeset_lock_all(dev
);
714 /* remove from any CRTC */
715 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
716 if (crtc
->primary
->fb
== fb
) {
717 /* should turn off the crtc */
718 memset(&set
, 0, sizeof(struct drm_mode_set
));
721 ret
= drm_mode_set_config_internal(&set
);
723 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
727 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
729 drm_plane_force_disable(plane
);
731 drm_modeset_unlock_all(dev
);
734 drm_framebuffer_unreference(fb
);
736 EXPORT_SYMBOL(drm_framebuffer_remove
);
738 DEFINE_WW_CLASS(crtc_ww_class
);
741 * drm_crtc_init_with_planes - Initialise a new CRTC object with
742 * specified primary and cursor planes.
744 * @crtc: CRTC object to init
745 * @primary: Primary plane for CRTC
746 * @cursor: Cursor plane for CRTC
747 * @funcs: callbacks for the new CRTC
749 * Inits a new object created as base part of a driver crtc object.
752 * Zero on success, error code on failure.
754 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
755 struct drm_plane
*primary
,
756 struct drm_plane
*cursor
,
757 const struct drm_crtc_funcs
*funcs
)
759 struct drm_mode_config
*config
= &dev
->mode_config
;
764 crtc
->invert_dimensions
= false;
766 drm_modeset_lock_all(dev
);
767 drm_modeset_lock_init(&crtc
->mutex
);
768 /* dropped by _unlock_all(): */
769 drm_modeset_lock(&crtc
->mutex
, config
->acquire_ctx
);
771 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
775 crtc
->base
.properties
= &crtc
->properties
;
777 list_add_tail(&crtc
->head
, &config
->crtc_list
);
780 crtc
->primary
= primary
;
781 crtc
->cursor
= cursor
;
783 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
785 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
788 drm_modeset_unlock_all(dev
);
792 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
795 * drm_crtc_cleanup - Clean up the core crtc usage
796 * @crtc: CRTC to cleanup
798 * This function cleans up @crtc and removes it from the DRM mode setting
799 * core. Note that the function does *not* free the crtc structure itself,
800 * this is the responsibility of the caller.
802 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
804 struct drm_device
*dev
= crtc
->dev
;
806 kfree(crtc
->gamma_store
);
807 crtc
->gamma_store
= NULL
;
809 drm_modeset_lock_fini(&crtc
->mutex
);
811 drm_mode_object_put(dev
, &crtc
->base
);
812 list_del(&crtc
->head
);
813 dev
->mode_config
.num_crtc
--;
815 EXPORT_SYMBOL(drm_crtc_cleanup
);
818 * drm_crtc_index - find the index of a registered CRTC
819 * @crtc: CRTC to find index for
821 * Given a registered CRTC, return the index of that CRTC within a DRM
822 * device's list of CRTCs.
824 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
826 unsigned int index
= 0;
827 struct drm_crtc
*tmp
;
829 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
838 EXPORT_SYMBOL(drm_crtc_index
);
841 * drm_mode_remove - remove and free a mode
842 * @connector: connector list to modify
843 * @mode: mode to remove
845 * Remove @mode from @connector's mode list, then free it.
847 static void drm_mode_remove(struct drm_connector
*connector
,
848 struct drm_display_mode
*mode
)
850 list_del(&mode
->head
);
851 drm_mode_destroy(connector
->dev
, mode
);
855 * drm_connector_init - Init a preallocated connector
857 * @connector: the connector to init
858 * @funcs: callbacks for this connector
859 * @connector_type: user visible type of the connector
861 * Initialises a preallocated connector. Connectors should be
862 * subclassed as part of driver connector objects.
865 * Zero on success, error code on failure.
867 int drm_connector_init(struct drm_device
*dev
,
868 struct drm_connector
*connector
,
869 const struct drm_connector_funcs
*funcs
,
873 struct ida
*connector_ida
=
874 &drm_connector_enum_list
[connector_type
].ida
;
876 drm_modeset_lock_all(dev
);
878 ret
= drm_mode_object_get_reg(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
, false);
882 connector
->base
.properties
= &connector
->properties
;
883 connector
->dev
= dev
;
884 connector
->funcs
= funcs
;
885 connector
->connector_type
= connector_type
;
886 connector
->connector_type_id
=
887 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
888 if (connector
->connector_type_id
< 0) {
889 ret
= connector
->connector_type_id
;
893 kasprintf(GFP_KERNEL
, "%s-%d",
894 drm_connector_enum_list
[connector_type
].name
,
895 connector
->connector_type_id
);
896 if (!connector
->name
) {
901 INIT_LIST_HEAD(&connector
->probed_modes
);
902 INIT_LIST_HEAD(&connector
->modes
);
903 connector
->edid_blob_ptr
= NULL
;
904 connector
->status
= connector_status_unknown
;
906 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
907 dev
->mode_config
.num_connector
++;
909 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
910 drm_object_attach_property(&connector
->base
,
911 dev
->mode_config
.edid_property
,
914 drm_object_attach_property(&connector
->base
,
915 dev
->mode_config
.dpms_property
, 0);
917 connector
->debugfs_entry
= NULL
;
921 drm_mode_object_put(dev
, &connector
->base
);
924 drm_modeset_unlock_all(dev
);
928 EXPORT_SYMBOL(drm_connector_init
);
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
934 * Cleans up the connector but doesn't free the object.
936 void drm_connector_cleanup(struct drm_connector
*connector
)
938 struct drm_device
*dev
= connector
->dev
;
939 struct drm_display_mode
*mode
, *t
;
941 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
942 drm_mode_remove(connector
, mode
);
944 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
945 drm_mode_remove(connector
, mode
);
947 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
948 connector
->connector_type_id
);
950 drm_mode_object_put(dev
, &connector
->base
);
951 kfree(connector
->name
);
952 connector
->name
= NULL
;
953 list_del(&connector
->head
);
954 dev
->mode_config
.num_connector
--;
956 EXPORT_SYMBOL(drm_connector_cleanup
);
959 * drm_connector_register - register a connector
960 * @connector: the connector to register
962 * Register userspace interfaces for a connector
965 * Zero on success, error code on failure.
967 int drm_connector_register(struct drm_connector
*connector
)
971 drm_mode_object_register(connector
->dev
, &connector
->base
);
973 ret
= drm_sysfs_connector_add(connector
);
977 ret
= drm_debugfs_connector_add(connector
);
979 drm_sysfs_connector_remove(connector
);
985 EXPORT_SYMBOL(drm_connector_register
);
988 * drm_connector_unregister - unregister a connector
989 * @connector: the connector to unregister
991 * Unregister userspace interfaces for a connector
993 void drm_connector_unregister(struct drm_connector
*connector
)
995 drm_sysfs_connector_remove(connector
);
996 drm_debugfs_connector_remove(connector
);
998 EXPORT_SYMBOL(drm_connector_unregister
);
1002 * drm_connector_unplug_all - unregister connector userspace interfaces
1005 * This function unregisters all connector userspace interfaces in sysfs. Should
1006 * be call when the device is disconnected, e.g. from an usb driver's
1007 * ->disconnect callback.
1009 void drm_connector_unplug_all(struct drm_device
*dev
)
1011 struct drm_connector
*connector
;
1013 /* taking the mode config mutex ends up in a clash with sysfs */
1014 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1015 drm_connector_unregister(connector
);
1018 EXPORT_SYMBOL(drm_connector_unplug_all
);
1021 * drm_bridge_init - initialize a drm transcoder/bridge
1023 * @bridge: transcoder/bridge to set up
1024 * @funcs: bridge function table
1026 * Initialises a preallocated bridge. Bridges should be
1027 * subclassed as part of driver connector objects.
1030 * Zero on success, error code on failure.
1032 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
1033 const struct drm_bridge_funcs
*funcs
)
1037 drm_modeset_lock_all(dev
);
1039 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1044 bridge
->funcs
= funcs
;
1046 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1047 dev
->mode_config
.num_bridge
++;
1050 drm_modeset_unlock_all(dev
);
1053 EXPORT_SYMBOL(drm_bridge_init
);
1056 * drm_bridge_cleanup - cleans up an initialised bridge
1057 * @bridge: bridge to cleanup
1059 * Cleans up the bridge but doesn't free the object.
1061 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1063 struct drm_device
*dev
= bridge
->dev
;
1065 drm_modeset_lock_all(dev
);
1066 drm_mode_object_put(dev
, &bridge
->base
);
1067 list_del(&bridge
->head
);
1068 dev
->mode_config
.num_bridge
--;
1069 drm_modeset_unlock_all(dev
);
1071 EXPORT_SYMBOL(drm_bridge_cleanup
);
1074 * drm_encoder_init - Init a preallocated encoder
1076 * @encoder: the encoder to init
1077 * @funcs: callbacks for this encoder
1078 * @encoder_type: user visible type of the encoder
1080 * Initialises a preallocated encoder. Encoder should be
1081 * subclassed as part of driver encoder objects.
1084 * Zero on success, error code on failure.
1086 int drm_encoder_init(struct drm_device
*dev
,
1087 struct drm_encoder
*encoder
,
1088 const struct drm_encoder_funcs
*funcs
,
1093 drm_modeset_lock_all(dev
);
1095 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1100 encoder
->encoder_type
= encoder_type
;
1101 encoder
->funcs
= funcs
;
1102 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1103 drm_encoder_enum_list
[encoder_type
].name
,
1105 if (!encoder
->name
) {
1110 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1111 dev
->mode_config
.num_encoder
++;
1115 drm_mode_object_put(dev
, &encoder
->base
);
1118 drm_modeset_unlock_all(dev
);
1122 EXPORT_SYMBOL(drm_encoder_init
);
1125 * drm_encoder_cleanup - cleans up an initialised encoder
1126 * @encoder: encoder to cleanup
1128 * Cleans up the encoder but doesn't free the object.
1130 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1132 struct drm_device
*dev
= encoder
->dev
;
1133 drm_modeset_lock_all(dev
);
1134 drm_mode_object_put(dev
, &encoder
->base
);
1135 kfree(encoder
->name
);
1136 encoder
->name
= NULL
;
1137 list_del(&encoder
->head
);
1138 dev
->mode_config
.num_encoder
--;
1139 drm_modeset_unlock_all(dev
);
1141 EXPORT_SYMBOL(drm_encoder_cleanup
);
1144 * drm_universal_plane_init - Initialize a new universal plane object
1146 * @plane: plane object to init
1147 * @possible_crtcs: bitmask of possible CRTCs
1148 * @funcs: callbacks for the new plane
1149 * @formats: array of supported formats (%DRM_FORMAT_*)
1150 * @format_count: number of elements in @formats
1151 * @type: type of plane (overlay, primary, cursor)
1153 * Initializes a plane object of type @type.
1156 * Zero on success, error code on failure.
1158 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1159 unsigned long possible_crtcs
,
1160 const struct drm_plane_funcs
*funcs
,
1161 const uint32_t *formats
, uint32_t format_count
,
1162 enum drm_plane_type type
)
1166 drm_modeset_lock_all(dev
);
1168 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1172 plane
->base
.properties
= &plane
->properties
;
1174 plane
->funcs
= funcs
;
1175 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1177 if (!plane
->format_types
) {
1178 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1179 drm_mode_object_put(dev
, &plane
->base
);
1184 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1185 plane
->format_count
= format_count
;
1186 plane
->possible_crtcs
= possible_crtcs
;
1189 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1190 dev
->mode_config
.num_total_plane
++;
1191 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1192 dev
->mode_config
.num_overlay_plane
++;
1194 drm_object_attach_property(&plane
->base
,
1195 dev
->mode_config
.plane_type_property
,
1199 drm_modeset_unlock_all(dev
);
1203 EXPORT_SYMBOL(drm_universal_plane_init
);
1206 * drm_plane_init - Initialize a legacy plane
1208 * @plane: plane object to init
1209 * @possible_crtcs: bitmask of possible CRTCs
1210 * @funcs: callbacks for the new plane
1211 * @formats: array of supported formats (%DRM_FORMAT_*)
1212 * @format_count: number of elements in @formats
1213 * @is_primary: plane type (primary vs overlay)
1215 * Legacy API to initialize a DRM plane.
1217 * New drivers should call drm_universal_plane_init() instead.
1220 * Zero on success, error code on failure.
1222 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1223 unsigned long possible_crtcs
,
1224 const struct drm_plane_funcs
*funcs
,
1225 const uint32_t *formats
, uint32_t format_count
,
1228 enum drm_plane_type type
;
1230 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1231 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1232 formats
, format_count
, type
);
1234 EXPORT_SYMBOL(drm_plane_init
);
1237 * drm_plane_cleanup - Clean up the core plane usage
1238 * @plane: plane to cleanup
1240 * This function cleans up @plane and removes it from the DRM mode setting
1241 * core. Note that the function does *not* free the plane structure itself,
1242 * this is the responsibility of the caller.
1244 void drm_plane_cleanup(struct drm_plane
*plane
)
1246 struct drm_device
*dev
= plane
->dev
;
1248 drm_modeset_lock_all(dev
);
1249 kfree(plane
->format_types
);
1250 drm_mode_object_put(dev
, &plane
->base
);
1252 BUG_ON(list_empty(&plane
->head
));
1254 list_del(&plane
->head
);
1255 dev
->mode_config
.num_total_plane
--;
1256 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1257 dev
->mode_config
.num_overlay_plane
--;
1258 drm_modeset_unlock_all(dev
);
1260 EXPORT_SYMBOL(drm_plane_cleanup
);
1263 * drm_plane_force_disable - Forcibly disable a plane
1264 * @plane: plane to disable
1266 * Forces the plane to be disabled.
1268 * Used when the plane's current framebuffer is destroyed,
1269 * and when restoring fbdev mode.
1271 void drm_plane_force_disable(struct drm_plane
*plane
)
1273 struct drm_framebuffer
*old_fb
= plane
->fb
;
1279 ret
= plane
->funcs
->disable_plane(plane
);
1281 DRM_ERROR("failed to disable plane with busy fb\n");
1284 /* disconnect the plane from the fb and crtc: */
1285 __drm_framebuffer_unreference(old_fb
);
1289 EXPORT_SYMBOL(drm_plane_force_disable
);
1291 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1293 struct drm_property
*edid
;
1294 struct drm_property
*dpms
;
1295 struct drm_property
*dev_path
;
1298 * Standard properties (apply to all connectors)
1300 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1301 DRM_MODE_PROP_IMMUTABLE
,
1303 dev
->mode_config
.edid_property
= edid
;
1305 dpms
= drm_property_create_enum(dev
, 0,
1306 "DPMS", drm_dpms_enum_list
,
1307 ARRAY_SIZE(drm_dpms_enum_list
));
1308 dev
->mode_config
.dpms_property
= dpms
;
1310 dev_path
= drm_property_create(dev
,
1311 DRM_MODE_PROP_BLOB
|
1312 DRM_MODE_PROP_IMMUTABLE
,
1314 dev
->mode_config
.path_property
= dev_path
;
1319 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1321 struct drm_property
*type
;
1324 * Standard properties (apply to all planes)
1326 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1327 "type", drm_plane_type_enum_list
,
1328 ARRAY_SIZE(drm_plane_type_enum_list
));
1329 dev
->mode_config
.plane_type_property
= type
;
1335 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1338 * Called by a driver the first time a DVI-I connector is made.
1340 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1342 struct drm_property
*dvi_i_selector
;
1343 struct drm_property
*dvi_i_subconnector
;
1345 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1349 drm_property_create_enum(dev
, 0,
1350 "select subconnector",
1351 drm_dvi_i_select_enum_list
,
1352 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1353 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1355 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1357 drm_dvi_i_subconnector_enum_list
,
1358 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1359 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1363 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1366 * drm_create_tv_properties - create TV specific connector properties
1368 * @num_modes: number of different TV formats (modes) supported
1369 * @modes: array of pointers to strings containing name of each format
1371 * Called by a driver's TV initialization routine, this function creates
1372 * the TV specific connector properties for a given device. Caller is
1373 * responsible for allocating a list of format names and passing them to
1376 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1379 struct drm_property
*tv_selector
;
1380 struct drm_property
*tv_subconnector
;
1383 if (dev
->mode_config
.tv_select_subconnector_property
)
1387 * Basic connector properties
1389 tv_selector
= drm_property_create_enum(dev
, 0,
1390 "select subconnector",
1391 drm_tv_select_enum_list
,
1392 ARRAY_SIZE(drm_tv_select_enum_list
));
1393 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1396 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1398 drm_tv_subconnector_enum_list
,
1399 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1400 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1403 * Other, TV specific properties: margins & TV modes.
1405 dev
->mode_config
.tv_left_margin_property
=
1406 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1408 dev
->mode_config
.tv_right_margin_property
=
1409 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1411 dev
->mode_config
.tv_top_margin_property
=
1412 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1414 dev
->mode_config
.tv_bottom_margin_property
=
1415 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1417 dev
->mode_config
.tv_mode_property
=
1418 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1420 for (i
= 0; i
< num_modes
; i
++)
1421 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1424 dev
->mode_config
.tv_brightness_property
=
1425 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1427 dev
->mode_config
.tv_contrast_property
=
1428 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1430 dev
->mode_config
.tv_flicker_reduction_property
=
1431 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1433 dev
->mode_config
.tv_overscan_property
=
1434 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1436 dev
->mode_config
.tv_saturation_property
=
1437 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1439 dev
->mode_config
.tv_hue_property
=
1440 drm_property_create_range(dev
, 0, "hue", 0, 100);
1444 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1447 * drm_mode_create_scaling_mode_property - create scaling mode property
1450 * Called by a driver the first time it's needed, must be attached to desired
1453 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1455 struct drm_property
*scaling_mode
;
1457 if (dev
->mode_config
.scaling_mode_property
)
1461 drm_property_create_enum(dev
, 0, "scaling mode",
1462 drm_scaling_mode_enum_list
,
1463 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1465 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1469 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1472 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1475 * Called by a driver the first time it's needed, must be attached to desired
1479 * Zero on success, errno on failure.
1481 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1483 if (dev
->mode_config
.aspect_ratio_property
)
1486 dev
->mode_config
.aspect_ratio_property
=
1487 drm_property_create_enum(dev
, 0, "aspect ratio",
1488 drm_aspect_ratio_enum_list
,
1489 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1491 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1496 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1499 * drm_mode_create_dirty_property - create dirty property
1502 * Called by a driver the first time it's needed, must be attached to desired
1505 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1507 struct drm_property
*dirty_info
;
1509 if (dev
->mode_config
.dirty_info_property
)
1513 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1515 drm_dirty_info_enum_list
,
1516 ARRAY_SIZE(drm_dirty_info_enum_list
));
1517 dev
->mode_config
.dirty_info_property
= dirty_info
;
1521 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1523 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1525 uint32_t total_objects
= 0;
1527 total_objects
+= dev
->mode_config
.num_crtc
;
1528 total_objects
+= dev
->mode_config
.num_connector
;
1529 total_objects
+= dev
->mode_config
.num_encoder
;
1530 total_objects
+= dev
->mode_config
.num_bridge
;
1532 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1533 if (!group
->id_list
)
1536 group
->num_crtcs
= 0;
1537 group
->num_connectors
= 0;
1538 group
->num_encoders
= 0;
1539 group
->num_bridges
= 0;
1543 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1545 kfree(group
->id_list
);
1546 group
->id_list
= NULL
;
1550 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1551 * the drm core's responsibility to set up mode control groups.
1553 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1554 struct drm_mode_group
*group
)
1556 struct drm_crtc
*crtc
;
1557 struct drm_encoder
*encoder
;
1558 struct drm_connector
*connector
;
1559 struct drm_bridge
*bridge
;
1562 if ((ret
= drm_mode_group_init(dev
, group
)))
1565 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1566 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1568 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1569 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1572 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1573 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1574 group
->num_connectors
++] = connector
->base
.id
;
1576 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1577 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1578 group
->num_connectors
+ group
->num_bridges
++] =
1583 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1585 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1587 drm_modeset_lock_all(dev
);
1588 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1589 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1590 drm_modeset_unlock_all(dev
);
1592 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1595 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1596 * @out: drm_mode_modeinfo struct to return to the user
1597 * @in: drm_display_mode to use
1599 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1602 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1603 const struct drm_display_mode
*in
)
1605 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1606 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1607 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1608 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1609 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1610 "timing values too large for mode info\n");
1612 out
->clock
= in
->clock
;
1613 out
->hdisplay
= in
->hdisplay
;
1614 out
->hsync_start
= in
->hsync_start
;
1615 out
->hsync_end
= in
->hsync_end
;
1616 out
->htotal
= in
->htotal
;
1617 out
->hskew
= in
->hskew
;
1618 out
->vdisplay
= in
->vdisplay
;
1619 out
->vsync_start
= in
->vsync_start
;
1620 out
->vsync_end
= in
->vsync_end
;
1621 out
->vtotal
= in
->vtotal
;
1622 out
->vscan
= in
->vscan
;
1623 out
->vrefresh
= in
->vrefresh
;
1624 out
->flags
= in
->flags
;
1625 out
->type
= in
->type
;
1626 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1627 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1631 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1632 * @out: drm_display_mode to return to the user
1633 * @in: drm_mode_modeinfo to use
1635 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1639 * Zero on success, errno on failure.
1641 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1642 const struct drm_mode_modeinfo
*in
)
1644 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1647 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1650 out
->clock
= in
->clock
;
1651 out
->hdisplay
= in
->hdisplay
;
1652 out
->hsync_start
= in
->hsync_start
;
1653 out
->hsync_end
= in
->hsync_end
;
1654 out
->htotal
= in
->htotal
;
1655 out
->hskew
= in
->hskew
;
1656 out
->vdisplay
= in
->vdisplay
;
1657 out
->vsync_start
= in
->vsync_start
;
1658 out
->vsync_end
= in
->vsync_end
;
1659 out
->vtotal
= in
->vtotal
;
1660 out
->vscan
= in
->vscan
;
1661 out
->vrefresh
= in
->vrefresh
;
1662 out
->flags
= in
->flags
;
1663 out
->type
= in
->type
;
1664 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1665 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1671 * drm_mode_getresources - get graphics configuration
1672 * @dev: drm device for the ioctl
1673 * @data: data pointer for the ioctl
1674 * @file_priv: drm file for the ioctl call
1676 * Construct a set of configuration description structures and return
1677 * them to the user, including CRTC, connector and framebuffer configuration.
1679 * Called by the user via ioctl.
1682 * Zero on success, errno on failure.
1684 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1685 struct drm_file
*file_priv
)
1687 struct drm_mode_card_res
*card_res
= data
;
1688 struct list_head
*lh
;
1689 struct drm_framebuffer
*fb
;
1690 struct drm_connector
*connector
;
1691 struct drm_crtc
*crtc
;
1692 struct drm_encoder
*encoder
;
1694 int connector_count
= 0;
1697 int encoder_count
= 0;
1699 uint32_t __user
*fb_id
;
1700 uint32_t __user
*crtc_id
;
1701 uint32_t __user
*connector_id
;
1702 uint32_t __user
*encoder_id
;
1703 struct drm_mode_group
*mode_group
;
1705 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1709 mutex_lock(&file_priv
->fbs_lock
);
1711 * For the non-control nodes we need to limit the list of resources
1712 * by IDs in the group list for this node
1714 list_for_each(lh
, &file_priv
->fbs
)
1717 /* handle this in 4 parts */
1719 if (card_res
->count_fbs
>= fb_count
) {
1721 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1722 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1723 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1724 mutex_unlock(&file_priv
->fbs_lock
);
1730 card_res
->count_fbs
= fb_count
;
1731 mutex_unlock(&file_priv
->fbs_lock
);
1733 drm_modeset_lock_all(dev
);
1734 if (!drm_is_primary_client(file_priv
)) {
1737 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1740 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1743 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1747 mode_group
= &file_priv
->master
->minor
->mode_group
;
1748 crtc_count
= mode_group
->num_crtcs
;
1749 connector_count
= mode_group
->num_connectors
;
1750 encoder_count
= mode_group
->num_encoders
;
1753 card_res
->max_height
= dev
->mode_config
.max_height
;
1754 card_res
->min_height
= dev
->mode_config
.min_height
;
1755 card_res
->max_width
= dev
->mode_config
.max_width
;
1756 card_res
->min_width
= dev
->mode_config
.min_width
;
1759 if (card_res
->count_crtcs
>= crtc_count
) {
1761 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1763 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1765 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1766 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1773 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1774 if (put_user(mode_group
->id_list
[i
],
1775 crtc_id
+ copied
)) {
1783 card_res
->count_crtcs
= crtc_count
;
1786 if (card_res
->count_encoders
>= encoder_count
) {
1788 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1790 list_for_each_entry(encoder
,
1791 &dev
->mode_config
.encoder_list
,
1793 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1795 if (put_user(encoder
->base
.id
, encoder_id
+
1803 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1804 if (put_user(mode_group
->id_list
[i
],
1805 encoder_id
+ copied
)) {
1814 card_res
->count_encoders
= encoder_count
;
1817 if (card_res
->count_connectors
>= connector_count
) {
1819 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1821 list_for_each_entry(connector
,
1822 &dev
->mode_config
.connector_list
,
1824 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1827 if (put_user(connector
->base
.id
,
1828 connector_id
+ copied
)) {
1835 int start
= mode_group
->num_crtcs
+
1836 mode_group
->num_encoders
;
1837 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1838 if (put_user(mode_group
->id_list
[i
],
1839 connector_id
+ copied
)) {
1847 card_res
->count_connectors
= connector_count
;
1849 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1850 card_res
->count_connectors
, card_res
->count_encoders
);
1853 drm_modeset_unlock_all(dev
);
1858 * drm_mode_getcrtc - get CRTC configuration
1859 * @dev: drm device for the ioctl
1860 * @data: data pointer for the ioctl
1861 * @file_priv: drm file for the ioctl call
1863 * Construct a CRTC configuration structure to return to the user.
1865 * Called by the user via ioctl.
1868 * Zero on success, errno on failure.
1870 int drm_mode_getcrtc(struct drm_device
*dev
,
1871 void *data
, struct drm_file
*file_priv
)
1873 struct drm_mode_crtc
*crtc_resp
= data
;
1874 struct drm_crtc
*crtc
;
1877 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1880 drm_modeset_lock_all(dev
);
1882 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1888 crtc_resp
->x
= crtc
->x
;
1889 crtc_resp
->y
= crtc
->y
;
1890 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1891 if (crtc
->primary
->fb
)
1892 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1894 crtc_resp
->fb_id
= 0;
1896 if (crtc
->enabled
) {
1898 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1899 crtc_resp
->mode_valid
= 1;
1902 crtc_resp
->mode_valid
= 0;
1906 drm_modeset_unlock_all(dev
);
1910 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1911 const struct drm_file
*file_priv
)
1914 * If user-space hasn't configured the driver to expose the stereo 3D
1915 * modes, don't expose them.
1917 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1924 * drm_mode_getconnector - get connector configuration
1925 * @dev: drm device for the ioctl
1926 * @data: data pointer for the ioctl
1927 * @file_priv: drm file for the ioctl call
1929 * Construct a connector configuration structure to return to the user.
1931 * Called by the user via ioctl.
1934 * Zero on success, errno on failure.
1936 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1937 struct drm_file
*file_priv
)
1939 struct drm_mode_get_connector
*out_resp
= data
;
1940 struct drm_connector
*connector
;
1941 struct drm_display_mode
*mode
;
1943 int props_count
= 0;
1944 int encoders_count
= 0;
1948 struct drm_mode_modeinfo u_mode
;
1949 struct drm_mode_modeinfo __user
*mode_ptr
;
1950 uint32_t __user
*prop_ptr
;
1951 uint64_t __user
*prop_values
;
1952 uint32_t __user
*encoder_ptr
;
1954 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1957 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1959 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1961 mutex_lock(&dev
->mode_config
.mutex
);
1963 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1969 props_count
= connector
->properties
.count
;
1971 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1972 if (connector
->encoder_ids
[i
] != 0) {
1977 if (out_resp
->count_modes
== 0) {
1978 connector
->funcs
->fill_modes(connector
,
1979 dev
->mode_config
.max_width
,
1980 dev
->mode_config
.max_height
);
1983 /* delayed so we get modes regardless of pre-fill_modes state */
1984 list_for_each_entry(mode
, &connector
->modes
, head
)
1985 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1988 out_resp
->connector_id
= connector
->base
.id
;
1989 out_resp
->connector_type
= connector
->connector_type
;
1990 out_resp
->connector_type_id
= connector
->connector_type_id
;
1991 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1992 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1993 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1994 out_resp
->connection
= connector
->status
;
1995 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1996 if (connector
->encoder
)
1997 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1999 out_resp
->encoder_id
= 0;
2000 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2003 * This ioctl is called twice, once to determine how much space is
2004 * needed, and the 2nd time to fill it.
2006 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2008 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2009 list_for_each_entry(mode
, &connector
->modes
, head
) {
2010 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2013 drm_crtc_convert_to_umode(&u_mode
, mode
);
2014 if (copy_to_user(mode_ptr
+ copied
,
2015 &u_mode
, sizeof(u_mode
))) {
2022 out_resp
->count_modes
= mode_count
;
2024 if ((out_resp
->count_props
>= props_count
) && props_count
) {
2026 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
2027 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
2028 for (i
= 0; i
< connector
->properties
.count
; i
++) {
2029 if (put_user(connector
->properties
.ids
[i
],
2030 prop_ptr
+ copied
)) {
2035 if (put_user(connector
->properties
.values
[i
],
2036 prop_values
+ copied
)) {
2043 out_resp
->count_props
= props_count
;
2045 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2047 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2048 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2049 if (connector
->encoder_ids
[i
] != 0) {
2050 if (put_user(connector
->encoder_ids
[i
],
2051 encoder_ptr
+ copied
)) {
2059 out_resp
->count_encoders
= encoders_count
;
2062 mutex_unlock(&dev
->mode_config
.mutex
);
2068 * drm_mode_getencoder - get encoder configuration
2069 * @dev: drm device for the ioctl
2070 * @data: data pointer for the ioctl
2071 * @file_priv: drm file for the ioctl call
2073 * Construct a encoder configuration structure to return to the user.
2075 * Called by the user via ioctl.
2078 * Zero on success, errno on failure.
2080 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2081 struct drm_file
*file_priv
)
2083 struct drm_mode_get_encoder
*enc_resp
= data
;
2084 struct drm_encoder
*encoder
;
2087 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2090 drm_modeset_lock_all(dev
);
2091 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2098 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2100 enc_resp
->crtc_id
= 0;
2101 enc_resp
->encoder_type
= encoder
->encoder_type
;
2102 enc_resp
->encoder_id
= encoder
->base
.id
;
2103 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2104 enc_resp
->possible_clones
= encoder
->possible_clones
;
2107 drm_modeset_unlock_all(dev
);
2112 * drm_mode_getplane_res - enumerate all plane resources
2115 * @file_priv: DRM file info
2117 * Construct a list of plane ids to return to the user.
2119 * Called by the user via ioctl.
2122 * Zero on success, errno on failure.
2124 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2125 struct drm_file
*file_priv
)
2127 struct drm_mode_get_plane_res
*plane_resp
= data
;
2128 struct drm_mode_config
*config
;
2129 struct drm_plane
*plane
;
2130 uint32_t __user
*plane_ptr
;
2131 int copied
= 0, ret
= 0;
2132 unsigned num_planes
;
2134 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2137 drm_modeset_lock_all(dev
);
2138 config
= &dev
->mode_config
;
2140 if (file_priv
->universal_planes
)
2141 num_planes
= config
->num_total_plane
;
2143 num_planes
= config
->num_overlay_plane
;
2146 * This ioctl is called twice, once to determine how much space is
2147 * needed, and the 2nd time to fill it.
2150 (plane_resp
->count_planes
>= num_planes
)) {
2151 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2153 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2155 * Unless userspace set the 'universal planes'
2156 * capability bit, only advertise overlays.
2158 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2159 !file_priv
->universal_planes
)
2162 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2169 plane_resp
->count_planes
= num_planes
;
2172 drm_modeset_unlock_all(dev
);
2177 * drm_mode_getplane - get plane configuration
2180 * @file_priv: DRM file info
2182 * Construct a plane configuration structure to return to the user.
2184 * Called by the user via ioctl.
2187 * Zero on success, errno on failure.
2189 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2190 struct drm_file
*file_priv
)
2192 struct drm_mode_get_plane
*plane_resp
= data
;
2193 struct drm_plane
*plane
;
2194 uint32_t __user
*format_ptr
;
2197 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2200 drm_modeset_lock_all(dev
);
2201 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2208 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2210 plane_resp
->crtc_id
= 0;
2213 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2215 plane_resp
->fb_id
= 0;
2217 plane_resp
->plane_id
= plane
->base
.id
;
2218 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2219 plane_resp
->gamma_size
= 0;
2222 * This ioctl is called twice, once to determine how much space is
2223 * needed, and the 2nd time to fill it.
2225 if (plane
->format_count
&&
2226 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2227 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2228 if (copy_to_user(format_ptr
,
2229 plane
->format_types
,
2230 sizeof(uint32_t) * plane
->format_count
)) {
2235 plane_resp
->count_format_types
= plane
->format_count
;
2238 drm_modeset_unlock_all(dev
);
2243 * setplane_internal - setplane handler for internal callers
2245 * Note that we assume an extra reference has already been taken on fb. If the
2246 * update fails, this reference will be dropped before return; if it succeeds,
2247 * the previous framebuffer (if any) will be unreferenced instead.
2249 * src_{x,y,w,h} are provided in 16.16 fixed point format
2251 static int setplane_internal(struct drm_plane
*plane
,
2252 struct drm_crtc
*crtc
,
2253 struct drm_framebuffer
*fb
,
2254 int32_t crtc_x
, int32_t crtc_y
,
2255 uint32_t crtc_w
, uint32_t crtc_h
,
2256 /* src_{x,y,w,h} values are 16.16 fixed point */
2257 uint32_t src_x
, uint32_t src_y
,
2258 uint32_t src_w
, uint32_t src_h
)
2260 struct drm_device
*dev
= plane
->dev
;
2261 struct drm_framebuffer
*old_fb
= NULL
;
2263 unsigned int fb_width
, fb_height
;
2266 /* No fb means shut it down */
2268 drm_modeset_lock_all(dev
);
2270 ret
= plane
->funcs
->disable_plane(plane
);
2277 drm_modeset_unlock_all(dev
);
2281 /* Check whether this plane is usable on this CRTC */
2282 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2283 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2288 /* Check whether this plane supports the fb pixel format. */
2289 for (i
= 0; i
< plane
->format_count
; i
++)
2290 if (fb
->pixel_format
== plane
->format_types
[i
])
2292 if (i
== plane
->format_count
) {
2293 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2294 drm_get_format_name(fb
->pixel_format
));
2299 fb_width
= fb
->width
<< 16;
2300 fb_height
= fb
->height
<< 16;
2302 /* Make sure source coordinates are inside the fb. */
2303 if (src_w
> fb_width
||
2304 src_x
> fb_width
- src_w
||
2305 src_h
> fb_height
||
2306 src_y
> fb_height
- src_h
) {
2307 DRM_DEBUG_KMS("Invalid source coordinates "
2308 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2309 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2310 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2311 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2312 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2317 drm_modeset_lock_all(dev
);
2319 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2320 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2321 src_x
, src_y
, src_w
, src_h
);
2329 drm_modeset_unlock_all(dev
);
2333 drm_framebuffer_unreference(fb
);
2335 drm_framebuffer_unreference(old_fb
);
2342 * drm_mode_setplane - configure a plane's configuration
2344 * @data: ioctl data*
2345 * @file_priv: DRM file info
2347 * Set plane configuration, including placement, fb, scaling, and other factors.
2348 * Or pass a NULL fb to disable (planes may be disabled without providing a
2352 * Zero on success, errno on failure.
2354 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2355 struct drm_file
*file_priv
)
2357 struct drm_mode_set_plane
*plane_req
= data
;
2358 struct drm_mode_object
*obj
;
2359 struct drm_plane
*plane
;
2360 struct drm_crtc
*crtc
= NULL
;
2361 struct drm_framebuffer
*fb
= NULL
;
2363 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2366 /* Give drivers some help against integer overflows */
2367 if (plane_req
->crtc_w
> INT_MAX
||
2368 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2369 plane_req
->crtc_h
> INT_MAX
||
2370 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2371 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2372 plane_req
->crtc_w
, plane_req
->crtc_h
,
2373 plane_req
->crtc_x
, plane_req
->crtc_y
);
2378 * First, find the plane, crtc, and fb objects. If not available,
2379 * we don't bother to call the driver.
2381 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2382 DRM_MODE_OBJECT_PLANE
);
2384 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2385 plane_req
->plane_id
);
2388 plane
= obj_to_plane(obj
);
2390 if (plane_req
->fb_id
) {
2391 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2393 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2398 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2399 DRM_MODE_OBJECT_CRTC
);
2401 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2402 plane_req
->crtc_id
);
2405 crtc
= obj_to_crtc(obj
);
2409 * setplane_internal will take care of deref'ing either the old or new
2410 * framebuffer depending on success.
2412 return setplane_internal(plane
, crtc
, fb
,
2413 plane_req
->crtc_x
, plane_req
->crtc_y
,
2414 plane_req
->crtc_w
, plane_req
->crtc_h
,
2415 plane_req
->src_x
, plane_req
->src_y
,
2416 plane_req
->src_w
, plane_req
->src_h
);
2420 * drm_mode_set_config_internal - helper to call ->set_config
2421 * @set: modeset config to set
2423 * This is a little helper to wrap internal calls to the ->set_config driver
2424 * interface. The only thing it adds is correct refcounting dance.
2427 * Zero on success, errno on failure.
2429 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2431 struct drm_crtc
*crtc
= set
->crtc
;
2432 struct drm_framebuffer
*fb
;
2433 struct drm_crtc
*tmp
;
2437 * NOTE: ->set_config can also disable other crtcs (if we steal all
2438 * connectors from it), hence we need to refcount the fbs across all
2439 * crtcs. Atomic modeset will have saner semantics ...
2441 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2442 tmp
->old_fb
= tmp
->primary
->fb
;
2446 ret
= crtc
->funcs
->set_config(set
);
2448 crtc
->primary
->crtc
= crtc
;
2449 crtc
->primary
->fb
= fb
;
2452 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2453 if (tmp
->primary
->fb
)
2454 drm_framebuffer_reference(tmp
->primary
->fb
);
2456 drm_framebuffer_unreference(tmp
->old_fb
);
2461 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2464 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2466 * @crtc: CRTC that framebuffer will be displayed on
2469 * @mode: mode that framebuffer will be displayed under
2470 * @fb: framebuffer to check size of
2472 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2474 const struct drm_display_mode
*mode
,
2475 const struct drm_framebuffer
*fb
)
2478 int hdisplay
, vdisplay
;
2480 hdisplay
= mode
->hdisplay
;
2481 vdisplay
= mode
->vdisplay
;
2483 if (drm_mode_is_stereo(mode
)) {
2484 struct drm_display_mode adjusted
= *mode
;
2486 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2487 hdisplay
= adjusted
.crtc_hdisplay
;
2488 vdisplay
= adjusted
.crtc_vdisplay
;
2491 if (crtc
->invert_dimensions
)
2492 swap(hdisplay
, vdisplay
);
2494 if (hdisplay
> fb
->width
||
2495 vdisplay
> fb
->height
||
2496 x
> fb
->width
- hdisplay
||
2497 y
> fb
->height
- vdisplay
) {
2498 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2499 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2500 crtc
->invert_dimensions
? " (inverted)" : "");
2506 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2509 * drm_mode_setcrtc - set CRTC configuration
2510 * @dev: drm device for the ioctl
2511 * @data: data pointer for the ioctl
2512 * @file_priv: drm file for the ioctl call
2514 * Build a new CRTC configuration based on user request.
2516 * Called by the user via ioctl.
2519 * Zero on success, errno on failure.
2521 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2522 struct drm_file
*file_priv
)
2524 struct drm_mode_config
*config
= &dev
->mode_config
;
2525 struct drm_mode_crtc
*crtc_req
= data
;
2526 struct drm_crtc
*crtc
;
2527 struct drm_connector
**connector_set
= NULL
, *connector
;
2528 struct drm_framebuffer
*fb
= NULL
;
2529 struct drm_display_mode
*mode
= NULL
;
2530 struct drm_mode_set set
;
2531 uint32_t __user
*set_connectors_ptr
;
2535 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2538 /* For some reason crtc x/y offsets are signed internally. */
2539 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2542 drm_modeset_lock_all(dev
);
2543 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2545 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2549 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2551 if (crtc_req
->mode_valid
) {
2552 /* If we have a mode we need a framebuffer. */
2553 /* If we pass -1, set the mode with the currently bound fb */
2554 if (crtc_req
->fb_id
== -1) {
2555 if (!crtc
->primary
->fb
) {
2556 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2560 fb
= crtc
->primary
->fb
;
2561 /* Make refcounting symmetric with the lookup path. */
2562 drm_framebuffer_reference(fb
);
2564 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2566 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2573 mode
= drm_mode_create(dev
);
2579 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2581 DRM_DEBUG_KMS("Invalid mode\n");
2585 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2587 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2594 if (crtc_req
->count_connectors
== 0 && mode
) {
2595 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2600 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2601 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2602 crtc_req
->count_connectors
);
2607 if (crtc_req
->count_connectors
> 0) {
2610 /* Avoid unbounded kernel memory allocation */
2611 if (crtc_req
->count_connectors
> config
->num_connector
) {
2616 connector_set
= kmalloc(crtc_req
->count_connectors
*
2617 sizeof(struct drm_connector
*),
2619 if (!connector_set
) {
2624 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2625 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2626 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2631 connector
= drm_connector_find(dev
, out_id
);
2633 DRM_DEBUG_KMS("Connector id %d unknown\n",
2638 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2642 connector_set
[i
] = connector
;
2647 set
.x
= crtc_req
->x
;
2648 set
.y
= crtc_req
->y
;
2650 set
.connectors
= connector_set
;
2651 set
.num_connectors
= crtc_req
->count_connectors
;
2653 ret
= drm_mode_set_config_internal(&set
);
2657 drm_framebuffer_unreference(fb
);
2659 kfree(connector_set
);
2660 drm_mode_destroy(dev
, mode
);
2661 drm_modeset_unlock_all(dev
);
2666 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2667 * universal plane handler call
2668 * @crtc: crtc to update cursor for
2669 * @req: data pointer for the ioctl
2670 * @file_priv: drm file for the ioctl call
2672 * Legacy cursor ioctl's work directly with driver buffer handles. To
2673 * translate legacy ioctl calls into universal plane handler calls, we need to
2674 * wrap the native buffer handle in a drm_framebuffer.
2676 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2677 * buffer with a pitch of 4*width; the universal plane interface should be used
2678 * directly in cases where the hardware can support other buffer settings and
2679 * userspace wants to make use of these capabilities.
2682 * Zero on success, errno on failure.
2684 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2685 struct drm_mode_cursor2
*req
,
2686 struct drm_file
*file_priv
)
2688 struct drm_device
*dev
= crtc
->dev
;
2689 struct drm_framebuffer
*fb
= NULL
;
2690 struct drm_mode_fb_cmd2 fbreq
= {
2691 .width
= req
->width
,
2692 .height
= req
->height
,
2693 .pixel_format
= DRM_FORMAT_ARGB8888
,
2694 .pitches
= { req
->width
* 4 },
2695 .handles
= { req
->handle
},
2697 int32_t crtc_x
, crtc_y
;
2698 uint32_t crtc_w
= 0, crtc_h
= 0;
2699 uint32_t src_w
= 0, src_h
= 0;
2702 BUG_ON(!crtc
->cursor
);
2705 * Obtain fb we'll be using (either new or existing) and take an extra
2706 * reference to it if fb != null. setplane will take care of dropping
2707 * the reference if the plane update fails.
2709 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2711 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2713 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2717 drm_framebuffer_reference(fb
);
2722 mutex_lock(&dev
->mode_config
.mutex
);
2723 fb
= crtc
->cursor
->fb
;
2725 drm_framebuffer_reference(fb
);
2726 mutex_unlock(&dev
->mode_config
.mutex
);
2729 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2733 crtc_x
= crtc
->cursor_x
;
2734 crtc_y
= crtc
->cursor_y
;
2739 crtc_h
= fb
->height
;
2740 src_w
= fb
->width
<< 16;
2741 src_h
= fb
->height
<< 16;
2745 * setplane_internal will take care of deref'ing either the old or new
2746 * framebuffer depending on success.
2748 ret
= setplane_internal(crtc
->cursor
, crtc
, fb
,
2749 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2750 0, 0, src_w
, src_h
);
2752 /* Update successful; save new cursor position, if necessary */
2753 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2754 crtc
->cursor_x
= req
->x
;
2755 crtc
->cursor_y
= req
->y
;
2761 static int drm_mode_cursor_common(struct drm_device
*dev
,
2762 struct drm_mode_cursor2
*req
,
2763 struct drm_file
*file_priv
)
2765 struct drm_crtc
*crtc
;
2768 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2771 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2774 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2776 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2781 * If this crtc has a universal cursor plane, call that plane's update
2782 * handler rather than using legacy cursor handlers.
2785 return drm_mode_cursor_universal(crtc
, req
, file_priv
);
2787 drm_modeset_lock(&crtc
->mutex
, NULL
);
2788 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2789 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2793 /* Turns off the cursor if handle is 0 */
2794 if (crtc
->funcs
->cursor_set2
)
2795 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2796 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2798 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2799 req
->width
, req
->height
);
2802 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2803 if (crtc
->funcs
->cursor_move
) {
2804 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2811 drm_modeset_unlock(&crtc
->mutex
);
2819 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2820 * @dev: drm device for the ioctl
2821 * @data: data pointer for the ioctl
2822 * @file_priv: drm file for the ioctl call
2824 * Set the cursor configuration based on user request.
2826 * Called by the user via ioctl.
2829 * Zero on success, errno on failure.
2831 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2832 void *data
, struct drm_file
*file_priv
)
2834 struct drm_mode_cursor
*req
= data
;
2835 struct drm_mode_cursor2 new_req
;
2837 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2838 new_req
.hot_x
= new_req
.hot_y
= 0;
2840 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2844 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2845 * @dev: drm device for the ioctl
2846 * @data: data pointer for the ioctl
2847 * @file_priv: drm file for the ioctl call
2849 * Set the cursor configuration based on user request. This implements the 2nd
2850 * version of the cursor ioctl, which allows userspace to additionally specify
2851 * the hotspot of the pointer.
2853 * Called by the user via ioctl.
2856 * Zero on success, errno on failure.
2858 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2859 void *data
, struct drm_file
*file_priv
)
2861 struct drm_mode_cursor2
*req
= data
;
2862 return drm_mode_cursor_common(dev
, req
, file_priv
);
2866 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2867 * @bpp: bits per pixels
2868 * @depth: bit depth per pixel
2870 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2871 * Useful in fbdev emulation code, since that deals in those values.
2873 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2879 fmt
= DRM_FORMAT_C8
;
2883 fmt
= DRM_FORMAT_XRGB1555
;
2885 fmt
= DRM_FORMAT_RGB565
;
2888 fmt
= DRM_FORMAT_RGB888
;
2892 fmt
= DRM_FORMAT_XRGB8888
;
2893 else if (depth
== 30)
2894 fmt
= DRM_FORMAT_XRGB2101010
;
2896 fmt
= DRM_FORMAT_ARGB8888
;
2899 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2900 fmt
= DRM_FORMAT_XRGB8888
;
2906 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2909 * drm_mode_addfb - add an FB to the graphics configuration
2910 * @dev: drm device for the ioctl
2911 * @data: data pointer for the ioctl
2912 * @file_priv: drm file for the ioctl call
2914 * Add a new FB to the specified CRTC, given a user request. This is the
2915 * original addfb ioclt which only supported RGB formats.
2917 * Called by the user via ioctl.
2920 * Zero on success, errno on failure.
2922 int drm_mode_addfb(struct drm_device
*dev
,
2923 void *data
, struct drm_file
*file_priv
)
2925 struct drm_mode_fb_cmd
*or = data
;
2926 struct drm_mode_fb_cmd2 r
= {};
2927 struct drm_mode_config
*config
= &dev
->mode_config
;
2928 struct drm_framebuffer
*fb
;
2931 /* Use new struct with format internally */
2932 r
.fb_id
= or->fb_id
;
2933 r
.width
= or->width
;
2934 r
.height
= or->height
;
2935 r
.pitches
[0] = or->pitch
;
2936 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2937 r
.handles
[0] = or->handle
;
2939 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2942 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2945 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2948 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2950 DRM_DEBUG_KMS("could not create framebuffer\n");
2954 mutex_lock(&file_priv
->fbs_lock
);
2955 or->fb_id
= fb
->base
.id
;
2956 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2957 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2958 mutex_unlock(&file_priv
->fbs_lock
);
2963 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2965 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2969 case DRM_FORMAT_RGB332
:
2970 case DRM_FORMAT_BGR233
:
2971 case DRM_FORMAT_XRGB4444
:
2972 case DRM_FORMAT_XBGR4444
:
2973 case DRM_FORMAT_RGBX4444
:
2974 case DRM_FORMAT_BGRX4444
:
2975 case DRM_FORMAT_ARGB4444
:
2976 case DRM_FORMAT_ABGR4444
:
2977 case DRM_FORMAT_RGBA4444
:
2978 case DRM_FORMAT_BGRA4444
:
2979 case DRM_FORMAT_XRGB1555
:
2980 case DRM_FORMAT_XBGR1555
:
2981 case DRM_FORMAT_RGBX5551
:
2982 case DRM_FORMAT_BGRX5551
:
2983 case DRM_FORMAT_ARGB1555
:
2984 case DRM_FORMAT_ABGR1555
:
2985 case DRM_FORMAT_RGBA5551
:
2986 case DRM_FORMAT_BGRA5551
:
2987 case DRM_FORMAT_RGB565
:
2988 case DRM_FORMAT_BGR565
:
2989 case DRM_FORMAT_RGB888
:
2990 case DRM_FORMAT_BGR888
:
2991 case DRM_FORMAT_XRGB8888
:
2992 case DRM_FORMAT_XBGR8888
:
2993 case DRM_FORMAT_RGBX8888
:
2994 case DRM_FORMAT_BGRX8888
:
2995 case DRM_FORMAT_ARGB8888
:
2996 case DRM_FORMAT_ABGR8888
:
2997 case DRM_FORMAT_RGBA8888
:
2998 case DRM_FORMAT_BGRA8888
:
2999 case DRM_FORMAT_XRGB2101010
:
3000 case DRM_FORMAT_XBGR2101010
:
3001 case DRM_FORMAT_RGBX1010102
:
3002 case DRM_FORMAT_BGRX1010102
:
3003 case DRM_FORMAT_ARGB2101010
:
3004 case DRM_FORMAT_ABGR2101010
:
3005 case DRM_FORMAT_RGBA1010102
:
3006 case DRM_FORMAT_BGRA1010102
:
3007 case DRM_FORMAT_YUYV
:
3008 case DRM_FORMAT_YVYU
:
3009 case DRM_FORMAT_UYVY
:
3010 case DRM_FORMAT_VYUY
:
3011 case DRM_FORMAT_AYUV
:
3012 case DRM_FORMAT_NV12
:
3013 case DRM_FORMAT_NV21
:
3014 case DRM_FORMAT_NV16
:
3015 case DRM_FORMAT_NV61
:
3016 case DRM_FORMAT_NV24
:
3017 case DRM_FORMAT_NV42
:
3018 case DRM_FORMAT_YUV410
:
3019 case DRM_FORMAT_YVU410
:
3020 case DRM_FORMAT_YUV411
:
3021 case DRM_FORMAT_YVU411
:
3022 case DRM_FORMAT_YUV420
:
3023 case DRM_FORMAT_YVU420
:
3024 case DRM_FORMAT_YUV422
:
3025 case DRM_FORMAT_YVU422
:
3026 case DRM_FORMAT_YUV444
:
3027 case DRM_FORMAT_YVU444
:
3030 DRM_DEBUG_KMS("invalid pixel format %s\n",
3031 drm_get_format_name(r
->pixel_format
));
3036 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3038 int ret
, hsub
, vsub
, num_planes
, i
;
3040 ret
= format_check(r
);
3042 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3043 drm_get_format_name(r
->pixel_format
));
3047 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3048 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3049 num_planes
= drm_format_num_planes(r
->pixel_format
);
3051 if (r
->width
== 0 || r
->width
% hsub
) {
3052 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
3056 if (r
->height
== 0 || r
->height
% vsub
) {
3057 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3061 for (i
= 0; i
< num_planes
; i
++) {
3062 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3063 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3064 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3066 if (!r
->handles
[i
]) {
3067 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3071 if ((uint64_t) width
* cpp
> UINT_MAX
)
3074 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3077 if (r
->pitches
[i
] < width
* cpp
) {
3078 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3086 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
3087 struct drm_mode_fb_cmd2
*r
,
3088 struct drm_file
*file_priv
)
3090 struct drm_mode_config
*config
= &dev
->mode_config
;
3091 struct drm_framebuffer
*fb
;
3094 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3095 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3096 return ERR_PTR(-EINVAL
);
3099 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3100 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3101 r
->width
, config
->min_width
, config
->max_width
);
3102 return ERR_PTR(-EINVAL
);
3104 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3105 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3106 r
->height
, config
->min_height
, config
->max_height
);
3107 return ERR_PTR(-EINVAL
);
3110 ret
= framebuffer_check(r
);
3112 return ERR_PTR(ret
);
3114 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3116 DRM_DEBUG_KMS("could not create framebuffer\n");
3120 mutex_lock(&file_priv
->fbs_lock
);
3121 r
->fb_id
= fb
->base
.id
;
3122 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3123 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3124 mutex_unlock(&file_priv
->fbs_lock
);
3130 * drm_mode_addfb2 - add an FB to the graphics configuration
3131 * @dev: drm device for the ioctl
3132 * @data: data pointer for the ioctl
3133 * @file_priv: drm file for the ioctl call
3135 * Add a new FB to the specified CRTC, given a user request with format. This is
3136 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3137 * and uses fourcc codes as pixel format specifiers.
3139 * Called by the user via ioctl.
3142 * Zero on success, errno on failure.
3144 int drm_mode_addfb2(struct drm_device
*dev
,
3145 void *data
, struct drm_file
*file_priv
)
3147 struct drm_framebuffer
*fb
;
3149 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3152 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3160 * drm_mode_rmfb - remove an FB from the configuration
3161 * @dev: drm device for the ioctl
3162 * @data: data pointer for the ioctl
3163 * @file_priv: drm file for the ioctl call
3165 * Remove the FB specified by the user.
3167 * Called by the user via ioctl.
3170 * Zero on success, errno on failure.
3172 int drm_mode_rmfb(struct drm_device
*dev
,
3173 void *data
, struct drm_file
*file_priv
)
3175 struct drm_framebuffer
*fb
= NULL
;
3176 struct drm_framebuffer
*fbl
= NULL
;
3177 uint32_t *id
= data
;
3180 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3183 mutex_lock(&file_priv
->fbs_lock
);
3184 mutex_lock(&dev
->mode_config
.fb_lock
);
3185 fb
= __drm_framebuffer_lookup(dev
, *id
);
3189 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3195 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3196 __drm_framebuffer_unregister(dev
, fb
);
3198 list_del_init(&fb
->filp_head
);
3199 mutex_unlock(&dev
->mode_config
.fb_lock
);
3200 mutex_unlock(&file_priv
->fbs_lock
);
3202 drm_framebuffer_remove(fb
);
3207 mutex_unlock(&dev
->mode_config
.fb_lock
);
3208 mutex_unlock(&file_priv
->fbs_lock
);
3214 * drm_mode_getfb - get FB info
3215 * @dev: drm device for the ioctl
3216 * @data: data pointer for the ioctl
3217 * @file_priv: drm file for the ioctl call
3219 * Lookup the FB given its ID and return info about it.
3221 * Called by the user via ioctl.
3224 * Zero on success, errno on failure.
3226 int drm_mode_getfb(struct drm_device
*dev
,
3227 void *data
, struct drm_file
*file_priv
)
3229 struct drm_mode_fb_cmd
*r
= data
;
3230 struct drm_framebuffer
*fb
;
3233 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3236 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3240 r
->height
= fb
->height
;
3241 r
->width
= fb
->width
;
3242 r
->depth
= fb
->depth
;
3243 r
->bpp
= fb
->bits_per_pixel
;
3244 r
->pitch
= fb
->pitches
[0];
3245 if (fb
->funcs
->create_handle
) {
3246 if (drm_is_master(file_priv
) || capable(CAP_SYS_ADMIN
) ||
3247 drm_is_control_client(file_priv
)) {
3248 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3251 /* GET_FB() is an unprivileged ioctl so we must not
3252 * return a buffer-handle to non-master processes! For
3253 * backwards-compatibility reasons, we cannot make
3254 * GET_FB() privileged, so just return an invalid handle
3255 * for non-masters. */
3263 drm_framebuffer_unreference(fb
);
3269 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3270 * @dev: drm device for the ioctl
3271 * @data: data pointer for the ioctl
3272 * @file_priv: drm file for the ioctl call
3274 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3275 * rectangle list. Generic userspace which does frontbuffer rendering must call
3276 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3277 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3279 * Modesetting drivers which always update the frontbuffer do not need to
3280 * implement the corresponding ->dirty framebuffer callback.
3282 * Called by the user via ioctl.
3285 * Zero on success, errno on failure.
3287 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3288 void *data
, struct drm_file
*file_priv
)
3290 struct drm_clip_rect __user
*clips_ptr
;
3291 struct drm_clip_rect
*clips
= NULL
;
3292 struct drm_mode_fb_dirty_cmd
*r
= data
;
3293 struct drm_framebuffer
*fb
;
3298 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3301 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3305 num_clips
= r
->num_clips
;
3306 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3308 if (!num_clips
!= !clips_ptr
) {
3313 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3315 /* If userspace annotates copy, clips must come in pairs */
3316 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3321 if (num_clips
&& clips_ptr
) {
3322 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3326 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3332 ret
= copy_from_user(clips
, clips_ptr
,
3333 num_clips
* sizeof(*clips
));
3340 if (fb
->funcs
->dirty
) {
3341 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3350 drm_framebuffer_unreference(fb
);
3357 * drm_fb_release - remove and free the FBs on this file
3358 * @priv: drm file for the ioctl
3360 * Destroy all the FBs associated with @filp.
3362 * Called by the user via ioctl.
3365 * Zero on success, errno on failure.
3367 void drm_fb_release(struct drm_file
*priv
)
3369 struct drm_device
*dev
= priv
->minor
->dev
;
3370 struct drm_framebuffer
*fb
, *tfb
;
3372 mutex_lock(&priv
->fbs_lock
);
3373 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3375 mutex_lock(&dev
->mode_config
.fb_lock
);
3376 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3377 __drm_framebuffer_unregister(dev
, fb
);
3378 mutex_unlock(&dev
->mode_config
.fb_lock
);
3380 list_del_init(&fb
->filp_head
);
3382 /* This will also drop the fpriv->fbs reference. */
3383 drm_framebuffer_remove(fb
);
3385 mutex_unlock(&priv
->fbs_lock
);
3389 * drm_property_create - create a new property type
3391 * @flags: flags specifying the property type
3392 * @name: name of the property
3393 * @num_values: number of pre-defined values
3395 * This creates a new generic drm property which can then be attached to a drm
3396 * object with drm_object_attach_property. The returned property object must be
3397 * freed with drm_property_destroy.
3400 * A pointer to the newly created property on success, NULL on failure.
3402 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3403 const char *name
, int num_values
)
3405 struct drm_property
*property
= NULL
;
3408 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3412 property
->dev
= dev
;
3415 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3416 if (!property
->values
)
3420 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3424 property
->flags
= flags
;
3425 property
->num_values
= num_values
;
3426 INIT_LIST_HEAD(&property
->enum_blob_list
);
3429 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3430 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3433 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3435 WARN_ON(!drm_property_type_valid(property
));
3439 kfree(property
->values
);
3443 EXPORT_SYMBOL(drm_property_create
);
3446 * drm_property_create_enum - create a new enumeration property type
3448 * @flags: flags specifying the property type
3449 * @name: name of the property
3450 * @props: enumeration lists with property values
3451 * @num_values: number of pre-defined values
3453 * This creates a new generic drm property which can then be attached to a drm
3454 * object with drm_object_attach_property. The returned property object must be
3455 * freed with drm_property_destroy.
3457 * Userspace is only allowed to set one of the predefined values for enumeration
3461 * A pointer to the newly created property on success, NULL on failure.
3463 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3465 const struct drm_prop_enum_list
*props
,
3468 struct drm_property
*property
;
3471 flags
|= DRM_MODE_PROP_ENUM
;
3473 property
= drm_property_create(dev
, flags
, name
, num_values
);
3477 for (i
= 0; i
< num_values
; i
++) {
3478 ret
= drm_property_add_enum(property
, i
,
3482 drm_property_destroy(dev
, property
);
3489 EXPORT_SYMBOL(drm_property_create_enum
);
3492 * drm_property_create_bitmask - create a new bitmask property type
3494 * @flags: flags specifying the property type
3495 * @name: name of the property
3496 * @props: enumeration lists with property bitflags
3497 * @num_values: number of pre-defined values
3499 * This creates a new generic drm property which can then be attached to a drm
3500 * object with drm_object_attach_property. The returned property object must be
3501 * freed with drm_property_destroy.
3503 * Compared to plain enumeration properties userspace is allowed to set any
3504 * or'ed together combination of the predefined property bitflag values
3507 * A pointer to the newly created property on success, NULL on failure.
3509 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3510 int flags
, const char *name
,
3511 const struct drm_prop_enum_list
*props
,
3513 uint64_t supported_bits
)
3515 struct drm_property
*property
;
3516 int i
, ret
, index
= 0;
3517 int num_values
= hweight64(supported_bits
);
3519 flags
|= DRM_MODE_PROP_BITMASK
;
3521 property
= drm_property_create(dev
, flags
, name
, num_values
);
3524 for (i
= 0; i
< num_props
; i
++) {
3525 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3528 if (WARN_ON(index
>= num_values
)) {
3529 drm_property_destroy(dev
, property
);
3533 ret
= drm_property_add_enum(property
, index
++,
3537 drm_property_destroy(dev
, property
);
3544 EXPORT_SYMBOL(drm_property_create_bitmask
);
3546 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3547 int flags
, const char *name
,
3548 uint64_t min
, uint64_t max
)
3550 struct drm_property
*property
;
3552 property
= drm_property_create(dev
, flags
, name
, 2);
3556 property
->values
[0] = min
;
3557 property
->values
[1] = max
;
3563 * drm_property_create_range - create a new ranged property type
3565 * @flags: flags specifying the property type
3566 * @name: name of the property
3567 * @min: minimum value of the property
3568 * @max: maximum value of the property
3570 * This creates a new generic drm property which can then be attached to a drm
3571 * object with drm_object_attach_property. The returned property object must be
3572 * freed with drm_property_destroy.
3574 * Userspace is allowed to set any interger value in the (min, max) range
3578 * A pointer to the newly created property on success, NULL on failure.
3580 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3582 uint64_t min
, uint64_t max
)
3584 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3587 EXPORT_SYMBOL(drm_property_create_range
);
3589 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3590 int flags
, const char *name
,
3591 int64_t min
, int64_t max
)
3593 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3594 name
, I642U64(min
), I642U64(max
));
3596 EXPORT_SYMBOL(drm_property_create_signed_range
);
3598 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3599 int flags
, const char *name
, uint32_t type
)
3601 struct drm_property
*property
;
3603 flags
|= DRM_MODE_PROP_OBJECT
;
3605 property
= drm_property_create(dev
, flags
, name
, 1);
3609 property
->values
[0] = type
;
3613 EXPORT_SYMBOL(drm_property_create_object
);
3616 * drm_property_add_enum - add a possible value to an enumeration property
3617 * @property: enumeration property to change
3618 * @index: index of the new enumeration
3619 * @value: value of the new enumeration
3620 * @name: symbolic name of the new enumeration
3622 * This functions adds enumerations to a property.
3624 * It's use is deprecated, drivers should use one of the more specific helpers
3625 * to directly create the property with all enumerations already attached.
3628 * Zero on success, error code on failure.
3630 int drm_property_add_enum(struct drm_property
*property
, int index
,
3631 uint64_t value
, const char *name
)
3633 struct drm_property_enum
*prop_enum
;
3635 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3636 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3640 * Bitmask enum properties have the additional constraint of values
3643 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3647 if (!list_empty(&property
->enum_blob_list
)) {
3648 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3649 if (prop_enum
->value
== value
) {
3650 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3651 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3657 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3661 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3662 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3663 prop_enum
->value
= value
;
3665 property
->values
[index
] = value
;
3666 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3669 EXPORT_SYMBOL(drm_property_add_enum
);
3672 * drm_property_destroy - destroy a drm property
3674 * @property: property to destry
3676 * This function frees a property including any attached resources like
3677 * enumeration values.
3679 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3681 struct drm_property_enum
*prop_enum
, *pt
;
3683 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3684 list_del(&prop_enum
->head
);
3688 if (property
->num_values
)
3689 kfree(property
->values
);
3690 drm_mode_object_put(dev
, &property
->base
);
3691 list_del(&property
->head
);
3694 EXPORT_SYMBOL(drm_property_destroy
);
3697 * drm_object_attach_property - attach a property to a modeset object
3698 * @obj: drm modeset object
3699 * @property: property to attach
3700 * @init_val: initial value of the property
3702 * This attaches the given property to the modeset object with the given initial
3703 * value. Currently this function cannot fail since the properties are stored in
3704 * a statically sized array.
3706 void drm_object_attach_property(struct drm_mode_object
*obj
,
3707 struct drm_property
*property
,
3710 int count
= obj
->properties
->count
;
3712 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3713 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3714 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3715 "you see this message on the same object type.\n",
3720 obj
->properties
->ids
[count
] = property
->base
.id
;
3721 obj
->properties
->values
[count
] = init_val
;
3722 obj
->properties
->count
++;
3724 EXPORT_SYMBOL(drm_object_attach_property
);
3727 * drm_object_property_set_value - set the value of a property
3728 * @obj: drm mode object to set property value for
3729 * @property: property to set
3730 * @val: value the property should be set to
3732 * This functions sets a given property on a given object. This function only
3733 * changes the software state of the property, it does not call into the
3734 * driver's ->set_property callback.
3737 * Zero on success, error code on failure.
3739 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3740 struct drm_property
*property
, uint64_t val
)
3744 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3745 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3746 obj
->properties
->values
[i
] = val
;
3753 EXPORT_SYMBOL(drm_object_property_set_value
);
3756 * drm_object_property_get_value - retrieve the value of a property
3757 * @obj: drm mode object to get property value from
3758 * @property: property to retrieve
3759 * @val: storage for the property value
3761 * This function retrieves the softare state of the given property for the given
3762 * property. Since there is no driver callback to retrieve the current property
3763 * value this might be out of sync with the hardware, depending upon the driver
3767 * Zero on success, error code on failure.
3769 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3770 struct drm_property
*property
, uint64_t *val
)
3774 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3775 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3776 *val
= obj
->properties
->values
[i
];
3783 EXPORT_SYMBOL(drm_object_property_get_value
);
3786 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3789 * @file_priv: DRM file info
3791 * This function retrieves the current value for an connectors's property.
3793 * Called by the user via ioctl.
3796 * Zero on success, errno on failure.
3798 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3799 void *data
, struct drm_file
*file_priv
)
3801 struct drm_mode_get_property
*out_resp
= data
;
3802 struct drm_property
*property
;
3805 int value_count
= 0;
3808 struct drm_property_enum
*prop_enum
;
3809 struct drm_mode_property_enum __user
*enum_ptr
;
3810 struct drm_property_blob
*prop_blob
;
3811 uint32_t __user
*blob_id_ptr
;
3812 uint64_t __user
*values_ptr
;
3813 uint32_t __user
*blob_length_ptr
;
3815 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3818 drm_modeset_lock_all(dev
);
3819 property
= drm_property_find(dev
, out_resp
->prop_id
);
3825 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3826 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3827 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3829 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3830 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3834 value_count
= property
->num_values
;
3836 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3837 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3838 out_resp
->flags
= property
->flags
;
3840 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3841 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3842 for (i
= 0; i
< value_count
; i
++) {
3843 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3849 out_resp
->count_values
= value_count
;
3851 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3852 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3853 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3855 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3856 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3858 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3863 if (copy_to_user(&enum_ptr
[copied
].name
,
3864 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3871 out_resp
->count_enum_blobs
= enum_count
;
3874 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3875 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3877 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3878 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3880 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3881 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3886 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3894 out_resp
->count_enum_blobs
= blob_count
;
3897 drm_modeset_unlock_all(dev
);
3901 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3904 struct drm_property_blob
*blob
;
3907 if (!length
|| !data
)
3910 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3914 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3920 blob
->length
= length
;
3922 memcpy(blob
->data
, data
, length
);
3924 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3928 static void drm_property_destroy_blob(struct drm_device
*dev
,
3929 struct drm_property_blob
*blob
)
3931 drm_mode_object_put(dev
, &blob
->base
);
3932 list_del(&blob
->head
);
3937 * drm_mode_getblob_ioctl - get the contents of a blob property value
3940 * @file_priv: DRM file info
3942 * This function retrieves the contents of a blob property. The value stored in
3943 * an object's blob property is just a normal modeset object id.
3945 * Called by the user via ioctl.
3948 * Zero on success, errno on failure.
3950 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3951 void *data
, struct drm_file
*file_priv
)
3953 struct drm_mode_get_blob
*out_resp
= data
;
3954 struct drm_property_blob
*blob
;
3956 void __user
*blob_ptr
;
3958 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3961 drm_modeset_lock_all(dev
);
3962 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3968 if (out_resp
->length
== blob
->length
) {
3969 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3970 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3975 out_resp
->length
= blob
->length
;
3978 drm_modeset_unlock_all(dev
);
3982 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
3985 struct drm_device
*dev
= connector
->dev
;
3987 size
= strlen(path
) + 1;
3989 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
3991 if (!connector
->path_blob_ptr
)
3994 ret
= drm_object_property_set_value(&connector
->base
,
3995 dev
->mode_config
.path_property
,
3996 connector
->path_blob_ptr
->base
.id
);
3999 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4002 * drm_mode_connector_update_edid_property - update the edid property of a connector
4003 * @connector: drm connector
4004 * @edid: new value of the edid property
4006 * This function creates a new blob modeset object and assigns its id to the
4007 * connector's edid property.
4010 * Zero on success, errno on failure.
4012 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4015 struct drm_device
*dev
= connector
->dev
;
4018 /* ignore requests to set edid when overridden */
4019 if (connector
->override_edid
)
4022 if (connector
->edid_blob_ptr
)
4023 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
4025 /* Delete edid, when there is none. */
4027 connector
->edid_blob_ptr
= NULL
;
4028 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
4032 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4033 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
4035 if (!connector
->edid_blob_ptr
)
4038 ret
= drm_object_property_set_value(&connector
->base
,
4039 dev
->mode_config
.edid_property
,
4040 connector
->edid_blob_ptr
->base
.id
);
4044 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4046 static bool drm_property_change_is_valid(struct drm_property
*property
,
4049 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4052 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4053 if (value
< property
->values
[0] || value
> property
->values
[1])
4056 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4057 int64_t svalue
= U642I64(value
);
4058 if (svalue
< U642I64(property
->values
[0]) ||
4059 svalue
> U642I64(property
->values
[1]))
4062 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4064 uint64_t valid_mask
= 0;
4065 for (i
= 0; i
< property
->num_values
; i
++)
4066 valid_mask
|= (1ULL << property
->values
[i
]);
4067 return !(value
& ~valid_mask
);
4068 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4069 /* Only the driver knows */
4071 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4072 struct drm_mode_object
*obj
;
4073 /* a zero value for an object property translates to null: */
4077 * NOTE: use _object_find() directly to bypass restriction on
4078 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4079 * object this could race against object finalization, so it
4080 * simply tells us that the object *was* valid. Which is good
4083 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
4087 for (i
= 0; i
< property
->num_values
; i
++)
4088 if (property
->values
[i
] == value
)
4095 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4098 * @file_priv: DRM file info
4100 * This function sets the current value for a connectors's property. It also
4101 * calls into a driver's ->set_property callback to update the hardware state
4103 * Called by the user via ioctl.
4106 * Zero on success, errno on failure.
4108 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4109 void *data
, struct drm_file
*file_priv
)
4111 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4112 struct drm_mode_obj_set_property obj_set_prop
= {
4113 .value
= conn_set_prop
->value
,
4114 .prop_id
= conn_set_prop
->prop_id
,
4115 .obj_id
= conn_set_prop
->connector_id
,
4116 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4119 /* It does all the locking and checking we need */
4120 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4123 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4124 struct drm_property
*property
,
4128 struct drm_connector
*connector
= obj_to_connector(obj
);
4130 /* Do DPMS ourselves */
4131 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4132 if (connector
->funcs
->dpms
)
4133 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4135 } else if (connector
->funcs
->set_property
)
4136 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4138 /* store the property value if successful */
4140 drm_object_property_set_value(&connector
->base
, property
, value
);
4144 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4145 struct drm_property
*property
,
4149 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4151 if (crtc
->funcs
->set_property
)
4152 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4154 drm_object_property_set_value(obj
, property
, value
);
4159 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
4160 struct drm_property
*property
,
4164 struct drm_plane
*plane
= obj_to_plane(obj
);
4166 if (plane
->funcs
->set_property
)
4167 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4169 drm_object_property_set_value(obj
, property
, value
);
4175 * drm_mode_getproperty_ioctl - get the current value of a object's property
4178 * @file_priv: DRM file info
4180 * This function retrieves the current value for an object's property. Compared
4181 * to the connector specific ioctl this one is extended to also work on crtc and
4184 * Called by the user via ioctl.
4187 * Zero on success, errno on failure.
4189 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4190 struct drm_file
*file_priv
)
4192 struct drm_mode_obj_get_properties
*arg
= data
;
4193 struct drm_mode_object
*obj
;
4197 int props_count
= 0;
4198 uint32_t __user
*props_ptr
;
4199 uint64_t __user
*prop_values_ptr
;
4201 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4204 drm_modeset_lock_all(dev
);
4206 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4211 if (!obj
->properties
) {
4216 props_count
= obj
->properties
->count
;
4218 /* This ioctl is called twice, once to determine how much space is
4219 * needed, and the 2nd time to fill it. */
4220 if ((arg
->count_props
>= props_count
) && props_count
) {
4222 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
4223 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
4224 (arg
->prop_values_ptr
);
4225 for (i
= 0; i
< props_count
; i
++) {
4226 if (put_user(obj
->properties
->ids
[i
],
4227 props_ptr
+ copied
)) {
4231 if (put_user(obj
->properties
->values
[i
],
4232 prop_values_ptr
+ copied
)) {
4239 arg
->count_props
= props_count
;
4241 drm_modeset_unlock_all(dev
);
4246 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4249 * @file_priv: DRM file info
4251 * This function sets the current value for an object's property. It also calls
4252 * into a driver's ->set_property callback to update the hardware state.
4253 * Compared to the connector specific ioctl this one is extended to also work on
4254 * crtc and plane objects.
4256 * Called by the user via ioctl.
4259 * Zero on success, errno on failure.
4261 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4262 struct drm_file
*file_priv
)
4264 struct drm_mode_obj_set_property
*arg
= data
;
4265 struct drm_mode_object
*arg_obj
;
4266 struct drm_mode_object
*prop_obj
;
4267 struct drm_property
*property
;
4271 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4274 drm_modeset_lock_all(dev
);
4276 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4281 if (!arg_obj
->properties
)
4284 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4285 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4288 if (i
== arg_obj
->properties
->count
)
4291 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4292 DRM_MODE_OBJECT_PROPERTY
);
4297 property
= obj_to_property(prop_obj
);
4299 if (!drm_property_change_is_valid(property
, arg
->value
))
4302 switch (arg_obj
->type
) {
4303 case DRM_MODE_OBJECT_CONNECTOR
:
4304 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4307 case DRM_MODE_OBJECT_CRTC
:
4308 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4310 case DRM_MODE_OBJECT_PLANE
:
4311 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4316 drm_modeset_unlock_all(dev
);
4321 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4322 * @connector: connector to attach
4323 * @encoder: encoder to attach @connector to
4325 * This function links up a connector to an encoder. Note that the routing
4326 * restrictions between encoders and crtcs are exposed to userspace through the
4327 * possible_clones and possible_crtcs bitmasks.
4330 * Zero on success, errno on failure.
4332 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4333 struct drm_encoder
*encoder
)
4337 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4338 if (connector
->encoder_ids
[i
] == 0) {
4339 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4345 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4348 * drm_mode_crtc_set_gamma_size - set the gamma table size
4349 * @crtc: CRTC to set the gamma table size for
4350 * @gamma_size: size of the gamma table
4352 * Drivers which support gamma tables should set this to the supported gamma
4353 * table size when initializing the CRTC. Currently the drm core only supports a
4354 * fixed gamma table size.
4357 * Zero on success, errno on failure.
4359 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4362 crtc
->gamma_size
= gamma_size
;
4364 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4365 if (!crtc
->gamma_store
) {
4366 crtc
->gamma_size
= 0;
4372 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4375 * drm_mode_gamma_set_ioctl - set the gamma table
4378 * @file_priv: DRM file info
4380 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4381 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4383 * Called by the user via ioctl.
4386 * Zero on success, errno on failure.
4388 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4389 void *data
, struct drm_file
*file_priv
)
4391 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4392 struct drm_crtc
*crtc
;
4393 void *r_base
, *g_base
, *b_base
;
4397 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4400 drm_modeset_lock_all(dev
);
4401 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4407 if (crtc
->funcs
->gamma_set
== NULL
) {
4412 /* memcpy into gamma store */
4413 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4418 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4419 r_base
= crtc
->gamma_store
;
4420 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4425 g_base
= r_base
+ size
;
4426 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4431 b_base
= g_base
+ size
;
4432 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4437 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4440 drm_modeset_unlock_all(dev
);
4446 * drm_mode_gamma_get_ioctl - get the gamma table
4449 * @file_priv: DRM file info
4451 * Copy the current gamma table into the storage provided. This also provides
4452 * the gamma table size the driver expects, which can be used to size the
4453 * allocated storage.
4455 * Called by the user via ioctl.
4458 * Zero on success, errno on failure.
4460 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4461 void *data
, struct drm_file
*file_priv
)
4463 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4464 struct drm_crtc
*crtc
;
4465 void *r_base
, *g_base
, *b_base
;
4469 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4472 drm_modeset_lock_all(dev
);
4473 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4479 /* memcpy into gamma store */
4480 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4485 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4486 r_base
= crtc
->gamma_store
;
4487 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4492 g_base
= r_base
+ size
;
4493 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4498 b_base
= g_base
+ size
;
4499 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4504 drm_modeset_unlock_all(dev
);
4509 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4512 * @file_priv: DRM file info
4514 * This schedules an asynchronous update on a given CRTC, called page flip.
4515 * Optionally a drm event is generated to signal the completion of the event.
4516 * Generic drivers cannot assume that a pageflip with changed framebuffer
4517 * properties (including driver specific metadata like tiling layout) will work,
4518 * but some drivers support e.g. pixel format changes through the pageflip
4521 * Called by the user via ioctl.
4524 * Zero on success, errno on failure.
4526 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4527 void *data
, struct drm_file
*file_priv
)
4529 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4530 struct drm_crtc
*crtc
;
4531 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4532 struct drm_pending_vblank_event
*e
= NULL
;
4533 unsigned long flags
;
4536 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4537 page_flip
->reserved
!= 0)
4540 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4543 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4547 drm_modeset_lock(&crtc
->mutex
, NULL
);
4548 if (crtc
->primary
->fb
== NULL
) {
4549 /* The framebuffer is currently unbound, presumably
4550 * due to a hotplug event, that userspace has not
4557 if (crtc
->funcs
->page_flip
== NULL
)
4560 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4566 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4570 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4571 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4576 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4578 spin_lock_irqsave(&dev
->event_lock
, flags
);
4579 if (file_priv
->event_space
< sizeof e
->event
) {
4580 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4583 file_priv
->event_space
-= sizeof e
->event
;
4584 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4586 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4588 spin_lock_irqsave(&dev
->event_lock
, flags
);
4589 file_priv
->event_space
+= sizeof e
->event
;
4590 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4594 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4595 e
->event
.base
.length
= sizeof e
->event
;
4596 e
->event
.user_data
= page_flip
->user_data
;
4597 e
->base
.event
= &e
->event
.base
;
4598 e
->base
.file_priv
= file_priv
;
4600 (void (*) (struct drm_pending_event
*)) kfree
;
4603 old_fb
= crtc
->primary
->fb
;
4604 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4606 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4607 spin_lock_irqsave(&dev
->event_lock
, flags
);
4608 file_priv
->event_space
+= sizeof e
->event
;
4609 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4612 /* Keep the old fb, don't unref it. */
4616 * Warn if the driver hasn't properly updated the crtc->fb
4617 * field to reflect that the new framebuffer is now used.
4618 * Failing to do so will screw with the reference counting
4621 WARN_ON(crtc
->primary
->fb
!= fb
);
4622 /* Unref only the old framebuffer. */
4628 drm_framebuffer_unreference(fb
);
4630 drm_framebuffer_unreference(old_fb
);
4631 drm_modeset_unlock(&crtc
->mutex
);
4637 * drm_mode_config_reset - call ->reset callbacks
4640 * This functions calls all the crtc's, encoder's and connector's ->reset
4641 * callback. Drivers can use this in e.g. their driver load or resume code to
4642 * reset hardware and software state.
4644 void drm_mode_config_reset(struct drm_device
*dev
)
4646 struct drm_crtc
*crtc
;
4647 struct drm_encoder
*encoder
;
4648 struct drm_connector
*connector
;
4650 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4651 if (crtc
->funcs
->reset
)
4652 crtc
->funcs
->reset(crtc
);
4654 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4655 if (encoder
->funcs
->reset
)
4656 encoder
->funcs
->reset(encoder
);
4658 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4659 connector
->status
= connector_status_unknown
;
4661 if (connector
->funcs
->reset
)
4662 connector
->funcs
->reset(connector
);
4665 EXPORT_SYMBOL(drm_mode_config_reset
);
4668 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4671 * @file_priv: DRM file info
4673 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4674 * TTM or something else entirely) and returns the resulting buffer handle. This
4675 * handle can then be wrapped up into a framebuffer modeset object.
4677 * Note that userspace is not allowed to use such objects for render
4678 * acceleration - drivers must create their own private ioctls for such a use
4681 * Called by the user via ioctl.
4684 * Zero on success, errno on failure.
4686 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4687 void *data
, struct drm_file
*file_priv
)
4689 struct drm_mode_create_dumb
*args
= data
;
4690 u32 cpp
, stride
, size
;
4692 if (!dev
->driver
->dumb_create
)
4694 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4697 /* overflow checks for 32bit size calculations */
4698 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4699 if (cpp
> 0xffffffffU
/ args
->width
)
4701 stride
= cpp
* args
->width
;
4702 if (args
->height
> 0xffffffffU
/ stride
)
4705 /* test for wrap-around */
4706 size
= args
->height
* stride
;
4707 if (PAGE_ALIGN(size
) == 0)
4710 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4714 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4717 * @file_priv: DRM file info
4719 * Allocate an offset in the drm device node's address space to be able to
4720 * memory map a dumb buffer.
4722 * Called by the user via ioctl.
4725 * Zero on success, errno on failure.
4727 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4728 void *data
, struct drm_file
*file_priv
)
4730 struct drm_mode_map_dumb
*args
= data
;
4732 /* call driver ioctl to get mmap offset */
4733 if (!dev
->driver
->dumb_map_offset
)
4736 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4740 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4743 * @file_priv: DRM file info
4745 * This destroys the userspace handle for the given dumb backing storage buffer.
4746 * Since buffer objects must be reference counted in the kernel a buffer object
4747 * won't be immediately freed if a framebuffer modeset object still uses it.
4749 * Called by the user via ioctl.
4752 * Zero on success, errno on failure.
4754 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4755 void *data
, struct drm_file
*file_priv
)
4757 struct drm_mode_destroy_dumb
*args
= data
;
4759 if (!dev
->driver
->dumb_destroy
)
4762 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4766 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4767 * @format: pixel format (DRM_FORMAT_*)
4768 * @depth: storage for the depth value
4769 * @bpp: storage for the bpp value
4771 * This only supports RGB formats here for compat with code that doesn't use
4772 * pixel formats directly yet.
4774 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4779 case DRM_FORMAT_RGB332
:
4780 case DRM_FORMAT_BGR233
:
4784 case DRM_FORMAT_XRGB1555
:
4785 case DRM_FORMAT_XBGR1555
:
4786 case DRM_FORMAT_RGBX5551
:
4787 case DRM_FORMAT_BGRX5551
:
4788 case DRM_FORMAT_ARGB1555
:
4789 case DRM_FORMAT_ABGR1555
:
4790 case DRM_FORMAT_RGBA5551
:
4791 case DRM_FORMAT_BGRA5551
:
4795 case DRM_FORMAT_RGB565
:
4796 case DRM_FORMAT_BGR565
:
4800 case DRM_FORMAT_RGB888
:
4801 case DRM_FORMAT_BGR888
:
4805 case DRM_FORMAT_XRGB8888
:
4806 case DRM_FORMAT_XBGR8888
:
4807 case DRM_FORMAT_RGBX8888
:
4808 case DRM_FORMAT_BGRX8888
:
4812 case DRM_FORMAT_XRGB2101010
:
4813 case DRM_FORMAT_XBGR2101010
:
4814 case DRM_FORMAT_RGBX1010102
:
4815 case DRM_FORMAT_BGRX1010102
:
4816 case DRM_FORMAT_ARGB2101010
:
4817 case DRM_FORMAT_ABGR2101010
:
4818 case DRM_FORMAT_RGBA1010102
:
4819 case DRM_FORMAT_BGRA1010102
:
4823 case DRM_FORMAT_ARGB8888
:
4824 case DRM_FORMAT_ABGR8888
:
4825 case DRM_FORMAT_RGBA8888
:
4826 case DRM_FORMAT_BGRA8888
:
4831 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4832 drm_get_format_name(format
));
4838 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4841 * drm_format_num_planes - get the number of planes for format
4842 * @format: pixel format (DRM_FORMAT_*)
4845 * The number of planes used by the specified pixel format.
4847 int drm_format_num_planes(uint32_t format
)
4850 case DRM_FORMAT_YUV410
:
4851 case DRM_FORMAT_YVU410
:
4852 case DRM_FORMAT_YUV411
:
4853 case DRM_FORMAT_YVU411
:
4854 case DRM_FORMAT_YUV420
:
4855 case DRM_FORMAT_YVU420
:
4856 case DRM_FORMAT_YUV422
:
4857 case DRM_FORMAT_YVU422
:
4858 case DRM_FORMAT_YUV444
:
4859 case DRM_FORMAT_YVU444
:
4861 case DRM_FORMAT_NV12
:
4862 case DRM_FORMAT_NV21
:
4863 case DRM_FORMAT_NV16
:
4864 case DRM_FORMAT_NV61
:
4865 case DRM_FORMAT_NV24
:
4866 case DRM_FORMAT_NV42
:
4872 EXPORT_SYMBOL(drm_format_num_planes
);
4875 * drm_format_plane_cpp - determine the bytes per pixel value
4876 * @format: pixel format (DRM_FORMAT_*)
4877 * @plane: plane index
4880 * The bytes per pixel value for the specified plane.
4882 int drm_format_plane_cpp(uint32_t format
, int plane
)
4887 if (plane
>= drm_format_num_planes(format
))
4891 case DRM_FORMAT_YUYV
:
4892 case DRM_FORMAT_YVYU
:
4893 case DRM_FORMAT_UYVY
:
4894 case DRM_FORMAT_VYUY
:
4896 case DRM_FORMAT_NV12
:
4897 case DRM_FORMAT_NV21
:
4898 case DRM_FORMAT_NV16
:
4899 case DRM_FORMAT_NV61
:
4900 case DRM_FORMAT_NV24
:
4901 case DRM_FORMAT_NV42
:
4902 return plane
? 2 : 1;
4903 case DRM_FORMAT_YUV410
:
4904 case DRM_FORMAT_YVU410
:
4905 case DRM_FORMAT_YUV411
:
4906 case DRM_FORMAT_YVU411
:
4907 case DRM_FORMAT_YUV420
:
4908 case DRM_FORMAT_YVU420
:
4909 case DRM_FORMAT_YUV422
:
4910 case DRM_FORMAT_YVU422
:
4911 case DRM_FORMAT_YUV444
:
4912 case DRM_FORMAT_YVU444
:
4915 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4919 EXPORT_SYMBOL(drm_format_plane_cpp
);
4922 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4923 * @format: pixel format (DRM_FORMAT_*)
4926 * The horizontal chroma subsampling factor for the
4927 * specified pixel format.
4929 int drm_format_horz_chroma_subsampling(uint32_t format
)
4932 case DRM_FORMAT_YUV411
:
4933 case DRM_FORMAT_YVU411
:
4934 case DRM_FORMAT_YUV410
:
4935 case DRM_FORMAT_YVU410
:
4937 case DRM_FORMAT_YUYV
:
4938 case DRM_FORMAT_YVYU
:
4939 case DRM_FORMAT_UYVY
:
4940 case DRM_FORMAT_VYUY
:
4941 case DRM_FORMAT_NV12
:
4942 case DRM_FORMAT_NV21
:
4943 case DRM_FORMAT_NV16
:
4944 case DRM_FORMAT_NV61
:
4945 case DRM_FORMAT_YUV422
:
4946 case DRM_FORMAT_YVU422
:
4947 case DRM_FORMAT_YUV420
:
4948 case DRM_FORMAT_YVU420
:
4954 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4957 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4958 * @format: pixel format (DRM_FORMAT_*)
4961 * The vertical chroma subsampling factor for the
4962 * specified pixel format.
4964 int drm_format_vert_chroma_subsampling(uint32_t format
)
4967 case DRM_FORMAT_YUV410
:
4968 case DRM_FORMAT_YVU410
:
4970 case DRM_FORMAT_YUV420
:
4971 case DRM_FORMAT_YVU420
:
4972 case DRM_FORMAT_NV12
:
4973 case DRM_FORMAT_NV21
:
4979 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4982 * drm_rotation_simplify() - Try to simplify the rotation
4983 * @rotation: Rotation to be simplified
4984 * @supported_rotations: Supported rotations
4986 * Attempt to simplify the rotation to a form that is supported.
4987 * Eg. if the hardware supports everything except DRM_REFLECT_X
4988 * one could call this function like this:
4990 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
4991 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
4992 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
4994 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4995 * transforms the hardware supports, this function may not
4996 * be able to produce a supported transform, so the caller should
4997 * check the result afterwards.
4999 unsigned int drm_rotation_simplify(unsigned int rotation
,
5000 unsigned int supported_rotations
)
5002 if (rotation
& ~supported_rotations
) {
5003 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5004 rotation
= (rotation
& ~0xf) | BIT((ffs(rotation
& 0xf) + 1) % 4);
5009 EXPORT_SYMBOL(drm_rotation_simplify
);
5012 * drm_mode_config_init - initialize DRM mode_configuration structure
5015 * Initialize @dev's mode_config structure, used for tracking the graphics
5016 * configuration of @dev.
5018 * Since this initializes the modeset locks, no locking is possible. Which is no
5019 * problem, since this should happen single threaded at init time. It is the
5020 * driver's problem to ensure this guarantee.
5023 void drm_mode_config_init(struct drm_device
*dev
)
5025 mutex_init(&dev
->mode_config
.mutex
);
5026 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5027 mutex_init(&dev
->mode_config
.idr_mutex
);
5028 mutex_init(&dev
->mode_config
.fb_lock
);
5029 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5030 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5031 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5032 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
5033 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5034 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5035 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5036 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5037 idr_init(&dev
->mode_config
.crtc_idr
);
5039 drm_modeset_lock_all(dev
);
5040 drm_mode_create_standard_connector_properties(dev
);
5041 drm_mode_create_standard_plane_properties(dev
);
5042 drm_modeset_unlock_all(dev
);
5044 /* Just to be sure */
5045 dev
->mode_config
.num_fb
= 0;
5046 dev
->mode_config
.num_connector
= 0;
5047 dev
->mode_config
.num_crtc
= 0;
5048 dev
->mode_config
.num_encoder
= 0;
5049 dev
->mode_config
.num_overlay_plane
= 0;
5050 dev
->mode_config
.num_total_plane
= 0;
5052 EXPORT_SYMBOL(drm_mode_config_init
);
5055 * drm_mode_config_cleanup - free up DRM mode_config info
5058 * Free up all the connectors and CRTCs associated with this DRM device, then
5059 * free up the framebuffers and associated buffer objects.
5061 * Note that since this /should/ happen single-threaded at driver/device
5062 * teardown time, no locking is required. It's the driver's job to ensure that
5063 * this guarantee actually holds true.
5065 * FIXME: cleanup any dangling user buffer objects too
5067 void drm_mode_config_cleanup(struct drm_device
*dev
)
5069 struct drm_connector
*connector
, *ot
;
5070 struct drm_crtc
*crtc
, *ct
;
5071 struct drm_encoder
*encoder
, *enct
;
5072 struct drm_bridge
*bridge
, *brt
;
5073 struct drm_framebuffer
*fb
, *fbt
;
5074 struct drm_property
*property
, *pt
;
5075 struct drm_property_blob
*blob
, *bt
;
5076 struct drm_plane
*plane
, *plt
;
5078 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5080 encoder
->funcs
->destroy(encoder
);
5083 list_for_each_entry_safe(bridge
, brt
,
5084 &dev
->mode_config
.bridge_list
, head
) {
5085 bridge
->funcs
->destroy(bridge
);
5088 list_for_each_entry_safe(connector
, ot
,
5089 &dev
->mode_config
.connector_list
, head
) {
5090 connector
->funcs
->destroy(connector
);
5093 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5095 drm_property_destroy(dev
, property
);
5098 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5100 drm_property_destroy_blob(dev
, blob
);
5104 * Single-threaded teardown context, so it's not required to grab the
5105 * fb_lock to protect against concurrent fb_list access. Contrary, it
5106 * would actually deadlock with the drm_framebuffer_cleanup function.
5108 * Also, if there are any framebuffers left, that's a driver leak now,
5109 * so politely WARN about this.
5111 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5112 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5113 drm_framebuffer_remove(fb
);
5116 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5118 plane
->funcs
->destroy(plane
);
5121 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5122 crtc
->funcs
->destroy(crtc
);
5125 idr_destroy(&dev
->mode_config
.crtc_idr
);
5126 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5128 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5130 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5131 unsigned int supported_rotations
)
5133 static const struct drm_prop_enum_list props
[] = {
5134 { DRM_ROTATE_0
, "rotate-0" },
5135 { DRM_ROTATE_90
, "rotate-90" },
5136 { DRM_ROTATE_180
, "rotate-180" },
5137 { DRM_ROTATE_270
, "rotate-270" },
5138 { DRM_REFLECT_X
, "reflect-x" },
5139 { DRM_REFLECT_Y
, "reflect-y" },
5142 return drm_property_create_bitmask(dev
, 0, "rotation",
5143 props
, ARRAY_SIZE(props
),
5144 supported_rotations
);
5146 EXPORT_SYMBOL(drm_mode_create_rotation_property
);