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