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