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