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