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