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