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