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