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