]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/drm_crtc.c
drm/fsl-dcu: use drm_mode_config_cleanup on initialization errors
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
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.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
6ba6d03e 32#include <linux/ctype.h>
f453ba04 33#include <linux/list.h>
5a0e3ad6 34#include <linux/slab.h>
2d1a8a48 35#include <linux/export.h>
760285e7
DH
36#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
51fd371b 40#include <drm/drm_modeset_lock.h>
88a48e29 41#include <drm/drm_atomic.h>
f453ba04 42
8bd441b2 43#include "drm_crtc_internal.h"
67d0ec4e 44#include "drm_internal.h"
8bd441b2 45
9a6f5130
CW
46static struct drm_framebuffer *
47internal_framebuffer_create(struct drm_device *dev,
1eb83451 48 const struct drm_mode_fb_cmd2 *r,
9a6f5130 49 struct drm_file *file_priv);
c394c2b0 50
f453ba04
DA
51/* Avoid boilerplate. I'm tired of typing. */
52#define DRM_ENUM_NAME_FN(fnname, list) \
d20d3174 53 const char *fnname(int val) \
f453ba04
DA
54 { \
55 int i; \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
59 } \
60 return "(unknown)"; \
61 }
62
63/*
64 * Global properties
65 */
4dfd909f
TR
66static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
f453ba04
DA
68 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
71};
72
73DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
74
4dfd909f 75static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
9922ab5a
RC
76 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
79};
80
f453ba04
DA
81/*
82 * Optional properties
83 */
4dfd909f 84static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
53bd8389
JB
85 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
f453ba04
DA
89};
90
ff587e45
VK
91static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
95};
96
f453ba04
DA
97/*
98 * Non-global properties, but "required" for certain connectors.
99 */
4dfd909f 100static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
f453ba04
DA
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104};
105
106DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
107
4dfd909f 108static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
f453ba04
DA
109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
112};
113
114DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
116
4dfd909f 117static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
f453ba04
DA
118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
aeaa1ad3 122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
f453ba04
DA
123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
126
4dfd909f 127static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
f453ba04
DA
128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
aeaa1ad3 132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
f453ba04
DA
133};
134
135DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
137
d20d3174 138static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
884840aa
JB
139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
142};
143
f453ba04
DA
144struct drm_conn_prop_enum_list {
145 int type;
d20d3174 146 const char *name;
b21e3afe 147 struct ida ida;
f453ba04
DA
148};
149
150/*
151 * Connector and encoder types.
152 */
4dfd909f
TR
153static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
b21e3afe
IM
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
b8923273 170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
0b27c02a 171 { DRM_MODE_CONNECTOR_DPI, "DPI" },
f453ba04
DA
172};
173
4dfd909f
TR
174static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
175 { DRM_MODE_ENCODER_NONE, "None" },
f453ba04
DA
176 { DRM_MODE_ENCODER_DAC, "DAC" },
177 { DRM_MODE_ENCODER_TMDS, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC, "TV" },
a7331e5c 180 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
b8923273 181 { DRM_MODE_ENCODER_DSI, "DSI" },
182407a6 182 { DRM_MODE_ENCODER_DPMST, "DP MST" },
0b27c02a 183 { DRM_MODE_ENCODER_DPI, "DPI" },
f453ba04
DA
184};
185
4dfd909f 186static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
ac1bb36c
JB
187 { SubPixelUnknown, "Unknown" },
188 { SubPixelHorizontalRGB, "Horizontal RGB" },
189 { SubPixelHorizontalBGR, "Horizontal BGR" },
190 { SubPixelVerticalRGB, "Vertical RGB" },
191 { SubPixelVerticalBGR, "Vertical BGR" },
192 { SubPixelNone, "None" },
193};
194
b21e3afe
IM
195void drm_connector_ida_init(void)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
200 ida_init(&drm_connector_enum_list[i].ida);
201}
202
203void drm_connector_ida_destroy(void)
204{
205 int i;
206
207 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
208 ida_destroy(&drm_connector_enum_list[i].ida);
209}
210
c8e32cc1
DV
211/**
212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
214 *
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
217 */
d20d3174 218const char *drm_get_connector_status_name(enum drm_connector_status status)
f453ba04
DA
219{
220 if (status == connector_status_connected)
221 return "connected";
222 else if (status == connector_status_disconnected)
223 return "disconnected";
224 else
225 return "unknown";
226}
ed7951dc 227EXPORT_SYMBOL(drm_get_connector_status_name);
f453ba04 228
ac1bb36c
JB
229/**
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
232 *
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
235 */
236const char *drm_get_subpixel_order_name(enum subpixel_order order)
237{
238 return drm_subpixel_enum_list[order].name;
239}
240EXPORT_SYMBOL(drm_get_subpixel_order_name);
241
2ee39452
DA
242/*
243 * Internal function to assign a slot in the object idr and optionally
244 * register the object into the idr.
245 */
246static int drm_mode_object_get_reg(struct drm_device *dev,
247 struct drm_mode_object *obj,
248 uint32_t obj_type,
d0f37cf6
DA
249 bool register_obj,
250 void (*obj_free_cb)(struct kref *kref))
2ee39452
DA
251{
252 int ret;
253
254 mutex_lock(&dev->mode_config.idr_mutex);
255 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
256 if (ret >= 0) {
257 /*
258 * Set up the object linking under the protection of the idr
259 * lock so that other users can't see inconsistent state.
260 */
261 obj->id = ret;
262 obj->type = obj_type;
d0f37cf6
DA
263 if (obj_free_cb) {
264 obj->free_cb = obj_free_cb;
265 kref_init(&obj->refcount);
266 }
2ee39452
DA
267 }
268 mutex_unlock(&dev->mode_config.idr_mutex);
269
270 return ret < 0 ? ret : 0;
271}
272
f453ba04 273/**
065a50ed 274 * drm_mode_object_get - allocate a new modeset identifier
f453ba04 275 * @dev: DRM device
065a50ed
DV
276 * @obj: object pointer, used to generate unique ID
277 * @obj_type: object type
f453ba04 278 *
f453ba04 279 * Create a unique identifier based on @ptr in @dev's identifier space. Used
c8e32cc1
DV
280 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
281 * modeset identifiers are _not_ reference counted. Hence don't use this for
282 * reference counted modeset objects like framebuffers.
f453ba04 283 *
c8e32cc1 284 * Returns:
3c67d839 285 * Zero on success, error code on failure.
f453ba04 286 */
8bd441b2
DV
287int drm_mode_object_get(struct drm_device *dev,
288 struct drm_mode_object *obj, uint32_t obj_type)
f453ba04 289{
d0f37cf6 290 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
2ee39452 291}
f453ba04 292
2ee39452
DA
293static void drm_mode_object_register(struct drm_device *dev,
294 struct drm_mode_object *obj)
295{
ad2563c2 296 mutex_lock(&dev->mode_config.idr_mutex);
2ee39452 297 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
ad2563c2 298 mutex_unlock(&dev->mode_config.idr_mutex);
f453ba04
DA
299}
300
301/**
7c8f6d25 302 * drm_mode_object_unregister - free a modeset identifer
f453ba04 303 * @dev: DRM device
065a50ed 304 * @object: object to free
f453ba04 305 *
7c8f6d25
DA
306 * Free @id from @dev's unique identifier pool.
307 * This function can be called multiple times, and guards against
308 * multiple removals.
309 * These modeset identifiers are _not_ reference counted. Hence don't use this
c8e32cc1 310 * for reference counted modeset objects like framebuffers.
f453ba04 311 */
7c8f6d25 312void drm_mode_object_unregister(struct drm_device *dev,
8bd441b2 313 struct drm_mode_object *object)
f453ba04 314{
ad2563c2 315 mutex_lock(&dev->mode_config.idr_mutex);
7c8f6d25
DA
316 if (object->id) {
317 idr_remove(&dev->mode_config.crtc_idr, object->id);
318 object->id = 0;
319 }
ad2563c2 320 mutex_unlock(&dev->mode_config.idr_mutex);
f453ba04
DA
321}
322
98f75de4
RC
323static struct drm_mode_object *_object_find(struct drm_device *dev,
324 uint32_t id, uint32_t type)
325{
326 struct drm_mode_object *obj = NULL;
327
328 mutex_lock(&dev->mode_config.idr_mutex);
329 obj = idr_find(&dev->mode_config.crtc_idr, id);
168c02ec
DV
330 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
331 obj = NULL;
332 if (obj && obj->id != id)
333 obj = NULL;
72fe90b8
DA
334
335 if (obj && obj->free_cb) {
336 if (!kref_get_unless_zero(&obj->refcount))
337 obj = NULL;
338 }
98f75de4
RC
339 mutex_unlock(&dev->mode_config.idr_mutex);
340
341 return obj;
342}
343
786b99ed
DV
344/**
345 * drm_mode_object_find - look up a drm object with static lifetime
346 * @dev: drm device
347 * @id: id of the mode object
348 * @type: type of the mode object
349 *
05981422
DV
350 * This function is used to look up a modeset object. It will acquire a
351 * reference for reference counted objects. This reference must be dropped again
352 * by callind drm_mode_object_unreference().
786b99ed 353 */
7a9c9060
DV
354struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
355 uint32_t id, uint32_t type)
f453ba04 356{
ad2563c2 357 struct drm_mode_object *obj = NULL;
f453ba04 358
98f75de4 359 obj = _object_find(dev, id, type);
f453ba04
DA
360 return obj;
361}
362EXPORT_SYMBOL(drm_mode_object_find);
363
05981422
DV
364/**
365 * drm_mode_object_unreference - decr the object refcnt
366 * @obj: mode_object
367 *
368 * This functions decrements the object's refcount if it is a refcounted modeset
369 * object. It is a no-op on any other object. This is used to drop references
370 * acquired with drm_mode_object_reference().
371 */
d0f37cf6
DA
372void drm_mode_object_unreference(struct drm_mode_object *obj)
373{
374 if (obj->free_cb) {
375 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
376 kref_put(&obj->refcount, obj->free_cb);
377 }
378}
379EXPORT_SYMBOL(drm_mode_object_unreference);
380
381/**
05981422 382 * drm_mode_object_reference - incr the object refcnt
d0f37cf6
DA
383 * @obj: mode_object
384 *
05981422
DV
385 * This functions increments the object's refcount if it is a refcounted modeset
386 * object. It is a no-op on any other object. References should be dropped again
387 * by calling drm_mode_object_unreference().
d0f37cf6
DA
388 */
389void drm_mode_object_reference(struct drm_mode_object *obj)
390{
391 if (obj->free_cb) {
392 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
393 kref_get(&obj->refcount);
394 }
395}
396EXPORT_SYMBOL(drm_mode_object_reference);
397
f55f1f91
DA
398static void drm_framebuffer_free(struct kref *kref)
399{
400 struct drm_framebuffer *fb =
d0f37cf6 401 container_of(kref, struct drm_framebuffer, base.refcount);
f55f1f91
DA
402 struct drm_device *dev = fb->dev;
403
404 /*
405 * The lookup idr holds a weak reference, which has not necessarily been
406 * removed at this point. Check for that.
407 */
19ab3f8b 408 drm_mode_object_unregister(dev, &fb->base);
f55f1f91
DA
409
410 fb->funcs->destroy(fb);
411}
412
f453ba04
DA
413/**
414 * drm_framebuffer_init - initialize a framebuffer
415 * @dev: DRM device
065a50ed
DV
416 * @fb: framebuffer to be initialized
417 * @funcs: ... with these functions
f453ba04 418 *
f453ba04
DA
419 * Allocates an ID for the framebuffer's parent mode object, sets its mode
420 * functions & device file and adds it to the master fd list.
421 *
4b096ac1
DV
422 * IMPORTANT:
423 * This functions publishes the fb and makes it available for concurrent access
424 * by other users. Which means by this point the fb _must_ be fully set up -
425 * since all the fb attributes are invariant over its lifetime, no further
426 * locking but only correct reference counting is required.
427 *
c8e32cc1 428 * Returns:
af901ca1 429 * Zero on success, error code on failure.
f453ba04
DA
430 */
431int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
432 const struct drm_framebuffer_funcs *funcs)
433{
434 int ret;
435
4b096ac1
DV
436 INIT_LIST_HEAD(&fb->filp_head);
437 fb->dev = dev;
438 fb->funcs = funcs;
f7eff60e 439
d0f37cf6 440 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
9cd47424 441 false, drm_framebuffer_free);
6bfc56aa 442 if (ret)
4b096ac1 443 goto out;
f453ba04 444
9cd47424 445 mutex_lock(&dev->mode_config.fb_lock);
f453ba04
DA
446 dev->mode_config.num_fb++;
447 list_add(&fb->head, &dev->mode_config.fb_list);
2ddea3fd 448 mutex_unlock(&dev->mode_config.fb_lock);
f453ba04 449
9cd47424 450 drm_mode_object_register(dev, &fb->base);
9cd47424 451out:
3c67d839 452 return ret;
f453ba04
DA
453}
454EXPORT_SYMBOL(drm_framebuffer_init);
455
786b99ed
DV
456/**
457 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
458 * @dev: drm device
459 * @id: id of the fb object
460 *
461 * If successful, this grabs an additional reference to the framebuffer -
462 * callers need to make sure to eventually unreference the returned framebuffer
c8e32cc1 463 * again, using @drm_framebuffer_unreference.
786b99ed
DV
464 */
465struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
466 uint32_t id)
467{
cee26ac4
DA
468 struct drm_mode_object *obj;
469 struct drm_framebuffer *fb = NULL;
786b99ed 470
cee26ac4 471 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
72fe90b8 472 if (obj)
cee26ac4 473 fb = obj_to_fb(obj);
786b99ed
DV
474 return fb;
475}
476EXPORT_SYMBOL(drm_framebuffer_lookup);
477
36206361
DV
478/**
479 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
480 * @fb: fb to unregister
481 *
482 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
483 * those used for fbdev. Note that the caller must hold a reference of it's own,
484 * i.e. the object may not be destroyed through this call (since it'll lead to a
485 * locking inversion).
486 */
487void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
488{
a39a357c
DV
489 struct drm_device *dev;
490
491 if (!fb)
492 return;
493
494 dev = fb->dev;
2b677e8c 495
2b677e8c 496 /* Mark fb as reaped and drop idr ref. */
19ab3f8b 497 drm_mode_object_unregister(dev, &fb->base);
36206361
DV
498}
499EXPORT_SYMBOL(drm_framebuffer_unregister_private);
500
f453ba04
DA
501/**
502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
504 *
c8e32cc1
DV
505 * Cleanup framebuffer. This function is intended to be used from the drivers
506 * ->destroy callback. It can also be used to clean up driver private
2e7a5701 507 * framebuffers embedded into a larger structure.
36206361
DV
508 *
509 * Note that this function does not remove the fb from active usuage - if it is
510 * still used anywhere, hilarity can ensue since userspace could call getfb on
511 * the id and get back -EINVAL. Obviously no concern at driver unload time.
512 *
513 * Also, the framebuffer will not be removed from the lookup idr - for
514 * user-created framebuffers this will happen in in the rmfb ioctl. For
515 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
516 * drm_framebuffer_unregister_private.
f453ba04
DA
517 */
518void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
f7eff60e
RC
519{
520 struct drm_device *dev = fb->dev;
8faf6b18 521
4b096ac1 522 mutex_lock(&dev->mode_config.fb_lock);
f7eff60e
RC
523 list_del(&fb->head);
524 dev->mode_config.num_fb--;
4b096ac1 525 mutex_unlock(&dev->mode_config.fb_lock);
f7eff60e
RC
526}
527EXPORT_SYMBOL(drm_framebuffer_cleanup);
528
529/**
530 * drm_framebuffer_remove - remove and unreference a framebuffer object
531 * @fb: framebuffer to remove
532 *
f7eff60e 533 * Scans all the CRTCs and planes in @dev's mode_config. If they're
36206361 534 * using @fb, removes it, setting it to NULL. Then drops the reference to the
b62584e3
DV
535 * passed-in framebuffer. Might take the modeset locks.
536 *
537 * Note that this function optimizes the cleanup away if the caller holds the
538 * last reference to the framebuffer. It is also guaranteed to not take the
539 * modeset locks in this case.
f7eff60e
RC
540 */
541void drm_framebuffer_remove(struct drm_framebuffer *fb)
f453ba04 542{
a39a357c 543 struct drm_device *dev;
f453ba04 544 struct drm_crtc *crtc;
8cf5c917 545 struct drm_plane *plane;
5ef5f72f
DA
546 struct drm_mode_set set;
547 int ret;
f453ba04 548
a39a357c
DV
549 if (!fb)
550 return;
551
552 dev = fb->dev;
553
4b096ac1 554 WARN_ON(!list_empty(&fb->filp_head));
8faf6b18 555
b62584e3
DV
556 /*
557 * drm ABI mandates that we remove any deleted framebuffers from active
558 * useage. But since most sane clients only remove framebuffers they no
559 * longer need, try to optimize this away.
560 *
561 * Since we're holding a reference ourselves, observing a refcount of 1
562 * means that we're the last holder and can skip it. Also, the refcount
563 * can never increase from 1 again, so we don't need any barriers or
564 * locks.
565 *
566 * Note that userspace could try to race with use and instate a new
567 * usage _after_ we've cleared all current ones. End result will be an
568 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
569 * in this manner.
570 */
747a598f 571 if (drm_framebuffer_read_refcount(fb) > 1) {
b62584e3
DV
572 drm_modeset_lock_all(dev);
573 /* remove from any CRTC */
6295d607 574 drm_for_each_crtc(crtc, dev) {
f4510a27 575 if (crtc->primary->fb == fb) {
b62584e3
DV
576 /* should turn off the crtc */
577 memset(&set, 0, sizeof(struct drm_mode_set));
578 set.crtc = crtc;
579 set.fb = NULL;
580 ret = drm_mode_set_config_internal(&set);
581 if (ret)
582 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
583 }
5ef5f72f 584 }
f453ba04 585
6295d607 586 drm_for_each_plane(plane, dev) {
9125e618
VS
587 if (plane->fb == fb)
588 drm_plane_force_disable(plane);
8cf5c917 589 }
b62584e3 590 drm_modeset_unlock_all(dev);
8cf5c917
JB
591 }
592
f7eff60e 593 drm_framebuffer_unreference(fb);
f453ba04 594}
f7eff60e 595EXPORT_SYMBOL(drm_framebuffer_remove);
f453ba04 596
51fd371b
RC
597DEFINE_WW_CLASS(crtc_ww_class);
598
fa3ab4c2
VS
599static unsigned int drm_num_crtcs(struct drm_device *dev)
600{
601 unsigned int num = 0;
602 struct drm_crtc *tmp;
603
604 drm_for_each_crtc(tmp, dev) {
605 num++;
606 }
607
608 return num;
609}
610
f453ba04 611/**
e13161af
MR
612 * drm_crtc_init_with_planes - Initialise a new CRTC object with
613 * specified primary and cursor planes.
f453ba04
DA
614 * @dev: DRM device
615 * @crtc: CRTC object to init
e13161af
MR
616 * @primary: Primary plane for CRTC
617 * @cursor: Cursor plane for CRTC
f453ba04 618 * @funcs: callbacks for the new CRTC
f9882876 619 * @name: printf style format string for the CRTC name, or NULL for default name
f453ba04 620 *
ad6f5c34 621 * Inits a new object created as base part of a driver crtc object.
6bfc56aa 622 *
c8e32cc1 623 * Returns:
6bfc56aa 624 * Zero on success, error code on failure.
f453ba04 625 */
e13161af
MR
626int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
627 struct drm_plane *primary,
fc1d3e44 628 struct drm_plane *cursor,
f9882876
VS
629 const struct drm_crtc_funcs *funcs,
630 const char *name, ...)
f453ba04 631{
51fd371b 632 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa
VS
633 int ret;
634
522cf91f
BG
635 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
636 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
637
f453ba04
DA
638 crtc->dev = dev;
639 crtc->funcs = funcs;
640
3b24f7d6
DV
641 INIT_LIST_HEAD(&crtc->commit_list);
642 spin_lock_init(&crtc->commit_lock);
643
51fd371b 644 drm_modeset_lock_init(&crtc->mutex);
6bfc56aa
VS
645 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
646 if (ret)
baf698b0 647 return ret;
f453ba04 648
fa3ab4c2
VS
649 if (name) {
650 va_list ap;
651
652 va_start(ap, name);
653 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
654 va_end(ap);
655 } else {
656 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
657 drm_num_crtcs(dev));
658 }
659 if (!crtc->name) {
7c8f6d25 660 drm_mode_object_unregister(dev, &crtc->base);
fa3ab4c2
VS
661 return -ENOMEM;
662 }
663
bffd9de0
PZ
664 crtc->base.properties = &crtc->properties;
665
51fd371b 666 list_add_tail(&crtc->head, &config->crtc_list);
490d3d1b 667 crtc->index = config->num_crtc++;
6bfc56aa 668
e13161af 669 crtc->primary = primary;
fc1d3e44 670 crtc->cursor = cursor;
e13161af
MR
671 if (primary)
672 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
fc1d3e44
MR
673 if (cursor)
674 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
e13161af 675
eab3bbef
DV
676 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
677 drm_object_attach_property(&crtc->base, config->prop_active, 0);
955f3c33 678 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
eab3bbef
DV
679 }
680
baf698b0 681 return 0;
f453ba04 682}
e13161af 683EXPORT_SYMBOL(drm_crtc_init_with_planes);
f453ba04
DA
684
685/**
ad6f5c34 686 * drm_crtc_cleanup - Clean up the core crtc usage
f453ba04
DA
687 * @crtc: CRTC to cleanup
688 *
ad6f5c34
VS
689 * This function cleans up @crtc and removes it from the DRM mode setting
690 * core. Note that the function does *not* free the crtc structure itself,
691 * this is the responsibility of the caller.
f453ba04
DA
692 */
693void drm_crtc_cleanup(struct drm_crtc *crtc)
694{
695 struct drm_device *dev = crtc->dev;
696
490d3d1b
CW
697 /* Note that the crtc_list is considered to be static; should we
698 * remove the drm_crtc at runtime we would have to decrement all
699 * the indices on the drm_crtc after us in the crtc_list.
700 */
701
9e1c156f
SK
702 kfree(crtc->gamma_store);
703 crtc->gamma_store = NULL;
f453ba04 704
51fd371b
RC
705 drm_modeset_lock_fini(&crtc->mutex);
706
7c8f6d25 707 drm_mode_object_unregister(dev, &crtc->base);
f453ba04
DA
708 list_del(&crtc->head);
709 dev->mode_config.num_crtc--;
3009c037
TR
710
711 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
712 if (crtc->state && crtc->funcs->atomic_destroy_state)
713 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
a18c0af1 714
fa3ab4c2
VS
715 kfree(crtc->name);
716
a18c0af1 717 memset(crtc, 0, sizeof(*crtc));
f453ba04
DA
718}
719EXPORT_SYMBOL(drm_crtc_cleanup);
720
86f422d5 721/*
f453ba04
DA
722 * drm_mode_remove - remove and free a mode
723 * @connector: connector list to modify
724 * @mode: mode to remove
725 *
f453ba04
DA
726 * Remove @mode from @connector's mode list, then free it.
727 */
86f422d5
LD
728static void drm_mode_remove(struct drm_connector *connector,
729 struct drm_display_mode *mode)
f453ba04
DA
730{
731 list_del(&mode->head);
554f1d78 732 drm_mode_destroy(connector->dev, mode);
f453ba04 733}
f453ba04 734
b5571e9d
BB
735/**
736 * drm_display_info_set_bus_formats - set the supported bus formats
737 * @info: display info to store bus formats in
e37bfa1a
BB
738 * @formats: array containing the supported bus formats
739 * @num_formats: the number of entries in the fmts array
b5571e9d
BB
740 *
741 * Store the supported bus formats in display info structure.
742 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
743 * a full list of available formats.
744 */
745int drm_display_info_set_bus_formats(struct drm_display_info *info,
746 const u32 *formats,
747 unsigned int num_formats)
748{
749 u32 *fmts = NULL;
750
751 if (!formats && num_formats)
752 return -EINVAL;
753
754 if (formats && num_formats) {
755 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
756 GFP_KERNEL);
944579c5 757 if (!fmts)
b5571e9d
BB
758 return -ENOMEM;
759 }
760
761 kfree(info->bus_formats);
762 info->bus_formats = fmts;
763 info->num_bus_formats = num_formats;
764
765 return 0;
766}
767EXPORT_SYMBOL(drm_display_info_set_bus_formats);
768
eaf99c74
CW
769/**
770 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
771 * @connector: connector to quwery
eaf99c74
CW
772 *
773 * The kernel supports per-connector configration of its consoles through
774 * use of the video= parameter. This function parses that option and
775 * extracts the user's specified mode (or enable/disable status) for a
776 * particular connector. This is typically only used during the early fbdev
777 * setup.
778 */
779static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
780{
781 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
782 char *option = NULL;
783
784 if (fb_get_options(connector->name, &option))
785 return;
786
787 if (!drm_mode_parse_command_line_for_connector(option,
788 connector,
789 mode))
790 return;
791
792 if (mode->force) {
793 const char *s;
794
795 switch (mode->force) {
796 case DRM_FORCE_OFF:
797 s = "OFF";
798 break;
799 case DRM_FORCE_ON_DIGITAL:
800 s = "ON - dig";
801 break;
802 default:
803 case DRM_FORCE_ON:
804 s = "ON";
805 break;
806 }
807
808 DRM_INFO("forcing %s connector %s\n", connector->name, s);
809 connector->force = mode->force;
810 }
811
812 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
813 connector->name,
814 mode->xres, mode->yres,
815 mode->refresh_specified ? mode->refresh : 60,
816 mode->rb ? " reduced blanking" : "",
817 mode->margins ? " with margins" : "",
818 mode->interlace ? " interlaced" : "");
819}
820
b164d31f
DA
821static void drm_connector_free(struct kref *kref)
822{
823 struct drm_connector *connector =
824 container_of(kref, struct drm_connector, base.refcount);
825 struct drm_device *dev = connector->dev;
826
827 drm_mode_object_unregister(dev, &connector->base);
828 connector->funcs->destroy(connector);
829}
830
f453ba04
DA
831/**
832 * drm_connector_init - Init a preallocated connector
833 * @dev: DRM device
834 * @connector: the connector to init
835 * @funcs: callbacks for this connector
065a50ed 836 * @connector_type: user visible type of the connector
f453ba04 837 *
f453ba04
DA
838 * Initialises a preallocated connector. Connectors should be
839 * subclassed as part of driver connector objects.
6bfc56aa 840 *
c8e32cc1 841 * Returns:
6bfc56aa 842 * Zero on success, error code on failure.
f453ba04 843 */
6bfc56aa
VS
844int drm_connector_init(struct drm_device *dev,
845 struct drm_connector *connector,
846 const struct drm_connector_funcs *funcs,
847 int connector_type)
f453ba04 848{
ae16c597 849 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa 850 int ret;
b21e3afe
IM
851 struct ida *connector_ida =
852 &drm_connector_enum_list[connector_type].ida;
6bfc56aa 853
84849903 854 drm_modeset_lock_all(dev);
f453ba04 855
b164d31f
DA
856 ret = drm_mode_object_get_reg(dev, &connector->base,
857 DRM_MODE_OBJECT_CONNECTOR,
858 false, drm_connector_free);
6bfc56aa 859 if (ret)
2abdd313 860 goto out_unlock;
6bfc56aa 861
7e3bdf4a 862 connector->base.properties = &connector->properties;
f453ba04
DA
863 connector->dev = dev;
864 connector->funcs = funcs;
5fff80bb
ML
865
866 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
867 if (connector->connector_id < 0) {
868 ret = connector->connector_id;
869 goto out_put;
870 }
871
f453ba04
DA
872 connector->connector_type = connector_type;
873 connector->connector_type_id =
b21e3afe
IM
874 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
875 if (connector->connector_type_id < 0) {
876 ret = connector->connector_type_id;
5fff80bb 877 goto out_put_id;
b21e3afe 878 }
2abdd313
JN
879 connector->name =
880 kasprintf(GFP_KERNEL, "%s-%d",
881 drm_connector_enum_list[connector_type].name,
882 connector->connector_type_id);
883 if (!connector->name) {
884 ret = -ENOMEM;
5fff80bb 885 goto out_put_type_id;
2abdd313
JN
886 }
887
f453ba04
DA
888 INIT_LIST_HEAD(&connector->probed_modes);
889 INIT_LIST_HEAD(&connector->modes);
890 connector->edid_blob_ptr = NULL;
5e2cb2f6 891 connector->status = connector_status_unknown;
f453ba04 892
eaf99c74
CW
893 drm_connector_get_cmdline_mode(connector);
894
c7eb76f4
DV
895 /* We should add connectors at the end to avoid upsetting the connector
896 * index too much. */
ae16c597
RC
897 list_add_tail(&connector->head, &config->connector_list);
898 config->num_connector++;
f453ba04 899
a7331e5c 900 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
58495563 901 drm_object_attach_property(&connector->base,
ae16c597 902 config->edid_property,
a7331e5c 903 0);
f453ba04 904
58495563 905 drm_object_attach_property(&connector->base,
ae16c597
RC
906 config->dpms_property, 0);
907
908 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
909 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
910 }
f453ba04 911
30f65707 912 connector->debugfs_entry = NULL;
5fff80bb
ML
913out_put_type_id:
914 if (ret)
915 ida_remove(connector_ida, connector->connector_type_id);
916out_put_id:
917 if (ret)
918 ida_remove(&config->connector_ida, connector->connector_id);
2abdd313
JN
919out_put:
920 if (ret)
7c8f6d25 921 drm_mode_object_unregister(dev, &connector->base);
2abdd313
JN
922
923out_unlock:
84849903 924 drm_modeset_unlock_all(dev);
6bfc56aa
VS
925
926 return ret;
f453ba04
DA
927}
928EXPORT_SYMBOL(drm_connector_init);
929
930/**
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
933 *
f453ba04
DA
934 * Cleans up the connector but doesn't free the object.
935 */
936void drm_connector_cleanup(struct drm_connector *connector)
937{
938 struct drm_device *dev = connector->dev;
939 struct drm_display_mode *mode, *t;
940
80de3461
CW
941 /* The connector should have been removed from userspace long before
942 * it is finally destroyed.
943 */
944 if (WARN_ON(connector->registered))
945 drm_connector_unregister(connector);
946
40d9b043
DA
947 if (connector->tile_group) {
948 drm_mode_put_tile_group(dev, connector->tile_group);
949 connector->tile_group = NULL;
950 }
951
f453ba04
DA
952 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
953 drm_mode_remove(connector, mode);
954
955 list_for_each_entry_safe(mode, t, &connector->modes, head)
956 drm_mode_remove(connector, mode);
957
b21e3afe
IM
958 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
959 connector->connector_type_id);
960
5fff80bb
ML
961 ida_remove(&dev->mode_config.connector_ida,
962 connector->connector_id);
963
b5571e9d 964 kfree(connector->display_info.bus_formats);
7c8f6d25 965 drm_mode_object_unregister(dev, &connector->base);
2abdd313
JN
966 kfree(connector->name);
967 connector->name = NULL;
f453ba04 968 list_del(&connector->head);
6380c509 969 dev->mode_config.num_connector--;
3009c037
TR
970
971 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
972 if (connector->state && connector->funcs->atomic_destroy_state)
973 connector->funcs->atomic_destroy_state(connector,
974 connector->state);
a18c0af1
TR
975
976 memset(connector, 0, sizeof(*connector));
f453ba04
DA
977}
978EXPORT_SYMBOL(drm_connector_cleanup);
979
34ea3d38
TW
980/**
981 * drm_connector_register - register a connector
982 * @connector: the connector to register
983 *
984 * Register userspace interfaces for a connector
985 *
986 * Returns:
987 * Zero on success, error code on failure.
988 */
989int drm_connector_register(struct drm_connector *connector)
990{
30f65707
TW
991 int ret;
992
40daac61
CW
993 if (connector->registered)
994 return 0;
995
30f65707
TW
996 ret = drm_sysfs_connector_add(connector);
997 if (ret)
998 return ret;
999
1000 ret = drm_debugfs_connector_add(connector);
1001 if (ret) {
aaf285e2
CW
1002 goto err_sysfs;
1003 }
1004
1005 if (connector->funcs->late_register) {
1006 ret = connector->funcs->late_register(connector);
1007 if (ret)
1008 goto err_debugfs;
30f65707
TW
1009 }
1010
fdf2c85f
DV
1011 drm_mode_object_register(connector->dev, &connector->base);
1012
40daac61 1013 connector->registered = true;
30f65707 1014 return 0;
aaf285e2
CW
1015
1016err_debugfs:
1017 drm_debugfs_connector_remove(connector);
1018err_sysfs:
1019 drm_sysfs_connector_remove(connector);
1020 return ret;
34ea3d38
TW
1021}
1022EXPORT_SYMBOL(drm_connector_register);
1023
1024/**
1025 * drm_connector_unregister - unregister a connector
1026 * @connector: the connector to unregister
1027 *
1028 * Unregister userspace interfaces for a connector
1029 */
1030void drm_connector_unregister(struct drm_connector *connector)
1031{
40daac61
CW
1032 if (!connector->registered)
1033 return;
1034
aaf285e2
CW
1035 if (connector->funcs->early_unregister)
1036 connector->funcs->early_unregister(connector);
1037
34ea3d38 1038 drm_sysfs_connector_remove(connector);
30f65707 1039 drm_debugfs_connector_remove(connector);
40daac61
CW
1040
1041 connector->registered = false;
34ea3d38
TW
1042}
1043EXPORT_SYMBOL(drm_connector_unregister);
1044
54d2c2da
AB
1045/**
1046 * drm_connector_register_all - register all connectors
1047 * @dev: drm device
1048 *
1049 * This function registers all connectors in sysfs and other places so that
e28cd4d0
CW
1050 * userspace can start to access them. drm_connector_register_all() is called
1051 * automatically from drm_dev_register() to complete the device registration,
1052 * if they don't call drm_connector_register() on each connector individually.
54d2c2da
AB
1053 *
1054 * When a device is unplugged and should be removed from userspace access,
1055 * call drm_connector_unregister_all(), which is the inverse of this
1056 * function.
1057 *
1058 * Returns:
1059 * Zero on success, error code on failure.
1060 */
1061int drm_connector_register_all(struct drm_device *dev)
1062{
1063 struct drm_connector *connector;
1064 int ret;
1065
1066 mutex_lock(&dev->mode_config.mutex);
1067
1068 drm_for_each_connector(connector, dev) {
1069 ret = drm_connector_register(connector);
1070 if (ret)
1071 goto err;
1072 }
1073
1074 mutex_unlock(&dev->mode_config.mutex);
1075
1076 return 0;
1077
1078err:
1079 mutex_unlock(&dev->mode_config.mutex);
1080 drm_connector_unregister_all(dev);
1081 return ret;
1082}
1083EXPORT_SYMBOL(drm_connector_register_all);
1084
c8e32cc1 1085/**
6c87e5c3 1086 * drm_connector_unregister_all - unregister connector userspace interfaces
c8e32cc1
DV
1087 * @dev: drm device
1088 *
6c87e5c3
AB
1089 * This functions unregisters all connectors from sysfs and other places so
1090 * that userspace can no longer access them. Drivers should call this as the
1091 * first step tearing down the device instace, or when the underlying
1092 * physical device disappeared (e.g. USB unplug), right before calling
1093 * drm_dev_unregister().
c8e32cc1 1094 */
6c87e5c3 1095void drm_connector_unregister_all(struct drm_device *dev)
cbc7e221
DA
1096{
1097 struct drm_connector *connector;
1098
9a9f5ce8 1099 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
14ba0031 1100 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
34ea3d38 1101 drm_connector_unregister(connector);
cbc7e221 1102}
6c87e5c3 1103EXPORT_SYMBOL(drm_connector_unregister_all);
cbc7e221 1104
c8e32cc1
DV
1105/**
1106 * drm_encoder_init - Init a preallocated encoder
1107 * @dev: drm device
1108 * @encoder: the encoder to init
1109 * @funcs: callbacks for this encoder
1110 * @encoder_type: user visible type of the encoder
13a3d91f 1111 * @name: printf style format string for the encoder name, or NULL for default name
c8e32cc1
DV
1112 *
1113 * Initialises a preallocated encoder. Encoder should be
1114 * subclassed as part of driver encoder objects.
1115 *
1116 * Returns:
1117 * Zero on success, error code on failure.
1118 */
6bfc56aa 1119int drm_encoder_init(struct drm_device *dev,
cbc7e221
DA
1120 struct drm_encoder *encoder,
1121 const struct drm_encoder_funcs *funcs,
13a3d91f 1122 int encoder_type, const char *name, ...)
f453ba04 1123{
6bfc56aa
VS
1124 int ret;
1125
84849903 1126 drm_modeset_lock_all(dev);
f453ba04 1127
6bfc56aa
VS
1128 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1129 if (ret)
e5748946 1130 goto out_unlock;
f453ba04 1131
6bfc56aa 1132 encoder->dev = dev;
f453ba04
DA
1133 encoder->encoder_type = encoder_type;
1134 encoder->funcs = funcs;
86bf546b
VS
1135 if (name) {
1136 va_list ap;
1137
1138 va_start(ap, name);
1139 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1140 va_end(ap);
1141 } else {
1142 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1143 drm_encoder_enum_list[encoder_type].name,
1144 encoder->base.id);
1145 }
e5748946
JN
1146 if (!encoder->name) {
1147 ret = -ENOMEM;
1148 goto out_put;
1149 }
f453ba04
DA
1150
1151 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
490d3d1b 1152 encoder->index = dev->mode_config.num_encoder++;
f453ba04 1153
e5748946
JN
1154out_put:
1155 if (ret)
7c8f6d25 1156 drm_mode_object_unregister(dev, &encoder->base);
e5748946
JN
1157
1158out_unlock:
84849903 1159 drm_modeset_unlock_all(dev);
6bfc56aa
VS
1160
1161 return ret;
f453ba04
DA
1162}
1163EXPORT_SYMBOL(drm_encoder_init);
1164
c8e32cc1
DV
1165/**
1166 * drm_encoder_cleanup - cleans up an initialised encoder
1167 * @encoder: encoder to cleanup
1168 *
1169 * Cleans up the encoder but doesn't free the object.
1170 */
f453ba04
DA
1171void drm_encoder_cleanup(struct drm_encoder *encoder)
1172{
1173 struct drm_device *dev = encoder->dev;
4dfd909f 1174
490d3d1b
CW
1175 /* Note that the encoder_list is considered to be static; should we
1176 * remove the drm_encoder at runtime we would have to decrement all
1177 * the indices on the drm_encoder after us in the encoder_list.
1178 */
1179
84849903 1180 drm_modeset_lock_all(dev);
7c8f6d25 1181 drm_mode_object_unregister(dev, &encoder->base);
e5748946 1182 kfree(encoder->name);
f453ba04 1183 list_del(&encoder->head);
6380c509 1184 dev->mode_config.num_encoder--;
84849903 1185 drm_modeset_unlock_all(dev);
a18c0af1
TR
1186
1187 memset(encoder, 0, sizeof(*encoder));
f453ba04
DA
1188}
1189EXPORT_SYMBOL(drm_encoder_cleanup);
1190
9f4c97a2
VS
1191static unsigned int drm_num_planes(struct drm_device *dev)
1192{
1193 unsigned int num = 0;
1194 struct drm_plane *tmp;
1195
1196 drm_for_each_plane(tmp, dev) {
1197 num++;
1198 }
1199
1200 return num;
1201}
1202
35f2c3ae 1203/**
dc415ff9 1204 * drm_universal_plane_init - Initialize a new universal plane object
35f2c3ae
VS
1205 * @dev: DRM device
1206 * @plane: plane object to init
1207 * @possible_crtcs: bitmask of possible CRTCs
1208 * @funcs: callbacks for the new plane
1209 * @formats: array of supported formats (%DRM_FORMAT_*)
1210 * @format_count: number of elements in @formats
dc415ff9 1211 * @type: type of plane (overlay, primary, cursor)
b0b3b795 1212 * @name: printf style format string for the plane name, or NULL for default name
35f2c3ae 1213 *
dc415ff9 1214 * Initializes a plane object of type @type.
35f2c3ae 1215 *
c8e32cc1 1216 * Returns:
35f2c3ae
VS
1217 * Zero on success, error code on failure.
1218 */
dc415ff9
MR
1219int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1220 unsigned long possible_crtcs,
1221 const struct drm_plane_funcs *funcs,
45e3743a 1222 const uint32_t *formats, unsigned int format_count,
b0b3b795
VS
1223 enum drm_plane_type type,
1224 const char *name, ...)
8cf5c917 1225{
6b4959f4 1226 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa
VS
1227 int ret;
1228
6bfc56aa
VS
1229 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1230 if (ret)
baf698b0 1231 return ret;
6bfc56aa 1232
4d02e2de
DV
1233 drm_modeset_lock_init(&plane->mutex);
1234
4d93914a 1235 plane->base.properties = &plane->properties;
8cf5c917 1236 plane->dev = dev;
8cf5c917 1237 plane->funcs = funcs;
2f6c5389
TR
1238 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1239 GFP_KERNEL);
8cf5c917
JB
1240 if (!plane->format_types) {
1241 DRM_DEBUG_KMS("out of memory when allocating plane\n");
7c8f6d25 1242 drm_mode_object_unregister(dev, &plane->base);
baf698b0 1243 return -ENOMEM;
8cf5c917
JB
1244 }
1245
9f4c97a2
VS
1246 if (name) {
1247 va_list ap;
1248
1249 va_start(ap, name);
1250 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1251 va_end(ap);
1252 } else {
1253 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1254 drm_num_planes(dev));
1255 }
1256 if (!plane->name) {
1257 kfree(plane->format_types);
7c8f6d25 1258 drm_mode_object_unregister(dev, &plane->base);
9f4c97a2
VS
1259 return -ENOMEM;
1260 }
1261
308e5bcb 1262 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
8cf5c917
JB
1263 plane->format_count = format_count;
1264 plane->possible_crtcs = possible_crtcs;
dc415ff9 1265 plane->type = type;
8cf5c917 1266
6b4959f4 1267 list_add_tail(&plane->head, &config->plane_list);
490d3d1b 1268 plane->index = config->num_total_plane++;
dc415ff9 1269 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
6b4959f4 1270 config->num_overlay_plane++;
8cf5c917 1271
9922ab5a 1272 drm_object_attach_property(&plane->base,
6b4959f4 1273 config->plane_type_property,
9922ab5a
RC
1274 plane->type);
1275
6b4959f4
RC
1276 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1277 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1278 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1279 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1280 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1281 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1282 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1283 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1284 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1285 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1286 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1287 }
1288
baf698b0 1289 return 0;
8cf5c917 1290}
dc415ff9
MR
1291EXPORT_SYMBOL(drm_universal_plane_init);
1292
1293/**
1294 * drm_plane_init - Initialize a legacy plane
1295 * @dev: DRM device
1296 * @plane: plane object to init
1297 * @possible_crtcs: bitmask of possible CRTCs
1298 * @funcs: callbacks for the new plane
1299 * @formats: array of supported formats (%DRM_FORMAT_*)
1300 * @format_count: number of elements in @formats
1301 * @is_primary: plane type (primary vs overlay)
1302 *
1303 * Legacy API to initialize a DRM plane.
1304 *
1305 * New drivers should call drm_universal_plane_init() instead.
1306 *
1307 * Returns:
1308 * Zero on success, error code on failure.
1309 */
1310int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1311 unsigned long possible_crtcs,
1312 const struct drm_plane_funcs *funcs,
45e3743a 1313 const uint32_t *formats, unsigned int format_count,
dc415ff9
MR
1314 bool is_primary)
1315{
1316 enum drm_plane_type type;
1317
1318 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1319 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
b0b3b795 1320 formats, format_count, type, NULL);
dc415ff9 1321}
8cf5c917
JB
1322EXPORT_SYMBOL(drm_plane_init);
1323
35f2c3ae
VS
1324/**
1325 * drm_plane_cleanup - Clean up the core plane usage
1326 * @plane: plane to cleanup
1327 *
1328 * This function cleans up @plane and removes it from the DRM mode setting
1329 * core. Note that the function does *not* free the plane structure itself,
1330 * this is the responsibility of the caller.
1331 */
8cf5c917
JB
1332void drm_plane_cleanup(struct drm_plane *plane)
1333{
1334 struct drm_device *dev = plane->dev;
1335
84849903 1336 drm_modeset_lock_all(dev);
8cf5c917 1337 kfree(plane->format_types);
7c8f6d25 1338 drm_mode_object_unregister(dev, &plane->base);
dc415ff9
MR
1339
1340 BUG_ON(list_empty(&plane->head));
1341
490d3d1b
CW
1342 /* Note that the plane_list is considered to be static; should we
1343 * remove the drm_plane at runtime we would have to decrement all
1344 * the indices on the drm_plane after us in the plane_list.
1345 */
1346
dc415ff9
MR
1347 list_del(&plane->head);
1348 dev->mode_config.num_total_plane--;
1349 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1350 dev->mode_config.num_overlay_plane--;
84849903 1351 drm_modeset_unlock_all(dev);
3009c037
TR
1352
1353 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1354 if (plane->state && plane->funcs->atomic_destroy_state)
1355 plane->funcs->atomic_destroy_state(plane, plane->state);
a18c0af1 1356
9f4c97a2
VS
1357 kfree(plane->name);
1358
a18c0af1 1359 memset(plane, 0, sizeof(*plane));
8cf5c917
JB
1360}
1361EXPORT_SYMBOL(drm_plane_cleanup);
1362
f81338a5
CK
1363/**
1364 * drm_plane_from_index - find the registered plane at an index
1365 * @dev: DRM device
1366 * @idx: index of registered plane to find for
1367 *
1368 * Given a plane index, return the registered plane from DRM device's
1369 * list of planes with matching index.
1370 */
1371struct drm_plane *
1372drm_plane_from_index(struct drm_device *dev, int idx)
1373{
1374 struct drm_plane *plane;
f81338a5 1375
490d3d1b
CW
1376 drm_for_each_plane(plane, dev)
1377 if (idx == plane->index)
f81338a5 1378 return plane;
490d3d1b 1379
f81338a5
CK
1380 return NULL;
1381}
1382EXPORT_SYMBOL(drm_plane_from_index);
1383
35f2c3ae
VS
1384/**
1385 * drm_plane_force_disable - Forcibly disable a plane
1386 * @plane: plane to disable
1387 *
1388 * Forces the plane to be disabled.
1389 *
1390 * Used when the plane's current framebuffer is destroyed,
1391 * and when restoring fbdev mode.
1392 */
9125e618
VS
1393void drm_plane_force_disable(struct drm_plane *plane)
1394{
1395 int ret;
1396
3d30a59b 1397 if (!plane->fb)
9125e618
VS
1398 return;
1399
3d30a59b 1400 plane->old_fb = plane->fb;
9125e618 1401 ret = plane->funcs->disable_plane(plane);
731cce48 1402 if (ret) {
9125e618 1403 DRM_ERROR("failed to disable plane with busy fb\n");
3d30a59b 1404 plane->old_fb = NULL;
731cce48
DV
1405 return;
1406 }
9125e618 1407 /* disconnect the plane from the fb and crtc: */
220dd2bc 1408 drm_framebuffer_unreference(plane->old_fb);
3d30a59b 1409 plane->old_fb = NULL;
9125e618
VS
1410 plane->fb = NULL;
1411 plane->crtc = NULL;
1412}
1413EXPORT_SYMBOL(drm_plane_force_disable);
1414
6b4959f4 1415static int drm_mode_create_standard_properties(struct drm_device *dev)
f453ba04 1416{
356af0e1 1417 struct drm_property *prop;
f453ba04
DA
1418
1419 /*
1420 * Standard properties (apply to all connectors)
1421 */
356af0e1 1422 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
f453ba04
DA
1423 DRM_MODE_PROP_IMMUTABLE,
1424 "EDID", 0);
356af0e1
RC
1425 if (!prop)
1426 return -ENOMEM;
1427 dev->mode_config.edid_property = prop;
f453ba04 1428
356af0e1 1429 prop = drm_property_create_enum(dev, 0,
4a67d391
SH
1430 "DPMS", drm_dpms_enum_list,
1431 ARRAY_SIZE(drm_dpms_enum_list));
356af0e1
RC
1432 if (!prop)
1433 return -ENOMEM;
1434 dev->mode_config.dpms_property = prop;
6f134d7b 1435
356af0e1
RC
1436 prop = drm_property_create(dev,
1437 DRM_MODE_PROP_BLOB |
1438 DRM_MODE_PROP_IMMUTABLE,
1439 "PATH", 0);
1440 if (!prop)
1441 return -ENOMEM;
1442 dev->mode_config.path_property = prop;
f453ba04 1443
356af0e1
RC
1444 prop = drm_property_create(dev,
1445 DRM_MODE_PROP_BLOB |
1446 DRM_MODE_PROP_IMMUTABLE,
1447 "TILE", 0);
1448 if (!prop)
1449 return -ENOMEM;
1450 dev->mode_config.tile_property = prop;
9922ab5a 1451
6b4959f4 1452 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
9922ab5a
RC
1453 "type", drm_plane_type_enum_list,
1454 ARRAY_SIZE(drm_plane_type_enum_list));
6b4959f4
RC
1455 if (!prop)
1456 return -ENOMEM;
1457 dev->mode_config.plane_type_property = prop;
1458
1459 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1460 "SRC_X", 0, UINT_MAX);
1461 if (!prop)
1462 return -ENOMEM;
1463 dev->mode_config.prop_src_x = prop;
1464
1465 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1466 "SRC_Y", 0, UINT_MAX);
1467 if (!prop)
1468 return -ENOMEM;
1469 dev->mode_config.prop_src_y = prop;
1470
1471 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1472 "SRC_W", 0, UINT_MAX);
1473 if (!prop)
1474 return -ENOMEM;
1475 dev->mode_config.prop_src_w = prop;
1476
1477 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1478 "SRC_H", 0, UINT_MAX);
1479 if (!prop)
1480 return -ENOMEM;
1481 dev->mode_config.prop_src_h = prop;
1482
1483 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1484 "CRTC_X", INT_MIN, INT_MAX);
1485 if (!prop)
1486 return -ENOMEM;
1487 dev->mode_config.prop_crtc_x = prop;
1488
1489 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1490 "CRTC_Y", INT_MIN, INT_MAX);
1491 if (!prop)
1492 return -ENOMEM;
1493 dev->mode_config.prop_crtc_y = prop;
1494
1495 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1496 "CRTC_W", 0, INT_MAX);
1497 if (!prop)
1498 return -ENOMEM;
1499 dev->mode_config.prop_crtc_w = prop;
1500
1501 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1502 "CRTC_H", 0, INT_MAX);
1503 if (!prop)
1504 return -ENOMEM;
1505 dev->mode_config.prop_crtc_h = prop;
1506
1507 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1508 "FB_ID", DRM_MODE_OBJECT_FB);
1509 if (!prop)
1510 return -ENOMEM;
1511 dev->mode_config.prop_fb_id = prop;
1512
1513 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1514 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1515 if (!prop)
1516 return -ENOMEM;
1517 dev->mode_config.prop_crtc_id = prop;
9922ab5a 1518
eab3bbef
DV
1519 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1520 "ACTIVE");
1521 if (!prop)
1522 return -ENOMEM;
1523 dev->mode_config.prop_active = prop;
1524
955f3c33
DS
1525 prop = drm_property_create(dev,
1526 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1527 "MODE_ID", 0);
1528 if (!prop)
1529 return -ENOMEM;
1530 dev->mode_config.prop_mode_id = prop;
1531
5488dc16
LL
1532 prop = drm_property_create(dev,
1533 DRM_MODE_PROP_BLOB,
1534 "DEGAMMA_LUT", 0);
1535 if (!prop)
1536 return -ENOMEM;
1537 dev->mode_config.degamma_lut_property = prop;
1538
1539 prop = drm_property_create_range(dev,
1540 DRM_MODE_PROP_IMMUTABLE,
1541 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1542 if (!prop)
1543 return -ENOMEM;
1544 dev->mode_config.degamma_lut_size_property = prop;
1545
1546 prop = drm_property_create(dev,
1547 DRM_MODE_PROP_BLOB,
1548 "CTM", 0);
1549 if (!prop)
1550 return -ENOMEM;
1551 dev->mode_config.ctm_property = prop;
1552
1553 prop = drm_property_create(dev,
1554 DRM_MODE_PROP_BLOB,
1555 "GAMMA_LUT", 0);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.gamma_lut_property = prop;
1559
1560 prop = drm_property_create_range(dev,
1561 DRM_MODE_PROP_IMMUTABLE,
1562 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1563 if (!prop)
1564 return -ENOMEM;
1565 dev->mode_config.gamma_lut_size_property = prop;
1566
9922ab5a
RC
1567 return 0;
1568}
1569
f453ba04
DA
1570/**
1571 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1572 * @dev: DRM device
1573 *
1574 * Called by a driver the first time a DVI-I connector is made.
1575 */
1576int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1577{
1578 struct drm_property *dvi_i_selector;
1579 struct drm_property *dvi_i_subconnector;
f453ba04
DA
1580
1581 if (dev->mode_config.dvi_i_select_subconnector_property)
1582 return 0;
1583
1584 dvi_i_selector =
4a67d391 1585 drm_property_create_enum(dev, 0,
f453ba04 1586 "select subconnector",
4a67d391 1587 drm_dvi_i_select_enum_list,
f453ba04 1588 ARRAY_SIZE(drm_dvi_i_select_enum_list));
f453ba04
DA
1589 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1590
4a67d391 1591 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
f453ba04 1592 "subconnector",
4a67d391 1593 drm_dvi_i_subconnector_enum_list,
f453ba04 1594 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
f453ba04
DA
1595 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1596
1597 return 0;
1598}
1599EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1600
1601/**
1602 * drm_create_tv_properties - create TV specific connector properties
1603 * @dev: DRM device
1604 * @num_modes: number of different TV formats (modes) supported
1605 * @modes: array of pointers to strings containing name of each format
1606 *
1607 * Called by a driver's TV initialization routine, this function creates
1608 * the TV specific connector properties for a given device. Caller is
1609 * responsible for allocating a list of format names and passing them to
1610 * this routine.
1611 */
2f763312
TR
1612int drm_mode_create_tv_properties(struct drm_device *dev,
1613 unsigned int num_modes,
b7c914b3 1614 const char * const modes[])
f453ba04
DA
1615{
1616 struct drm_property *tv_selector;
1617 struct drm_property *tv_subconnector;
2f763312 1618 unsigned int i;
f453ba04
DA
1619
1620 if (dev->mode_config.tv_select_subconnector_property)
1621 return 0;
1622
1623 /*
1624 * Basic connector properties
1625 */
4a67d391 1626 tv_selector = drm_property_create_enum(dev, 0,
f453ba04 1627 "select subconnector",
4a67d391 1628 drm_tv_select_enum_list,
f453ba04 1629 ARRAY_SIZE(drm_tv_select_enum_list));
48aa1e74
IY
1630 if (!tv_selector)
1631 goto nomem;
1632
f453ba04
DA
1633 dev->mode_config.tv_select_subconnector_property = tv_selector;
1634
1635 tv_subconnector =
4a67d391
SH
1636 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1637 "subconnector",
1638 drm_tv_subconnector_enum_list,
f453ba04 1639 ARRAY_SIZE(drm_tv_subconnector_enum_list));
48aa1e74
IY
1640 if (!tv_subconnector)
1641 goto nomem;
f453ba04
DA
1642 dev->mode_config.tv_subconnector_property = tv_subconnector;
1643
1644 /*
1645 * Other, TV specific properties: margins & TV modes.
1646 */
1647 dev->mode_config.tv_left_margin_property =
d9bc3c02 1648 drm_property_create_range(dev, 0, "left margin", 0, 100);
48aa1e74
IY
1649 if (!dev->mode_config.tv_left_margin_property)
1650 goto nomem;
f453ba04
DA
1651
1652 dev->mode_config.tv_right_margin_property =
d9bc3c02 1653 drm_property_create_range(dev, 0, "right margin", 0, 100);
48aa1e74
IY
1654 if (!dev->mode_config.tv_right_margin_property)
1655 goto nomem;
f453ba04
DA
1656
1657 dev->mode_config.tv_top_margin_property =
d9bc3c02 1658 drm_property_create_range(dev, 0, "top margin", 0, 100);
48aa1e74
IY
1659 if (!dev->mode_config.tv_top_margin_property)
1660 goto nomem;
f453ba04
DA
1661
1662 dev->mode_config.tv_bottom_margin_property =
d9bc3c02 1663 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
48aa1e74
IY
1664 if (!dev->mode_config.tv_bottom_margin_property)
1665 goto nomem;
f453ba04
DA
1666
1667 dev->mode_config.tv_mode_property =
1668 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1669 "mode", num_modes);
48aa1e74
IY
1670 if (!dev->mode_config.tv_mode_property)
1671 goto nomem;
1672
f453ba04
DA
1673 for (i = 0; i < num_modes; i++)
1674 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1675 i, modes[i]);
1676
b6b7902e 1677 dev->mode_config.tv_brightness_property =
d9bc3c02 1678 drm_property_create_range(dev, 0, "brightness", 0, 100);
48aa1e74
IY
1679 if (!dev->mode_config.tv_brightness_property)
1680 goto nomem;
b6b7902e
FJ
1681
1682 dev->mode_config.tv_contrast_property =
d9bc3c02 1683 drm_property_create_range(dev, 0, "contrast", 0, 100);
48aa1e74
IY
1684 if (!dev->mode_config.tv_contrast_property)
1685 goto nomem;
b6b7902e
FJ
1686
1687 dev->mode_config.tv_flicker_reduction_property =
d9bc3c02 1688 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
48aa1e74
IY
1689 if (!dev->mode_config.tv_flicker_reduction_property)
1690 goto nomem;
b6b7902e 1691
a75f0236 1692 dev->mode_config.tv_overscan_property =
d9bc3c02 1693 drm_property_create_range(dev, 0, "overscan", 0, 100);
48aa1e74
IY
1694 if (!dev->mode_config.tv_overscan_property)
1695 goto nomem;
a75f0236
FJ
1696
1697 dev->mode_config.tv_saturation_property =
d9bc3c02 1698 drm_property_create_range(dev, 0, "saturation", 0, 100);
48aa1e74
IY
1699 if (!dev->mode_config.tv_saturation_property)
1700 goto nomem;
a75f0236
FJ
1701
1702 dev->mode_config.tv_hue_property =
d9bc3c02 1703 drm_property_create_range(dev, 0, "hue", 0, 100);
48aa1e74
IY
1704 if (!dev->mode_config.tv_hue_property)
1705 goto nomem;
a75f0236 1706
f453ba04 1707 return 0;
48aa1e74
IY
1708nomem:
1709 return -ENOMEM;
f453ba04
DA
1710}
1711EXPORT_SYMBOL(drm_mode_create_tv_properties);
1712
1713/**
1714 * drm_mode_create_scaling_mode_property - create scaling mode property
1715 * @dev: DRM device
1716 *
1717 * Called by a driver the first time it's needed, must be attached to desired
1718 * connectors.
1719 */
1720int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1721{
1722 struct drm_property *scaling_mode;
f453ba04
DA
1723
1724 if (dev->mode_config.scaling_mode_property)
1725 return 0;
1726
1727 scaling_mode =
4a67d391
SH
1728 drm_property_create_enum(dev, 0, "scaling mode",
1729 drm_scaling_mode_enum_list,
f453ba04 1730 ARRAY_SIZE(drm_scaling_mode_enum_list));
f453ba04
DA
1731
1732 dev->mode_config.scaling_mode_property = scaling_mode;
1733
1734 return 0;
1735}
1736EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1737
ff587e45
VK
1738/**
1739 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1740 * @dev: DRM device
1741 *
1742 * Called by a driver the first time it's needed, must be attached to desired
1743 * connectors.
1744 *
1745 * Returns:
1a498633 1746 * Zero on success, negative errno on failure.
ff587e45
VK
1747 */
1748int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1749{
1750 if (dev->mode_config.aspect_ratio_property)
1751 return 0;
1752
1753 dev->mode_config.aspect_ratio_property =
1754 drm_property_create_enum(dev, 0, "aspect ratio",
1755 drm_aspect_ratio_enum_list,
1756 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1757
1758 if (dev->mode_config.aspect_ratio_property == NULL)
1759 return -ENOMEM;
1760
1761 return 0;
1762}
1763EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1764
884840aa
JB
1765/**
1766 * drm_mode_create_dirty_property - create dirty property
1767 * @dev: DRM device
1768 *
1769 * Called by a driver the first time it's needed, must be attached to desired
1770 * connectors.
1771 */
1772int drm_mode_create_dirty_info_property(struct drm_device *dev)
1773{
1774 struct drm_property *dirty_info;
884840aa
JB
1775
1776 if (dev->mode_config.dirty_info_property)
1777 return 0;
1778
1779 dirty_info =
4a67d391 1780 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
884840aa 1781 "dirty",
4a67d391 1782 drm_dirty_info_enum_list,
884840aa 1783 ARRAY_SIZE(drm_dirty_info_enum_list));
884840aa
JB
1784 dev->mode_config.dirty_info_property = dirty_info;
1785
1786 return 0;
1787}
1788EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1789
5bb2bbf5
DA
1790/**
1791 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1792 * @dev: DRM device
1793 *
1794 * Create the the suggested x/y offset property for connectors.
1795 */
1796int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1797{
1798 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1799 return 0;
1800
1801 dev->mode_config.suggested_x_property =
1802 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1803
1804 dev->mode_config.suggested_y_property =
1805 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1806
1807 if (dev->mode_config.suggested_x_property == NULL ||
1808 dev->mode_config.suggested_y_property == NULL)
1809 return -ENOMEM;
1810 return 0;
1811}
1812EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1813
f453ba04
DA
1814/**
1815 * drm_mode_getresources - get graphics configuration
065a50ed
DV
1816 * @dev: drm device for the ioctl
1817 * @data: data pointer for the ioctl
1818 * @file_priv: drm file for the ioctl call
f453ba04 1819 *
f453ba04
DA
1820 * Construct a set of configuration description structures and return
1821 * them to the user, including CRTC, connector and framebuffer configuration.
1822 *
1823 * Called by the user via ioctl.
1824 *
c8e32cc1 1825 * Returns:
1a498633 1826 * Zero on success, negative errno on failure.
f453ba04
DA
1827 */
1828int drm_mode_getresources(struct drm_device *dev, void *data,
1829 struct drm_file *file_priv)
1830{
1831 struct drm_mode_card_res *card_res = data;
1832 struct list_head *lh;
1833 struct drm_framebuffer *fb;
1834 struct drm_connector *connector;
1835 struct drm_crtc *crtc;
1836 struct drm_encoder *encoder;
1837 int ret = 0;
1838 int connector_count = 0;
1839 int crtc_count = 0;
1840 int fb_count = 0;
1841 int encoder_count = 0;
9c7060f7 1842 int copied = 0;
f453ba04
DA
1843 uint32_t __user *fb_id;
1844 uint32_t __user *crtc_id;
1845 uint32_t __user *connector_id;
1846 uint32_t __user *encoder_id;
f453ba04 1847
fb3b06c8
DA
1848 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1849 return -EINVAL;
1850
f453ba04 1851
4b096ac1 1852 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
1853 /*
1854 * For the non-control nodes we need to limit the list of resources
1855 * by IDs in the group list for this node
1856 */
1857 list_for_each(lh, &file_priv->fbs)
1858 fb_count++;
1859
4b096ac1
DV
1860 /* handle this in 4 parts */
1861 /* FBs */
1862 if (card_res->count_fbs >= fb_count) {
1863 copied = 0;
1864 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1865 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1866 if (put_user(fb->base.id, fb_id + copied)) {
1867 mutex_unlock(&file_priv->fbs_lock);
1868 return -EFAULT;
1869 }
1870 copied++;
1871 }
1872 }
1873 card_res->count_fbs = fb_count;
1874 mutex_unlock(&file_priv->fbs_lock);
1875
fcf93f69
DV
1876 /* mode_config.mutex protects the connector list against e.g. DP MST
1877 * connector hot-adding. CRTC/Plane lists are invariant. */
1878 mutex_lock(&dev->mode_config.mutex);
9c7060f7
DV
1879 drm_for_each_crtc(crtc, dev)
1880 crtc_count++;
f453ba04 1881
9c7060f7
DV
1882 drm_for_each_connector(connector, dev)
1883 connector_count++;
f453ba04 1884
9c7060f7
DV
1885 drm_for_each_encoder(encoder, dev)
1886 encoder_count++;
f453ba04
DA
1887
1888 card_res->max_height = dev->mode_config.max_height;
1889 card_res->min_height = dev->mode_config.min_height;
1890 card_res->max_width = dev->mode_config.max_width;
1891 card_res->min_width = dev->mode_config.min_width;
1892
f453ba04
DA
1893 /* CRTCs */
1894 if (card_res->count_crtcs >= crtc_count) {
1895 copied = 0;
1896 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
9c7060f7 1897 drm_for_each_crtc(crtc, dev) {
9c7060f7
DV
1898 if (put_user(crtc->base.id, crtc_id + copied)) {
1899 ret = -EFAULT;
1900 goto out;
f453ba04 1901 }
9c7060f7 1902 copied++;
f453ba04
DA
1903 }
1904 }
1905 card_res->count_crtcs = crtc_count;
1906
1907 /* Encoders */
1908 if (card_res->count_encoders >= encoder_count) {
1909 copied = 0;
1910 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
9c7060f7 1911 drm_for_each_encoder(encoder, dev) {
9c7060f7
DV
1912 if (put_user(encoder->base.id, encoder_id +
1913 copied)) {
1914 ret = -EFAULT;
1915 goto out;
f453ba04 1916 }
9c7060f7 1917 copied++;
f453ba04
DA
1918 }
1919 }
1920 card_res->count_encoders = encoder_count;
1921
1922 /* Connectors */
1923 if (card_res->count_connectors >= connector_count) {
1924 copied = 0;
1925 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
9c7060f7 1926 drm_for_each_connector(connector, dev) {
9c7060f7
DV
1927 if (put_user(connector->base.id,
1928 connector_id + copied)) {
1929 ret = -EFAULT;
1930 goto out;
f453ba04 1931 }
9c7060f7 1932 copied++;
f453ba04
DA
1933 }
1934 }
1935 card_res->count_connectors = connector_count;
1936
f453ba04 1937out:
fcf93f69 1938 mutex_unlock(&dev->mode_config.mutex);
f453ba04
DA
1939 return ret;
1940}
1941
1942/**
1943 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
1944 * @dev: drm device for the ioctl
1945 * @data: data pointer for the ioctl
1946 * @file_priv: drm file for the ioctl call
f453ba04 1947 *
f453ba04
DA
1948 * Construct a CRTC configuration structure to return to the user.
1949 *
1950 * Called by the user via ioctl.
1951 *
c8e32cc1 1952 * Returns:
1a498633 1953 * Zero on success, negative errno on failure.
f453ba04
DA
1954 */
1955int drm_mode_getcrtc(struct drm_device *dev,
1956 void *data, struct drm_file *file_priv)
1957{
1958 struct drm_mode_crtc *crtc_resp = data;
1959 struct drm_crtc *crtc;
f453ba04 1960
fb3b06c8
DA
1961 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1962 return -EINVAL;
1963
a2b34e22 1964 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
fcf93f69
DV
1965 if (!crtc)
1966 return -ENOENT;
f453ba04 1967
fcf93f69 1968 drm_modeset_lock_crtc(crtc, crtc->primary);
f453ba04 1969 crtc_resp->gamma_size = crtc->gamma_size;
f4510a27
MR
1970 if (crtc->primary->fb)
1971 crtc_resp->fb_id = crtc->primary->fb->base.id;
f453ba04
DA
1972 else
1973 crtc_resp->fb_id = 0;
1974
31c946e8
DV
1975 if (crtc->state) {
1976 crtc_resp->x = crtc->primary->state->src_x >> 16;
1977 crtc_resp->y = crtc->primary->state->src_y >> 16;
1978 if (crtc->state->enable) {
934a8a89 1979 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
31c946e8 1980 crtc_resp->mode_valid = 1;
f453ba04 1981
31c946e8
DV
1982 } else {
1983 crtc_resp->mode_valid = 0;
1984 }
f453ba04 1985 } else {
31c946e8
DV
1986 crtc_resp->x = crtc->x;
1987 crtc_resp->y = crtc->y;
1988 if (crtc->enabled) {
934a8a89 1989 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
31c946e8
DV
1990 crtc_resp->mode_valid = 1;
1991
1992 } else {
1993 crtc_resp->mode_valid = 0;
1994 }
f453ba04 1995 }
fcf93f69 1996 drm_modeset_unlock_crtc(crtc);
f453ba04 1997
baf698b0 1998 return 0;
f453ba04
DA
1999}
2000
61d8e328
DL
2001static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2002 const struct drm_file *file_priv)
2003{
2004 /*
2005 * If user-space hasn't configured the driver to expose the stereo 3D
2006 * modes, don't expose them.
2007 */
2008 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2009 return false;
2010
2011 return true;
2012}
2013
abd69c55
DV
2014static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2015{
2016 /* For atomic drivers only state objects are synchronously updated and
2017 * protected by modeset locks, so check those first. */
2018 if (connector->state)
2019 return connector->state->best_encoder;
2020 return connector->encoder;
2021}
2022
95cbf110 2023/* helper for getconnector and getproperties ioctls */
88a48e29 2024static int get_properties(struct drm_mode_object *obj, bool atomic,
95cbf110
RC
2025 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2026 uint32_t *arg_count_props)
2027{
88a48e29
RC
2028 int props_count;
2029 int i, ret, copied;
2030
2031 props_count = obj->properties->count;
2032 if (!atomic)
2033 props_count -= obj->properties->atomic_count;
95cbf110
RC
2034
2035 if ((*arg_count_props >= props_count) && props_count) {
88a48e29 2036 for (i = 0, copied = 0; copied < props_count; i++) {
95cbf110
RC
2037 struct drm_property *prop = obj->properties->properties[i];
2038 uint64_t val;
2039
88a48e29
RC
2040 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2041 continue;
2042
95cbf110
RC
2043 ret = drm_object_property_get_value(obj, prop, &val);
2044 if (ret)
2045 return ret;
2046
2047 if (put_user(prop->base.id, prop_ptr + copied))
2048 return -EFAULT;
2049
2050 if (put_user(val, prop_values + copied))
2051 return -EFAULT;
2052
2053 copied++;
2054 }
2055 }
2056 *arg_count_props = props_count;
2057
2058 return 0;
2059}
2060
f453ba04
DA
2061/**
2062 * drm_mode_getconnector - get connector configuration
065a50ed
DV
2063 * @dev: drm device for the ioctl
2064 * @data: data pointer for the ioctl
2065 * @file_priv: drm file for the ioctl call
f453ba04 2066 *
f453ba04
DA
2067 * Construct a connector configuration structure to return to the user.
2068 *
2069 * Called by the user via ioctl.
2070 *
c8e32cc1 2071 * Returns:
1a498633 2072 * Zero on success, negative errno on failure.
f453ba04
DA
2073 */
2074int drm_mode_getconnector(struct drm_device *dev, void *data,
2075 struct drm_file *file_priv)
2076{
2077 struct drm_mode_get_connector *out_resp = data;
f453ba04 2078 struct drm_connector *connector;
abd69c55 2079 struct drm_encoder *encoder;
f453ba04
DA
2080 struct drm_display_mode *mode;
2081 int mode_count = 0;
f453ba04
DA
2082 int encoders_count = 0;
2083 int ret = 0;
2084 int copied = 0;
2085 int i;
2086 struct drm_mode_modeinfo u_mode;
2087 struct drm_mode_modeinfo __user *mode_ptr;
f453ba04
DA
2088 uint32_t __user *encoder_ptr;
2089
fb3b06c8
DA
2090 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2091 return -EINVAL;
2092
f453ba04
DA
2093 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2094
7b24056b 2095 mutex_lock(&dev->mode_config.mutex);
f453ba04 2096
b164d31f 2097 connector = drm_connector_lookup(dev, out_resp->connector_id);
a2b34e22 2098 if (!connector) {
f27657f2 2099 ret = -ENOENT;
04bdf441 2100 goto out_unlock;
f453ba04 2101 }
f453ba04 2102
01073b08
TR
2103 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2104 if (connector->encoder_ids[i] != 0)
f453ba04 2105 encoders_count++;
f453ba04
DA
2106
2107 if (out_resp->count_modes == 0) {
2108 connector->funcs->fill_modes(connector,
2109 dev->mode_config.max_width,
2110 dev->mode_config.max_height);
2111 }
2112
2113 /* delayed so we get modes regardless of pre-fill_modes state */
2114 list_for_each_entry(mode, &connector->modes, head)
61d8e328
DL
2115 if (drm_mode_expose_to_userspace(mode, file_priv))
2116 mode_count++;
f453ba04
DA
2117
2118 out_resp->connector_id = connector->base.id;
2119 out_resp->connector_type = connector->connector_type;
2120 out_resp->connector_type_id = connector->connector_type_id;
2121 out_resp->mm_width = connector->display_info.width_mm;
2122 out_resp->mm_height = connector->display_info.height_mm;
2123 out_resp->subpixel = connector->display_info.subpixel_order;
2124 out_resp->connection = connector->status;
2caa80e7
DV
2125
2126 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
abd69c55
DV
2127 encoder = drm_connector_get_encoder(connector);
2128 if (encoder)
2129 out_resp->encoder_id = encoder->base.id;
f453ba04
DA
2130 else
2131 out_resp->encoder_id = 0;
2132
2133 /*
2134 * This ioctl is called twice, once to determine how much space is
2135 * needed, and the 2nd time to fill it.
2136 */
2137 if ((out_resp->count_modes >= mode_count) && mode_count) {
2138 copied = 0;
81f6c7f8 2139 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
f453ba04 2140 list_for_each_entry(mode, &connector->modes, head) {
61d8e328
DL
2141 if (!drm_mode_expose_to_userspace(mode, file_priv))
2142 continue;
2143
934a8a89 2144 drm_mode_convert_to_umode(&u_mode, mode);
f453ba04
DA
2145 if (copy_to_user(mode_ptr + copied,
2146 &u_mode, sizeof(u_mode))) {
2147 ret = -EFAULT;
2148 goto out;
2149 }
2150 copied++;
2151 }
2152 }
2153 out_resp->count_modes = mode_count;
2154
88a48e29 2155 ret = get_properties(&connector->base, file_priv->atomic,
95cbf110
RC
2156 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2157 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2158 &out_resp->count_props);
2159 if (ret)
2160 goto out;
f453ba04
DA
2161
2162 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2163 copied = 0;
81f6c7f8 2164 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
f453ba04
DA
2165 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2166 if (connector->encoder_ids[i] != 0) {
2167 if (put_user(connector->encoder_ids[i],
2168 encoder_ptr + copied)) {
2169 ret = -EFAULT;
2170 goto out;
2171 }
2172 copied++;
2173 }
2174 }
2175 }
2176 out_resp->count_encoders = encoders_count;
2177
2178out:
ccfc0865 2179 drm_modeset_unlock(&dev->mode_config.connection_mutex);
04bdf441 2180
b164d31f 2181 drm_connector_unreference(connector);
04bdf441 2182out_unlock:
7b24056b
DV
2183 mutex_unlock(&dev->mode_config.mutex);
2184
f453ba04
DA
2185 return ret;
2186}
2187
abd69c55
DV
2188static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2189{
2190 struct drm_connector *connector;
2191 struct drm_device *dev = encoder->dev;
2192 bool uses_atomic = false;
2193
2194 /* For atomic drivers only state objects are synchronously updated and
2195 * protected by modeset locks, so check those first. */
6295d607 2196 drm_for_each_connector(connector, dev) {
abd69c55
DV
2197 if (!connector->state)
2198 continue;
2199
2200 uses_atomic = true;
2201
2202 if (connector->state->best_encoder != encoder)
2203 continue;
2204
2205 return connector->state->crtc;
2206 }
2207
2208 /* Don't return stale data (e.g. pending async disable). */
2209 if (uses_atomic)
2210 return NULL;
2211
2212 return encoder->crtc;
2213}
2214
c8e32cc1
DV
2215/**
2216 * drm_mode_getencoder - get encoder configuration
2217 * @dev: drm device for the ioctl
2218 * @data: data pointer for the ioctl
2219 * @file_priv: drm file for the ioctl call
2220 *
2221 * Construct a encoder configuration structure to return to the user.
2222 *
2223 * Called by the user via ioctl.
2224 *
2225 * Returns:
1a498633 2226 * Zero on success, negative errno on failure.
c8e32cc1 2227 */
f453ba04
DA
2228int drm_mode_getencoder(struct drm_device *dev, void *data,
2229 struct drm_file *file_priv)
2230{
2231 struct drm_mode_get_encoder *enc_resp = data;
f453ba04 2232 struct drm_encoder *encoder;
abd69c55 2233 struct drm_crtc *crtc;
f453ba04 2234
fb3b06c8
DA
2235 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2236 return -EINVAL;
2237
a2b34e22 2238 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
fcf93f69
DV
2239 if (!encoder)
2240 return -ENOENT;
f453ba04 2241
fcf93f69 2242 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
abd69c55
DV
2243 crtc = drm_encoder_get_crtc(encoder);
2244 if (crtc)
2245 enc_resp->crtc_id = crtc->base.id;
f453ba04
DA
2246 else
2247 enc_resp->crtc_id = 0;
fcf93f69
DV
2248 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2249
f453ba04
DA
2250 enc_resp->encoder_type = encoder->encoder_type;
2251 enc_resp->encoder_id = encoder->base.id;
2252 enc_resp->possible_crtcs = encoder->possible_crtcs;
2253 enc_resp->possible_clones = encoder->possible_clones;
2254
baf698b0 2255 return 0;
f453ba04
DA
2256}
2257
8cf5c917 2258/**
c8e32cc1 2259 * drm_mode_getplane_res - enumerate all plane resources
8cf5c917
JB
2260 * @dev: DRM device
2261 * @data: ioctl data
2262 * @file_priv: DRM file info
2263 *
c8e32cc1
DV
2264 * Construct a list of plane ids to return to the user.
2265 *
2266 * Called by the user via ioctl.
2267 *
2268 * Returns:
1a498633 2269 * Zero on success, negative errno on failure.
8cf5c917
JB
2270 */
2271int drm_mode_getplane_res(struct drm_device *dev, void *data,
c8e32cc1 2272 struct drm_file *file_priv)
8cf5c917
JB
2273{
2274 struct drm_mode_get_plane_res *plane_resp = data;
2275 struct drm_mode_config *config;
2276 struct drm_plane *plane;
2277 uint32_t __user *plane_ptr;
fcf93f69 2278 int copied = 0;
681e7ec7 2279 unsigned num_planes;
8cf5c917
JB
2280
2281 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2282 return -EINVAL;
2283
8cf5c917
JB
2284 config = &dev->mode_config;
2285
681e7ec7
MR
2286 if (file_priv->universal_planes)
2287 num_planes = config->num_total_plane;
2288 else
2289 num_planes = config->num_overlay_plane;
2290
8cf5c917
JB
2291 /*
2292 * This ioctl is called twice, once to determine how much space is
2293 * needed, and the 2nd time to fill it.
2294 */
681e7ec7
MR
2295 if (num_planes &&
2296 (plane_resp->count_planes >= num_planes)) {
81f6c7f8 2297 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
8cf5c917 2298
fcf93f69 2299 /* Plane lists are invariant, no locking needed. */
e4f62546 2300 drm_for_each_plane(plane, dev) {
681e7ec7
MR
2301 /*
2302 * Unless userspace set the 'universal planes'
2303 * capability bit, only advertise overlays.
2304 */
2305 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2306 !file_priv->universal_planes)
e27dde3e
MR
2307 continue;
2308
fcf93f69
DV
2309 if (put_user(plane->base.id, plane_ptr + copied))
2310 return -EFAULT;
8cf5c917
JB
2311 copied++;
2312 }
2313 }
681e7ec7 2314 plane_resp->count_planes = num_planes;
8cf5c917 2315
fcf93f69 2316 return 0;
8cf5c917
JB
2317}
2318
2319/**
c8e32cc1 2320 * drm_mode_getplane - get plane configuration
8cf5c917
JB
2321 * @dev: DRM device
2322 * @data: ioctl data
2323 * @file_priv: DRM file info
2324 *
c8e32cc1
DV
2325 * Construct a plane configuration structure to return to the user.
2326 *
2327 * Called by the user via ioctl.
2328 *
2329 * Returns:
1a498633 2330 * Zero on success, negative errno on failure.
8cf5c917
JB
2331 */
2332int drm_mode_getplane(struct drm_device *dev, void *data,
c8e32cc1 2333 struct drm_file *file_priv)
8cf5c917
JB
2334{
2335 struct drm_mode_get_plane *plane_resp = data;
8cf5c917
JB
2336 struct drm_plane *plane;
2337 uint32_t __user *format_ptr;
8cf5c917
JB
2338
2339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 return -EINVAL;
2341
a2b34e22 2342 plane = drm_plane_find(dev, plane_resp->plane_id);
fcf93f69
DV
2343 if (!plane)
2344 return -ENOENT;
8cf5c917 2345
fcf93f69 2346 drm_modeset_lock(&plane->mutex, NULL);
8cf5c917
JB
2347 if (plane->crtc)
2348 plane_resp->crtc_id = plane->crtc->base.id;
2349 else
2350 plane_resp->crtc_id = 0;
2351
2352 if (plane->fb)
2353 plane_resp->fb_id = plane->fb->base.id;
2354 else
2355 plane_resp->fb_id = 0;
fcf93f69 2356 drm_modeset_unlock(&plane->mutex);
8cf5c917
JB
2357
2358 plane_resp->plane_id = plane->base.id;
2359 plane_resp->possible_crtcs = plane->possible_crtcs;
778ad903 2360 plane_resp->gamma_size = 0;
8cf5c917
JB
2361
2362 /*
2363 * This ioctl is called twice, once to determine how much space is
2364 * needed, and the 2nd time to fill it.
2365 */
2366 if (plane->format_count &&
2367 (plane_resp->count_format_types >= plane->format_count)) {
81f6c7f8 2368 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
8cf5c917
JB
2369 if (copy_to_user(format_ptr,
2370 plane->format_types,
2371 sizeof(uint32_t) * plane->format_count)) {
fcf93f69 2372 return -EFAULT;
8cf5c917
JB
2373 }
2374 }
2375 plane_resp->count_format_types = plane->format_count;
2376
baf698b0 2377 return 0;
8cf5c917
JB
2378}
2379
ead8610d
LP
2380/**
2381 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2382 * @plane: plane to check for format support
2383 * @format: the pixel format
2384 *
2385 * Returns:
2386 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2387 * otherwise.
2388 */
2389int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2390{
2391 unsigned int i;
2392
2393 for (i = 0; i < plane->format_count; i++) {
2394 if (format == plane->format_types[i])
2395 return 0;
2396 }
2397
2398 return -EINVAL;
2399}
2400
ce8d9ecc
VS
2401static int check_src_coords(uint32_t src_x, uint32_t src_y,
2402 uint32_t src_w, uint32_t src_h,
2403 const struct drm_framebuffer *fb)
2404{
2405 unsigned int fb_width, fb_height;
2406
2407 fb_width = fb->width << 16;
2408 fb_height = fb->height << 16;
2409
2410 /* Make sure source coordinates are inside the fb. */
2411 if (src_w > fb_width ||
2412 src_x > fb_width - src_w ||
2413 src_h > fb_height ||
2414 src_y > fb_height - src_h) {
2415 DRM_DEBUG_KMS("Invalid source coordinates "
2416 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2417 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2418 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2419 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2420 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2421 return -ENOSPC;
2422 }
2423
2424 return 0;
2425}
2426
b36552b3
MR
2427/*
2428 * setplane_internal - setplane handler for internal callers
8cf5c917 2429 *
b36552b3
MR
2430 * Note that we assume an extra reference has already been taken on fb. If the
2431 * update fails, this reference will be dropped before return; if it succeeds,
2432 * the previous framebuffer (if any) will be unreferenced instead.
c8e32cc1 2433 *
b36552b3 2434 * src_{x,y,w,h} are provided in 16.16 fixed point format
8cf5c917 2435 */
f2b50c11
DV
2436static int __setplane_internal(struct drm_plane *plane,
2437 struct drm_crtc *crtc,
2438 struct drm_framebuffer *fb,
2439 int32_t crtc_x, int32_t crtc_y,
2440 uint32_t crtc_w, uint32_t crtc_h,
2441 /* src_{x,y,w,h} values are 16.16 fixed point */
2442 uint32_t src_x, uint32_t src_y,
2443 uint32_t src_w, uint32_t src_h)
8cf5c917 2444{
8cf5c917
JB
2445 int ret = 0;
2446
8cf5c917 2447 /* No fb means shut it down */
b36552b3 2448 if (!fb) {
3d30a59b 2449 plane->old_fb = plane->fb;
731cce48
DV
2450 ret = plane->funcs->disable_plane(plane);
2451 if (!ret) {
2452 plane->crtc = NULL;
2453 plane->fb = NULL;
2454 } else {
3d30a59b 2455 plane->old_fb = NULL;
731cce48 2456 }
8cf5c917
JB
2457 goto out;
2458 }
2459
7f994f3f
MR
2460 /* Check whether this plane is usable on this CRTC */
2461 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2462 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2463 ret = -EINVAL;
2464 goto out;
2465 }
2466
62443be6 2467 /* Check whether this plane supports the fb pixel format. */
ead8610d
LP
2468 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2469 if (ret) {
6ba6d03e
VS
2470 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2471 drm_get_format_name(fb->pixel_format));
62443be6
VS
2472 goto out;
2473 }
2474
3968be94
MR
2475 /* Give drivers some help against integer overflows */
2476 if (crtc_w > INT_MAX ||
2477 crtc_x > INT_MAX - (int32_t) crtc_w ||
2478 crtc_h > INT_MAX ||
2479 crtc_y > INT_MAX - (int32_t) crtc_h) {
2480 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2481 crtc_w, crtc_h, crtc_x, crtc_y);
c390eed0
VS
2482 ret = -ERANGE;
2483 goto out;
3968be94
MR
2484 }
2485
ce8d9ecc
VS
2486 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2487 if (ret)
42ef8789 2488 goto out;
42ef8789 2489
3d30a59b 2490 plane->old_fb = plane->fb;
8cf5c917 2491 ret = plane->funcs->update_plane(plane, crtc, fb,
b36552b3
MR
2492 crtc_x, crtc_y, crtc_w, crtc_h,
2493 src_x, src_y, src_w, src_h);
8cf5c917
JB
2494 if (!ret) {
2495 plane->crtc = crtc;
2496 plane->fb = fb;
35f8badc 2497 fb = NULL;
0fe27f06 2498 } else {
3d30a59b 2499 plane->old_fb = NULL;
8cf5c917
JB
2500 }
2501
2502out:
6c2a7532
DV
2503 if (fb)
2504 drm_framebuffer_unreference(fb);
3d30a59b
DV
2505 if (plane->old_fb)
2506 drm_framebuffer_unreference(plane->old_fb);
2507 plane->old_fb = NULL;
8cf5c917
JB
2508
2509 return ret;
f2b50c11 2510}
b36552b3 2511
f2b50c11
DV
2512static int setplane_internal(struct drm_plane *plane,
2513 struct drm_crtc *crtc,
2514 struct drm_framebuffer *fb,
2515 int32_t crtc_x, int32_t crtc_y,
2516 uint32_t crtc_w, uint32_t crtc_h,
2517 /* src_{x,y,w,h} values are 16.16 fixed point */
2518 uint32_t src_x, uint32_t src_y,
2519 uint32_t src_w, uint32_t src_h)
2520{
2521 int ret;
2522
2523 drm_modeset_lock_all(plane->dev);
2524 ret = __setplane_internal(plane, crtc, fb,
2525 crtc_x, crtc_y, crtc_w, crtc_h,
2526 src_x, src_y, src_w, src_h);
2527 drm_modeset_unlock_all(plane->dev);
2528
2529 return ret;
b36552b3
MR
2530}
2531
2532/**
2533 * drm_mode_setplane - configure a plane's configuration
2534 * @dev: DRM device
2535 * @data: ioctl data*
2536 * @file_priv: DRM file info
2537 *
2538 * Set plane configuration, including placement, fb, scaling, and other factors.
2539 * Or pass a NULL fb to disable (planes may be disabled without providing a
2540 * valid crtc).
2541 *
2542 * Returns:
1a498633 2543 * Zero on success, negative errno on failure.
b36552b3
MR
2544 */
2545int drm_mode_setplane(struct drm_device *dev, void *data,
2546 struct drm_file *file_priv)
2547{
2548 struct drm_mode_set_plane *plane_req = data;
b36552b3
MR
2549 struct drm_plane *plane;
2550 struct drm_crtc *crtc = NULL;
2551 struct drm_framebuffer *fb = NULL;
2552
2553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2554 return -EINVAL;
2555
b36552b3
MR
2556 /*
2557 * First, find the plane, crtc, and fb objects. If not available,
2558 * we don't bother to call the driver.
2559 */
933f622f
RC
2560 plane = drm_plane_find(dev, plane_req->plane_id);
2561 if (!plane) {
b36552b3
MR
2562 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2563 plane_req->plane_id);
2564 return -ENOENT;
2565 }
b36552b3
MR
2566
2567 if (plane_req->fb_id) {
2568 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2569 if (!fb) {
2570 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2571 plane_req->fb_id);
2572 return -ENOENT;
2573 }
2574
933f622f
RC
2575 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2576 if (!crtc) {
b36552b3
MR
2577 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2578 plane_req->crtc_id);
2579 return -ENOENT;
2580 }
b36552b3
MR
2581 }
2582
161d0dc1
MR
2583 /*
2584 * setplane_internal will take care of deref'ing either the old or new
2585 * framebuffer depending on success.
2586 */
17cfd91f 2587 return setplane_internal(plane, crtc, fb,
b36552b3
MR
2588 plane_req->crtc_x, plane_req->crtc_y,
2589 plane_req->crtc_w, plane_req->crtc_h,
2590 plane_req->src_x, plane_req->src_y,
2591 plane_req->src_w, plane_req->src_h);
8cf5c917
JB
2592}
2593
2d13b679
DV
2594/**
2595 * drm_mode_set_config_internal - helper to call ->set_config
2596 * @set: modeset config to set
2597 *
2598 * This is a little helper to wrap internal calls to the ->set_config driver
2599 * interface. The only thing it adds is correct refcounting dance.
4dfd909f 2600 *
c8e32cc1 2601 * Returns:
1a498633 2602 * Zero on success, negative errno on failure.
2d13b679
DV
2603 */
2604int drm_mode_set_config_internal(struct drm_mode_set *set)
2605{
2606 struct drm_crtc *crtc = set->crtc;
5cef29aa
DV
2607 struct drm_framebuffer *fb;
2608 struct drm_crtc *tmp;
b0d12325
DV
2609 int ret;
2610
5cef29aa
DV
2611 /*
2612 * NOTE: ->set_config can also disable other crtcs (if we steal all
2613 * connectors from it), hence we need to refcount the fbs across all
2614 * crtcs. Atomic modeset will have saner semantics ...
2615 */
e4f62546 2616 drm_for_each_crtc(tmp, crtc->dev)
3d30a59b 2617 tmp->primary->old_fb = tmp->primary->fb;
5cef29aa 2618
b0d12325 2619 fb = set->fb;
2d13b679 2620
b0d12325
DV
2621 ret = crtc->funcs->set_config(set);
2622 if (ret == 0) {
e13161af 2623 crtc->primary->crtc = crtc;
0fe27f06 2624 crtc->primary->fb = fb;
5cef29aa 2625 }
cc85e121 2626
e4f62546 2627 drm_for_each_crtc(tmp, crtc->dev) {
f4510a27
MR
2628 if (tmp->primary->fb)
2629 drm_framebuffer_reference(tmp->primary->fb);
3d30a59b
DV
2630 if (tmp->primary->old_fb)
2631 drm_framebuffer_unreference(tmp->primary->old_fb);
2632 tmp->primary->old_fb = NULL;
b0d12325
DV
2633 }
2634
2635 return ret;
2d13b679
DV
2636}
2637EXPORT_SYMBOL(drm_mode_set_config_internal);
2638
ecb7e16b
GP
2639/**
2640 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2641 * @mode: mode to query
2642 * @hdisplay: hdisplay value to fill in
2643 * @vdisplay: vdisplay value to fill in
2644 *
2645 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2646 * the appropriate layout.
2647 */
2648void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2649 int *hdisplay, int *vdisplay)
2650{
2651 struct drm_display_mode adjusted;
2652
2653 drm_mode_copy(&adjusted, mode);
2654 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2655 *hdisplay = adjusted.crtc_hdisplay;
2656 *vdisplay = adjusted.crtc_vdisplay;
2657}
2658EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2659
af93629d
MR
2660/**
2661 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2662 * CRTC viewport
2663 * @crtc: CRTC that framebuffer will be displayed on
2664 * @x: x panning
2665 * @y: y panning
2666 * @mode: mode that framebuffer will be displayed under
2667 * @fb: framebuffer to check size of
c11e9283 2668 */
af93629d
MR
2669int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2670 int x, int y,
2671 const struct drm_display_mode *mode,
2672 const struct drm_framebuffer *fb)
c11e9283
DL
2673
2674{
2675 int hdisplay, vdisplay;
2676
ecb7e16b 2677 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
a0c1bbb0 2678
33e0be63
VS
2679 if (crtc->state &&
2680 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2681 BIT(DRM_ROTATE_270)))
c11e9283
DL
2682 swap(hdisplay, vdisplay);
2683
ce8d9ecc
VS
2684 return check_src_coords(x << 16, y << 16,
2685 hdisplay << 16, vdisplay << 16, fb);
c11e9283 2686}
af93629d 2687EXPORT_SYMBOL(drm_crtc_check_viewport);
c11e9283 2688
f453ba04
DA
2689/**
2690 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
2691 * @dev: drm device for the ioctl
2692 * @data: data pointer for the ioctl
2693 * @file_priv: drm file for the ioctl call
f453ba04 2694 *
f453ba04
DA
2695 * Build a new CRTC configuration based on user request.
2696 *
2697 * Called by the user via ioctl.
2698 *
c8e32cc1 2699 * Returns:
1a498633 2700 * Zero on success, negative errno on failure.
f453ba04
DA
2701 */
2702int drm_mode_setcrtc(struct drm_device *dev, void *data,
2703 struct drm_file *file_priv)
2704{
2705 struct drm_mode_config *config = &dev->mode_config;
2706 struct drm_mode_crtc *crtc_req = data;
6653cc8d 2707 struct drm_crtc *crtc;
f453ba04
DA
2708 struct drm_connector **connector_set = NULL, *connector;
2709 struct drm_framebuffer *fb = NULL;
2710 struct drm_display_mode *mode = NULL;
2711 struct drm_mode_set set;
2712 uint32_t __user *set_connectors_ptr;
4a1b0714 2713 int ret;
f453ba04
DA
2714 int i;
2715
fb3b06c8
DA
2716 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2717 return -EINVAL;
2718
01447e9f
ZJ
2719 /*
2720 * Universal plane src offsets are only 16.16, prevent havoc for
2721 * drivers using universal plane code internally.
2722 */
2723 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1d97e915
VS
2724 return -ERANGE;
2725
84849903 2726 drm_modeset_lock_all(dev);
a2b34e22
RC
2727 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2728 if (!crtc) {
58367ed6 2729 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f27657f2 2730 ret = -ENOENT;
f453ba04
DA
2731 goto out;
2732 }
fa3ab4c2 2733 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
f453ba04
DA
2734
2735 if (crtc_req->mode_valid) {
2736 /* If we have a mode we need a framebuffer. */
2737 /* If we pass -1, set the mode with the currently bound fb */
2738 if (crtc_req->fb_id == -1) {
f4510a27 2739 if (!crtc->primary->fb) {
6653cc8d
VS
2740 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2741 ret = -EINVAL;
2742 goto out;
f453ba04 2743 }
f4510a27 2744 fb = crtc->primary->fb;
b0d12325
DV
2745 /* Make refcounting symmetric with the lookup path. */
2746 drm_framebuffer_reference(fb);
f453ba04 2747 } else {
786b99ed
DV
2748 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2749 if (!fb) {
58367ed6
ZY
2750 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2751 crtc_req->fb_id);
37c4e705 2752 ret = -ENOENT;
f453ba04
DA
2753 goto out;
2754 }
f453ba04
DA
2755 }
2756
2757 mode = drm_mode_create(dev);
ee34ab5b
VS
2758 if (!mode) {
2759 ret = -ENOMEM;
2760 goto out;
2761 }
2762
934a8a89 2763 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
90367bf6
VS
2764 if (ret) {
2765 DRM_DEBUG_KMS("Invalid mode\n");
2766 goto out;
2767 }
2768
7eb5f302
LP
2769 /*
2770 * Check whether the primary plane supports the fb pixel format.
2771 * Drivers not implementing the universal planes API use a
2772 * default formats list provided by the DRM core which doesn't
2773 * match real hardware capabilities. Skip the check in that
2774 * case.
2775 */
2776 if (!crtc->primary->format_default) {
2777 ret = drm_plane_check_pixel_format(crtc->primary,
2778 fb->pixel_format);
2779 if (ret) {
2780 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2781 drm_get_format_name(fb->pixel_format));
2782 goto out;
2783 }
2784 }
2785
c11e9283
DL
2786 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2787 mode, fb);
2788 if (ret)
5f61bb42 2789 goto out;
c11e9283 2790
f453ba04
DA
2791 }
2792
2793 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 2794 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
2795 ret = -EINVAL;
2796 goto out;
2797 }
2798
7781de74 2799 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 2800 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
2801 crtc_req->count_connectors);
2802 ret = -EINVAL;
2803 goto out;
2804 }
2805
2806 if (crtc_req->count_connectors > 0) {
2807 u32 out_id;
2808
2809 /* Avoid unbounded kernel memory allocation */
2810 if (crtc_req->count_connectors > config->num_connector) {
2811 ret = -EINVAL;
2812 goto out;
2813 }
2814
2f6c5389
TR
2815 connector_set = kmalloc_array(crtc_req->count_connectors,
2816 sizeof(struct drm_connector *),
2817 GFP_KERNEL);
f453ba04
DA
2818 if (!connector_set) {
2819 ret = -ENOMEM;
2820 goto out;
2821 }
2822
2823 for (i = 0; i < crtc_req->count_connectors; i++) {
b164d31f 2824 connector_set[i] = NULL;
81f6c7f8 2825 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
2826 if (get_user(out_id, &set_connectors_ptr[i])) {
2827 ret = -EFAULT;
2828 goto out;
2829 }
2830
b164d31f 2831 connector = drm_connector_lookup(dev, out_id);
a2b34e22 2832 if (!connector) {
58367ed6
ZY
2833 DRM_DEBUG_KMS("Connector id %d unknown\n",
2834 out_id);
f27657f2 2835 ret = -ENOENT;
f453ba04
DA
2836 goto out;
2837 }
9440106b
JG
2838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2839 connector->base.id,
25933820 2840 connector->name);
f453ba04
DA
2841
2842 connector_set[i] = connector;
2843 }
2844 }
2845
2846 set.crtc = crtc;
2847 set.x = crtc_req->x;
2848 set.y = crtc_req->y;
2849 set.mode = mode;
2850 set.connectors = connector_set;
2851 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 2852 set.fb = fb;
2d13b679 2853 ret = drm_mode_set_config_internal(&set);
f453ba04
DA
2854
2855out:
b0d12325
DV
2856 if (fb)
2857 drm_framebuffer_unreference(fb);
2858
b164d31f
DA
2859 if (connector_set) {
2860 for (i = 0; i < crtc_req->count_connectors; i++) {
2861 if (connector_set[i])
2862 drm_connector_unreference(connector_set[i]);
2863 }
2864 }
f453ba04 2865 kfree(connector_set);
ee34ab5b 2866 drm_mode_destroy(dev, mode);
84849903 2867 drm_modeset_unlock_all(dev);
f453ba04
DA
2868 return ret;
2869}
2870
161d0dc1
MR
2871/**
2872 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2873 * universal plane handler call
2874 * @crtc: crtc to update cursor for
2875 * @req: data pointer for the ioctl
2876 * @file_priv: drm file for the ioctl call
2877 *
2878 * Legacy cursor ioctl's work directly with driver buffer handles. To
2879 * translate legacy ioctl calls into universal plane handler calls, we need to
2880 * wrap the native buffer handle in a drm_framebuffer.
2881 *
2882 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2883 * buffer with a pitch of 4*width; the universal plane interface should be used
2884 * directly in cases where the hardware can support other buffer settings and
2885 * userspace wants to make use of these capabilities.
2886 *
2887 * Returns:
1a498633 2888 * Zero on success, negative errno on failure.
161d0dc1
MR
2889 */
2890static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2891 struct drm_mode_cursor2 *req,
2892 struct drm_file *file_priv)
2893{
2894 struct drm_device *dev = crtc->dev;
2895 struct drm_framebuffer *fb = NULL;
2896 struct drm_mode_fb_cmd2 fbreq = {
2897 .width = req->width,
2898 .height = req->height,
2899 .pixel_format = DRM_FORMAT_ARGB8888,
2900 .pitches = { req->width * 4 },
2901 .handles = { req->handle },
2902 };
2903 int32_t crtc_x, crtc_y;
2904 uint32_t crtc_w = 0, crtc_h = 0;
2905 uint32_t src_w = 0, src_h = 0;
2906 int ret = 0;
2907
2908 BUG_ON(!crtc->cursor);
f2b50c11 2909 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
161d0dc1
MR
2910
2911 /*
2912 * Obtain fb we'll be using (either new or existing) and take an extra
2913 * reference to it if fb != null. setplane will take care of dropping
2914 * the reference if the plane update fails.
2915 */
2916 if (req->flags & DRM_MODE_CURSOR_BO) {
2917 if (req->handle) {
9a6f5130 2918 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
161d0dc1
MR
2919 if (IS_ERR(fb)) {
2920 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2921 return PTR_ERR(fb);
2922 }
dd546591
GH
2923 fb->hot_x = req->hot_x;
2924 fb->hot_y = req->hot_y;
161d0dc1
MR
2925 } else {
2926 fb = NULL;
2927 }
2928 } else {
161d0dc1
MR
2929 fb = crtc->cursor->fb;
2930 if (fb)
2931 drm_framebuffer_reference(fb);
161d0dc1
MR
2932 }
2933
2934 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2935 crtc_x = req->x;
2936 crtc_y = req->y;
2937 } else {
2938 crtc_x = crtc->cursor_x;
2939 crtc_y = crtc->cursor_y;
2940 }
2941
2942 if (fb) {
2943 crtc_w = fb->width;
2944 crtc_h = fb->height;
2945 src_w = fb->width << 16;
2946 src_h = fb->height << 16;
2947 }
2948
2949 /*
2950 * setplane_internal will take care of deref'ing either the old or new
2951 * framebuffer depending on success.
2952 */
f2b50c11 2953 ret = __setplane_internal(crtc->cursor, crtc, fb,
161d0dc1
MR
2954 crtc_x, crtc_y, crtc_w, crtc_h,
2955 0, 0, src_w, src_h);
2956
2957 /* Update successful; save new cursor position, if necessary */
2958 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2959 crtc->cursor_x = req->x;
2960 crtc->cursor_y = req->y;
2961 }
2962
2963 return ret;
2964}
2965
4c813d4d
DA
2966static int drm_mode_cursor_common(struct drm_device *dev,
2967 struct drm_mode_cursor2 *req,
2968 struct drm_file *file_priv)
f453ba04 2969{
f453ba04
DA
2970 struct drm_crtc *crtc;
2971 int ret = 0;
2972
fb3b06c8
DA
2973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2974 return -EINVAL;
2975
7c4eaca4 2976 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
f453ba04 2977 return -EINVAL;
f453ba04 2978
a2b34e22
RC
2979 crtc = drm_crtc_find(dev, req->crtc_id);
2980 if (!crtc) {
58367ed6 2981 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
f27657f2 2982 return -ENOENT;
f453ba04 2983 }
f453ba04 2984
161d0dc1
MR
2985 /*
2986 * If this crtc has a universal cursor plane, call that plane's update
2987 * handler rather than using legacy cursor handlers.
2988 */
4d02e2de 2989 drm_modeset_lock_crtc(crtc, crtc->cursor);
f2b50c11
DV
2990 if (crtc->cursor) {
2991 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2992 goto out;
2993 }
2994
f453ba04 2995 if (req->flags & DRM_MODE_CURSOR_BO) {
4c813d4d 2996 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
f453ba04
DA
2997 ret = -ENXIO;
2998 goto out;
2999 }
3000 /* Turns off the cursor if handle is 0 */
4c813d4d
DA
3001 if (crtc->funcs->cursor_set2)
3002 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3003 req->width, req->height, req->hot_x, req->hot_y);
3004 else
3005 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3006 req->width, req->height);
f453ba04
DA
3007 }
3008
3009 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3010 if (crtc->funcs->cursor_move) {
3011 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3012 } else {
f453ba04
DA
3013 ret = -EFAULT;
3014 goto out;
3015 }
3016 }
3017out:
d059f652 3018 drm_modeset_unlock_crtc(crtc);
dac35663 3019
f453ba04 3020 return ret;
4c813d4d
DA
3021
3022}
c8e32cc1
DV
3023
3024
3025/**
3026 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3027 * @dev: drm device for the ioctl
3028 * @data: data pointer for the ioctl
3029 * @file_priv: drm file for the ioctl call
3030 *
3031 * Set the cursor configuration based on user request.
3032 *
3033 * Called by the user via ioctl.
3034 *
3035 * Returns:
1a498633 3036 * Zero on success, negative errno on failure.
c8e32cc1 3037 */
4c813d4d 3038int drm_mode_cursor_ioctl(struct drm_device *dev,
c8e32cc1 3039 void *data, struct drm_file *file_priv)
4c813d4d
DA
3040{
3041 struct drm_mode_cursor *req = data;
3042 struct drm_mode_cursor2 new_req;
3043
3044 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3045 new_req.hot_x = new_req.hot_y = 0;
3046
3047 return drm_mode_cursor_common(dev, &new_req, file_priv);
3048}
3049
c8e32cc1
DV
3050/**
3051 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3052 * @dev: drm device for the ioctl
3053 * @data: data pointer for the ioctl
3054 * @file_priv: drm file for the ioctl call
3055 *
3056 * Set the cursor configuration based on user request. This implements the 2nd
3057 * version of the cursor ioctl, which allows userspace to additionally specify
3058 * the hotspot of the pointer.
3059 *
3060 * Called by the user via ioctl.
3061 *
3062 * Returns:
1a498633 3063 * Zero on success, negative errno on failure.
c8e32cc1 3064 */
4c813d4d
DA
3065int drm_mode_cursor2_ioctl(struct drm_device *dev,
3066 void *data, struct drm_file *file_priv)
3067{
3068 struct drm_mode_cursor2 *req = data;
4dfd909f 3069
4c813d4d 3070 return drm_mode_cursor_common(dev, req, file_priv);
f453ba04
DA
3071}
3072
c8e32cc1
DV
3073/**
3074 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3075 * @bpp: bits per pixels
3076 * @depth: bit depth per pixel
3077 *
3078 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3079 * Useful in fbdev emulation code, since that deals in those values.
3080 */
308e5bcb
JB
3081uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3082{
3083 uint32_t fmt;
3084
3085 switch (bpp) {
3086 case 8:
d84f031b 3087 fmt = DRM_FORMAT_C8;
308e5bcb
JB
3088 break;
3089 case 16:
3090 if (depth == 15)
04b3924d 3091 fmt = DRM_FORMAT_XRGB1555;
308e5bcb 3092 else
04b3924d 3093 fmt = DRM_FORMAT_RGB565;
308e5bcb
JB
3094 break;
3095 case 24:
04b3924d 3096 fmt = DRM_FORMAT_RGB888;
308e5bcb
JB
3097 break;
3098 case 32:
3099 if (depth == 24)
04b3924d 3100 fmt = DRM_FORMAT_XRGB8888;
308e5bcb 3101 else if (depth == 30)
04b3924d 3102 fmt = DRM_FORMAT_XRGB2101010;
308e5bcb 3103 else
04b3924d 3104 fmt = DRM_FORMAT_ARGB8888;
308e5bcb
JB
3105 break;
3106 default:
04b3924d
VS
3107 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3108 fmt = DRM_FORMAT_XRGB8888;
308e5bcb
JB
3109 break;
3110 }
3111
3112 return fmt;
3113}
3114EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3115
f453ba04
DA
3116/**
3117 * drm_mode_addfb - add an FB to the graphics configuration
065a50ed
DV
3118 * @dev: drm device for the ioctl
3119 * @data: data pointer for the ioctl
3120 * @file_priv: drm file for the ioctl call
f453ba04 3121 *
c8e32cc1 3122 * Add a new FB to the specified CRTC, given a user request. This is the
209f5527 3123 * original addfb ioctl which only supported RGB formats.
f453ba04
DA
3124 *
3125 * Called by the user via ioctl.
3126 *
c8e32cc1 3127 * Returns:
1a498633 3128 * Zero on success, negative errno on failure.
f453ba04
DA
3129 */
3130int drm_mode_addfb(struct drm_device *dev,
3131 void *data, struct drm_file *file_priv)
3132{
308e5bcb
JB
3133 struct drm_mode_fb_cmd *or = data;
3134 struct drm_mode_fb_cmd2 r = {};
228f2cb3 3135 int ret;
308e5bcb 3136
228f2cb3 3137 /* convert to new format and call new ioctl */
308e5bcb
JB
3138 r.fb_id = or->fb_id;
3139 r.width = or->width;
3140 r.height = or->height;
3141 r.pitches[0] = or->pitch;
3142 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3143 r.handles[0] = or->handle;
3144
228f2cb3
CE
3145 ret = drm_mode_addfb2(dev, &r, file_priv);
3146 if (ret)
3147 return ret;
308e5bcb 3148
228f2cb3 3149 or->fb_id = r.fb_id;
4b096ac1 3150
baf698b0 3151 return 0;
308e5bcb
JB
3152}
3153
cff91b62 3154static int format_check(const struct drm_mode_fb_cmd2 *r)
935b5977
VS
3155{
3156 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3157
3158 switch (format) {
3159 case DRM_FORMAT_C8:
3160 case DRM_FORMAT_RGB332:
3161 case DRM_FORMAT_BGR233:
3162 case DRM_FORMAT_XRGB4444:
3163 case DRM_FORMAT_XBGR4444:
3164 case DRM_FORMAT_RGBX4444:
3165 case DRM_FORMAT_BGRX4444:
3166 case DRM_FORMAT_ARGB4444:
3167 case DRM_FORMAT_ABGR4444:
3168 case DRM_FORMAT_RGBA4444:
3169 case DRM_FORMAT_BGRA4444:
3170 case DRM_FORMAT_XRGB1555:
3171 case DRM_FORMAT_XBGR1555:
3172 case DRM_FORMAT_RGBX5551:
3173 case DRM_FORMAT_BGRX5551:
3174 case DRM_FORMAT_ARGB1555:
3175 case DRM_FORMAT_ABGR1555:
3176 case DRM_FORMAT_RGBA5551:
3177 case DRM_FORMAT_BGRA5551:
3178 case DRM_FORMAT_RGB565:
3179 case DRM_FORMAT_BGR565:
3180 case DRM_FORMAT_RGB888:
3181 case DRM_FORMAT_BGR888:
3182 case DRM_FORMAT_XRGB8888:
3183 case DRM_FORMAT_XBGR8888:
3184 case DRM_FORMAT_RGBX8888:
3185 case DRM_FORMAT_BGRX8888:
3186 case DRM_FORMAT_ARGB8888:
3187 case DRM_FORMAT_ABGR8888:
3188 case DRM_FORMAT_RGBA8888:
3189 case DRM_FORMAT_BGRA8888:
3190 case DRM_FORMAT_XRGB2101010:
3191 case DRM_FORMAT_XBGR2101010:
3192 case DRM_FORMAT_RGBX1010102:
3193 case DRM_FORMAT_BGRX1010102:
3194 case DRM_FORMAT_ARGB2101010:
3195 case DRM_FORMAT_ABGR2101010:
3196 case DRM_FORMAT_RGBA1010102:
3197 case DRM_FORMAT_BGRA1010102:
3198 case DRM_FORMAT_YUYV:
3199 case DRM_FORMAT_YVYU:
3200 case DRM_FORMAT_UYVY:
3201 case DRM_FORMAT_VYUY:
3202 case DRM_FORMAT_AYUV:
3203 case DRM_FORMAT_NV12:
3204 case DRM_FORMAT_NV21:
3205 case DRM_FORMAT_NV16:
3206 case DRM_FORMAT_NV61:
ba623f6a
LP
3207 case DRM_FORMAT_NV24:
3208 case DRM_FORMAT_NV42:
935b5977
VS
3209 case DRM_FORMAT_YUV410:
3210 case DRM_FORMAT_YVU410:
3211 case DRM_FORMAT_YUV411:
3212 case DRM_FORMAT_YVU411:
3213 case DRM_FORMAT_YUV420:
3214 case DRM_FORMAT_YVU420:
3215 case DRM_FORMAT_YUV422:
3216 case DRM_FORMAT_YVU422:
3217 case DRM_FORMAT_YUV444:
3218 case DRM_FORMAT_YVU444:
3219 return 0;
3220 default:
23c453a4
VS
3221 DRM_DEBUG_KMS("invalid pixel format %s\n",
3222 drm_get_format_name(r->pixel_format));
935b5977
VS
3223 return -EINVAL;
3224 }
3225}
3226
cff91b62 3227static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
d1b45d5f
VS
3228{
3229 int ret, hsub, vsub, num_planes, i;
3230
3231 ret = format_check(r);
3232 if (ret) {
6ba6d03e
VS
3233 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3234 drm_get_format_name(r->pixel_format));
d1b45d5f
VS
3235 return ret;
3236 }
3237
3238 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3239 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3240 num_planes = drm_format_num_planes(r->pixel_format);
3241
3242 if (r->width == 0 || r->width % hsub) {
209f5527 3243 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
d1b45d5f
VS
3244 return -EINVAL;
3245 }
3246
3247 if (r->height == 0 || r->height % vsub) {
1aa1b11c 3248 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
d1b45d5f
VS
3249 return -EINVAL;
3250 }
3251
3252 for (i = 0; i < num_planes; i++) {
3253 unsigned int width = r->width / (i != 0 ? hsub : 1);
b180b5d1
VS
3254 unsigned int height = r->height / (i != 0 ? vsub : 1);
3255 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
d1b45d5f
VS
3256
3257 if (!r->handles[i]) {
1aa1b11c 3258 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
d1b45d5f
VS
3259 return -EINVAL;
3260 }
3261
b180b5d1
VS
3262 if ((uint64_t) width * cpp > UINT_MAX)
3263 return -ERANGE;
3264
3265 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3266 return -ERANGE;
3267
3268 if (r->pitches[i] < width * cpp) {
1aa1b11c 3269 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
d1b45d5f
VS
3270 return -EINVAL;
3271 }
e3eb3250
RC
3272
3273 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3274 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3275 r->modifier[i], i);
3276 return -EINVAL;
3277 }
570655b0
RC
3278
3279 /* modifier specific checks: */
3280 switch (r->modifier[i]) {
3281 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3282 /* NOTE: the pitch restriction may be lifted later if it turns
3283 * out that no hw has this restriction:
3284 */
3285 if (r->pixel_format != DRM_FORMAT_NV12 ||
3286 width % 128 || height % 32 ||
3287 r->pitches[i] % 128) {
3288 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3289 return -EINVAL;
3290 }
3291 break;
3292
3293 default:
3294 break;
3295 }
d1b45d5f
VS
3296 }
3297
bbe16a40
DV
3298 for (i = num_planes; i < 4; i++) {
3299 if (r->modifier[i]) {
3300 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3301 return -EINVAL;
3302 }
3303
3304 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3305 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3306 continue;
3307
3308 if (r->handles[i]) {
3309 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3310 return -EINVAL;
3311 }
3312
3313 if (r->pitches[i]) {
3314 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3315 return -EINVAL;
3316 }
3317
3318 if (r->offsets[i]) {
3319 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3320 return -EINVAL;
3321 }
3322 }
3323
d1b45d5f
VS
3324 return 0;
3325}
3326
9a6f5130
CW
3327static struct drm_framebuffer *
3328internal_framebuffer_create(struct drm_device *dev,
1eb83451 3329 const struct drm_mode_fb_cmd2 *r,
9a6f5130 3330 struct drm_file *file_priv)
308e5bcb 3331{
f453ba04
DA
3332 struct drm_mode_config *config = &dev->mode_config;
3333 struct drm_framebuffer *fb;
4a1b0714 3334 int ret;
f453ba04 3335
e3eb3250 3336 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
e3cc3520 3337 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
c394c2b0 3338 return ERR_PTR(-EINVAL);
e3cc3520
VS
3339 }
3340
f453ba04 3341 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1aa1b11c 3342 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
8cf5c917 3343 r->width, config->min_width, config->max_width);
c394c2b0 3344 return ERR_PTR(-EINVAL);
f453ba04
DA
3345 }
3346 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1aa1b11c 3347 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
8cf5c917 3348 r->height, config->min_height, config->max_height);
c394c2b0 3349 return ERR_PTR(-EINVAL);
f453ba04
DA
3350 }
3351
e3eb3250
RC
3352 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3353 !dev->mode_config.allow_fb_modifiers) {
3354 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3355 return ERR_PTR(-EINVAL);
3356 }
3357
d1b45d5f
VS
3358 ret = framebuffer_check(r);
3359 if (ret)
c394c2b0 3360 return ERR_PTR(ret);
935b5977 3361
f453ba04 3362 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
cce13ff7 3363 if (IS_ERR(fb)) {
1aa1b11c 3364 DRM_DEBUG_KMS("could not create framebuffer\n");
c394c2b0 3365 return fb;
f453ba04
DA
3366 }
3367
c394c2b0
MR
3368 return fb;
3369}
4b096ac1 3370
c394c2b0
MR
3371/**
3372 * drm_mode_addfb2 - add an FB to the graphics configuration
3373 * @dev: drm device for the ioctl
3374 * @data: data pointer for the ioctl
3375 * @file_priv: drm file for the ioctl call
3376 *
3377 * Add a new FB to the specified CRTC, given a user request with format. This is
3378 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3379 * and uses fourcc codes as pixel format specifiers.
3380 *
3381 * Called by the user via ioctl.
3382 *
3383 * Returns:
1a498633 3384 * Zero on success, negative errno on failure.
c394c2b0
MR
3385 */
3386int drm_mode_addfb2(struct drm_device *dev,
3387 void *data, struct drm_file *file_priv)
3388{
9a6f5130 3389 struct drm_mode_fb_cmd2 *r = data;
c394c2b0
MR
3390 struct drm_framebuffer *fb;
3391
3392 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3393 return -EINVAL;
3394
9a6f5130 3395 fb = internal_framebuffer_create(dev, r, file_priv);
c394c2b0
MR
3396 if (IS_ERR(fb))
3397 return PTR_ERR(fb);
3398
9a6f5130 3399 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
9a6f5130 3400 r->fb_id = fb->base.id;
c7e1c59a
DA
3401
3402 /* Transfer ownership to the filp for reaping on close */
3403 mutex_lock(&file_priv->fbs_lock);
9a6f5130
CW
3404 list_add(&fb->filp_head, &file_priv->fbs);
3405 mutex_unlock(&file_priv->fbs_lock);
3406
c394c2b0 3407 return 0;
f453ba04
DA
3408}
3409
f2d580b9
ML
3410struct drm_mode_rmfb_work {
3411 struct work_struct work;
3412 struct list_head fbs;
3413};
3414
3415static void drm_mode_rmfb_work_fn(struct work_struct *w)
3416{
3417 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3418
3419 while (!list_empty(&arg->fbs)) {
3420 struct drm_framebuffer *fb =
3421 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3422
3423 list_del_init(&fb->filp_head);
3424 drm_framebuffer_remove(fb);
3425 }
3426}
3427
f453ba04
DA
3428/**
3429 * drm_mode_rmfb - remove an FB from the configuration
065a50ed
DV
3430 * @dev: drm device for the ioctl
3431 * @data: data pointer for the ioctl
3432 * @file_priv: drm file for the ioctl call
f453ba04 3433 *
f453ba04
DA
3434 * Remove the FB specified by the user.
3435 *
3436 * Called by the user via ioctl.
3437 *
c8e32cc1 3438 * Returns:
1a498633 3439 * Zero on success, negative errno on failure.
f453ba04
DA
3440 */
3441int drm_mode_rmfb(struct drm_device *dev,
3442 void *data, struct drm_file *file_priv)
3443{
f453ba04
DA
3444 struct drm_framebuffer *fb = NULL;
3445 struct drm_framebuffer *fbl = NULL;
3446 uint32_t *id = data;
f453ba04
DA
3447 int found = 0;
3448
fb3b06c8
DA
3449 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3450 return -EINVAL;
3451
72fe90b8
DA
3452 fb = drm_framebuffer_lookup(dev, *id);
3453 if (!fb)
3454 return -ENOENT;
3455
4b096ac1 3456 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
3457 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3458 if (fb == fbl)
3459 found = 1;
72fe90b8
DA
3460 if (!found) {
3461 mutex_unlock(&file_priv->fbs_lock);
3462 goto fail_unref;
3463 }
2b677e8c 3464
4b096ac1
DV
3465 list_del_init(&fb->filp_head);
3466 mutex_unlock(&file_priv->fbs_lock);
f453ba04 3467
72fe90b8
DA
3468 /* drop the reference we picked up in framebuffer lookup */
3469 drm_framebuffer_unreference(fb);
2b677e8c 3470
f2d580b9
ML
3471 /*
3472 * we now own the reference that was stored in the fbs list
3473 *
3474 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3475 * so run this in a separate stack as there's no way to correctly
3476 * handle this after the fb is already removed from the lookup table.
3477 */
3478 if (drm_framebuffer_read_refcount(fb) > 1) {
3479 struct drm_mode_rmfb_work arg;
3480
3481 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3482 INIT_LIST_HEAD(&arg.fbs);
3483 list_add_tail(&fb->filp_head, &arg.fbs);
3484
3485 schedule_work(&arg.work);
3486 flush_work(&arg.work);
3487 destroy_work_on_stack(&arg.work);
3488 } else
3489 drm_framebuffer_unreference(fb);
3490
72fe90b8 3491 return 0;
2b677e8c 3492
72fe90b8
DA
3493fail_unref:
3494 drm_framebuffer_unreference(fb);
37c4e705 3495 return -ENOENT;
f453ba04
DA
3496}
3497
3498/**
3499 * drm_mode_getfb - get FB info
065a50ed
DV
3500 * @dev: drm device for the ioctl
3501 * @data: data pointer for the ioctl
3502 * @file_priv: drm file for the ioctl call
f453ba04 3503 *
f453ba04
DA
3504 * Lookup the FB given its ID and return info about it.
3505 *
3506 * Called by the user via ioctl.
3507 *
c8e32cc1 3508 * Returns:
1a498633 3509 * Zero on success, negative errno on failure.
f453ba04
DA
3510 */
3511int drm_mode_getfb(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3513{
3514 struct drm_mode_fb_cmd *r = data;
f453ba04 3515 struct drm_framebuffer *fb;
58c0dca1 3516 int ret;
f453ba04 3517
fb3b06c8
DA
3518 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3519 return -EINVAL;
3520
786b99ed 3521 fb = drm_framebuffer_lookup(dev, r->fb_id);
58c0dca1 3522 if (!fb)
37c4e705 3523 return -ENOENT;
f453ba04
DA
3524
3525 r->height = fb->height;
3526 r->width = fb->width;
3527 r->depth = fb->depth;
3528 r->bpp = fb->bits_per_pixel;
01f2c773 3529 r->pitch = fb->pitches[0];
101b96f3 3530 if (fb->funcs->create_handle) {
09f308f7 3531 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
43683057 3532 drm_is_control_client(file_priv)) {
101b96f3
DH
3533 ret = fb->funcs->create_handle(fb, file_priv,
3534 &r->handle);
3535 } else {
3536 /* GET_FB() is an unprivileged ioctl so we must not
3537 * return a buffer-handle to non-master processes! For
3538 * backwards-compatibility reasons, we cannot make
3539 * GET_FB() privileged, so just return an invalid handle
3540 * for non-masters. */
3541 r->handle = 0;
3542 ret = 0;
3543 }
3544 } else {
af26ef3b 3545 ret = -ENODEV;
101b96f3 3546 }
f453ba04 3547
58c0dca1
DV
3548 drm_framebuffer_unreference(fb);
3549
f453ba04
DA
3550 return ret;
3551}
3552
c8e32cc1
DV
3553/**
3554 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3555 * @dev: drm device for the ioctl
3556 * @data: data pointer for the ioctl
3557 * @file_priv: drm file for the ioctl call
3558 *
3559 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3560 * rectangle list. Generic userspace which does frontbuffer rendering must call
3561 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3562 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3563 *
3564 * Modesetting drivers which always update the frontbuffer do not need to
3565 * implement the corresponding ->dirty framebuffer callback.
3566 *
3567 * Called by the user via ioctl.
3568 *
3569 * Returns:
1a498633 3570 * Zero on success, negative errno on failure.
c8e32cc1 3571 */
884840aa
JB
3572int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3573 void *data, struct drm_file *file_priv)
3574{
3575 struct drm_clip_rect __user *clips_ptr;
3576 struct drm_clip_rect *clips = NULL;
3577 struct drm_mode_fb_dirty_cmd *r = data;
884840aa
JB
3578 struct drm_framebuffer *fb;
3579 unsigned flags;
3580 int num_clips;
4a1b0714 3581 int ret;
884840aa 3582
fb3b06c8
DA
3583 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3584 return -EINVAL;
3585
786b99ed 3586 fb = drm_framebuffer_lookup(dev, r->fb_id);
4ccf097f 3587 if (!fb)
37c4e705 3588 return -ENOENT;
884840aa
JB
3589
3590 num_clips = r->num_clips;
81f6c7f8 3591 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
884840aa
JB
3592
3593 if (!num_clips != !clips_ptr) {
3594 ret = -EINVAL;
3595 goto out_err1;
3596 }
3597
3598 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3599
3600 /* If userspace annotates copy, clips must come in pairs */
3601 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3602 ret = -EINVAL;
3603 goto out_err1;
3604 }
3605
3606 if (num_clips && clips_ptr) {
a5cd3351
XW
3607 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3608 ret = -EINVAL;
3609 goto out_err1;
3610 }
bd3f0ff9 3611 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
884840aa
JB
3612 if (!clips) {
3613 ret = -ENOMEM;
3614 goto out_err1;
3615 }
3616
3617 ret = copy_from_user(clips, clips_ptr,
3618 num_clips * sizeof(*clips));
e902a358
DC
3619 if (ret) {
3620 ret = -EFAULT;
884840aa 3621 goto out_err2;
e902a358 3622 }
884840aa
JB
3623 }
3624
3625 if (fb->funcs->dirty) {
02b00162
TH
3626 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3627 clips, num_clips);
884840aa
JB
3628 } else {
3629 ret = -ENOSYS;
884840aa
JB
3630 }
3631
3632out_err2:
3633 kfree(clips);
3634out_err1:
4ccf097f
DV
3635 drm_framebuffer_unreference(fb);
3636
884840aa
JB
3637 return ret;
3638}
3639
f453ba04
DA
3640/**
3641 * drm_fb_release - remove and free the FBs on this file
065a50ed 3642 * @priv: drm file for the ioctl
f453ba04 3643 *
f453ba04
DA
3644 * Destroy all the FBs associated with @filp.
3645 *
3646 * Called by the user via ioctl.
3647 *
c8e32cc1 3648 * Returns:
1a498633 3649 * Zero on success, negative errno on failure.
f453ba04 3650 */
ea39f835 3651void drm_fb_release(struct drm_file *priv)
f453ba04 3652{
f453ba04 3653 struct drm_framebuffer *fb, *tfb;
f2d580b9
ML
3654 struct drm_mode_rmfb_work arg;
3655
3656 INIT_LIST_HEAD(&arg.fbs);
f453ba04 3657
1b116297
DV
3658 /*
3659 * When the file gets released that means no one else can access the fb
e2db726b 3660 * list any more, so no need to grab fpriv->fbs_lock. And we need to
1b116297
DV
3661 * avoid upsetting lockdep since the universal cursor code adds a
3662 * framebuffer while holding mutex locks.
3663 *
3664 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3665 * locks is impossible here since no one else but this function can get
3666 * at it any more.
3667 */
f453ba04 3668 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
f2d580b9
ML
3669 if (drm_framebuffer_read_refcount(fb) > 1) {
3670 list_move_tail(&fb->filp_head, &arg.fbs);
3671 } else {
3672 list_del_init(&fb->filp_head);
2b677e8c 3673
f2d580b9
ML
3674 /* This drops the fpriv->fbs reference. */
3675 drm_framebuffer_unreference(fb);
3676 }
3677 }
3678
3679 if (!list_empty(&arg.fbs)) {
3680 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3681
3682 schedule_work(&arg.work);
3683 flush_work(&arg.work);
3684 destroy_work_on_stack(&arg.work);
f453ba04 3685 }
f453ba04
DA
3686}
3687
c8e32cc1
DV
3688/**
3689 * drm_property_create - create a new property type
3690 * @dev: drm device
3691 * @flags: flags specifying the property type
3692 * @name: name of the property
3693 * @num_values: number of pre-defined values
3694 *
3695 * This creates a new generic drm property which can then be attached to a drm
3696 * object with drm_object_attach_property. The returned property object must be
3697 * freed with drm_property_destroy.
3698 *
3b5b9932
DL
3699 * Note that the DRM core keeps a per-device list of properties and that, if
3700 * drm_mode_config_cleanup() is called, it will destroy all properties created
3701 * by the driver.
3702 *
c8e32cc1
DV
3703 * Returns:
3704 * A pointer to the newly created property on success, NULL on failure.
3705 */
f453ba04
DA
3706struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3707 const char *name, int num_values)
3708{
3709 struct drm_property *property = NULL;
6bfc56aa 3710 int ret;
f453ba04
DA
3711
3712 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3713 if (!property)
3714 return NULL;
3715
98f75de4
RC
3716 property->dev = dev;
3717
f453ba04 3718 if (num_values) {
bd3f0ff9
TR
3719 property->values = kcalloc(num_values, sizeof(uint64_t),
3720 GFP_KERNEL);
f453ba04
DA
3721 if (!property->values)
3722 goto fail;
3723 }
3724
6bfc56aa
VS
3725 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3726 if (ret)
3727 goto fail;
3728
f453ba04
DA
3729 property->flags = flags;
3730 property->num_values = num_values;
3758b341 3731 INIT_LIST_HEAD(&property->enum_list);
f453ba04 3732
471dd2ef 3733 if (name) {
f453ba04 3734 strncpy(property->name, name, DRM_PROP_NAME_LEN);
471dd2ef
VL
3735 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3736 }
f453ba04
DA
3737
3738 list_add_tail(&property->head, &dev->mode_config.property_list);
5ea22f24
RC
3739
3740 WARN_ON(!drm_property_type_valid(property));
3741
f453ba04
DA
3742 return property;
3743fail:
6bfc56aa 3744 kfree(property->values);
f453ba04
DA
3745 kfree(property);
3746 return NULL;
3747}
3748EXPORT_SYMBOL(drm_property_create);
3749
c8e32cc1 3750/**
2aa9d2bc 3751 * drm_property_create_enum - create a new enumeration property type
c8e32cc1
DV
3752 * @dev: drm device
3753 * @flags: flags specifying the property type
3754 * @name: name of the property
3755 * @props: enumeration lists with property values
3756 * @num_values: number of pre-defined values
3757 *
3758 * This creates a new generic drm property which can then be attached to a drm
3759 * object with drm_object_attach_property. The returned property object must be
3760 * freed with drm_property_destroy.
3761 *
3762 * Userspace is only allowed to set one of the predefined values for enumeration
3763 * properties.
3764 *
3765 * Returns:
3766 * A pointer to the newly created property on success, NULL on failure.
3767 */
4a67d391
SH
3768struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3769 const char *name,
3770 const struct drm_prop_enum_list *props,
3771 int num_values)
3772{
3773 struct drm_property *property;
3774 int i, ret;
3775
3776 flags |= DRM_MODE_PROP_ENUM;
3777
3778 property = drm_property_create(dev, flags, name, num_values);
3779 if (!property)
3780 return NULL;
3781
3782 for (i = 0; i < num_values; i++) {
3783 ret = drm_property_add_enum(property, i,
3784 props[i].type,
3785 props[i].name);
3786 if (ret) {
3787 drm_property_destroy(dev, property);
3788 return NULL;
3789 }
3790 }
3791
3792 return property;
3793}
3794EXPORT_SYMBOL(drm_property_create_enum);
3795
c8e32cc1 3796/**
2aa9d2bc 3797 * drm_property_create_bitmask - create a new bitmask property type
c8e32cc1
DV
3798 * @dev: drm device
3799 * @flags: flags specifying the property type
3800 * @name: name of the property
3801 * @props: enumeration lists with property bitflags
295ee853
DV
3802 * @num_props: size of the @props array
3803 * @supported_bits: bitmask of all supported enumeration values
c8e32cc1 3804 *
295ee853 3805 * This creates a new bitmask drm property which can then be attached to a drm
c8e32cc1
DV
3806 * object with drm_object_attach_property. The returned property object must be
3807 * freed with drm_property_destroy.
3808 *
3809 * Compared to plain enumeration properties userspace is allowed to set any
3810 * or'ed together combination of the predefined property bitflag values
3811 *
3812 * Returns:
3813 * A pointer to the newly created property on success, NULL on failure.
3814 */
49e27545
RC
3815struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3816 int flags, const char *name,
3817 const struct drm_prop_enum_list *props,
7689ffb3
VS
3818 int num_props,
3819 uint64_t supported_bits)
49e27545
RC
3820{
3821 struct drm_property *property;
7689ffb3
VS
3822 int i, ret, index = 0;
3823 int num_values = hweight64(supported_bits);
49e27545
RC
3824
3825 flags |= DRM_MODE_PROP_BITMASK;
3826
3827 property = drm_property_create(dev, flags, name, num_values);
3828 if (!property)
3829 return NULL;
7689ffb3
VS
3830 for (i = 0; i < num_props; i++) {
3831 if (!(supported_bits & (1ULL << props[i].type)))
3832 continue;
49e27545 3833
7689ffb3
VS
3834 if (WARN_ON(index >= num_values)) {
3835 drm_property_destroy(dev, property);
3836 return NULL;
3837 }
3838
3839 ret = drm_property_add_enum(property, index++,
49e27545
RC
3840 props[i].type,
3841 props[i].name);
3842 if (ret) {
3843 drm_property_destroy(dev, property);
3844 return NULL;
3845 }
3846 }
3847
3848 return property;
3849}
3850EXPORT_SYMBOL(drm_property_create_bitmask);
3851
ebc44cf3
RC
3852static struct drm_property *property_create_range(struct drm_device *dev,
3853 int flags, const char *name,
3854 uint64_t min, uint64_t max)
3855{
3856 struct drm_property *property;
3857
3858 property = drm_property_create(dev, flags, name, 2);
3859 if (!property)
3860 return NULL;
3861
3862 property->values[0] = min;
3863 property->values[1] = max;
3864
3865 return property;
3866}
3867
c8e32cc1 3868/**
960cd9d4 3869 * drm_property_create_range - create a new unsigned ranged property type
c8e32cc1
DV
3870 * @dev: drm device
3871 * @flags: flags specifying the property type
3872 * @name: name of the property
3873 * @min: minimum value of the property
3874 * @max: maximum value of the property
3875 *
3876 * This creates a new generic drm property which can then be attached to a drm
3877 * object with drm_object_attach_property. The returned property object must be
3878 * freed with drm_property_destroy.
3879 *
960cd9d4
DV
3880 * Userspace is allowed to set any unsigned integer value in the (min, max)
3881 * range inclusive.
c8e32cc1
DV
3882 *
3883 * Returns:
3884 * A pointer to the newly created property on success, NULL on failure.
3885 */
d9bc3c02
SH
3886struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3887 const char *name,
3888 uint64_t min, uint64_t max)
3889{
ebc44cf3
RC
3890 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3891 name, min, max);
d9bc3c02
SH
3892}
3893EXPORT_SYMBOL(drm_property_create_range);
3894
960cd9d4
DV
3895/**
3896 * drm_property_create_signed_range - create a new signed ranged property type
3897 * @dev: drm device
3898 * @flags: flags specifying the property type
3899 * @name: name of the property
3900 * @min: minimum value of the property
3901 * @max: maximum value of the property
3902 *
3903 * This creates a new generic drm property which can then be attached to a drm
3904 * object with drm_object_attach_property. The returned property object must be
3905 * freed with drm_property_destroy.
3906 *
3907 * Userspace is allowed to set any signed integer value in the (min, max)
3908 * range inclusive.
3909 *
3910 * Returns:
3911 * A pointer to the newly created property on success, NULL on failure.
3912 */
ebc44cf3
RC
3913struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3914 int flags, const char *name,
3915 int64_t min, int64_t max)
3916{
3917 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3918 name, I642U64(min), I642U64(max));
3919}
3920EXPORT_SYMBOL(drm_property_create_signed_range);
3921
960cd9d4
DV
3922/**
3923 * drm_property_create_object - create a new object property type
3924 * @dev: drm device
3925 * @flags: flags specifying the property type
3926 * @name: name of the property
3927 * @type: object type from DRM_MODE_OBJECT_* defines
3928 *
3929 * This creates a new generic drm property which can then be attached to a drm
3930 * object with drm_object_attach_property. The returned property object must be
3931 * freed with drm_property_destroy.
3932 *
3933 * Userspace is only allowed to set this to any property value of the given
3934 * @type. Only useful for atomic properties, which is enforced.
3935 *
3936 * Returns:
3937 * A pointer to the newly created property on success, NULL on failure.
3938 */
98f75de4
RC
3939struct drm_property *drm_property_create_object(struct drm_device *dev,
3940 int flags, const char *name, uint32_t type)
3941{
3942 struct drm_property *property;
3943
3944 flags |= DRM_MODE_PROP_OBJECT;
3945
960cd9d4
DV
3946 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3947 return NULL;
3948
98f75de4
RC
3949 property = drm_property_create(dev, flags, name, 1);
3950 if (!property)
3951 return NULL;
3952
3953 property->values[0] = type;
3954
3955 return property;
3956}
3957EXPORT_SYMBOL(drm_property_create_object);
3958
960cd9d4
DV
3959/**
3960 * drm_property_create_bool - create a new boolean property type
3961 * @dev: drm device
3962 * @flags: flags specifying the property type
3963 * @name: name of the property
3964 *
3965 * This creates a new generic drm property which can then be attached to a drm
3966 * object with drm_object_attach_property. The returned property object must be
3967 * freed with drm_property_destroy.
3968 *
3969 * This is implemented as a ranged property with only {0, 1} as valid values.
3970 *
3971 * Returns:
3972 * A pointer to the newly created property on success, NULL on failure.
3973 */
3974struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3975 const char *name)
3976{
3977 return drm_property_create_range(dev, flags, name, 0, 1);
3978}
3979EXPORT_SYMBOL(drm_property_create_bool);
3980
c8e32cc1
DV
3981/**
3982 * drm_property_add_enum - add a possible value to an enumeration property
3983 * @property: enumeration property to change
3984 * @index: index of the new enumeration
3985 * @value: value of the new enumeration
3986 * @name: symbolic name of the new enumeration
3987 *
3988 * This functions adds enumerations to a property.
3989 *
3990 * It's use is deprecated, drivers should use one of the more specific helpers
3991 * to directly create the property with all enumerations already attached.
3992 *
3993 * Returns:
3994 * Zero on success, error code on failure.
3995 */
f453ba04
DA
3996int drm_property_add_enum(struct drm_property *property, int index,
3997 uint64_t value, const char *name)
3998{
3999 struct drm_property_enum *prop_enum;
4000
5ea22f24
RC
4001 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4002 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
49e27545
RC
4003 return -EINVAL;
4004
4005 /*
4006 * Bitmask enum properties have the additional constraint of values
4007 * from 0 to 63
4008 */
5ea22f24
RC
4009 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4010 (value > 63))
f453ba04
DA
4011 return -EINVAL;
4012
3758b341
DV
4013 if (!list_empty(&property->enum_list)) {
4014 list_for_each_entry(prop_enum, &property->enum_list, head) {
f453ba04
DA
4015 if (prop_enum->value == value) {
4016 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4017 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4018 return 0;
4019 }
4020 }
4021 }
4022
4023 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4024 if (!prop_enum)
4025 return -ENOMEM;
4026
4027 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4028 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4029 prop_enum->value = value;
4030
4031 property->values[index] = value;
3758b341 4032 list_add_tail(&prop_enum->head, &property->enum_list);
f453ba04
DA
4033 return 0;
4034}
4035EXPORT_SYMBOL(drm_property_add_enum);
4036
c8e32cc1
DV
4037/**
4038 * drm_property_destroy - destroy a drm property
4039 * @dev: drm device
4040 * @property: property to destry
4041 *
4042 * This function frees a property including any attached resources like
4043 * enumeration values.
4044 */
f453ba04
DA
4045void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4046{
4047 struct drm_property_enum *prop_enum, *pt;
4048
3758b341 4049 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
f453ba04
DA
4050 list_del(&prop_enum->head);
4051 kfree(prop_enum);
4052 }
4053
4054 if (property->num_values)
4055 kfree(property->values);
7c8f6d25 4056 drm_mode_object_unregister(dev, &property->base);
f453ba04
DA
4057 list_del(&property->head);
4058 kfree(property);
4059}
4060EXPORT_SYMBOL(drm_property_destroy);
4061
c8e32cc1
DV
4062/**
4063 * drm_object_attach_property - attach a property to a modeset object
4064 * @obj: drm modeset object
4065 * @property: property to attach
4066 * @init_val: initial value of the property
4067 *
4068 * This attaches the given property to the modeset object with the given initial
4069 * value. Currently this function cannot fail since the properties are stored in
4070 * a statically sized array.
4071 */
c543188a
PZ
4072void drm_object_attach_property(struct drm_mode_object *obj,
4073 struct drm_property *property,
4074 uint64_t init_val)
4075{
7f88a9be 4076 int count = obj->properties->count;
c543188a 4077
7f88a9be
PZ
4078 if (count == DRM_OBJECT_MAX_PROPERTY) {
4079 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4080 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4081 "you see this message on the same object type.\n",
4082 obj->type);
4083 return;
c543188a
PZ
4084 }
4085
b17cd757 4086 obj->properties->properties[count] = property;
7f88a9be
PZ
4087 obj->properties->values[count] = init_val;
4088 obj->properties->count++;
88a48e29
RC
4089 if (property->flags & DRM_MODE_PROP_ATOMIC)
4090 obj->properties->atomic_count++;
c543188a
PZ
4091}
4092EXPORT_SYMBOL(drm_object_attach_property);
4093
c8e32cc1
DV
4094/**
4095 * drm_object_property_set_value - set the value of a property
4096 * @obj: drm mode object to set property value for
4097 * @property: property to set
4098 * @val: value the property should be set to
4099 *
4100 * This functions sets a given property on a given object. This function only
4101 * changes the software state of the property, it does not call into the
4102 * driver's ->set_property callback.
4103 *
4104 * Returns:
4105 * Zero on success, error code on failure.
4106 */
c543188a
PZ
4107int drm_object_property_set_value(struct drm_mode_object *obj,
4108 struct drm_property *property, uint64_t val)
4109{
4110 int i;
4111
7f88a9be 4112 for (i = 0; i < obj->properties->count; i++) {
b17cd757 4113 if (obj->properties->properties[i] == property) {
c543188a
PZ
4114 obj->properties->values[i] = val;
4115 return 0;
4116 }
4117 }
4118
4119 return -EINVAL;
4120}
4121EXPORT_SYMBOL(drm_object_property_set_value);
4122
c8e32cc1
DV
4123/**
4124 * drm_object_property_get_value - retrieve the value of a property
4125 * @obj: drm mode object to get property value from
4126 * @property: property to retrieve
4127 * @val: storage for the property value
4128 *
4129 * This function retrieves the softare state of the given property for the given
4130 * property. Since there is no driver callback to retrieve the current property
4131 * value this might be out of sync with the hardware, depending upon the driver
4132 * and property.
4133 *
4134 * Returns:
4135 * Zero on success, error code on failure.
4136 */
c543188a
PZ
4137int drm_object_property_get_value(struct drm_mode_object *obj,
4138 struct drm_property *property, uint64_t *val)
4139{
4140 int i;
4141
88a48e29
RC
4142 /* read-only properties bypass atomic mechanism and still store
4143 * their value in obj->properties->values[].. mostly to avoid
4144 * having to deal w/ EDID and similar props in atomic paths:
4145 */
4146 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4147 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4148 return drm_atomic_get_property(obj, property, val);
4149
7f88a9be 4150 for (i = 0; i < obj->properties->count; i++) {
b17cd757 4151 if (obj->properties->properties[i] == property) {
c543188a
PZ
4152 *val = obj->properties->values[i];
4153 return 0;
4154 }
4155 }
4156
4157 return -EINVAL;
4158}
4159EXPORT_SYMBOL(drm_object_property_get_value);
4160
c8e32cc1 4161/**
1a498633 4162 * drm_mode_getproperty_ioctl - get the property metadata
c8e32cc1
DV
4163 * @dev: DRM device
4164 * @data: ioctl data
4165 * @file_priv: DRM file info
4166 *
1a498633
DV
4167 * This function retrieves the metadata for a given property, like the different
4168 * possible values for an enum property or the limits for a range property.
4169 *
4170 * Blob properties are special
c8e32cc1
DV
4171 *
4172 * Called by the user via ioctl.
4173 *
4174 * Returns:
1a498633 4175 * Zero on success, negative errno on failure.
c8e32cc1 4176 */
f453ba04
DA
4177int drm_mode_getproperty_ioctl(struct drm_device *dev,
4178 void *data, struct drm_file *file_priv)
4179{
f453ba04
DA
4180 struct drm_mode_get_property *out_resp = data;
4181 struct drm_property *property;
4182 int enum_count = 0;
f453ba04
DA
4183 int value_count = 0;
4184 int ret = 0, i;
4185 int copied;
4186 struct drm_property_enum *prop_enum;
4187 struct drm_mode_property_enum __user *enum_ptr;
f453ba04 4188 uint64_t __user *values_ptr;
f453ba04 4189
fb3b06c8
DA
4190 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4191 return -EINVAL;
4192
84849903 4193 drm_modeset_lock_all(dev);
a2b34e22
RC
4194 property = drm_property_find(dev, out_resp->prop_id);
4195 if (!property) {
f27657f2 4196 ret = -ENOENT;
f453ba04
DA
4197 goto done;
4198 }
f453ba04 4199
5ea22f24
RC
4200 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4201 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3758b341 4202 list_for_each_entry(prop_enum, &property->enum_list, head)
f453ba04 4203 enum_count++;
f453ba04
DA
4204 }
4205
4206 value_count = property->num_values;
4207
4208 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4209 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4210 out_resp->flags = property->flags;
4211
4212 if ((out_resp->count_values >= value_count) && value_count) {
81f6c7f8 4213 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
4214 for (i = 0; i < value_count; i++) {
4215 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4216 ret = -EFAULT;
4217 goto done;
4218 }
4219 }
4220 }
4221 out_resp->count_values = value_count;
4222
5ea22f24
RC
4223 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4224 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
f453ba04
DA
4225 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4226 copied = 0;
81f6c7f8 4227 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3758b341 4228 list_for_each_entry(prop_enum, &property->enum_list, head) {
f453ba04
DA
4229
4230 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4231 ret = -EFAULT;
4232 goto done;
4233 }
4234
4235 if (copy_to_user(&enum_ptr[copied].name,
4236 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4237 ret = -EFAULT;
4238 goto done;
4239 }
4240 copied++;
4241 }
4242 }
4243 out_resp->count_enum_blobs = enum_count;
4244 }
4245
3758b341
DV
4246 /*
4247 * NOTE: The idea seems to have been to use this to read all the blob
4248 * property values. But nothing ever added them to the corresponding
4249 * list, userspace always used the special-purpose get_blob ioctl to
4250 * read the value for a blob property. It also doesn't make a lot of
4251 * sense to return values here when everything else is just metadata for
4252 * the property itself.
4253 */
4254 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4255 out_resp->count_enum_blobs = 0;
f453ba04 4256done:
84849903 4257 drm_modeset_unlock_all(dev);
f453ba04
DA
4258 return ret;
4259}
4260
152ef5fa
DV
4261static void drm_property_free_blob(struct kref *kref)
4262{
4263 struct drm_property_blob *blob =
4264 container_of(kref, struct drm_property_blob, base.refcount);
4265
4266 mutex_lock(&blob->dev->mode_config.blob_lock);
4267 list_del(&blob->head_global);
4268 mutex_unlock(&blob->dev->mode_config.blob_lock);
4269
4270 drm_mode_object_unregister(blob->dev, &blob->base);
4271
4272 kfree(blob);
4273}
4274
99531d9b
DS
4275/**
4276 * drm_property_create_blob - Create new blob property
4277 *
4278 * Creates a new blob property for a specified DRM device, optionally
4279 * copying data.
4280 *
4281 * @dev: DRM device to create property for
4282 * @length: Length to allocate for blob data
4283 * @data: If specified, copies data into blob
10e8cb7e
DS
4284 *
4285 * Returns:
4286 * New blob property with a single reference on success, or an ERR_PTR
4287 * value on failure.
99531d9b 4288 */
6bcacf51 4289struct drm_property_blob *
ecbbe59b 4290drm_property_create_blob(struct drm_device *dev, size_t length,
12e6cecd 4291 const void *data)
f453ba04
DA
4292{
4293 struct drm_property_blob *blob;
6bfc56aa 4294 int ret;
f453ba04 4295
9ac0934b 4296 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
10e8cb7e 4297 return ERR_PTR(-EINVAL);
f453ba04
DA
4298
4299 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4300 if (!blob)
10e8cb7e 4301 return ERR_PTR(-ENOMEM);
f453ba04 4302
e2f5d2ea
DS
4303 /* This must be explicitly initialised, so we can safely call list_del
4304 * on it in the removal handler, even if it isn't in a file list. */
4305 INIT_LIST_HEAD(&blob->head_file);
8fb6e7a5 4306 blob->length = length;
6bcacf51 4307 blob->dev = dev;
8fb6e7a5 4308
99531d9b
DS
4309 if (data)
4310 memcpy(blob->data, data, length);
8fb6e7a5 4311
152ef5fa
DV
4312 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4313 true, drm_property_free_blob);
6bfc56aa
VS
4314 if (ret) {
4315 kfree(blob);
10e8cb7e 4316 return ERR_PTR(-EINVAL);
6bfc56aa
VS
4317 }
4318
152ef5fa 4319 mutex_lock(&dev->mode_config.blob_lock);
e2f5d2ea
DS
4320 list_add_tail(&blob->head_global,
4321 &dev->mode_config.property_blob_list);
8fb6e7a5 4322 mutex_unlock(&dev->mode_config.blob_lock);
f453ba04 4323
f453ba04
DA
4324 return blob;
4325}
6bcacf51 4326EXPORT_SYMBOL(drm_property_create_blob);
f453ba04 4327
6bcacf51
DS
4328/**
4329 * drm_property_unreference_blob - Unreference a blob property
4330 *
4331 * Drop a reference on a blob property. May free the object.
4332 *
f102c16e 4333 * @blob: Pointer to blob property
6bcacf51
DS
4334 */
4335void drm_property_unreference_blob(struct drm_property_blob *blob)
4336{
6bcacf51
DS
4337 if (!blob)
4338 return;
4339
152ef5fa 4340 drm_mode_object_unreference(&blob->base);
6bcacf51
DS
4341}
4342EXPORT_SYMBOL(drm_property_unreference_blob);
4343
e2f5d2ea
DS
4344/**
4345 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4346 * @dev: DRM device
4347 * @file_priv: destroy all blobs owned by this file handle
4348 */
4349void drm_property_destroy_user_blobs(struct drm_device *dev,
4350 struct drm_file *file_priv)
4351{
4352 struct drm_property_blob *blob, *bt;
4353
152ef5fa
DV
4354 /*
4355 * When the file gets released that means no one else can access the
4356 * blob list any more, so no need to grab dev->blob_lock.
4357 */
e2f5d2ea
DS
4358 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4359 list_del_init(&blob->head_file);
152ef5fa 4360 drm_property_unreference_blob(blob);
e2f5d2ea 4361 }
e2f5d2ea
DS
4362}
4363
6bcacf51
DS
4364/**
4365 * drm_property_reference_blob - Take a reference on an existing property
4366 *
4367 * Take a new reference on an existing blob property.
4368 *
f102c16e 4369 * @blob: Pointer to blob property
6bcacf51
DS
4370 */
4371struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4372{
152ef5fa 4373 drm_mode_object_reference(&blob->base);
6bcacf51
DS
4374 return blob;
4375}
4376EXPORT_SYMBOL(drm_property_reference_blob);
4377
6bcacf51
DS
4378/**
4379 * drm_property_lookup_blob - look up a blob property and take a reference
4380 * @dev: drm device
4381 * @id: id of the blob property
4382 *
4383 * If successful, this takes an additional reference to the blob property.
4384 * callers need to make sure to eventually unreference the returned property
4385 * again, using @drm_property_unreference_blob.
4386 */
4387struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4388 uint32_t id)
4389{
152ef5fa
DV
4390 struct drm_mode_object *obj;
4391 struct drm_property_blob *blob = NULL;
6bcacf51 4392
152ef5fa
DV
4393 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4394 if (obj)
4395 blob = obj_to_blob(obj);
6bcacf51
DS
4396 return blob;
4397}
4398EXPORT_SYMBOL(drm_property_lookup_blob);
4399
d2ed3436
DS
4400/**
4401 * drm_property_replace_global_blob - atomically replace existing blob property
4402 * @dev: drm device
4403 * @replace: location of blob property pointer to be replaced
4404 * @length: length of data for new blob, or 0 for no data
4405 * @data: content for new blob, or NULL for no data
4406 * @obj_holds_id: optional object for property holding blob ID
4407 * @prop_holds_id: optional property holding blob ID
4408 * @return 0 on success or error on failure
4409 *
4410 * This function will atomically replace a global property in the blob list,
4411 * optionally updating a property which holds the ID of that property. It is
4412 * guaranteed to be atomic: no caller will be allowed to see intermediate
4413 * results, and either the entire operation will succeed and clean up the
4414 * previous property, or it will fail and the state will be unchanged.
4415 *
4416 * If length is 0 or data is NULL, no new blob will be created, and the holding
4417 * property, if specified, will be set to 0.
4418 *
4419 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4420 * by holding the relevant modesetting object lock for its parent.
4421 *
4422 * For example, a drm_connector has a 'PATH' property, which contains the ID
4423 * of a blob property with the value of the MST path information. Calling this
4424 * function with replace pointing to the connector's path_blob_ptr, length and
4425 * data set for the new path information, obj_holds_id set to the connector's
4426 * base object, and prop_holds_id set to the path property name, will perform
4427 * a completely atomic update. The access to path_blob_ptr is protected by the
4428 * caller holding a lock on the connector.
4429 */
4430static int drm_property_replace_global_blob(struct drm_device *dev,
4431 struct drm_property_blob **replace,
4432 size_t length,
4433 const void *data,
4434 struct drm_mode_object *obj_holds_id,
4435 struct drm_property *prop_holds_id)
4436{
4437 struct drm_property_blob *new_blob = NULL;
4438 struct drm_property_blob *old_blob = NULL;
4439 int ret;
4440
4441 WARN_ON(replace == NULL);
4442
4443 old_blob = *replace;
4444
4445 if (length && data) {
4446 new_blob = drm_property_create_blob(dev, length, data);
10e8cb7e
DS
4447 if (IS_ERR(new_blob))
4448 return PTR_ERR(new_blob);
d2ed3436
DS
4449 }
4450
4451 /* This does not need to be synchronised with blob_lock, as the
4452 * get_properties ioctl locks all modesetting objects, and
4453 * obj_holds_id must be locked before calling here, so we cannot
4454 * have its value out of sync with the list membership modified
4455 * below under blob_lock. */
4456 if (obj_holds_id) {
4457 ret = drm_object_property_set_value(obj_holds_id,
4458 prop_holds_id,
4459 new_blob ?
4460 new_blob->base.id : 0);
4461 if (ret != 0)
4462 goto err_created;
4463 }
4464
ec530829 4465 drm_property_unreference_blob(old_blob);
d2ed3436
DS
4466 *replace = new_blob;
4467
4468 return 0;
4469
4470err_created:
6bcacf51 4471 drm_property_unreference_blob(new_blob);
d2ed3436
DS
4472 return ret;
4473}
4474
c8e32cc1
DV
4475/**
4476 * drm_mode_getblob_ioctl - get the contents of a blob property value
4477 * @dev: DRM device
4478 * @data: ioctl data
4479 * @file_priv: DRM file info
4480 *
4481 * This function retrieves the contents of a blob property. The value stored in
4482 * an object's blob property is just a normal modeset object id.
4483 *
4484 * Called by the user via ioctl.
4485 *
4486 * Returns:
1a498633 4487 * Zero on success, negative errno on failure.
c8e32cc1 4488 */
f453ba04
DA
4489int drm_mode_getblob_ioctl(struct drm_device *dev,
4490 void *data, struct drm_file *file_priv)
4491{
f453ba04
DA
4492 struct drm_mode_get_blob *out_resp = data;
4493 struct drm_property_blob *blob;
4494 int ret = 0;
81f6c7f8 4495 void __user *blob_ptr;
f453ba04 4496
fb3b06c8
DA
4497 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4498 return -EINVAL;
4499
152ef5fa
DV
4500 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4501 if (!blob)
4502 return -ENOENT;
f453ba04
DA
4503
4504 if (out_resp->length == blob->length) {
81f6c7f8 4505 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4dfd909f 4506 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
f453ba04 4507 ret = -EFAULT;
152ef5fa 4508 goto unref;
f453ba04
DA
4509 }
4510 }
4511 out_resp->length = blob->length;
152ef5fa
DV
4512unref:
4513 drm_property_unreference_blob(blob);
f453ba04 4514
f453ba04
DA
4515 return ret;
4516}
4517
e2f5d2ea
DS
4518/**
4519 * drm_mode_createblob_ioctl - create a new blob property
4520 * @dev: DRM device
4521 * @data: ioctl data
4522 * @file_priv: DRM file info
4523 *
4524 * This function creates a new blob property with user-defined values. In order
4525 * to give us sensible validation and checking when creating, rather than at
4526 * every potential use, we also require a type to be provided upfront.
4527 *
4528 * Called by the user via ioctl.
4529 *
4530 * Returns:
4531 * Zero on success, negative errno on failure.
4532 */
4533int drm_mode_createblob_ioctl(struct drm_device *dev,
4534 void *data, struct drm_file *file_priv)
4535{
4536 struct drm_mode_create_blob *out_resp = data;
4537 struct drm_property_blob *blob;
4538 void __user *blob_ptr;
4539 int ret = 0;
4540
4541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4542 return -EINVAL;
4543
4544 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4545 if (IS_ERR(blob))
4546 return PTR_ERR(blob);
4547
4548 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4549 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4550 ret = -EFAULT;
4551 goto out_blob;
4552 }
4553
4554 /* Dropping the lock between create_blob and our access here is safe
4555 * as only the same file_priv can remove the blob; at this point, it is
4556 * not associated with any file_priv. */
4557 mutex_lock(&dev->mode_config.blob_lock);
4558 out_resp->blob_id = blob->base.id;
8731b269 4559 list_add_tail(&blob->head_file, &file_priv->blobs);
e2f5d2ea
DS
4560 mutex_unlock(&dev->mode_config.blob_lock);
4561
4562 return 0;
4563
4564out_blob:
4565 drm_property_unreference_blob(blob);
4566 return ret;
4567}
4568
4569/**
4570 * drm_mode_destroyblob_ioctl - destroy a user blob property
4571 * @dev: DRM device
4572 * @data: ioctl data
4573 * @file_priv: DRM file info
4574 *
4575 * Destroy an existing user-defined blob property.
4576 *
4577 * Called by the user via ioctl.
4578 *
4579 * Returns:
4580 * Zero on success, negative errno on failure.
4581 */
4582int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4583 void *data, struct drm_file *file_priv)
4584{
4585 struct drm_mode_destroy_blob *out_resp = data;
4586 struct drm_property_blob *blob = NULL, *bt;
4587 bool found = false;
4588 int ret = 0;
4589
4590 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4591 return -EINVAL;
4592
152ef5fa
DV
4593 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4594 if (!blob)
4595 return -ENOENT;
e2f5d2ea 4596
152ef5fa 4597 mutex_lock(&dev->mode_config.blob_lock);
e2f5d2ea
DS
4598 /* Ensure the property was actually created by this user. */
4599 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4600 if (bt == blob) {
4601 found = true;
4602 break;
4603 }
4604 }
4605
4606 if (!found) {
4607 ret = -EPERM;
4608 goto err;
4609 }
4610
4611 /* We must drop head_file here, because we may not be the last
4612 * reference on the blob. */
4613 list_del_init(&blob->head_file);
e2f5d2ea
DS
4614 mutex_unlock(&dev->mode_config.blob_lock);
4615
152ef5fa
DV
4616 /* One reference from lookup, and one from the filp. */
4617 drm_property_unreference_blob(blob);
4618 drm_property_unreference_blob(blob);
4619
e2f5d2ea
DS
4620 return 0;
4621
4622err:
4623 mutex_unlock(&dev->mode_config.blob_lock);
152ef5fa
DV
4624 drm_property_unreference_blob(blob);
4625
e2f5d2ea
DS
4626 return ret;
4627}
4628
cc7096fb
DA
4629/**
4630 * drm_mode_connector_set_path_property - set tile property on connector
4631 * @connector: connector to set property on.
d2ed3436 4632 * @path: path to use for property; must not be NULL.
cc7096fb
DA
4633 *
4634 * This creates a property to expose to userspace to specify a
4635 * connector path. This is mainly used for DisplayPort MST where
4636 * connectors have a topology and we want to allow userspace to give
4637 * them more meaningful names.
4638 *
4639 * Returns:
1a498633 4640 * Zero on success, negative errno on failure.
cc7096fb 4641 */
43aba7eb 4642int drm_mode_connector_set_path_property(struct drm_connector *connector,
12e6cecd 4643 const char *path)
43aba7eb
DA
4644{
4645 struct drm_device *dev = connector->dev;
ecbbe59b 4646 int ret;
43aba7eb 4647
d2ed3436
DS
4648 ret = drm_property_replace_global_blob(dev,
4649 &connector->path_blob_ptr,
4650 strlen(path) + 1,
4651 path,
4652 &connector->base,
4653 dev->mode_config.path_property);
43aba7eb
DA
4654 return ret;
4655}
4656EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4657
6f134d7b
DA
4658/**
4659 * drm_mode_connector_set_tile_property - set tile property on connector
4660 * @connector: connector to set property on.
4661 *
4662 * This looks up the tile information for a connector, and creates a
4663 * property for userspace to parse if it exists. The property is of
4664 * the form of 8 integers using ':' as a separator.
4665 *
4666 * Returns:
4667 * Zero on success, errno on failure.
4668 */
4669int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4670{
4671 struct drm_device *dev = connector->dev;
6f134d7b 4672 char tile[256];
d2ed3436 4673 int ret;
6f134d7b
DA
4674
4675 if (!connector->has_tile) {
d2ed3436
DS
4676 ret = drm_property_replace_global_blob(dev,
4677 &connector->tile_blob_ptr,
4678 0,
4679 NULL,
4680 &connector->base,
4681 dev->mode_config.tile_property);
6f134d7b
DA
4682 return ret;
4683 }
4684
4685 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4686 connector->tile_group->id, connector->tile_is_single_monitor,
4687 connector->num_h_tile, connector->num_v_tile,
4688 connector->tile_h_loc, connector->tile_v_loc,
4689 connector->tile_h_size, connector->tile_v_size);
6f134d7b 4690
d2ed3436
DS
4691 ret = drm_property_replace_global_blob(dev,
4692 &connector->tile_blob_ptr,
4693 strlen(tile) + 1,
4694 tile,
4695 &connector->base,
4696 dev->mode_config.tile_property);
6f134d7b
DA
4697 return ret;
4698}
4699EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4700
c8e32cc1
DV
4701/**
4702 * drm_mode_connector_update_edid_property - update the edid property of a connector
4703 * @connector: drm connector
4704 * @edid: new value of the edid property
4705 *
4706 * This function creates a new blob modeset object and assigns its id to the
4707 * connector's edid property.
4708 *
4709 * Returns:
1a498633 4710 * Zero on success, negative errno on failure.
c8e32cc1 4711 */
f453ba04 4712int drm_mode_connector_update_edid_property(struct drm_connector *connector,
12e6cecd 4713 const struct edid *edid)
f453ba04
DA
4714{
4715 struct drm_device *dev = connector->dev;
d2ed3436 4716 size_t size = 0;
ecbbe59b 4717 int ret;
f453ba04 4718
4cf2b281
TW
4719 /* ignore requests to set edid when overridden */
4720 if (connector->override_edid)
4721 return 0;
4722
d2ed3436 4723 if (edid)
e24ff467 4724 size = EDID_LENGTH * (1 + edid->extensions);
f453ba04 4725
d2ed3436
DS
4726 ret = drm_property_replace_global_blob(dev,
4727 &connector->edid_blob_ptr,
4728 size,
4729 edid,
4730 &connector->base,
4731 dev->mode_config.edid_property);
f453ba04
DA
4732 return ret;
4733}
4734EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4735
3843e71f
RC
4736/* Some properties could refer to dynamic refcnt'd objects, or things that
4737 * need special locking to handle lifetime issues (ie. to ensure the prop
4738 * value doesn't become invalid part way through the property update due to
4739 * race). The value returned by reference via 'obj' should be passed back
4740 * to drm_property_change_valid_put() after the property is set (and the
4741 * object to which the property is attached has a chance to take it's own
4742 * reference).
4743 */
d34f20d6 4744bool drm_property_change_valid_get(struct drm_property *property,
3843e71f 4745 uint64_t value, struct drm_mode_object **ref)
26a34815 4746{
2ca651d1
TR
4747 int i;
4748
26a34815
PZ
4749 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4750 return false;
5ea22f24 4751
3843e71f
RC
4752 *ref = NULL;
4753
5ea22f24 4754 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
26a34815
PZ
4755 if (value < property->values[0] || value > property->values[1])
4756 return false;
4757 return true;
ebc44cf3
RC
4758 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4759 int64_t svalue = U642I64(value);
4dfd909f 4760
ebc44cf3
RC
4761 if (svalue < U642I64(property->values[0]) ||
4762 svalue > U642I64(property->values[1]))
4763 return false;
4764 return true;
5ea22f24 4765 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
592c20ee 4766 uint64_t valid_mask = 0;
4dfd909f 4767
49e27545
RC
4768 for (i = 0; i < property->num_values; i++)
4769 valid_mask |= (1ULL << property->values[i]);
4770 return !(value & ~valid_mask);
5ea22f24 4771 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
6bcacf51
DS
4772 struct drm_property_blob *blob;
4773
4774 if (value == 0)
4775 return true;
4776
4777 blob = drm_property_lookup_blob(property->dev, value);
4778 if (blob) {
4779 *ref = &blob->base;
4780 return true;
4781 } else {
4782 return false;
4783 }
98f75de4 4784 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
98f75de4
RC
4785 /* a zero value for an object property translates to null: */
4786 if (value == 0)
4787 return true;
3843e71f 4788
1e8985a8
TV
4789 *ref = _object_find(property->dev, value, property->values[0]);
4790 return *ref != NULL;
26a34815 4791 }
2ca651d1
TR
4792
4793 for (i = 0; i < property->num_values; i++)
4794 if (property->values[i] == value)
4795 return true;
4796 return false;
26a34815
PZ
4797}
4798
d34f20d6 4799void drm_property_change_valid_put(struct drm_property *property,
3843e71f
RC
4800 struct drm_mode_object *ref)
4801{
4802 if (!ref)
4803 return;
4804
4805 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
027b3f8b 4806 drm_mode_object_unreference(ref);
da9b2a38
DS
4807 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4808 drm_property_unreference_blob(obj_to_blob(ref));
3843e71f
RC
4809}
4810
c8e32cc1
DV
4811/**
4812 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4813 * @dev: DRM device
4814 * @data: ioctl data
4815 * @file_priv: DRM file info
4816 *
4817 * This function sets the current value for a connectors's property. It also
4818 * calls into a driver's ->set_property callback to update the hardware state
4819 *
4820 * Called by the user via ioctl.
4821 *
4822 * Returns:
1a498633 4823 * Zero on success, negative errno on failure.
c8e32cc1 4824 */
f453ba04
DA
4825int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4826 void *data, struct drm_file *file_priv)
4827{
0057d8dd
PZ
4828 struct drm_mode_connector_set_property *conn_set_prop = data;
4829 struct drm_mode_obj_set_property obj_set_prop = {
4830 .value = conn_set_prop->value,
4831 .prop_id = conn_set_prop->prop_id,
4832 .obj_id = conn_set_prop->connector_id,
4833 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4834 };
fb3b06c8 4835
0057d8dd
PZ
4836 /* It does all the locking and checking we need */
4837 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
f453ba04
DA
4838}
4839
c543188a
PZ
4840static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4841 struct drm_property *property,
4842 uint64_t value)
4843{
4844 int ret = -EINVAL;
4845 struct drm_connector *connector = obj_to_connector(obj);
4846
4847 /* Do DPMS ourselves */
4848 if (property == connector->dev->mode_config.dpms_property) {
3558c112 4849 ret = (*connector->funcs->dpms)(connector, (int)value);
c543188a
PZ
4850 } else if (connector->funcs->set_property)
4851 ret = connector->funcs->set_property(connector, property, value);
4852
4853 /* store the property value if successful */
4854 if (!ret)
58495563 4855 drm_object_property_set_value(&connector->base, property, value);
c543188a
PZ
4856 return ret;
4857}
4858
bffd9de0
PZ
4859static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4860 struct drm_property *property,
4861 uint64_t value)
4862{
4863 int ret = -EINVAL;
4864 struct drm_crtc *crtc = obj_to_crtc(obj);
4865
4866 if (crtc->funcs->set_property)
4867 ret = crtc->funcs->set_property(crtc, property, value);
4868 if (!ret)
4869 drm_object_property_set_value(obj, property, value);
4870
4871 return ret;
4872}
4873
3a5f87c2
TW
4874/**
4875 * drm_mode_plane_set_obj_prop - set the value of a property
4876 * @plane: drm plane object to set property value for
4877 * @property: property to set
4878 * @value: value the property should be set to
4879 *
4880 * This functions sets a given property on a given plane object. This function
4881 * calls the driver's ->set_property callback and changes the software state of
4882 * the property if the callback succeeds.
4883 *
4884 * Returns:
4885 * Zero on success, error code on failure.
4886 */
4887int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4888 struct drm_property *property,
4889 uint64_t value)
4d93914a
RC
4890{
4891 int ret = -EINVAL;
3a5f87c2 4892 struct drm_mode_object *obj = &plane->base;
4d93914a
RC
4893
4894 if (plane->funcs->set_property)
4895 ret = plane->funcs->set_property(plane, property, value);
4896 if (!ret)
4897 drm_object_property_set_value(obj, property, value);
4898
4899 return ret;
4900}
3a5f87c2 4901EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4d93914a 4902
c8e32cc1 4903/**
1a498633 4904 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
c8e32cc1
DV
4905 * @dev: DRM device
4906 * @data: ioctl data
4907 * @file_priv: DRM file info
4908 *
4909 * This function retrieves the current value for an object's property. Compared
4910 * to the connector specific ioctl this one is extended to also work on crtc and
4911 * plane objects.
4912 *
4913 * Called by the user via ioctl.
4914 *
4915 * Returns:
1a498633 4916 * Zero on success, negative errno on failure.
c8e32cc1 4917 */
c543188a
PZ
4918int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4919 struct drm_file *file_priv)
4920{
4921 struct drm_mode_obj_get_properties *arg = data;
4922 struct drm_mode_object *obj;
4923 int ret = 0;
c543188a
PZ
4924
4925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4926 return -EINVAL;
4927
84849903 4928 drm_modeset_lock_all(dev);
c543188a
PZ
4929
4930 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4931 if (!obj) {
f27657f2 4932 ret = -ENOENT;
c543188a
PZ
4933 goto out;
4934 }
4935 if (!obj->properties) {
4936 ret = -EINVAL;
1649c33b 4937 goto out_unref;
c543188a
PZ
4938 }
4939
88a48e29 4940 ret = get_properties(obj, file_priv->atomic,
95cbf110
RC
4941 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4942 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4943 &arg->count_props);
c543188a 4944
1649c33b
DV
4945out_unref:
4946 drm_mode_object_unreference(obj);
c543188a 4947out:
84849903 4948 drm_modeset_unlock_all(dev);
c543188a
PZ
4949 return ret;
4950}
4951
c8e32cc1
DV
4952/**
4953 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4954 * @dev: DRM device
4955 * @data: ioctl data
4956 * @file_priv: DRM file info
4957 *
4958 * This function sets the current value for an object's property. It also calls
4959 * into a driver's ->set_property callback to update the hardware state.
4960 * Compared to the connector specific ioctl this one is extended to also work on
4961 * crtc and plane objects.
4962 *
4963 * Called by the user via ioctl.
4964 *
4965 * Returns:
1a498633 4966 * Zero on success, negative errno on failure.
c8e32cc1 4967 */
c543188a
PZ
4968int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4969 struct drm_file *file_priv)
4970{
4971 struct drm_mode_obj_set_property *arg = data;
4972 struct drm_mode_object *arg_obj;
4973 struct drm_mode_object *prop_obj;
4974 struct drm_property *property;
3843e71f
RC
4975 int i, ret = -EINVAL;
4976 struct drm_mode_object *ref;
c543188a
PZ
4977
4978 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4979 return -EINVAL;
4980
84849903 4981 drm_modeset_lock_all(dev);
c543188a
PZ
4982
4983 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
f27657f2
VS
4984 if (!arg_obj) {
4985 ret = -ENOENT;
c543188a 4986 goto out;
f27657f2 4987 }
c543188a 4988 if (!arg_obj->properties)
1649c33b 4989 goto out_unref;
c543188a 4990
7f88a9be 4991 for (i = 0; i < arg_obj->properties->count; i++)
b17cd757 4992 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
c543188a
PZ
4993 break;
4994
7f88a9be 4995 if (i == arg_obj->properties->count)
1649c33b 4996 goto out_unref;
c543188a
PZ
4997
4998 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4999 DRM_MODE_OBJECT_PROPERTY);
f27657f2
VS
5000 if (!prop_obj) {
5001 ret = -ENOENT;
1649c33b 5002 goto out_unref;
f27657f2 5003 }
c543188a
PZ
5004 property = obj_to_property(prop_obj);
5005
3843e71f 5006 if (!drm_property_change_valid_get(property, arg->value, &ref))
b164d31f 5007 goto out_unref;
c543188a
PZ
5008
5009 switch (arg_obj->type) {
5010 case DRM_MODE_OBJECT_CONNECTOR:
5011 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5012 arg->value);
5013 break;
bffd9de0
PZ
5014 case DRM_MODE_OBJECT_CRTC:
5015 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5016 break;
4d93914a 5017 case DRM_MODE_OBJECT_PLANE:
3a5f87c2
TW
5018 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5019 property, arg->value);
4d93914a 5020 break;
c543188a
PZ
5021 }
5022
3843e71f
RC
5023 drm_property_change_valid_put(property, ref);
5024
1649c33b
DV
5025out_unref:
5026 drm_mode_object_unreference(arg_obj);
c543188a 5027out:
84849903 5028 drm_modeset_unlock_all(dev);
c543188a
PZ
5029 return ret;
5030}
5031
c8e32cc1
DV
5032/**
5033 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5034 * @connector: connector to attach
5035 * @encoder: encoder to attach @connector to
5036 *
5037 * This function links up a connector to an encoder. Note that the routing
5038 * restrictions between encoders and crtcs are exposed to userspace through the
5039 * possible_clones and possible_crtcs bitmasks.
5040 *
5041 * Returns:
1a498633 5042 * Zero on success, negative errno on failure.
c8e32cc1 5043 */
f453ba04
DA
5044int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5045 struct drm_encoder *encoder)
5046{
5047 int i;
5048
eb47fe80
TR
5049 /*
5050 * In the past, drivers have attempted to model the static association
5051 * of connector to encoder in simple connector/encoder devices using a
5052 * direct assignment of connector->encoder = encoder. This connection
5053 * is a logical one and the responsibility of the core, so drivers are
5054 * expected not to mess with this.
5055 *
5056 * Note that the error return should've been enough here, but a large
5057 * majority of drivers ignores the return value, so add in a big WARN
5058 * to get people's attention.
5059 */
5060 if (WARN_ON(connector->encoder))
5061 return -EINVAL;
5062
f453ba04
DA
5063 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5064 if (connector->encoder_ids[i] == 0) {
5065 connector->encoder_ids[i] = encoder->base.id;
5066 return 0;
5067 }
5068 }
5069 return -ENOMEM;
5070}
5071EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5072
c8e32cc1
DV
5073/**
5074 * drm_mode_crtc_set_gamma_size - set the gamma table size
5075 * @crtc: CRTC to set the gamma table size for
5076 * @gamma_size: size of the gamma table
5077 *
5078 * Drivers which support gamma tables should set this to the supported gamma
5079 * table size when initializing the CRTC. Currently the drm core only supports a
5080 * fixed gamma table size.
5081 *
5082 * Returns:
1a498633 5083 * Zero on success, negative errno on failure.
c8e32cc1 5084 */
4cae5b84 5085int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
c8e32cc1 5086 int gamma_size)
f453ba04 5087{
cf48e292
DV
5088 uint16_t *r_base, *g_base, *b_base;
5089 int i;
5090
f453ba04
DA
5091 crtc->gamma_size = gamma_size;
5092
bd3f0ff9
TR
5093 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5094 GFP_KERNEL);
f453ba04
DA
5095 if (!crtc->gamma_store) {
5096 crtc->gamma_size = 0;
4cae5b84 5097 return -ENOMEM;
f453ba04
DA
5098 }
5099
cf48e292
DV
5100 r_base = crtc->gamma_store;
5101 g_base = r_base + gamma_size;
5102 b_base = g_base + gamma_size;
5103 for (i = 0; i < gamma_size; i++) {
5104 r_base[i] = i << 8;
5105 g_base[i] = i << 8;
5106 b_base[i] = i << 8;
5107 }
5108
5109
4cae5b84 5110 return 0;
f453ba04
DA
5111}
5112EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5113
c8e32cc1
DV
5114/**
5115 * drm_mode_gamma_set_ioctl - set the gamma table
5116 * @dev: DRM device
5117 * @data: ioctl data
5118 * @file_priv: DRM file info
5119 *
5120 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5121 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5122 *
5123 * Called by the user via ioctl.
5124 *
5125 * Returns:
1a498633 5126 * Zero on success, negative errno on failure.
c8e32cc1 5127 */
f453ba04
DA
5128int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5129 void *data, struct drm_file *file_priv)
5130{
5131 struct drm_mode_crtc_lut *crtc_lut = data;
f453ba04
DA
5132 struct drm_crtc *crtc;
5133 void *r_base, *g_base, *b_base;
5134 int size;
5135 int ret = 0;
5136
fb3b06c8
DA
5137 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5138 return -EINVAL;
5139
84849903 5140 drm_modeset_lock_all(dev);
a2b34e22
RC
5141 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5142 if (!crtc) {
f27657f2 5143 ret = -ENOENT;
f453ba04
DA
5144 goto out;
5145 }
f453ba04 5146
ebe0f244
LP
5147 if (crtc->funcs->gamma_set == NULL) {
5148 ret = -ENOSYS;
5149 goto out;
5150 }
5151
f453ba04
DA
5152 /* memcpy into gamma store */
5153 if (crtc_lut->gamma_size != crtc->gamma_size) {
5154 ret = -EINVAL;
5155 goto out;
5156 }
5157
5158 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5159 r_base = crtc->gamma_store;
5160 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5161 ret = -EFAULT;
5162 goto out;
5163 }
5164
5165 g_base = r_base + size;
5166 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5167 ret = -EFAULT;
5168 goto out;
5169 }
5170
5171 b_base = g_base + size;
5172 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5173 ret = -EFAULT;
5174 goto out;
5175 }
5176
7ea77283 5177 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
f453ba04
DA
5178
5179out:
84849903 5180 drm_modeset_unlock_all(dev);
f453ba04
DA
5181 return ret;
5182
5183}
5184
c8e32cc1
DV
5185/**
5186 * drm_mode_gamma_get_ioctl - get the gamma table
5187 * @dev: DRM device
5188 * @data: ioctl data
5189 * @file_priv: DRM file info
5190 *
5191 * Copy the current gamma table into the storage provided. This also provides
5192 * the gamma table size the driver expects, which can be used to size the
5193 * allocated storage.
5194 *
5195 * Called by the user via ioctl.
5196 *
5197 * Returns:
1a498633 5198 * Zero on success, negative errno on failure.
c8e32cc1 5199 */
f453ba04
DA
5200int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5201 void *data, struct drm_file *file_priv)
5202{
5203 struct drm_mode_crtc_lut *crtc_lut = data;
f453ba04
DA
5204 struct drm_crtc *crtc;
5205 void *r_base, *g_base, *b_base;
5206 int size;
5207 int ret = 0;
5208
fb3b06c8
DA
5209 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5210 return -EINVAL;
5211
84849903 5212 drm_modeset_lock_all(dev);
a2b34e22
RC
5213 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5214 if (!crtc) {
f27657f2 5215 ret = -ENOENT;
f453ba04
DA
5216 goto out;
5217 }
f453ba04
DA
5218
5219 /* memcpy into gamma store */
5220 if (crtc_lut->gamma_size != crtc->gamma_size) {
5221 ret = -EINVAL;
5222 goto out;
5223 }
5224
5225 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5226 r_base = crtc->gamma_store;
5227 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5228 ret = -EFAULT;
5229 goto out;
5230 }
5231
5232 g_base = r_base + size;
5233 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5234 ret = -EFAULT;
5235 goto out;
5236 }
5237
5238 b_base = g_base + size;
5239 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5240 ret = -EFAULT;
5241 goto out;
5242 }
5243out:
84849903 5244 drm_modeset_unlock_all(dev);
f453ba04
DA
5245 return ret;
5246}
d91d8a3f 5247
c8e32cc1
DV
5248/**
5249 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5250 * @dev: DRM device
5251 * @data: ioctl data
5252 * @file_priv: DRM file info
5253 *
5254 * This schedules an asynchronous update on a given CRTC, called page flip.
5255 * Optionally a drm event is generated to signal the completion of the event.
5256 * Generic drivers cannot assume that a pageflip with changed framebuffer
5257 * properties (including driver specific metadata like tiling layout) will work,
5258 * but some drivers support e.g. pixel format changes through the pageflip
5259 * ioctl.
5260 *
5261 * Called by the user via ioctl.
5262 *
5263 * Returns:
1a498633 5264 * Zero on success, negative errno on failure.
c8e32cc1 5265 */
d91d8a3f
KH
5266int drm_mode_page_flip_ioctl(struct drm_device *dev,
5267 void *data, struct drm_file *file_priv)
5268{
5269 struct drm_mode_crtc_page_flip *page_flip = data;
d91d8a3f 5270 struct drm_crtc *crtc;
3d30a59b 5271 struct drm_framebuffer *fb = NULL;
d91d8a3f 5272 struct drm_pending_vblank_event *e = NULL;
d91d8a3f
KH
5273 int ret = -EINVAL;
5274
5275 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5276 page_flip->reserved != 0)
5277 return -EINVAL;
5278
62f2104f
KP
5279 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5280 return -EINVAL;
5281
a2b34e22
RC
5282 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5283 if (!crtc)
f27657f2 5284 return -ENOENT;
d91d8a3f 5285
4d02e2de 5286 drm_modeset_lock_crtc(crtc, crtc->primary);
f4510a27 5287 if (crtc->primary->fb == NULL) {
90c1efdd
CW
5288 /* The framebuffer is currently unbound, presumably
5289 * due to a hotplug event, that userspace has not
5290 * yet discovered.
5291 */
5292 ret = -EBUSY;
5293 goto out;
5294 }
5295
d91d8a3f
KH
5296 if (crtc->funcs->page_flip == NULL)
5297 goto out;
5298
786b99ed 5299 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
37c4e705
VS
5300 if (!fb) {
5301 ret = -ENOENT;
d91d8a3f 5302 goto out;
37c4e705 5303 }
d91d8a3f 5304
2afa701d
VS
5305 if (crtc->state) {
5306 const struct drm_plane_state *state = crtc->primary->state;
5307
5308 ret = check_src_coords(state->src_x, state->src_y,
5309 state->src_w, state->src_h, fb);
5310 } else {
5311 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5312 }
c11e9283 5313 if (ret)
5f61bb42 5314 goto out;
5f61bb42 5315
f4510a27 5316 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
909d9cda
LP
5317 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5318 ret = -EINVAL;
5319 goto out;
5320 }
5321
d91d8a3f 5322 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2dd500f1
DV
5323 e = kzalloc(sizeof *e, GFP_KERNEL);
5324 if (!e) {
5325 ret = -ENOMEM;
d91d8a3f
KH
5326 goto out;
5327 }
7bd4d7be 5328 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
f76511b9 5329 e->event.base.length = sizeof(e->event);
d91d8a3f 5330 e->event.user_data = page_flip->user_data;
2dd500f1
DV
5331 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5332 if (ret) {
5333 kfree(e);
5334 goto out;
5335 }
d91d8a3f
KH
5336 }
5337
3d30a59b 5338 crtc->primary->old_fb = crtc->primary->fb;
ed8d1975 5339 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
d91d8a3f 5340 if (ret) {
2dd500f1
DV
5341 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5342 drm_event_cancel_free(dev, &e->base);
b0d12325 5343 /* Keep the old fb, don't unref it. */
3d30a59b 5344 crtc->primary->old_fb = NULL;
b0d12325 5345 } else {
3cb43cc0 5346 crtc->primary->fb = fb;
b0d12325
DV
5347 /* Unref only the old framebuffer. */
5348 fb = NULL;
d91d8a3f
KH
5349 }
5350
5351out:
b0d12325
DV
5352 if (fb)
5353 drm_framebuffer_unreference(fb);
3d30a59b
DV
5354 if (crtc->primary->old_fb)
5355 drm_framebuffer_unreference(crtc->primary->old_fb);
5356 crtc->primary->old_fb = NULL;
d059f652 5357 drm_modeset_unlock_crtc(crtc);
b4d5e7d1 5358
d91d8a3f
KH
5359 return ret;
5360}
eb033556 5361
c8e32cc1
DV
5362/**
5363 * drm_mode_config_reset - call ->reset callbacks
5364 * @dev: drm device
5365 *
5366 * This functions calls all the crtc's, encoder's and connector's ->reset
5367 * callback. Drivers can use this in e.g. their driver load or resume code to
5368 * reset hardware and software state.
5369 */
eb033556
CW
5370void drm_mode_config_reset(struct drm_device *dev)
5371{
5372 struct drm_crtc *crtc;
2a0d7cfd 5373 struct drm_plane *plane;
eb033556
CW
5374 struct drm_encoder *encoder;
5375 struct drm_connector *connector;
5376
e4f62546 5377 drm_for_each_plane(plane, dev)
2a0d7cfd
DV
5378 if (plane->funcs->reset)
5379 plane->funcs->reset(plane);
5380
e4f62546 5381 drm_for_each_crtc(crtc, dev)
eb033556
CW
5382 if (crtc->funcs->reset)
5383 crtc->funcs->reset(crtc);
5384
e4f62546 5385 drm_for_each_encoder(encoder, dev)
eb033556
CW
5386 if (encoder->funcs->reset)
5387 encoder->funcs->reset(encoder);
5388
f8c2ba31 5389 mutex_lock(&dev->mode_config.mutex);
4eebf60b 5390 drm_for_each_connector(connector, dev)
eb033556
CW
5391 if (connector->funcs->reset)
5392 connector->funcs->reset(connector);
f8c2ba31 5393 mutex_unlock(&dev->mode_config.mutex);
eb033556
CW
5394}
5395EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b 5396
c8e32cc1
DV
5397/**
5398 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5399 * @dev: DRM device
5400 * @data: ioctl data
5401 * @file_priv: DRM file info
5402 *
5403 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5404 * TTM or something else entirely) and returns the resulting buffer handle. This
5405 * handle can then be wrapped up into a framebuffer modeset object.
5406 *
5407 * Note that userspace is not allowed to use such objects for render
5408 * acceleration - drivers must create their own private ioctls for such a use
5409 * case.
5410 *
5411 * Called by the user via ioctl.
5412 *
5413 * Returns:
1a498633 5414 * Zero on success, negative errno on failure.
c8e32cc1 5415 */
ff72145b
DA
5416int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5417 void *data, struct drm_file *file_priv)
5418{
5419 struct drm_mode_create_dumb *args = data;
b28cd41f 5420 u32 cpp, stride, size;
ff72145b
DA
5421
5422 if (!dev->driver->dumb_create)
5423 return -ENOSYS;
b28cd41f
DH
5424 if (!args->width || !args->height || !args->bpp)
5425 return -EINVAL;
5426
5427 /* overflow checks for 32bit size calculations */
00e72089 5428 /* NOTE: DIV_ROUND_UP() can overflow */
b28cd41f 5429 cpp = DIV_ROUND_UP(args->bpp, 8);
00e72089 5430 if (!cpp || cpp > 0xffffffffU / args->width)
b28cd41f
DH
5431 return -EINVAL;
5432 stride = cpp * args->width;
5433 if (args->height > 0xffffffffU / stride)
5434 return -EINVAL;
5435
5436 /* test for wrap-around */
5437 size = args->height * stride;
5438 if (PAGE_ALIGN(size) == 0)
5439 return -EINVAL;
5440
f6085952
TR
5441 /*
5442 * handle, pitch and size are output parameters. Zero them out to
5443 * prevent drivers from accidentally using uninitialized data. Since
5444 * not all existing userspace is clearing these fields properly we
5445 * cannot reject IOCTL with garbage in them.
5446 */
5447 args->handle = 0;
5448 args->pitch = 0;
5449 args->size = 0;
5450
ff72145b
DA
5451 return dev->driver->dumb_create(file_priv, dev, args);
5452}
5453
c8e32cc1
DV
5454/**
5455 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5456 * @dev: DRM device
5457 * @data: ioctl data
5458 * @file_priv: DRM file info
5459 *
5460 * Allocate an offset in the drm device node's address space to be able to
5461 * memory map a dumb buffer.
5462 *
5463 * Called by the user via ioctl.
5464 *
5465 * Returns:
1a498633 5466 * Zero on success, negative errno on failure.
c8e32cc1 5467 */
ff72145b
DA
5468int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5469 void *data, struct drm_file *file_priv)
5470{
5471 struct drm_mode_map_dumb *args = data;
5472
5473 /* call driver ioctl to get mmap offset */
5474 if (!dev->driver->dumb_map_offset)
5475 return -ENOSYS;
5476
5477 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5478}
5479
c8e32cc1
DV
5480/**
5481 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5482 * @dev: DRM device
5483 * @data: ioctl data
5484 * @file_priv: DRM file info
5485 *
5486 * This destroys the userspace handle for the given dumb backing storage buffer.
5487 * Since buffer objects must be reference counted in the kernel a buffer object
5488 * won't be immediately freed if a framebuffer modeset object still uses it.
5489 *
5490 * Called by the user via ioctl.
5491 *
5492 * Returns:
1a498633 5493 * Zero on success, negative errno on failure.
c8e32cc1 5494 */
ff72145b
DA
5495int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5496 void *data, struct drm_file *file_priv)
5497{
5498 struct drm_mode_destroy_dumb *args = data;
5499
5500 if (!dev->driver->dumb_destroy)
5501 return -ENOSYS;
5502
5503 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5504}
248dbc23 5505
3c9855f6
VS
5506/**
5507 * drm_rotation_simplify() - Try to simplify the rotation
5508 * @rotation: Rotation to be simplified
5509 * @supported_rotations: Supported rotations
5510 *
5511 * Attempt to simplify the rotation to a form that is supported.
5512 * Eg. if the hardware supports everything except DRM_REFLECT_X
5513 * one could call this function like this:
5514 *
5515 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5516 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5517 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5518 *
5519 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5520 * transforms the hardware supports, this function may not
5521 * be able to produce a supported transform, so the caller should
5522 * check the result afterwards.
5523 */
5524unsigned int drm_rotation_simplify(unsigned int rotation,
5525 unsigned int supported_rotations)
5526{
5527 if (rotation & ~supported_rotations) {
5528 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
14152c8d
JL
5529 rotation = (rotation & DRM_REFLECT_MASK) |
5530 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
3c9855f6
VS
5531 }
5532
5533 return rotation;
5534}
5535EXPORT_SYMBOL(drm_rotation_simplify);
5536
87d24fc3
LP
5537/**
5538 * drm_mode_config_init - initialize DRM mode_configuration structure
5539 * @dev: DRM device
5540 *
5541 * Initialize @dev's mode_config structure, used for tracking the graphics
5542 * configuration of @dev.
5543 *
5544 * Since this initializes the modeset locks, no locking is possible. Which is no
5545 * problem, since this should happen single threaded at init time. It is the
5546 * driver's problem to ensure this guarantee.
5547 *
5548 */
5549void drm_mode_config_init(struct drm_device *dev)
5550{
5551 mutex_init(&dev->mode_config.mutex);
51fd371b 5552 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
87d24fc3
LP
5553 mutex_init(&dev->mode_config.idr_mutex);
5554 mutex_init(&dev->mode_config.fb_lock);
8fb6e7a5 5555 mutex_init(&dev->mode_config.blob_lock);
87d24fc3
LP
5556 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5557 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5558 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5559 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5560 INIT_LIST_HEAD(&dev->mode_config.property_list);
5561 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5562 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5563 idr_init(&dev->mode_config.crtc_idr);
138f9ebb 5564 idr_init(&dev->mode_config.tile_idr);
5fff80bb 5565 ida_init(&dev->mode_config.connector_ida);
87d24fc3
LP
5566
5567 drm_modeset_lock_all(dev);
6b4959f4 5568 drm_mode_create_standard_properties(dev);
87d24fc3
LP
5569 drm_modeset_unlock_all(dev);
5570
5571 /* Just to be sure */
5572 dev->mode_config.num_fb = 0;
5573 dev->mode_config.num_connector = 0;
5574 dev->mode_config.num_crtc = 0;
5575 dev->mode_config.num_encoder = 0;
e27dde3e
MR
5576 dev->mode_config.num_overlay_plane = 0;
5577 dev->mode_config.num_total_plane = 0;
87d24fc3
LP
5578}
5579EXPORT_SYMBOL(drm_mode_config_init);
5580
5581/**
5582 * drm_mode_config_cleanup - free up DRM mode_config info
5583 * @dev: DRM device
5584 *
5585 * Free up all the connectors and CRTCs associated with this DRM device, then
5586 * free up the framebuffers and associated buffer objects.
5587 *
5588 * Note that since this /should/ happen single-threaded at driver/device
5589 * teardown time, no locking is required. It's the driver's job to ensure that
5590 * this guarantee actually holds true.
5591 *
5592 * FIXME: cleanup any dangling user buffer objects too
5593 */
5594void drm_mode_config_cleanup(struct drm_device *dev)
5595{
5596 struct drm_connector *connector, *ot;
5597 struct drm_crtc *crtc, *ct;
5598 struct drm_encoder *encoder, *enct;
5599 struct drm_framebuffer *fb, *fbt;
5600 struct drm_property *property, *pt;
5601 struct drm_property_blob *blob, *bt;
5602 struct drm_plane *plane, *plt;
5603
5604 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5605 head) {
5606 encoder->funcs->destroy(encoder);
5607 }
5608
5609 list_for_each_entry_safe(connector, ot,
5610 &dev->mode_config.connector_list, head) {
5611 connector->funcs->destroy(connector);
5612 }
5613
5614 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5615 head) {
5616 drm_property_destroy(dev, property);
5617 }
5618
f35034f8
ML
5619 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5620 head) {
5621 plane->funcs->destroy(plane);
5622 }
5623
5624 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5625 crtc->funcs->destroy(crtc);
5626 }
5627
87d24fc3 5628 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
e2f5d2ea 5629 head_global) {
6bcacf51 5630 drm_property_unreference_blob(blob);
87d24fc3
LP
5631 }
5632
5633 /*
5634 * Single-threaded teardown context, so it's not required to grab the
5635 * fb_lock to protect against concurrent fb_list access. Contrary, it
5636 * would actually deadlock with the drm_framebuffer_cleanup function.
5637 *
5638 * Also, if there are any framebuffers left, that's a driver leak now,
5639 * so politely WARN about this.
5640 */
5641 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5642 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
d0f37cf6 5643 drm_framebuffer_free(&fb->base.refcount);
87d24fc3
LP
5644 }
5645
5fff80bb 5646 ida_destroy(&dev->mode_config.connector_ida);
138f9ebb 5647 idr_destroy(&dev->mode_config.tile_idr);
87d24fc3 5648 idr_destroy(&dev->mode_config.crtc_idr);
51fd371b 5649 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
87d24fc3
LP
5650}
5651EXPORT_SYMBOL(drm_mode_config_cleanup);
c1df5f3c
VS
5652
5653struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5654 unsigned int supported_rotations)
5655{
5656 static const struct drm_prop_enum_list props[] = {
5657 { DRM_ROTATE_0, "rotate-0" },
5658 { DRM_ROTATE_90, "rotate-90" },
5659 { DRM_ROTATE_180, "rotate-180" },
5660 { DRM_ROTATE_270, "rotate-270" },
5661 { DRM_REFLECT_X, "reflect-x" },
5662 { DRM_REFLECT_Y, "reflect-y" },
5663 };
5664
5665 return drm_property_create_bitmask(dev, 0, "rotation",
5666 props, ARRAY_SIZE(props),
5667 supported_rotations);
5668}
5669EXPORT_SYMBOL(drm_mode_create_rotation_property);
138f9ebb
DA
5670
5671/**
5672 * DOC: Tile group
5673 *
5674 * Tile groups are used to represent tiled monitors with a unique
5675 * integer identifier. Tiled monitors using DisplayID v1.3 have
5676 * a unique 8-byte handle, we store this in a tile group, so we
5677 * have a common identifier for all tiles in a monitor group.
5678 */
5679static void drm_tile_group_free(struct kref *kref)
5680{
5681 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5682 struct drm_device *dev = tg->dev;
5683 mutex_lock(&dev->mode_config.idr_mutex);
5684 idr_remove(&dev->mode_config.tile_idr, tg->id);
5685 mutex_unlock(&dev->mode_config.idr_mutex);
5686 kfree(tg);
5687}
5688
5689/**
5690 * drm_mode_put_tile_group - drop a reference to a tile group.
5691 * @dev: DRM device
5692 * @tg: tile group to drop reference to.
5693 *
5694 * drop reference to tile group and free if 0.
5695 */
5696void drm_mode_put_tile_group(struct drm_device *dev,
5697 struct drm_tile_group *tg)
5698{
5699 kref_put(&tg->refcount, drm_tile_group_free);
5700}
5701
5702/**
5703 * drm_mode_get_tile_group - get a reference to an existing tile group
5704 * @dev: DRM device
5705 * @topology: 8-bytes unique per monitor.
5706 *
5707 * Use the unique bytes to get a reference to an existing tile group.
5708 *
5709 * RETURNS:
5710 * tile group or NULL if not found.
5711 */
5712struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5713 char topology[8])
5714{
5715 struct drm_tile_group *tg;
5716 int id;
5717 mutex_lock(&dev->mode_config.idr_mutex);
5718 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5719 if (!memcmp(tg->group_data, topology, 8)) {
5720 if (!kref_get_unless_zero(&tg->refcount))
5721 tg = NULL;
5722 mutex_unlock(&dev->mode_config.idr_mutex);
5723 return tg;
5724 }
5725 }
5726 mutex_unlock(&dev->mode_config.idr_mutex);
5727 return NULL;
5728}
81ddd1bc 5729EXPORT_SYMBOL(drm_mode_get_tile_group);
138f9ebb
DA
5730
5731/**
5732 * drm_mode_create_tile_group - create a tile group from a displayid description
5733 * @dev: DRM device
5734 * @topology: 8-bytes unique per monitor.
5735 *
5736 * Create a tile group for the unique monitor, and get a unique
5737 * identifier for the tile group.
5738 *
5739 * RETURNS:
5740 * new tile group or error.
5741 */
5742struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5743 char topology[8])
5744{
5745 struct drm_tile_group *tg;
5746 int ret;
5747
5748 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5749 if (!tg)
5750 return ERR_PTR(-ENOMEM);
5751
5752 kref_init(&tg->refcount);
5753 memcpy(tg->group_data, topology, 8);
5754 tg->dev = dev;
5755
5756 mutex_lock(&dev->mode_config.idr_mutex);
5757 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5758 if (ret >= 0) {
5759 tg->id = ret;
5760 } else {
5761 kfree(tg);
5762 tg = ERR_PTR(ret);
5763 }
5764
5765 mutex_unlock(&dev->mode_config.idr_mutex);
5766 return tg;
5767}
81ddd1bc 5768EXPORT_SYMBOL(drm_mode_create_tile_group);
f8ed34ac
JS
5769
5770/**
5771 * drm_crtc_enable_color_mgmt - enable color management properties
5772 * @crtc: DRM CRTC
5773 * @degamma_lut_size: the size of the degamma lut (before CSC)
5774 * @has_ctm: whether to attach ctm_property for CSC matrix
5775 * @gamma_lut_size: the size of the gamma lut (after CSC)
5776 *
5777 * This function lets the driver enable the color correction
5778 * properties on a CRTC. This includes 3 degamma, csc and gamma
5779 * properties that userspace can set and 2 size properties to inform
5780 * the userspace of the lut sizes. Each of the properties are
5781 * optional. The gamma and degamma properties are only attached if
5782 * their size is not 0 and ctm_property is only attached if has_ctm is
5783 * true.
5784 */
5785void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5786 uint degamma_lut_size,
5787 bool has_ctm,
5788 uint gamma_lut_size)
5789{
5790 struct drm_device *dev = crtc->dev;
5791 struct drm_mode_config *config = &dev->mode_config;
5792
5793 if (degamma_lut_size) {
5794 drm_object_attach_property(&crtc->base,
5795 config->degamma_lut_property, 0);
5796 drm_object_attach_property(&crtc->base,
5797 config->degamma_lut_size_property,
5798 degamma_lut_size);
5799 }
5800
5801 if (has_ctm)
5802 drm_object_attach_property(&crtc->base,
5803 config->ctm_property, 0);
5804
5805 if (gamma_lut_size) {
5806 drm_object_attach_property(&crtc->base,
5807 config->gamma_lut_property, 0);
5808 drm_object_attach_property(&crtc->base,
5809 config->gamma_lut_size_property,
5810 gamma_lut_size);
5811 }
5812}
5813EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);