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