blob: f939b058953cae0cb11bb38975e8e05032322e83 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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 */
Ville Syrjälä6ba6d032013-06-10 11:15:10 +030032#include <linux/ctype.h>
Dave Airlief453ba02008-11-07 14:05:41 -080033#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040035#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
Rob Clark51fd3712013-11-19 12:10:12 -050040#include <drm/drm_modeset_lock.h>
Dave Airlief453ba02008-11-07 14:05:41 -080041
Daniel Vetter8bd441b2014-01-23 15:35:24 +010042#include "drm_crtc_internal.h"
43
Daniel Vetter84849902012-12-02 00:28:11 +010044/**
45 * drm_modeset_lock_all - take all modeset locks
46 * @dev: drm device
47 *
48 * This function takes all modeset locks, suitable where a more fine-grained
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010049 * scheme isn't (yet) implemented. Locks must be dropped with
50 * drm_modeset_unlock_all.
Daniel Vetter84849902012-12-02 00:28:11 +010051 */
52void drm_modeset_lock_all(struct drm_device *dev)
53{
Rob Clark51fd3712013-11-19 12:10:12 -050054 struct drm_mode_config *config = &dev->mode_config;
55 struct drm_modeset_acquire_ctx *ctx;
56 int ret;
Daniel Vetter29494c12012-12-02 02:18:25 +010057
Rob Clark51fd3712013-11-19 12:10:12 -050058 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
59 if (WARN_ON(!ctx))
60 return;
Daniel Vetter29494c12012-12-02 02:18:25 +010061
Rob Clark51fd3712013-11-19 12:10:12 -050062 mutex_lock(&config->mutex);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020063
Rob Clark51fd3712013-11-19 12:10:12 -050064 drm_modeset_acquire_init(ctx, 0);
65
66retry:
67 ret = drm_modeset_lock(&config->connection_mutex, ctx);
68 if (ret)
69 goto fail;
70 ret = drm_modeset_lock_all_crtcs(dev, ctx);
71 if (ret)
72 goto fail;
73
74 WARN_ON(config->acquire_ctx);
75
76 /* now we hold the locks, so now that it is safe, stash the
77 * ctx for drm_modeset_unlock_all():
78 */
79 config->acquire_ctx = ctx;
80
81 drm_warn_on_modeset_not_all_locked(dev);
82
83 return;
84
85fail:
86 if (ret == -EDEADLK) {
87 drm_modeset_backoff(ctx);
88 goto retry;
89 }
Daniel Vetter84849902012-12-02 00:28:11 +010090}
91EXPORT_SYMBOL(drm_modeset_lock_all);
92
93/**
94 * drm_modeset_unlock_all - drop all modeset locks
95 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010096 *
97 * This function drop all modeset locks taken by drm_modeset_lock_all.
Daniel Vetter84849902012-12-02 00:28:11 +010098 */
99void drm_modeset_unlock_all(struct drm_device *dev)
100{
Rob Clark51fd3712013-11-19 12:10:12 -0500101 struct drm_mode_config *config = &dev->mode_config;
102 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter29494c12012-12-02 02:18:25 +0100103
Rob Clark51fd3712013-11-19 12:10:12 -0500104 if (WARN_ON(!ctx))
105 return;
Daniel Vetter29494c12012-12-02 02:18:25 +0100106
Rob Clark51fd3712013-11-19 12:10:12 -0500107 config->acquire_ctx = NULL;
108 drm_modeset_drop_locks(ctx);
109 drm_modeset_acquire_fini(ctx);
110
111 kfree(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +0200112
Daniel Vetter84849902012-12-02 00:28:11 +0100113 mutex_unlock(&dev->mode_config.mutex);
114}
115EXPORT_SYMBOL(drm_modeset_unlock_all);
116
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100117/**
118 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
119 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100120 *
121 * Useful as a debug assert.
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100122 */
123void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
124{
125 struct drm_crtc *crtc;
126
Daniel Vettera9b054e2013-05-02 09:43:05 +0200127 /* Locking is currently fubar in the panic handler. */
128 if (oops_in_progress)
129 return;
130
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100131 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Rob Clark51fd3712013-11-19 12:10:12 -0500132 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100133
Rob Clark51fd3712013-11-19 12:10:12 -0500134 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100135 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
136}
137EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
138
Dave Airlief453ba02008-11-07 14:05:41 -0800139/* Avoid boilerplate. I'm tired of typing. */
140#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000141 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -0800142 { \
143 int i; \
144 for (i = 0; i < ARRAY_SIZE(list); i++) { \
145 if (list[i].type == val) \
146 return list[i].name; \
147 } \
148 return "(unknown)"; \
149 }
150
151/*
152 * Global properties
153 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000154static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800155{ { DRM_MODE_DPMS_ON, "On" },
156 { DRM_MODE_DPMS_STANDBY, "Standby" },
157 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
158 { DRM_MODE_DPMS_OFF, "Off" }
159};
160
161DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
162
Rob Clark9922ab52014-04-01 20:16:57 -0400163static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
164{
165 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
166 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
167 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
168};
169
Dave Airlief453ba02008-11-07 14:05:41 -0800170/*
171 * Optional properties
172 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000173static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800174{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700175 { DRM_MODE_SCALE_NONE, "None" },
176 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
177 { DRM_MODE_SCALE_CENTER, "Center" },
178 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800179};
180
Dave Airlief453ba02008-11-07 14:05:41 -0800181/*
182 * Non-global properties, but "required" for certain connectors.
183 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000184static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800185{
186 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
187 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
188 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
189};
190
191DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
192
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000193static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800194{
195 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
196 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
197 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
198};
199
200DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
201 drm_dvi_i_subconnector_enum_list)
202
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000203static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800204{
205 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
207 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
208 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200209 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800210};
211
212DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
213
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000214static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800215{
216 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
217 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
219 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200220 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800221};
222
223DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
224 drm_tv_subconnector_enum_list)
225
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000226static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000227 { DRM_MODE_DIRTY_OFF, "Off" },
228 { DRM_MODE_DIRTY_ON, "On" },
229 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
230};
231
Dave Airlief453ba02008-11-07 14:05:41 -0800232struct drm_conn_prop_enum_list {
233 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000234 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400235 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800236};
237
238/*
239 * Connector and encoder types.
240 */
241static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400242{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
243 { DRM_MODE_CONNECTOR_VGA, "VGA" },
244 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
245 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
246 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
247 { DRM_MODE_CONNECTOR_Composite, "Composite" },
248 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
249 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
250 { DRM_MODE_CONNECTOR_Component, "Component" },
251 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
252 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
253 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
254 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
255 { DRM_MODE_CONNECTOR_TV, "TV" },
256 { DRM_MODE_CONNECTOR_eDP, "eDP" },
257 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300258 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800259};
260
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000261static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800262{ { DRM_MODE_ENCODER_NONE, "None" },
263 { DRM_MODE_ENCODER_DAC, "DAC" },
264 { DRM_MODE_ENCODER_TMDS, "TMDS" },
265 { DRM_MODE_ENCODER_LVDS, "LVDS" },
266 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200267 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300268 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000269 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800270};
271
Jesse Barnesac1bb362014-02-10 15:32:44 -0800272static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
273{
274 { SubPixelUnknown, "Unknown" },
275 { SubPixelHorizontalRGB, "Horizontal RGB" },
276 { SubPixelHorizontalBGR, "Horizontal BGR" },
277 { SubPixelVerticalRGB, "Vertical RGB" },
278 { SubPixelVerticalBGR, "Vertical BGR" },
279 { SubPixelNone, "None" },
280};
281
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400282void drm_connector_ida_init(void)
283{
284 int i;
285
286 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
287 ida_init(&drm_connector_enum_list[i].ida);
288}
289
290void drm_connector_ida_destroy(void)
291{
292 int i;
293
294 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
295 ida_destroy(&drm_connector_enum_list[i].ida);
296}
297
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100298/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100299 * drm_get_connector_status_name - return a string for connector status
300 * @status: connector status to compute name of
301 *
302 * In contrast to the other drm_get_*_name functions this one here returns a
303 * const pointer and hence is threadsafe.
304 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000305const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800306{
307 if (status == connector_status_connected)
308 return "connected";
309 else if (status == connector_status_disconnected)
310 return "disconnected";
311 else
312 return "unknown";
313}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000314EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800315
Jesse Barnesac1bb362014-02-10 15:32:44 -0800316/**
317 * drm_get_subpixel_order_name - return a string for a given subpixel enum
318 * @order: enum of subpixel_order
319 *
320 * Note you could abuse this and return something out of bounds, but that
321 * would be a caller error. No unscrubbed user data should make it here.
322 */
323const char *drm_get_subpixel_order_name(enum subpixel_order order)
324{
325 return drm_subpixel_enum_list[order].name;
326}
327EXPORT_SYMBOL(drm_get_subpixel_order_name);
328
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300329static char printable_char(int c)
330{
331 return isascii(c) && isprint(c) ? c : '?';
332}
333
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100334/**
335 * drm_get_format_name - return a string for drm fourcc format
336 * @format: format to compute name of
337 *
338 * Note that the buffer used by this function is globally shared and owned by
339 * the function itself.
340 *
341 * FIXME: This isn't really multithreading safe.
342 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000343const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300344{
345 static char buf[32];
346
347 snprintf(buf, sizeof(buf),
348 "%c%c%c%c %s-endian (0x%08x)",
349 printable_char(format & 0xff),
350 printable_char((format >> 8) & 0xff),
351 printable_char((format >> 16) & 0xff),
352 printable_char((format >> 24) & 0x7f),
353 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
354 format);
355
356 return buf;
357}
358EXPORT_SYMBOL(drm_get_format_name);
359
Dave Airlief453ba02008-11-07 14:05:41 -0800360/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100361 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800362 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100363 * @obj: object pointer, used to generate unique ID
364 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800365 *
Dave Airlief453ba02008-11-07 14:05:41 -0800366 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100367 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368 * modeset identifiers are _not_ reference counted. Hence don't use this for
369 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800370 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100371 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800372 * New unique (relative to other objects in @dev) integer identifier for the
373 * object.
374 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100375int drm_mode_object_get(struct drm_device *dev,
376 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800377{
Dave Airlief453ba02008-11-07 14:05:41 -0800378 int ret;
379
Jesse Barnesad2563c2009-01-19 17:21:45 +1000380 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800381 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
382 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100383 /*
384 * Set up the object linking under the protection of the idr
385 * lock so that other users can't see inconsistent state.
386 */
Tejun Heo2e928812013-02-27 17:04:08 -0800387 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100388 obj->type = obj_type;
389 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000390 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100391
Tejun Heo2e928812013-02-27 17:04:08 -0800392 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800393}
394
395/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100396 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800397 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100398 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800399 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100400 * Free @id from @dev's unique identifier pool. Note that despite the _get
401 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
402 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800403 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100404void drm_mode_object_put(struct drm_device *dev,
405 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800406{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000407 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800408 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000409 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800410}
411
Rob Clark98f75de2014-05-30 11:37:03 -0400412static struct drm_mode_object *_object_find(struct drm_device *dev,
413 uint32_t id, uint32_t type)
414{
415 struct drm_mode_object *obj = NULL;
416
417 mutex_lock(&dev->mode_config.idr_mutex);
418 obj = idr_find(&dev->mode_config.crtc_idr, id);
419 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
420 (obj->id != id))
421 obj = NULL;
422 mutex_unlock(&dev->mode_config.idr_mutex);
423
424 return obj;
425}
426
Daniel Vetter786b99e2012-12-02 21:53:40 +0100427/**
428 * drm_mode_object_find - look up a drm object with static lifetime
429 * @dev: drm device
430 * @id: id of the mode object
431 * @type: type of the mode object
432 *
433 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400434 * are reference counted, they need special treatment. Even with
435 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
436 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100437 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200438struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
439 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800440{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000441 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800442
Daniel Vetter786b99e2012-12-02 21:53:40 +0100443 /* Framebuffers are reference counted and need their own lookup
444 * function.*/
445 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400446 obj = _object_find(dev, id, type);
447 /* don't leak out unref'd fb's */
448 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000449 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800450 return obj;
451}
452EXPORT_SYMBOL(drm_mode_object_find);
453
454/**
Dave Airlief453ba02008-11-07 14:05:41 -0800455 * drm_framebuffer_init - initialize a framebuffer
456 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100457 * @fb: framebuffer to be initialized
458 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800459 *
Dave Airlief453ba02008-11-07 14:05:41 -0800460 * Allocates an ID for the framebuffer's parent mode object, sets its mode
461 * functions & device file and adds it to the master fd list.
462 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100463 * IMPORTANT:
464 * This functions publishes the fb and makes it available for concurrent access
465 * by other users. Which means by this point the fb _must_ be fully set up -
466 * since all the fb attributes are invariant over its lifetime, no further
467 * locking but only correct reference counting is required.
468 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100469 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200470 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800471 */
472int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
473 const struct drm_framebuffer_funcs *funcs)
474{
475 int ret;
476
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100477 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000478 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100479 INIT_LIST_HEAD(&fb->filp_head);
480 fb->dev = dev;
481 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000482
Dave Airlief453ba02008-11-07 14:05:41 -0800483 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200484 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100485 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800486
Daniel Vetter2b677e82012-12-10 21:16:05 +0100487 /* Grab the idr reference. */
488 drm_framebuffer_reference(fb);
489
Dave Airlief453ba02008-11-07 14:05:41 -0800490 dev->mode_config.num_fb++;
491 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100492out:
493 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800494
495 return 0;
496}
497EXPORT_SYMBOL(drm_framebuffer_init);
498
Rob Clarkf7eff602012-09-05 21:48:38 +0000499static void drm_framebuffer_free(struct kref *kref)
500{
501 struct drm_framebuffer *fb =
502 container_of(kref, struct drm_framebuffer, refcount);
503 fb->funcs->destroy(fb);
504}
505
Daniel Vetter2b677e82012-12-10 21:16:05 +0100506static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
507 uint32_t id)
508{
509 struct drm_mode_object *obj = NULL;
510 struct drm_framebuffer *fb;
511
512 mutex_lock(&dev->mode_config.idr_mutex);
513 obj = idr_find(&dev->mode_config.crtc_idr, id);
514 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
515 fb = NULL;
516 else
517 fb = obj_to_fb(obj);
518 mutex_unlock(&dev->mode_config.idr_mutex);
519
520 return fb;
521}
522
Rob Clarkf7eff602012-09-05 21:48:38 +0000523/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100524 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
525 * @dev: drm device
526 * @id: id of the fb object
527 *
528 * If successful, this grabs an additional reference to the framebuffer -
529 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100530 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100531 */
532struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
533 uint32_t id)
534{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100535 struct drm_framebuffer *fb;
536
537 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100538 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100539 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000540 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100541 mutex_unlock(&dev->mode_config.fb_lock);
542
543 return fb;
544}
545EXPORT_SYMBOL(drm_framebuffer_lookup);
546
547/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000548 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100549 * @fb: framebuffer to unref
550 *
551 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000552 */
553void drm_framebuffer_unreference(struct drm_framebuffer *fb)
554{
Rob Clark82912722014-03-18 10:07:08 -0400555 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000556 kref_put(&fb->refcount, drm_framebuffer_free);
557}
558EXPORT_SYMBOL(drm_framebuffer_unreference);
559
560/**
561 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100562 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100563 *
564 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000565 */
566void drm_framebuffer_reference(struct drm_framebuffer *fb)
567{
Rob Clark82912722014-03-18 10:07:08 -0400568 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000569 kref_get(&fb->refcount);
570}
571EXPORT_SYMBOL(drm_framebuffer_reference);
572
Daniel Vetter2b677e82012-12-10 21:16:05 +0100573static void drm_framebuffer_free_bug(struct kref *kref)
574{
575 BUG();
576}
577
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100578static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
579{
Rob Clark82912722014-03-18 10:07:08 -0400580 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100581 kref_put(&fb->refcount, drm_framebuffer_free_bug);
582}
583
Daniel Vetter2b677e82012-12-10 21:16:05 +0100584/* dev->mode_config.fb_lock must be held! */
585static void __drm_framebuffer_unregister(struct drm_device *dev,
586 struct drm_framebuffer *fb)
587{
588 mutex_lock(&dev->mode_config.idr_mutex);
589 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
590 mutex_unlock(&dev->mode_config.idr_mutex);
591
592 fb->base.id = 0;
593
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100594 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100595}
596
Dave Airlief453ba02008-11-07 14:05:41 -0800597/**
Daniel Vetter36206362012-12-10 20:42:17 +0100598 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
599 * @fb: fb to unregister
600 *
601 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
602 * those used for fbdev. Note that the caller must hold a reference of it's own,
603 * i.e. the object may not be destroyed through this call (since it'll lead to a
604 * locking inversion).
605 */
606void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
607{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100608 struct drm_device *dev = fb->dev;
609
610 mutex_lock(&dev->mode_config.fb_lock);
611 /* Mark fb as reaped and drop idr ref. */
612 __drm_framebuffer_unregister(dev, fb);
613 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100614}
615EXPORT_SYMBOL(drm_framebuffer_unregister_private);
616
617/**
Dave Airlief453ba02008-11-07 14:05:41 -0800618 * drm_framebuffer_cleanup - remove a framebuffer object
619 * @fb: framebuffer to remove
620 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100621 * Cleanup framebuffer. This function is intended to be used from the drivers
622 * ->destroy callback. It can also be used to clean up driver private
623 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100624 *
625 * Note that this function does not remove the fb from active usuage - if it is
626 * still used anywhere, hilarity can ensue since userspace could call getfb on
627 * the id and get back -EINVAL. Obviously no concern at driver unload time.
628 *
629 * Also, the framebuffer will not be removed from the lookup idr - for
630 * user-created framebuffers this will happen in in the rmfb ioctl. For
631 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
632 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800633 */
634void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
635{
636 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100637
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100638 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000639 list_del(&fb->head);
640 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100641 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000642}
643EXPORT_SYMBOL(drm_framebuffer_cleanup);
644
645/**
646 * drm_framebuffer_remove - remove and unreference a framebuffer object
647 * @fb: framebuffer to remove
648 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000649 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100650 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100651 * passed-in framebuffer. Might take the modeset locks.
652 *
653 * Note that this function optimizes the cleanup away if the caller holds the
654 * last reference to the framebuffer. It is also guaranteed to not take the
655 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000656 */
657void drm_framebuffer_remove(struct drm_framebuffer *fb)
658{
659 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800660 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800661 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000662 struct drm_mode_set set;
663 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800664
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100665 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100666
Daniel Vetterb62584e2012-12-11 16:51:35 +0100667 /*
668 * drm ABI mandates that we remove any deleted framebuffers from active
669 * useage. But since most sane clients only remove framebuffers they no
670 * longer need, try to optimize this away.
671 *
672 * Since we're holding a reference ourselves, observing a refcount of 1
673 * means that we're the last holder and can skip it. Also, the refcount
674 * can never increase from 1 again, so we don't need any barriers or
675 * locks.
676 *
677 * Note that userspace could try to race with use and instate a new
678 * usage _after_ we've cleared all current ones. End result will be an
679 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
680 * in this manner.
681 */
682 if (atomic_read(&fb->refcount.refcount) > 1) {
683 drm_modeset_lock_all(dev);
684 /* remove from any CRTC */
685 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700686 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100687 /* should turn off the crtc */
688 memset(&set, 0, sizeof(struct drm_mode_set));
689 set.crtc = crtc;
690 set.fb = NULL;
691 ret = drm_mode_set_config_internal(&set);
692 if (ret)
693 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
694 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000695 }
Dave Airlief453ba02008-11-07 14:05:41 -0800696
Daniel Vetterb62584e2012-12-11 16:51:35 +0100697 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300698 if (plane->fb == fb)
699 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800700 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100701 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800702 }
703
Rob Clarkf7eff602012-09-05 21:48:38 +0000704 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800705}
Rob Clarkf7eff602012-09-05 21:48:38 +0000706EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800707
Rob Clark51fd3712013-11-19 12:10:12 -0500708DEFINE_WW_CLASS(crtc_ww_class);
709
Dave Airlief453ba02008-11-07 14:05:41 -0800710/**
Matt Ropere13161a2014-04-01 15:22:38 -0700711 * drm_crtc_init_with_planes - Initialise a new CRTC object with
712 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800713 * @dev: DRM device
714 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700715 * @primary: Primary plane for CRTC
716 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800717 * @funcs: callbacks for the new CRTC
718 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000719 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200720 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100721 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200722 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800723 */
Matt Ropere13161a2014-04-01 15:22:38 -0700724int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
725 struct drm_plane *primary,
726 void *cursor,
727 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800728{
Rob Clark51fd3712013-11-19 12:10:12 -0500729 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200730 int ret;
731
Dave Airlief453ba02008-11-07 14:05:41 -0800732 crtc->dev = dev;
733 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000734 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800735
Daniel Vetter84849902012-12-02 00:28:11 +0100736 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500737 drm_modeset_lock_init(&crtc->mutex);
738 /* dropped by _unlock_all(): */
739 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200740
741 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
742 if (ret)
743 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800744
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300745 crtc->base.properties = &crtc->properties;
746
Rob Clark51fd3712013-11-19 12:10:12 -0500747 list_add_tail(&crtc->head, &config->crtc_list);
748 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200749
Matt Ropere13161a2014-04-01 15:22:38 -0700750 crtc->primary = primary;
751 if (primary)
752 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
753
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200754 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100755 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200756
757 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800758}
Matt Ropere13161a2014-04-01 15:22:38 -0700759EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800760
761/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000762 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800763 * @crtc: CRTC to cleanup
764 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000765 * This function cleans up @crtc and removes it from the DRM mode setting
766 * core. Note that the function does *not* free the crtc structure itself,
767 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800768 */
769void drm_crtc_cleanup(struct drm_crtc *crtc)
770{
771 struct drm_device *dev = crtc->dev;
772
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000773 kfree(crtc->gamma_store);
774 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800775
Rob Clark51fd3712013-11-19 12:10:12 -0500776 drm_modeset_lock_fini(&crtc->mutex);
777
Dave Airlief453ba02008-11-07 14:05:41 -0800778 drm_mode_object_put(dev, &crtc->base);
779 list_del(&crtc->head);
780 dev->mode_config.num_crtc--;
781}
782EXPORT_SYMBOL(drm_crtc_cleanup);
783
784/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000785 * drm_crtc_index - find the index of a registered CRTC
786 * @crtc: CRTC to find index for
787 *
788 * Given a registered CRTC, return the index of that CRTC within a DRM
789 * device's list of CRTCs.
790 */
791unsigned int drm_crtc_index(struct drm_crtc *crtc)
792{
793 unsigned int index = 0;
794 struct drm_crtc *tmp;
795
796 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
797 if (tmp == crtc)
798 return index;
799
800 index++;
801 }
802
803 BUG();
804}
805EXPORT_SYMBOL(drm_crtc_index);
806
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100807/*
Dave Airlief453ba02008-11-07 14:05:41 -0800808 * drm_mode_remove - remove and free a mode
809 * @connector: connector list to modify
810 * @mode: mode to remove
811 *
Dave Airlief453ba02008-11-07 14:05:41 -0800812 * Remove @mode from @connector's mode list, then free it.
813 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100814static void drm_mode_remove(struct drm_connector *connector,
815 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800816{
817 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100818 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800819}
Dave Airlief453ba02008-11-07 14:05:41 -0800820
821/**
822 * drm_connector_init - Init a preallocated connector
823 * @dev: DRM device
824 * @connector: the connector to init
825 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100826 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800827 *
Dave Airlief453ba02008-11-07 14:05:41 -0800828 * Initialises a preallocated connector. Connectors should be
829 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200830 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100831 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200832 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800833 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200834int drm_connector_init(struct drm_device *dev,
835 struct drm_connector *connector,
836 const struct drm_connector_funcs *funcs,
837 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800838{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200839 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400840 struct ida *connector_ida =
841 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842
Daniel Vetter84849902012-12-02 00:28:11 +0100843 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800844
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200845 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
846 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300847 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200848
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300849 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800850 connector->dev = dev;
851 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800852 connector->connector_type = connector_type;
853 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400854 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
855 if (connector->connector_type_id < 0) {
856 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300857 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400858 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300859 connector->name =
860 kasprintf(GFP_KERNEL, "%s-%d",
861 drm_connector_enum_list[connector_type].name,
862 connector->connector_type_id);
863 if (!connector->name) {
864 ret = -ENOMEM;
865 goto out_put;
866 }
867
Dave Airlief453ba02008-11-07 14:05:41 -0800868 INIT_LIST_HEAD(&connector->probed_modes);
869 INIT_LIST_HEAD(&connector->modes);
870 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000871 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800872
873 list_add_tail(&connector->head, &dev->mode_config.connector_list);
874 dev->mode_config.num_connector++;
875
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200876 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500877 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200878 dev->mode_config.edid_property,
879 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800880
Rob Clark58495562012-10-11 20:50:56 -0500881 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800882 dev->mode_config.dpms_property, 0);
883
Thomas Wood30f65702014-06-18 17:52:32 +0100884 connector->debugfs_entry = NULL;
885
Jani Nikula2abdd312014-05-14 16:58:19 +0300886out_put:
887 if (ret)
888 drm_mode_object_put(dev, &connector->base);
889
890out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100891 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200892
893 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800894}
895EXPORT_SYMBOL(drm_connector_init);
896
897/**
898 * drm_connector_cleanup - cleans up an initialised connector
899 * @connector: connector to cleanup
900 *
Dave Airlief453ba02008-11-07 14:05:41 -0800901 * Cleans up the connector but doesn't free the object.
902 */
903void drm_connector_cleanup(struct drm_connector *connector)
904{
905 struct drm_device *dev = connector->dev;
906 struct drm_display_mode *mode, *t;
907
908 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
909 drm_mode_remove(connector, mode);
910
911 list_for_each_entry_safe(mode, t, &connector->modes, head)
912 drm_mode_remove(connector, mode);
913
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400914 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
915 connector->connector_type_id);
916
Dave Airlief453ba02008-11-07 14:05:41 -0800917 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300918 kfree(connector->name);
919 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800920 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000921 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800922}
923EXPORT_SYMBOL(drm_connector_cleanup);
924
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100925/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100926 * drm_connector_register - register a connector
927 * @connector: the connector to register
928 *
929 * Register userspace interfaces for a connector
930 *
931 * Returns:
932 * Zero on success, error code on failure.
933 */
934int drm_connector_register(struct drm_connector *connector)
935{
Thomas Wood30f65702014-06-18 17:52:32 +0100936 int ret;
937
938 ret = drm_sysfs_connector_add(connector);
939 if (ret)
940 return ret;
941
942 ret = drm_debugfs_connector_add(connector);
943 if (ret) {
944 drm_sysfs_connector_remove(connector);
945 return ret;
946 }
947
948 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100949}
950EXPORT_SYMBOL(drm_connector_register);
951
952/**
953 * drm_connector_unregister - unregister a connector
954 * @connector: the connector to unregister
955 *
956 * Unregister userspace interfaces for a connector
957 */
958void drm_connector_unregister(struct drm_connector *connector)
959{
960 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100961 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100962}
963EXPORT_SYMBOL(drm_connector_unregister);
964
965
966/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100967 * drm_connector_unplug_all - unregister connector userspace interfaces
968 * @dev: drm device
969 *
970 * This function unregisters all connector userspace interfaces in sysfs. Should
971 * be call when the device is disconnected, e.g. from an usb driver's
972 * ->disconnect callback.
973 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000974void drm_connector_unplug_all(struct drm_device *dev)
975{
976 struct drm_connector *connector;
977
978 /* taking the mode config mutex ends up in a clash with sysfs */
979 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +0100980 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +0000981
982}
983EXPORT_SYMBOL(drm_connector_unplug_all);
984
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100985/**
986 * drm_bridge_init - initialize a drm transcoder/bridge
987 * @dev: drm device
988 * @bridge: transcoder/bridge to set up
989 * @funcs: bridge function table
990 *
991 * Initialises a preallocated bridge. Bridges should be
992 * subclassed as part of driver connector objects.
993 *
994 * Returns:
995 * Zero on success, error code on failure.
996 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400997int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
998 const struct drm_bridge_funcs *funcs)
999{
1000 int ret;
1001
1002 drm_modeset_lock_all(dev);
1003
1004 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1005 if (ret)
1006 goto out;
1007
1008 bridge->dev = dev;
1009 bridge->funcs = funcs;
1010
1011 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1012 dev->mode_config.num_bridge++;
1013
1014 out:
1015 drm_modeset_unlock_all(dev);
1016 return ret;
1017}
1018EXPORT_SYMBOL(drm_bridge_init);
1019
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001020/**
1021 * drm_bridge_cleanup - cleans up an initialised bridge
1022 * @bridge: bridge to cleanup
1023 *
1024 * Cleans up the bridge but doesn't free the object.
1025 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001026void drm_bridge_cleanup(struct drm_bridge *bridge)
1027{
1028 struct drm_device *dev = bridge->dev;
1029
1030 drm_modeset_lock_all(dev);
1031 drm_mode_object_put(dev, &bridge->base);
1032 list_del(&bridge->head);
1033 dev->mode_config.num_bridge--;
1034 drm_modeset_unlock_all(dev);
1035}
1036EXPORT_SYMBOL(drm_bridge_cleanup);
1037
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001038/**
1039 * drm_encoder_init - Init a preallocated encoder
1040 * @dev: drm device
1041 * @encoder: the encoder to init
1042 * @funcs: callbacks for this encoder
1043 * @encoder_type: user visible type of the encoder
1044 *
1045 * Initialises a preallocated encoder. Encoder should be
1046 * subclassed as part of driver encoder objects.
1047 *
1048 * Returns:
1049 * Zero on success, error code on failure.
1050 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001051int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001052 struct drm_encoder *encoder,
1053 const struct drm_encoder_funcs *funcs,
1054 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001055{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001056 int ret;
1057
Daniel Vetter84849902012-12-02 00:28:11 +01001058 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001059
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001060 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1061 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001062 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001063
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001064 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001065 encoder->encoder_type = encoder_type;
1066 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001067 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1068 drm_encoder_enum_list[encoder_type].name,
1069 encoder->base.id);
1070 if (!encoder->name) {
1071 ret = -ENOMEM;
1072 goto out_put;
1073 }
Dave Airlief453ba02008-11-07 14:05:41 -08001074
1075 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1076 dev->mode_config.num_encoder++;
1077
Jani Nikulae5748942014-05-14 16:58:20 +03001078out_put:
1079 if (ret)
1080 drm_mode_object_put(dev, &encoder->base);
1081
1082out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001083 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001084
1085 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001086}
1087EXPORT_SYMBOL(drm_encoder_init);
1088
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001089/**
1090 * drm_encoder_cleanup - cleans up an initialised encoder
1091 * @encoder: encoder to cleanup
1092 *
1093 * Cleans up the encoder but doesn't free the object.
1094 */
Dave Airlief453ba02008-11-07 14:05:41 -08001095void drm_encoder_cleanup(struct drm_encoder *encoder)
1096{
1097 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001098 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001099 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001100 kfree(encoder->name);
1101 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001102 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001103 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001104 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001105}
1106EXPORT_SYMBOL(drm_encoder_cleanup);
1107
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001108/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001109 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001110 * @dev: DRM device
1111 * @plane: plane object to init
1112 * @possible_crtcs: bitmask of possible CRTCs
1113 * @funcs: callbacks for the new plane
1114 * @formats: array of supported formats (%DRM_FORMAT_*)
1115 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001116 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001117 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001118 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001119 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001120 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001121 * Zero on success, error code on failure.
1122 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001123int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1124 unsigned long possible_crtcs,
1125 const struct drm_plane_funcs *funcs,
1126 const uint32_t *formats, uint32_t format_count,
1127 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001128{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001129 int ret;
1130
Daniel Vetter84849902012-12-02 00:28:11 +01001131 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001132
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001133 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1134 if (ret)
1135 goto out;
1136
Rob Clark4d939142012-05-17 02:23:27 -06001137 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001138 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001139 plane->funcs = funcs;
1140 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1141 GFP_KERNEL);
1142 if (!plane->format_types) {
1143 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1144 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001145 ret = -ENOMEM;
1146 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001147 }
1148
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001149 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001150 plane->format_count = format_count;
1151 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001152 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001153
Matt Roperdc415ff2014-04-01 15:22:36 -07001154 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1155 dev->mode_config.num_total_plane++;
1156 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1157 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001158
Rob Clark9922ab52014-04-01 20:16:57 -04001159 drm_object_attach_property(&plane->base,
1160 dev->mode_config.plane_type_property,
1161 plane->type);
1162
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001163 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001164 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001165
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001166 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001167}
Matt Roperdc415ff2014-04-01 15:22:36 -07001168EXPORT_SYMBOL(drm_universal_plane_init);
1169
1170/**
1171 * drm_plane_init - Initialize a legacy plane
1172 * @dev: DRM device
1173 * @plane: plane object to init
1174 * @possible_crtcs: bitmask of possible CRTCs
1175 * @funcs: callbacks for the new plane
1176 * @formats: array of supported formats (%DRM_FORMAT_*)
1177 * @format_count: number of elements in @formats
1178 * @is_primary: plane type (primary vs overlay)
1179 *
1180 * Legacy API to initialize a DRM plane.
1181 *
1182 * New drivers should call drm_universal_plane_init() instead.
1183 *
1184 * Returns:
1185 * Zero on success, error code on failure.
1186 */
1187int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1188 unsigned long possible_crtcs,
1189 const struct drm_plane_funcs *funcs,
1190 const uint32_t *formats, uint32_t format_count,
1191 bool is_primary)
1192{
1193 enum drm_plane_type type;
1194
1195 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1196 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1197 formats, format_count, type);
1198}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001199EXPORT_SYMBOL(drm_plane_init);
1200
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001201/**
1202 * drm_plane_cleanup - Clean up the core plane usage
1203 * @plane: plane to cleanup
1204 *
1205 * This function cleans up @plane and removes it from the DRM mode setting
1206 * core. Note that the function does *not* free the plane structure itself,
1207 * this is the responsibility of the caller.
1208 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001209void drm_plane_cleanup(struct drm_plane *plane)
1210{
1211 struct drm_device *dev = plane->dev;
1212
Daniel Vetter84849902012-12-02 00:28:11 +01001213 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001214 kfree(plane->format_types);
1215 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001216
1217 BUG_ON(list_empty(&plane->head));
1218
1219 list_del(&plane->head);
1220 dev->mode_config.num_total_plane--;
1221 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1222 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001223 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001224}
1225EXPORT_SYMBOL(drm_plane_cleanup);
1226
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001227/**
1228 * drm_plane_force_disable - Forcibly disable a plane
1229 * @plane: plane to disable
1230 *
1231 * Forces the plane to be disabled.
1232 *
1233 * Used when the plane's current framebuffer is destroyed,
1234 * and when restoring fbdev mode.
1235 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001236void drm_plane_force_disable(struct drm_plane *plane)
1237{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001238 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001239 int ret;
1240
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001241 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001242 return;
1243
1244 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001245 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001246 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001247 return;
1248 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001249 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001250 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001251 plane->fb = NULL;
1252 plane->crtc = NULL;
1253}
1254EXPORT_SYMBOL(drm_plane_force_disable);
1255
Dave Airlief453ba02008-11-07 14:05:41 -08001256static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1257{
1258 struct drm_property *edid;
1259 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001260
1261 /*
1262 * Standard properties (apply to all connectors)
1263 */
1264 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1265 DRM_MODE_PROP_IMMUTABLE,
1266 "EDID", 0);
1267 dev->mode_config.edid_property = edid;
1268
Sascha Hauer4a67d392012-02-06 10:58:17 +01001269 dpms = drm_property_create_enum(dev, 0,
1270 "DPMS", drm_dpms_enum_list,
1271 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001272 dev->mode_config.dpms_property = dpms;
1273
1274 return 0;
1275}
1276
Rob Clark9922ab52014-04-01 20:16:57 -04001277static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1278{
1279 struct drm_property *type;
1280
1281 /*
1282 * Standard properties (apply to all planes)
1283 */
1284 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1285 "type", drm_plane_type_enum_list,
1286 ARRAY_SIZE(drm_plane_type_enum_list));
1287 dev->mode_config.plane_type_property = type;
1288
1289 return 0;
1290}
1291
Dave Airlief453ba02008-11-07 14:05:41 -08001292/**
1293 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1294 * @dev: DRM device
1295 *
1296 * Called by a driver the first time a DVI-I connector is made.
1297 */
1298int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1299{
1300 struct drm_property *dvi_i_selector;
1301 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001302
1303 if (dev->mode_config.dvi_i_select_subconnector_property)
1304 return 0;
1305
1306 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001307 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001308 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001309 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001310 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001311 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1312
Sascha Hauer4a67d392012-02-06 10:58:17 +01001313 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001314 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001315 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001316 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001317 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1318
1319 return 0;
1320}
1321EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1322
1323/**
1324 * drm_create_tv_properties - create TV specific connector properties
1325 * @dev: DRM device
1326 * @num_modes: number of different TV formats (modes) supported
1327 * @modes: array of pointers to strings containing name of each format
1328 *
1329 * Called by a driver's TV initialization routine, this function creates
1330 * the TV specific connector properties for a given device. Caller is
1331 * responsible for allocating a list of format names and passing them to
1332 * this routine.
1333 */
1334int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1335 char *modes[])
1336{
1337 struct drm_property *tv_selector;
1338 struct drm_property *tv_subconnector;
1339 int i;
1340
1341 if (dev->mode_config.tv_select_subconnector_property)
1342 return 0;
1343
1344 /*
1345 * Basic connector properties
1346 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001347 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001348 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001349 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001350 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001351 dev->mode_config.tv_select_subconnector_property = tv_selector;
1352
1353 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001354 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1355 "subconnector",
1356 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001357 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001358 dev->mode_config.tv_subconnector_property = tv_subconnector;
1359
1360 /*
1361 * Other, TV specific properties: margins & TV modes.
1362 */
1363 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001364 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001365
1366 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001367 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001368
1369 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001370 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001371
1372 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001373 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001374
1375 dev->mode_config.tv_mode_property =
1376 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1377 "mode", num_modes);
1378 for (i = 0; i < num_modes; i++)
1379 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1380 i, modes[i]);
1381
Francisco Jerezb6b79022009-08-02 04:19:20 +02001382 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001383 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001384
1385 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001386 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001387
1388 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001389 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001390
Francisco Jereza75f0232009-08-12 02:30:10 +02001391 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001392 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001393
1394 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001395 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001396
1397 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001398 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001399
Dave Airlief453ba02008-11-07 14:05:41 -08001400 return 0;
1401}
1402EXPORT_SYMBOL(drm_mode_create_tv_properties);
1403
1404/**
1405 * drm_mode_create_scaling_mode_property - create scaling mode property
1406 * @dev: DRM device
1407 *
1408 * Called by a driver the first time it's needed, must be attached to desired
1409 * connectors.
1410 */
1411int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1412{
1413 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001414
1415 if (dev->mode_config.scaling_mode_property)
1416 return 0;
1417
1418 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001419 drm_property_create_enum(dev, 0, "scaling mode",
1420 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001421 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001422
1423 dev->mode_config.scaling_mode_property = scaling_mode;
1424
1425 return 0;
1426}
1427EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1428
1429/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001430 * drm_mode_create_dirty_property - create dirty property
1431 * @dev: DRM device
1432 *
1433 * Called by a driver the first time it's needed, must be attached to desired
1434 * connectors.
1435 */
1436int drm_mode_create_dirty_info_property(struct drm_device *dev)
1437{
1438 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001439
1440 if (dev->mode_config.dirty_info_property)
1441 return 0;
1442
1443 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001444 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001445 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001446 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001447 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001448 dev->mode_config.dirty_info_property = dirty_info;
1449
1450 return 0;
1451}
1452EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1453
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001454static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001455{
1456 uint32_t total_objects = 0;
1457
1458 total_objects += dev->mode_config.num_crtc;
1459 total_objects += dev->mode_config.num_connector;
1460 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001461 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001462
Dave Airlief453ba02008-11-07 14:05:41 -08001463 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1464 if (!group->id_list)
1465 return -ENOMEM;
1466
1467 group->num_crtcs = 0;
1468 group->num_connectors = 0;
1469 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001470 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001471 return 0;
1472}
1473
Dave Airliead222792014-05-02 13:22:19 +10001474void drm_mode_group_destroy(struct drm_mode_group *group)
1475{
1476 kfree(group->id_list);
1477 group->id_list = NULL;
1478}
1479
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001480/*
1481 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1482 * the drm core's responsibility to set up mode control groups.
1483 */
Dave Airlief453ba02008-11-07 14:05:41 -08001484int drm_mode_group_init_legacy_group(struct drm_device *dev,
1485 struct drm_mode_group *group)
1486{
1487 struct drm_crtc *crtc;
1488 struct drm_encoder *encoder;
1489 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001490 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001491 int ret;
1492
1493 if ((ret = drm_mode_group_init(dev, group)))
1494 return ret;
1495
1496 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1497 group->id_list[group->num_crtcs++] = crtc->base.id;
1498
1499 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1500 group->id_list[group->num_crtcs + group->num_encoders++] =
1501 encoder->base.id;
1502
1503 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1504 group->id_list[group->num_crtcs + group->num_encoders +
1505 group->num_connectors++] = connector->base.id;
1506
Sean Paul3b336ec2013-08-14 16:47:37 -04001507 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1508 group->id_list[group->num_crtcs + group->num_encoders +
1509 group->num_connectors + group->num_bridges++] =
1510 bridge->base.id;
1511
Dave Airlief453ba02008-11-07 14:05:41 -08001512 return 0;
1513}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001514EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001515
Dave Airlie2390cd12014-06-05 14:01:29 +10001516void drm_reinit_primary_mode_group(struct drm_device *dev)
1517{
1518 drm_modeset_lock_all(dev);
1519 drm_mode_group_destroy(&dev->primary->mode_group);
1520 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1521 drm_modeset_unlock_all(dev);
1522}
1523EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1524
Dave Airlief453ba02008-11-07 14:05:41 -08001525/**
Dave Airlief453ba02008-11-07 14:05:41 -08001526 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1527 * @out: drm_mode_modeinfo struct to return to the user
1528 * @in: drm_display_mode to use
1529 *
Dave Airlief453ba02008-11-07 14:05:41 -08001530 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1531 * the user.
1532 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001533static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1534 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001535{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001536 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1537 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1538 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1539 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1540 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1541 "timing values too large for mode info\n");
1542
Dave Airlief453ba02008-11-07 14:05:41 -08001543 out->clock = in->clock;
1544 out->hdisplay = in->hdisplay;
1545 out->hsync_start = in->hsync_start;
1546 out->hsync_end = in->hsync_end;
1547 out->htotal = in->htotal;
1548 out->hskew = in->hskew;
1549 out->vdisplay = in->vdisplay;
1550 out->vsync_start = in->vsync_start;
1551 out->vsync_end = in->vsync_end;
1552 out->vtotal = in->vtotal;
1553 out->vscan = in->vscan;
1554 out->vrefresh = in->vrefresh;
1555 out->flags = in->flags;
1556 out->type = in->type;
1557 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1558 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1559}
1560
1561/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001562 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001563 * @out: drm_display_mode to return to the user
1564 * @in: drm_mode_modeinfo to use
1565 *
Dave Airlief453ba02008-11-07 14:05:41 -08001566 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1567 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001568 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001569 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001570 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001571 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001572static int drm_crtc_convert_umode(struct drm_display_mode *out,
1573 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001574{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001575 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1576 return -ERANGE;
1577
Damien Lespiau5848ad42013-09-27 12:11:50 +01001578 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1579 return -EINVAL;
1580
Dave Airlief453ba02008-11-07 14:05:41 -08001581 out->clock = in->clock;
1582 out->hdisplay = in->hdisplay;
1583 out->hsync_start = in->hsync_start;
1584 out->hsync_end = in->hsync_end;
1585 out->htotal = in->htotal;
1586 out->hskew = in->hskew;
1587 out->vdisplay = in->vdisplay;
1588 out->vsync_start = in->vsync_start;
1589 out->vsync_end = in->vsync_end;
1590 out->vtotal = in->vtotal;
1591 out->vscan = in->vscan;
1592 out->vrefresh = in->vrefresh;
1593 out->flags = in->flags;
1594 out->type = in->type;
1595 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1596 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001597
1598 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001599}
1600
1601/**
1602 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001603 * @dev: drm device for the ioctl
1604 * @data: data pointer for the ioctl
1605 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001606 *
Dave Airlief453ba02008-11-07 14:05:41 -08001607 * Construct a set of configuration description structures and return
1608 * them to the user, including CRTC, connector and framebuffer configuration.
1609 *
1610 * Called by the user via ioctl.
1611 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001612 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001613 * Zero on success, errno on failure.
1614 */
1615int drm_mode_getresources(struct drm_device *dev, void *data,
1616 struct drm_file *file_priv)
1617{
1618 struct drm_mode_card_res *card_res = data;
1619 struct list_head *lh;
1620 struct drm_framebuffer *fb;
1621 struct drm_connector *connector;
1622 struct drm_crtc *crtc;
1623 struct drm_encoder *encoder;
1624 int ret = 0;
1625 int connector_count = 0;
1626 int crtc_count = 0;
1627 int fb_count = 0;
1628 int encoder_count = 0;
1629 int copied = 0, i;
1630 uint32_t __user *fb_id;
1631 uint32_t __user *crtc_id;
1632 uint32_t __user *connector_id;
1633 uint32_t __user *encoder_id;
1634 struct drm_mode_group *mode_group;
1635
Dave Airliefb3b06c2011-02-08 13:55:21 +10001636 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1637 return -EINVAL;
1638
Dave Airlief453ba02008-11-07 14:05:41 -08001639
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001640 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001641 /*
1642 * For the non-control nodes we need to limit the list of resources
1643 * by IDs in the group list for this node
1644 */
1645 list_for_each(lh, &file_priv->fbs)
1646 fb_count++;
1647
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001648 /* handle this in 4 parts */
1649 /* FBs */
1650 if (card_res->count_fbs >= fb_count) {
1651 copied = 0;
1652 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1653 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1654 if (put_user(fb->base.id, fb_id + copied)) {
1655 mutex_unlock(&file_priv->fbs_lock);
1656 return -EFAULT;
1657 }
1658 copied++;
1659 }
1660 }
1661 card_res->count_fbs = fb_count;
1662 mutex_unlock(&file_priv->fbs_lock);
1663
1664 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001665 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001666
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001667 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001668 list_for_each(lh, &dev->mode_config.crtc_list)
1669 crtc_count++;
1670
1671 list_for_each(lh, &dev->mode_config.connector_list)
1672 connector_count++;
1673
1674 list_for_each(lh, &dev->mode_config.encoder_list)
1675 encoder_count++;
1676 } else {
1677
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001678 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001679 crtc_count = mode_group->num_crtcs;
1680 connector_count = mode_group->num_connectors;
1681 encoder_count = mode_group->num_encoders;
1682 }
1683
1684 card_res->max_height = dev->mode_config.max_height;
1685 card_res->min_height = dev->mode_config.min_height;
1686 card_res->max_width = dev->mode_config.max_width;
1687 card_res->min_width = dev->mode_config.min_width;
1688
Dave Airlief453ba02008-11-07 14:05:41 -08001689 /* CRTCs */
1690 if (card_res->count_crtcs >= crtc_count) {
1691 copied = 0;
1692 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001693 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001694 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1695 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001696 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001697 if (put_user(crtc->base.id, crtc_id + copied)) {
1698 ret = -EFAULT;
1699 goto out;
1700 }
1701 copied++;
1702 }
1703 } else {
1704 for (i = 0; i < mode_group->num_crtcs; i++) {
1705 if (put_user(mode_group->id_list[i],
1706 crtc_id + copied)) {
1707 ret = -EFAULT;
1708 goto out;
1709 }
1710 copied++;
1711 }
1712 }
1713 }
1714 card_res->count_crtcs = crtc_count;
1715
1716 /* Encoders */
1717 if (card_res->count_encoders >= encoder_count) {
1718 copied = 0;
1719 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001720 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001721 list_for_each_entry(encoder,
1722 &dev->mode_config.encoder_list,
1723 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001724 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001725 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001726 if (put_user(encoder->base.id, encoder_id +
1727 copied)) {
1728 ret = -EFAULT;
1729 goto out;
1730 }
1731 copied++;
1732 }
1733 } else {
1734 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1735 if (put_user(mode_group->id_list[i],
1736 encoder_id + copied)) {
1737 ret = -EFAULT;
1738 goto out;
1739 }
1740 copied++;
1741 }
1742
1743 }
1744 }
1745 card_res->count_encoders = encoder_count;
1746
1747 /* Connectors */
1748 if (card_res->count_connectors >= connector_count) {
1749 copied = 0;
1750 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001751 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001752 list_for_each_entry(connector,
1753 &dev->mode_config.connector_list,
1754 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001755 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1756 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001757 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001758 if (put_user(connector->base.id,
1759 connector_id + copied)) {
1760 ret = -EFAULT;
1761 goto out;
1762 }
1763 copied++;
1764 }
1765 } else {
1766 int start = mode_group->num_crtcs +
1767 mode_group->num_encoders;
1768 for (i = start; i < start + mode_group->num_connectors; i++) {
1769 if (put_user(mode_group->id_list[i],
1770 connector_id + copied)) {
1771 ret = -EFAULT;
1772 goto out;
1773 }
1774 copied++;
1775 }
1776 }
1777 }
1778 card_res->count_connectors = connector_count;
1779
Jerome Glisse94401062010-07-15 15:43:25 -04001780 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001781 card_res->count_connectors, card_res->count_encoders);
1782
1783out:
Daniel Vetter84849902012-12-02 00:28:11 +01001784 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001785 return ret;
1786}
1787
1788/**
1789 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001790 * @dev: drm device for the ioctl
1791 * @data: data pointer for the ioctl
1792 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001793 *
Dave Airlief453ba02008-11-07 14:05:41 -08001794 * Construct a CRTC configuration structure to return to the user.
1795 *
1796 * Called by the user via ioctl.
1797 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001798 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001799 * Zero on success, errno on failure.
1800 */
1801int drm_mode_getcrtc(struct drm_device *dev,
1802 void *data, struct drm_file *file_priv)
1803{
1804 struct drm_mode_crtc *crtc_resp = data;
1805 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001806 int ret = 0;
1807
Dave Airliefb3b06c2011-02-08 13:55:21 +10001808 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1809 return -EINVAL;
1810
Daniel Vetter84849902012-12-02 00:28:11 +01001811 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001812
Rob Clarka2b34e22013-10-05 16:36:52 -04001813 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1814 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001815 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001816 goto out;
1817 }
Dave Airlief453ba02008-11-07 14:05:41 -08001818
1819 crtc_resp->x = crtc->x;
1820 crtc_resp->y = crtc->y;
1821 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001822 if (crtc->primary->fb)
1823 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001824 else
1825 crtc_resp->fb_id = 0;
1826
1827 if (crtc->enabled) {
1828
1829 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1830 crtc_resp->mode_valid = 1;
1831
1832 } else {
1833 crtc_resp->mode_valid = 0;
1834 }
1835
1836out:
Daniel Vetter84849902012-12-02 00:28:11 +01001837 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001838 return ret;
1839}
1840
Damien Lespiau61d8e322013-09-25 16:45:22 +01001841static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1842 const struct drm_file *file_priv)
1843{
1844 /*
1845 * If user-space hasn't configured the driver to expose the stereo 3D
1846 * modes, don't expose them.
1847 */
1848 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1849 return false;
1850
1851 return true;
1852}
1853
Dave Airlief453ba02008-11-07 14:05:41 -08001854/**
1855 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001856 * @dev: drm device for the ioctl
1857 * @data: data pointer for the ioctl
1858 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001859 *
Dave Airlief453ba02008-11-07 14:05:41 -08001860 * Construct a connector configuration structure to return to the user.
1861 *
1862 * Called by the user via ioctl.
1863 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001864 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001865 * Zero on success, errno on failure.
1866 */
1867int drm_mode_getconnector(struct drm_device *dev, void *data,
1868 struct drm_file *file_priv)
1869{
1870 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001871 struct drm_connector *connector;
1872 struct drm_display_mode *mode;
1873 int mode_count = 0;
1874 int props_count = 0;
1875 int encoders_count = 0;
1876 int ret = 0;
1877 int copied = 0;
1878 int i;
1879 struct drm_mode_modeinfo u_mode;
1880 struct drm_mode_modeinfo __user *mode_ptr;
1881 uint32_t __user *prop_ptr;
1882 uint64_t __user *prop_values;
1883 uint32_t __user *encoder_ptr;
1884
Dave Airliefb3b06c2011-02-08 13:55:21 +10001885 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1886 return -EINVAL;
1887
Dave Airlief453ba02008-11-07 14:05:41 -08001888 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1889
Jerome Glisse94401062010-07-15 15:43:25 -04001890 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001891
Daniel Vetter7b240562012-12-12 00:35:33 +01001892 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001893
Rob Clarka2b34e22013-10-05 16:36:52 -04001894 connector = drm_connector_find(dev, out_resp->connector_id);
1895 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001896 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001897 goto out;
1898 }
Dave Airlief453ba02008-11-07 14:05:41 -08001899
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001900 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001901
1902 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1903 if (connector->encoder_ids[i] != 0) {
1904 encoders_count++;
1905 }
1906 }
1907
1908 if (out_resp->count_modes == 0) {
1909 connector->funcs->fill_modes(connector,
1910 dev->mode_config.max_width,
1911 dev->mode_config.max_height);
1912 }
1913
1914 /* delayed so we get modes regardless of pre-fill_modes state */
1915 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001916 if (drm_mode_expose_to_userspace(mode, file_priv))
1917 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001918
1919 out_resp->connector_id = connector->base.id;
1920 out_resp->connector_type = connector->connector_type;
1921 out_resp->connector_type_id = connector->connector_type_id;
1922 out_resp->mm_width = connector->display_info.width_mm;
1923 out_resp->mm_height = connector->display_info.height_mm;
1924 out_resp->subpixel = connector->display_info.subpixel_order;
1925 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001926 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001927 if (connector->encoder)
1928 out_resp->encoder_id = connector->encoder->base.id;
1929 else
1930 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001931 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001932
1933 /*
1934 * This ioctl is called twice, once to determine how much space is
1935 * needed, and the 2nd time to fill it.
1936 */
1937 if ((out_resp->count_modes >= mode_count) && mode_count) {
1938 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001939 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001940 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001941 if (!drm_mode_expose_to_userspace(mode, file_priv))
1942 continue;
1943
Dave Airlief453ba02008-11-07 14:05:41 -08001944 drm_crtc_convert_to_umode(&u_mode, mode);
1945 if (copy_to_user(mode_ptr + copied,
1946 &u_mode, sizeof(u_mode))) {
1947 ret = -EFAULT;
1948 goto out;
1949 }
1950 copied++;
1951 }
1952 }
1953 out_resp->count_modes = mode_count;
1954
1955 if ((out_resp->count_props >= props_count) && props_count) {
1956 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001957 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1958 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001959 for (i = 0; i < connector->properties.count; i++) {
1960 if (put_user(connector->properties.ids[i],
1961 prop_ptr + copied)) {
1962 ret = -EFAULT;
1963 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001964 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001965
1966 if (put_user(connector->properties.values[i],
1967 prop_values + copied)) {
1968 ret = -EFAULT;
1969 goto out;
1970 }
1971 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001972 }
1973 }
1974 out_resp->count_props = props_count;
1975
1976 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1977 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001978 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001979 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1980 if (connector->encoder_ids[i] != 0) {
1981 if (put_user(connector->encoder_ids[i],
1982 encoder_ptr + copied)) {
1983 ret = -EFAULT;
1984 goto out;
1985 }
1986 copied++;
1987 }
1988 }
1989 }
1990 out_resp->count_encoders = encoders_count;
1991
1992out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001993 mutex_unlock(&dev->mode_config.mutex);
1994
Dave Airlief453ba02008-11-07 14:05:41 -08001995 return ret;
1996}
1997
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001998/**
1999 * drm_mode_getencoder - get encoder configuration
2000 * @dev: drm device for the ioctl
2001 * @data: data pointer for the ioctl
2002 * @file_priv: drm file for the ioctl call
2003 *
2004 * Construct a encoder configuration structure to return to the user.
2005 *
2006 * Called by the user via ioctl.
2007 *
2008 * Returns:
2009 * Zero on success, errno on failure.
2010 */
Dave Airlief453ba02008-11-07 14:05:41 -08002011int drm_mode_getencoder(struct drm_device *dev, void *data,
2012 struct drm_file *file_priv)
2013{
2014 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002015 struct drm_encoder *encoder;
2016 int ret = 0;
2017
Dave Airliefb3b06c2011-02-08 13:55:21 +10002018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2019 return -EINVAL;
2020
Daniel Vetter84849902012-12-02 00:28:11 +01002021 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002022 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2023 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002024 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002025 goto out;
2026 }
Dave Airlief453ba02008-11-07 14:05:41 -08002027
2028 if (encoder->crtc)
2029 enc_resp->crtc_id = encoder->crtc->base.id;
2030 else
2031 enc_resp->crtc_id = 0;
2032 enc_resp->encoder_type = encoder->encoder_type;
2033 enc_resp->encoder_id = encoder->base.id;
2034 enc_resp->possible_crtcs = encoder->possible_crtcs;
2035 enc_resp->possible_clones = encoder->possible_clones;
2036
2037out:
Daniel Vetter84849902012-12-02 00:28:11 +01002038 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002039 return ret;
2040}
2041
2042/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002043 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002044 * @dev: DRM device
2045 * @data: ioctl data
2046 * @file_priv: DRM file info
2047 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002048 * Construct a list of plane ids to return to the user.
2049 *
2050 * Called by the user via ioctl.
2051 *
2052 * Returns:
2053 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002054 */
2055int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002056 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002057{
2058 struct drm_mode_get_plane_res *plane_resp = data;
2059 struct drm_mode_config *config;
2060 struct drm_plane *plane;
2061 uint32_t __user *plane_ptr;
2062 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002063 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002064
2065 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2066 return -EINVAL;
2067
Daniel Vetter84849902012-12-02 00:28:11 +01002068 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002069 config = &dev->mode_config;
2070
Matt Roper681e7ec2014-04-01 15:22:42 -07002071 if (file_priv->universal_planes)
2072 num_planes = config->num_total_plane;
2073 else
2074 num_planes = config->num_overlay_plane;
2075
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002076 /*
2077 * This ioctl is called twice, once to determine how much space is
2078 * needed, and the 2nd time to fill it.
2079 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002080 if (num_planes &&
2081 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002082 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002083
2084 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002085 /*
2086 * Unless userspace set the 'universal planes'
2087 * capability bit, only advertise overlays.
2088 */
2089 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2090 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002091 continue;
2092
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002093 if (put_user(plane->base.id, plane_ptr + copied)) {
2094 ret = -EFAULT;
2095 goto out;
2096 }
2097 copied++;
2098 }
2099 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002100 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002101
2102out:
Daniel Vetter84849902012-12-02 00:28:11 +01002103 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002104 return ret;
2105}
2106
2107/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002108 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002109 * @dev: DRM device
2110 * @data: ioctl data
2111 * @file_priv: DRM file info
2112 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002113 * Construct a plane configuration structure to return to the user.
2114 *
2115 * Called by the user via ioctl.
2116 *
2117 * Returns:
2118 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002119 */
2120int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002121 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002122{
2123 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002124 struct drm_plane *plane;
2125 uint32_t __user *format_ptr;
2126 int ret = 0;
2127
2128 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2129 return -EINVAL;
2130
Daniel Vetter84849902012-12-02 00:28:11 +01002131 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002132 plane = drm_plane_find(dev, plane_resp->plane_id);
2133 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002134 ret = -ENOENT;
2135 goto out;
2136 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002137
2138 if (plane->crtc)
2139 plane_resp->crtc_id = plane->crtc->base.id;
2140 else
2141 plane_resp->crtc_id = 0;
2142
2143 if (plane->fb)
2144 plane_resp->fb_id = plane->fb->base.id;
2145 else
2146 plane_resp->fb_id = 0;
2147
2148 plane_resp->plane_id = plane->base.id;
2149 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002150 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002151
2152 /*
2153 * This ioctl is called twice, once to determine how much space is
2154 * needed, and the 2nd time to fill it.
2155 */
2156 if (plane->format_count &&
2157 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002158 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002159 if (copy_to_user(format_ptr,
2160 plane->format_types,
2161 sizeof(uint32_t) * plane->format_count)) {
2162 ret = -EFAULT;
2163 goto out;
2164 }
2165 }
2166 plane_resp->count_format_types = plane->format_count;
2167
2168out:
Daniel Vetter84849902012-12-02 00:28:11 +01002169 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002170 return ret;
2171}
2172
2173/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002174 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002175 * @dev: DRM device
2176 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002177 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002178 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002179 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002180 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002181 *
2182 * Returns:
2183 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002184 */
2185int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002186 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002187{
2188 struct drm_mode_set_plane *plane_req = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002189 struct drm_plane *plane;
2190 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002191 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002192 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002193 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002194 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002195
2196 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2197 return -EINVAL;
2198
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002199 /*
2200 * First, find the plane, crtc, and fb objects. If not available,
2201 * we don't bother to call the driver.
2202 */
Rob Clarka2b34e22013-10-05 16:36:52 -04002203 plane = drm_plane_find(dev, plane_req->plane_id);
2204 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002205 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2206 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002207 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002208 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002209
2210 /* No fb means shut it down */
2211 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002212 drm_modeset_lock_all(dev);
2213 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002214 ret = plane->funcs->disable_plane(plane);
2215 if (!ret) {
2216 plane->crtc = NULL;
2217 plane->fb = NULL;
2218 } else {
2219 old_fb = NULL;
2220 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002221 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002222 goto out;
2223 }
2224
Rob Clarka2b34e22013-10-05 16:36:52 -04002225 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2226 if (!crtc) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002227 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2228 plane_req->crtc_id);
2229 ret = -ENOENT;
2230 goto out;
2231 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002232
Matt Roper7f994f32014-05-29 08:06:51 -07002233 /* Check whether this plane is usable on this CRTC */
2234 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2235 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2236 ret = -EINVAL;
2237 goto out;
2238 }
2239
Daniel Vetter786b99e2012-12-02 21:53:40 +01002240 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2241 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002242 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2243 plane_req->fb_id);
2244 ret = -ENOENT;
2245 goto out;
2246 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002247
Ville Syrjälä62443be2011-12-20 00:06:47 +02002248 /* Check whether this plane supports the fb pixel format. */
2249 for (i = 0; i < plane->format_count; i++)
2250 if (fb->pixel_format == plane->format_types[i])
2251 break;
2252 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002253 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2254 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002255 ret = -EINVAL;
2256 goto out;
2257 }
2258
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002259 fb_width = fb->width << 16;
2260 fb_height = fb->height << 16;
2261
2262 /* Make sure source coordinates are inside the fb. */
2263 if (plane_req->src_w > fb_width ||
2264 plane_req->src_x > fb_width - plane_req->src_w ||
2265 plane_req->src_h > fb_height ||
2266 plane_req->src_y > fb_height - plane_req->src_h) {
2267 DRM_DEBUG_KMS("Invalid source coordinates "
2268 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2269 plane_req->src_w >> 16,
2270 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2271 plane_req->src_h >> 16,
2272 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2273 plane_req->src_x >> 16,
2274 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2275 plane_req->src_y >> 16,
2276 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2277 ret = -ENOSPC;
2278 goto out;
2279 }
2280
Ville Syrjälä687a0402011-12-20 00:06:45 +02002281 /* Give drivers some help against integer overflows */
2282 if (plane_req->crtc_w > INT_MAX ||
2283 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2284 plane_req->crtc_h > INT_MAX ||
2285 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2286 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2287 plane_req->crtc_w, plane_req->crtc_h,
2288 plane_req->crtc_x, plane_req->crtc_y);
2289 ret = -ERANGE;
2290 goto out;
2291 }
2292
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002293 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002294 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002295 ret = plane->funcs->update_plane(plane, crtc, fb,
2296 plane_req->crtc_x, plane_req->crtc_y,
2297 plane_req->crtc_w, plane_req->crtc_h,
2298 plane_req->src_x, plane_req->src_y,
2299 plane_req->src_w, plane_req->src_h);
2300 if (!ret) {
2301 plane->crtc = crtc;
2302 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002303 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002304 } else {
2305 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002306 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002307 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002308
2309out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002310 if (fb)
2311 drm_framebuffer_unreference(fb);
2312 if (old_fb)
2313 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002314
2315 return ret;
2316}
2317
2318/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002319 * drm_mode_set_config_internal - helper to call ->set_config
2320 * @set: modeset config to set
2321 *
2322 * This is a little helper to wrap internal calls to the ->set_config driver
2323 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002324 *
2325 * Returns:
2326 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002327 */
2328int drm_mode_set_config_internal(struct drm_mode_set *set)
2329{
2330 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002331 struct drm_framebuffer *fb;
2332 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002333 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002334
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002335 /*
2336 * NOTE: ->set_config can also disable other crtcs (if we steal all
2337 * connectors from it), hence we need to refcount the fbs across all
2338 * crtcs. Atomic modeset will have saner semantics ...
2339 */
2340 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002341 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002342
Daniel Vetterb0d12322012-12-11 01:07:12 +01002343 fb = set->fb;
2344
2345 ret = crtc->funcs->set_config(set);
2346 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002347 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002348 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002349 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002350
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002351 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002352 if (tmp->primary->fb)
2353 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002354 if (tmp->old_fb)
2355 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002356 }
2357
2358 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002359}
2360EXPORT_SYMBOL(drm_mode_set_config_internal);
2361
Matt Roperaf936292014-04-01 15:22:34 -07002362/**
2363 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2364 * CRTC viewport
2365 * @crtc: CRTC that framebuffer will be displayed on
2366 * @x: x panning
2367 * @y: y panning
2368 * @mode: mode that framebuffer will be displayed under
2369 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002370 */
Matt Roperaf936292014-04-01 15:22:34 -07002371int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2372 int x, int y,
2373 const struct drm_display_mode *mode,
2374 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002375
2376{
2377 int hdisplay, vdisplay;
2378
2379 hdisplay = mode->hdisplay;
2380 vdisplay = mode->vdisplay;
2381
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002382 if (drm_mode_is_stereo(mode)) {
2383 struct drm_display_mode adjusted = *mode;
2384
2385 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2386 hdisplay = adjusted.crtc_hdisplay;
2387 vdisplay = adjusted.crtc_vdisplay;
2388 }
2389
Damien Lespiauc11e9282013-09-25 16:45:30 +01002390 if (crtc->invert_dimensions)
2391 swap(hdisplay, vdisplay);
2392
2393 if (hdisplay > fb->width ||
2394 vdisplay > fb->height ||
2395 x > fb->width - hdisplay ||
2396 y > fb->height - vdisplay) {
2397 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2398 fb->width, fb->height, hdisplay, vdisplay, x, y,
2399 crtc->invert_dimensions ? " (inverted)" : "");
2400 return -ENOSPC;
2401 }
2402
2403 return 0;
2404}
Matt Roperaf936292014-04-01 15:22:34 -07002405EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002406
Daniel Vetter2d13b672012-12-11 13:47:23 +01002407/**
Dave Airlief453ba02008-11-07 14:05:41 -08002408 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002409 * @dev: drm device for the ioctl
2410 * @data: data pointer for the ioctl
2411 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002412 *
Dave Airlief453ba02008-11-07 14:05:41 -08002413 * Build a new CRTC configuration based on user request.
2414 *
2415 * Called by the user via ioctl.
2416 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002417 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002418 * Zero on success, errno on failure.
2419 */
2420int drm_mode_setcrtc(struct drm_device *dev, void *data,
2421 struct drm_file *file_priv)
2422{
2423 struct drm_mode_config *config = &dev->mode_config;
2424 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002425 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002426 struct drm_connector **connector_set = NULL, *connector;
2427 struct drm_framebuffer *fb = NULL;
2428 struct drm_display_mode *mode = NULL;
2429 struct drm_mode_set set;
2430 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002431 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002432 int i;
2433
Dave Airliefb3b06c2011-02-08 13:55:21 +10002434 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2435 return -EINVAL;
2436
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002437 /* For some reason crtc x/y offsets are signed internally. */
2438 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2439 return -ERANGE;
2440
Daniel Vetter84849902012-12-02 00:28:11 +01002441 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002442 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2443 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002444 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002445 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002446 goto out;
2447 }
Jerome Glisse94401062010-07-15 15:43:25 -04002448 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002449
2450 if (crtc_req->mode_valid) {
2451 /* If we have a mode we need a framebuffer. */
2452 /* If we pass -1, set the mode with the currently bound fb */
2453 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002454 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002455 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2456 ret = -EINVAL;
2457 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002458 }
Matt Roperf4510a22014-04-01 15:22:40 -07002459 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002460 /* Make refcounting symmetric with the lookup path. */
2461 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002462 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002463 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2464 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002465 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2466 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002467 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002468 goto out;
2469 }
Dave Airlief453ba02008-11-07 14:05:41 -08002470 }
2471
2472 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002473 if (!mode) {
2474 ret = -ENOMEM;
2475 goto out;
2476 }
2477
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002478 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2479 if (ret) {
2480 DRM_DEBUG_KMS("Invalid mode\n");
2481 goto out;
2482 }
2483
Dave Airlief453ba02008-11-07 14:05:41 -08002484 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002485
Damien Lespiauc11e9282013-09-25 16:45:30 +01002486 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2487 mode, fb);
2488 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002489 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002490
Dave Airlief453ba02008-11-07 14:05:41 -08002491 }
2492
2493 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002494 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002495 ret = -EINVAL;
2496 goto out;
2497 }
2498
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002499 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002500 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002501 crtc_req->count_connectors);
2502 ret = -EINVAL;
2503 goto out;
2504 }
2505
2506 if (crtc_req->count_connectors > 0) {
2507 u32 out_id;
2508
2509 /* Avoid unbounded kernel memory allocation */
2510 if (crtc_req->count_connectors > config->num_connector) {
2511 ret = -EINVAL;
2512 goto out;
2513 }
2514
2515 connector_set = kmalloc(crtc_req->count_connectors *
2516 sizeof(struct drm_connector *),
2517 GFP_KERNEL);
2518 if (!connector_set) {
2519 ret = -ENOMEM;
2520 goto out;
2521 }
2522
2523 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002524 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002525 if (get_user(out_id, &set_connectors_ptr[i])) {
2526 ret = -EFAULT;
2527 goto out;
2528 }
2529
Rob Clarka2b34e22013-10-05 16:36:52 -04002530 connector = drm_connector_find(dev, out_id);
2531 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002532 DRM_DEBUG_KMS("Connector id %d unknown\n",
2533 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002534 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002535 goto out;
2536 }
Jerome Glisse94401062010-07-15 15:43:25 -04002537 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2538 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002539 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002540
2541 connector_set[i] = connector;
2542 }
2543 }
2544
2545 set.crtc = crtc;
2546 set.x = crtc_req->x;
2547 set.y = crtc_req->y;
2548 set.mode = mode;
2549 set.connectors = connector_set;
2550 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002551 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002552 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002553
2554out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002555 if (fb)
2556 drm_framebuffer_unreference(fb);
2557
Dave Airlief453ba02008-11-07 14:05:41 -08002558 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002559 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002560 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002561 return ret;
2562}
2563
Dave Airlie4c813d42013-06-20 11:48:52 +10002564static int drm_mode_cursor_common(struct drm_device *dev,
2565 struct drm_mode_cursor2 *req,
2566 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002567{
Dave Airlief453ba02008-11-07 14:05:41 -08002568 struct drm_crtc *crtc;
2569 int ret = 0;
2570
Dave Airliefb3b06c2011-02-08 13:55:21 +10002571 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2572 return -EINVAL;
2573
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002574 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002575 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002576
Rob Clarka2b34e22013-10-05 16:36:52 -04002577 crtc = drm_crtc_find(dev, req->crtc_id);
2578 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002579 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002580 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002581 }
Dave Airlief453ba02008-11-07 14:05:41 -08002582
Rob Clark51fd3712013-11-19 12:10:12 -05002583 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002584 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002585 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002586 ret = -ENXIO;
2587 goto out;
2588 }
2589 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002590 if (crtc->funcs->cursor_set2)
2591 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2592 req->width, req->height, req->hot_x, req->hot_y);
2593 else
2594 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2595 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002596 }
2597
2598 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2599 if (crtc->funcs->cursor_move) {
2600 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2601 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002602 ret = -EFAULT;
2603 goto out;
2604 }
2605 }
2606out:
Rob Clark51fd3712013-11-19 12:10:12 -05002607 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002608
Dave Airlief453ba02008-11-07 14:05:41 -08002609 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002610
2611}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002612
2613
2614/**
2615 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2616 * @dev: drm device for the ioctl
2617 * @data: data pointer for the ioctl
2618 * @file_priv: drm file for the ioctl call
2619 *
2620 * Set the cursor configuration based on user request.
2621 *
2622 * Called by the user via ioctl.
2623 *
2624 * Returns:
2625 * Zero on success, errno on failure.
2626 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002627int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002628 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002629{
2630 struct drm_mode_cursor *req = data;
2631 struct drm_mode_cursor2 new_req;
2632
2633 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2634 new_req.hot_x = new_req.hot_y = 0;
2635
2636 return drm_mode_cursor_common(dev, &new_req, file_priv);
2637}
2638
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002639/**
2640 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2641 * @dev: drm device for the ioctl
2642 * @data: data pointer for the ioctl
2643 * @file_priv: drm file for the ioctl call
2644 *
2645 * Set the cursor configuration based on user request. This implements the 2nd
2646 * version of the cursor ioctl, which allows userspace to additionally specify
2647 * the hotspot of the pointer.
2648 *
2649 * Called by the user via ioctl.
2650 *
2651 * Returns:
2652 * Zero on success, errno on failure.
2653 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002654int drm_mode_cursor2_ioctl(struct drm_device *dev,
2655 void *data, struct drm_file *file_priv)
2656{
2657 struct drm_mode_cursor2 *req = data;
2658 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002659}
2660
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002661/**
2662 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2663 * @bpp: bits per pixels
2664 * @depth: bit depth per pixel
2665 *
2666 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2667 * Useful in fbdev emulation code, since that deals in those values.
2668 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002669uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2670{
2671 uint32_t fmt;
2672
2673 switch (bpp) {
2674 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002675 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002676 break;
2677 case 16:
2678 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002679 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002680 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002681 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002682 break;
2683 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002684 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002685 break;
2686 case 32:
2687 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002688 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002689 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002690 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002691 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002692 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002693 break;
2694 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002695 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2696 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002697 break;
2698 }
2699
2700 return fmt;
2701}
2702EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2703
Dave Airlief453ba02008-11-07 14:05:41 -08002704/**
2705 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002706 * @dev: drm device for the ioctl
2707 * @data: data pointer for the ioctl
2708 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002709 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002710 * Add a new FB to the specified CRTC, given a user request. This is the
2711 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002712 *
2713 * Called by the user via ioctl.
2714 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002715 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002716 * Zero on success, errno on failure.
2717 */
2718int drm_mode_addfb(struct drm_device *dev,
2719 void *data, struct drm_file *file_priv)
2720{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002721 struct drm_mode_fb_cmd *or = data;
2722 struct drm_mode_fb_cmd2 r = {};
2723 struct drm_mode_config *config = &dev->mode_config;
2724 struct drm_framebuffer *fb;
2725 int ret = 0;
2726
2727 /* Use new struct with format internally */
2728 r.fb_id = or->fb_id;
2729 r.width = or->width;
2730 r.height = or->height;
2731 r.pitches[0] = or->pitch;
2732 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2733 r.handles[0] = or->handle;
2734
2735 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2736 return -EINVAL;
2737
Jesse Barnesacb4b992011-11-07 12:03:23 -08002738 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002739 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002740
2741 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002742 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002743
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002744 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2745 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002746 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002747 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002748 }
2749
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002750 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002751 or->fb_id = fb->base.id;
2752 list_add(&fb->filp_head, &file_priv->fbs);
2753 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002754 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002755
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002756 return ret;
2757}
2758
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002759static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002760{
2761 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2762
2763 switch (format) {
2764 case DRM_FORMAT_C8:
2765 case DRM_FORMAT_RGB332:
2766 case DRM_FORMAT_BGR233:
2767 case DRM_FORMAT_XRGB4444:
2768 case DRM_FORMAT_XBGR4444:
2769 case DRM_FORMAT_RGBX4444:
2770 case DRM_FORMAT_BGRX4444:
2771 case DRM_FORMAT_ARGB4444:
2772 case DRM_FORMAT_ABGR4444:
2773 case DRM_FORMAT_RGBA4444:
2774 case DRM_FORMAT_BGRA4444:
2775 case DRM_FORMAT_XRGB1555:
2776 case DRM_FORMAT_XBGR1555:
2777 case DRM_FORMAT_RGBX5551:
2778 case DRM_FORMAT_BGRX5551:
2779 case DRM_FORMAT_ARGB1555:
2780 case DRM_FORMAT_ABGR1555:
2781 case DRM_FORMAT_RGBA5551:
2782 case DRM_FORMAT_BGRA5551:
2783 case DRM_FORMAT_RGB565:
2784 case DRM_FORMAT_BGR565:
2785 case DRM_FORMAT_RGB888:
2786 case DRM_FORMAT_BGR888:
2787 case DRM_FORMAT_XRGB8888:
2788 case DRM_FORMAT_XBGR8888:
2789 case DRM_FORMAT_RGBX8888:
2790 case DRM_FORMAT_BGRX8888:
2791 case DRM_FORMAT_ARGB8888:
2792 case DRM_FORMAT_ABGR8888:
2793 case DRM_FORMAT_RGBA8888:
2794 case DRM_FORMAT_BGRA8888:
2795 case DRM_FORMAT_XRGB2101010:
2796 case DRM_FORMAT_XBGR2101010:
2797 case DRM_FORMAT_RGBX1010102:
2798 case DRM_FORMAT_BGRX1010102:
2799 case DRM_FORMAT_ARGB2101010:
2800 case DRM_FORMAT_ABGR2101010:
2801 case DRM_FORMAT_RGBA1010102:
2802 case DRM_FORMAT_BGRA1010102:
2803 case DRM_FORMAT_YUYV:
2804 case DRM_FORMAT_YVYU:
2805 case DRM_FORMAT_UYVY:
2806 case DRM_FORMAT_VYUY:
2807 case DRM_FORMAT_AYUV:
2808 case DRM_FORMAT_NV12:
2809 case DRM_FORMAT_NV21:
2810 case DRM_FORMAT_NV16:
2811 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002812 case DRM_FORMAT_NV24:
2813 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002814 case DRM_FORMAT_YUV410:
2815 case DRM_FORMAT_YVU410:
2816 case DRM_FORMAT_YUV411:
2817 case DRM_FORMAT_YVU411:
2818 case DRM_FORMAT_YUV420:
2819 case DRM_FORMAT_YVU420:
2820 case DRM_FORMAT_YUV422:
2821 case DRM_FORMAT_YVU422:
2822 case DRM_FORMAT_YUV444:
2823 case DRM_FORMAT_YVU444:
2824 return 0;
2825 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002826 DRM_DEBUG_KMS("invalid pixel format %s\n",
2827 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002828 return -EINVAL;
2829 }
2830}
2831
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002832static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002833{
2834 int ret, hsub, vsub, num_planes, i;
2835
2836 ret = format_check(r);
2837 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002838 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2839 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002840 return ret;
2841 }
2842
2843 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2844 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2845 num_planes = drm_format_num_planes(r->pixel_format);
2846
2847 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002848 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002849 return -EINVAL;
2850 }
2851
2852 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002853 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002854 return -EINVAL;
2855 }
2856
2857 for (i = 0; i < num_planes; i++) {
2858 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002859 unsigned int height = r->height / (i != 0 ? vsub : 1);
2860 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002861
2862 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002863 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002864 return -EINVAL;
2865 }
2866
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002867 if ((uint64_t) width * cpp > UINT_MAX)
2868 return -ERANGE;
2869
2870 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2871 return -ERANGE;
2872
2873 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002874 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002875 return -EINVAL;
2876 }
2877 }
2878
2879 return 0;
2880}
2881
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002882/**
2883 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002884 * @dev: drm device for the ioctl
2885 * @data: data pointer for the ioctl
2886 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002887 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002888 * Add a new FB to the specified CRTC, given a user request with format. This is
2889 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2890 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002891 *
2892 * Called by the user via ioctl.
2893 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002894 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002895 * Zero on success, errno on failure.
2896 */
2897int drm_mode_addfb2(struct drm_device *dev,
2898 void *data, struct drm_file *file_priv)
2899{
2900 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002901 struct drm_mode_config *config = &dev->mode_config;
2902 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002903 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002904
Dave Airliefb3b06c2011-02-08 13:55:21 +10002905 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2906 return -EINVAL;
2907
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002908 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2909 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2910 return -EINVAL;
2911 }
2912
Dave Airlief453ba02008-11-07 14:05:41 -08002913 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002914 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002915 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002916 return -EINVAL;
2917 }
2918 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002919 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002920 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002921 return -EINVAL;
2922 }
2923
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002924 ret = framebuffer_check(r);
2925 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002926 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002927
Dave Airlief453ba02008-11-07 14:05:41 -08002928 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002929 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002930 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002931 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002932 }
2933
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002934 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002935 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002936 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002937 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002938 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002939
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002940
Dave Airlief453ba02008-11-07 14:05:41 -08002941 return ret;
2942}
2943
2944/**
2945 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002946 * @dev: drm device for the ioctl
2947 * @data: data pointer for the ioctl
2948 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002949 *
Dave Airlief453ba02008-11-07 14:05:41 -08002950 * Remove the FB specified by the user.
2951 *
2952 * Called by the user via ioctl.
2953 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002954 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002955 * Zero on success, errno on failure.
2956 */
2957int drm_mode_rmfb(struct drm_device *dev,
2958 void *data, struct drm_file *file_priv)
2959{
Dave Airlief453ba02008-11-07 14:05:41 -08002960 struct drm_framebuffer *fb = NULL;
2961 struct drm_framebuffer *fbl = NULL;
2962 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002963 int found = 0;
2964
Dave Airliefb3b06c2011-02-08 13:55:21 +10002965 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2966 return -EINVAL;
2967
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002968 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002969 mutex_lock(&dev->mode_config.fb_lock);
2970 fb = __drm_framebuffer_lookup(dev, *id);
2971 if (!fb)
2972 goto fail_lookup;
2973
Dave Airlief453ba02008-11-07 14:05:41 -08002974 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2975 if (fb == fbl)
2976 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002977 if (!found)
2978 goto fail_lookup;
2979
2980 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2981 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002982
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002983 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002984 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002985 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002986
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002987 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002988
Daniel Vetter2b677e82012-12-10 21:16:05 +01002989 return 0;
2990
2991fail_lookup:
2992 mutex_unlock(&dev->mode_config.fb_lock);
2993 mutex_unlock(&file_priv->fbs_lock);
2994
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002995 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002996}
2997
2998/**
2999 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003000 * @dev: drm device for the ioctl
3001 * @data: data pointer for the ioctl
3002 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003003 *
Dave Airlief453ba02008-11-07 14:05:41 -08003004 * Lookup the FB given its ID and return info about it.
3005 *
3006 * Called by the user via ioctl.
3007 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003008 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003009 * Zero on success, errno on failure.
3010 */
3011int drm_mode_getfb(struct drm_device *dev,
3012 void *data, struct drm_file *file_priv)
3013{
3014 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003015 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003016 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003017
Dave Airliefb3b06c2011-02-08 13:55:21 +10003018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3019 return -EINVAL;
3020
Daniel Vetter786b99e2012-12-02 21:53:40 +01003021 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003022 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003023 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003024
3025 r->height = fb->height;
3026 r->width = fb->width;
3027 r->depth = fb->depth;
3028 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003029 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003030 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003031 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003032 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003033 ret = fb->funcs->create_handle(fb, file_priv,
3034 &r->handle);
3035 } else {
3036 /* GET_FB() is an unprivileged ioctl so we must not
3037 * return a buffer-handle to non-master processes! For
3038 * backwards-compatibility reasons, we cannot make
3039 * GET_FB() privileged, so just return an invalid handle
3040 * for non-masters. */
3041 r->handle = 0;
3042 ret = 0;
3043 }
3044 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003045 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003046 }
Dave Airlief453ba02008-11-07 14:05:41 -08003047
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003048 drm_framebuffer_unreference(fb);
3049
Dave Airlief453ba02008-11-07 14:05:41 -08003050 return ret;
3051}
3052
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003053/**
3054 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3055 * @dev: drm device for the ioctl
3056 * @data: data pointer for the ioctl
3057 * @file_priv: drm file for the ioctl call
3058 *
3059 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3060 * rectangle list. Generic userspace which does frontbuffer rendering must call
3061 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3062 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3063 *
3064 * Modesetting drivers which always update the frontbuffer do not need to
3065 * implement the corresponding ->dirty framebuffer callback.
3066 *
3067 * Called by the user via ioctl.
3068 *
3069 * Returns:
3070 * Zero on success, errno on failure.
3071 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003072int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3073 void *data, struct drm_file *file_priv)
3074{
3075 struct drm_clip_rect __user *clips_ptr;
3076 struct drm_clip_rect *clips = NULL;
3077 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003078 struct drm_framebuffer *fb;
3079 unsigned flags;
3080 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003081 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003082
Dave Airliefb3b06c2011-02-08 13:55:21 +10003083 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3084 return -EINVAL;
3085
Daniel Vetter786b99e2012-12-02 21:53:40 +01003086 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003087 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003088 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003089
3090 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003091 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003092
3093 if (!num_clips != !clips_ptr) {
3094 ret = -EINVAL;
3095 goto out_err1;
3096 }
3097
3098 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3099
3100 /* If userspace annotates copy, clips must come in pairs */
3101 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3102 ret = -EINVAL;
3103 goto out_err1;
3104 }
3105
3106 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003107 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3108 ret = -EINVAL;
3109 goto out_err1;
3110 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003111 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3112 if (!clips) {
3113 ret = -ENOMEM;
3114 goto out_err1;
3115 }
3116
3117 ret = copy_from_user(clips, clips_ptr,
3118 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003119 if (ret) {
3120 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003121 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003122 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003123 }
3124
3125 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003126 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3127 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003128 } else {
3129 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003130 }
3131
3132out_err2:
3133 kfree(clips);
3134out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003135 drm_framebuffer_unreference(fb);
3136
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003137 return ret;
3138}
3139
3140
Dave Airlief453ba02008-11-07 14:05:41 -08003141/**
3142 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003143 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003144 *
Dave Airlief453ba02008-11-07 14:05:41 -08003145 * Destroy all the FBs associated with @filp.
3146 *
3147 * Called by the user via ioctl.
3148 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003149 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003150 * Zero on success, errno on failure.
3151 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003152void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003153{
Dave Airlief453ba02008-11-07 14:05:41 -08003154 struct drm_device *dev = priv->minor->dev;
3155 struct drm_framebuffer *fb, *tfb;
3156
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003157 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003158 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003159
3160 mutex_lock(&dev->mode_config.fb_lock);
3161 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3162 __drm_framebuffer_unregister(dev, fb);
3163 mutex_unlock(&dev->mode_config.fb_lock);
3164
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003165 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003166
3167 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003168 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003169 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003170 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003171}
3172
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003173/**
3174 * drm_property_create - create a new property type
3175 * @dev: drm device
3176 * @flags: flags specifying the property type
3177 * @name: name of the property
3178 * @num_values: number of pre-defined values
3179 *
3180 * This creates a new generic drm property which can then be attached to a drm
3181 * object with drm_object_attach_property. The returned property object must be
3182 * freed with drm_property_destroy.
3183 *
3184 * Returns:
3185 * A pointer to the newly created property on success, NULL on failure.
3186 */
Dave Airlief453ba02008-11-07 14:05:41 -08003187struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3188 const char *name, int num_values)
3189{
3190 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003191 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003192
3193 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3194 if (!property)
3195 return NULL;
3196
Rob Clark98f75de2014-05-30 11:37:03 -04003197 property->dev = dev;
3198
Dave Airlief453ba02008-11-07 14:05:41 -08003199 if (num_values) {
3200 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3201 if (!property->values)
3202 goto fail;
3203 }
3204
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003205 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3206 if (ret)
3207 goto fail;
3208
Dave Airlief453ba02008-11-07 14:05:41 -08003209 property->flags = flags;
3210 property->num_values = num_values;
3211 INIT_LIST_HEAD(&property->enum_blob_list);
3212
Vinson Lee471dd2e2011-11-10 11:55:40 -08003213 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003214 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003215 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3216 }
Dave Airlief453ba02008-11-07 14:05:41 -08003217
3218 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003219
3220 WARN_ON(!drm_property_type_valid(property));
3221
Dave Airlief453ba02008-11-07 14:05:41 -08003222 return property;
3223fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003224 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003225 kfree(property);
3226 return NULL;
3227}
3228EXPORT_SYMBOL(drm_property_create);
3229
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003230/**
3231 * drm_property_create - create a new enumeration property type
3232 * @dev: drm device
3233 * @flags: flags specifying the property type
3234 * @name: name of the property
3235 * @props: enumeration lists with property values
3236 * @num_values: number of pre-defined values
3237 *
3238 * This creates a new generic drm property which can then be attached to a drm
3239 * object with drm_object_attach_property. The returned property object must be
3240 * freed with drm_property_destroy.
3241 *
3242 * Userspace is only allowed to set one of the predefined values for enumeration
3243 * properties.
3244 *
3245 * Returns:
3246 * A pointer to the newly created property on success, NULL on failure.
3247 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003248struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3249 const char *name,
3250 const struct drm_prop_enum_list *props,
3251 int num_values)
3252{
3253 struct drm_property *property;
3254 int i, ret;
3255
3256 flags |= DRM_MODE_PROP_ENUM;
3257
3258 property = drm_property_create(dev, flags, name, num_values);
3259 if (!property)
3260 return NULL;
3261
3262 for (i = 0; i < num_values; i++) {
3263 ret = drm_property_add_enum(property, i,
3264 props[i].type,
3265 props[i].name);
3266 if (ret) {
3267 drm_property_destroy(dev, property);
3268 return NULL;
3269 }
3270 }
3271
3272 return property;
3273}
3274EXPORT_SYMBOL(drm_property_create_enum);
3275
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003276/**
3277 * drm_property_create - create a new bitmask property type
3278 * @dev: drm device
3279 * @flags: flags specifying the property type
3280 * @name: name of the property
3281 * @props: enumeration lists with property bitflags
3282 * @num_values: number of pre-defined values
3283 *
3284 * This creates a new generic drm property which can then be attached to a drm
3285 * object with drm_object_attach_property. The returned property object must be
3286 * freed with drm_property_destroy.
3287 *
3288 * Compared to plain enumeration properties userspace is allowed to set any
3289 * or'ed together combination of the predefined property bitflag values
3290 *
3291 * Returns:
3292 * A pointer to the newly created property on success, NULL on failure.
3293 */
Rob Clark49e27542012-05-17 02:23:26 -06003294struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3295 int flags, const char *name,
3296 const struct drm_prop_enum_list *props,
3297 int num_values)
3298{
3299 struct drm_property *property;
3300 int i, ret;
3301
3302 flags |= DRM_MODE_PROP_BITMASK;
3303
3304 property = drm_property_create(dev, flags, name, num_values);
3305 if (!property)
3306 return NULL;
3307
3308 for (i = 0; i < num_values; i++) {
3309 ret = drm_property_add_enum(property, i,
3310 props[i].type,
3311 props[i].name);
3312 if (ret) {
3313 drm_property_destroy(dev, property);
3314 return NULL;
3315 }
3316 }
3317
3318 return property;
3319}
3320EXPORT_SYMBOL(drm_property_create_bitmask);
3321
Rob Clarkebc44cf2012-09-12 22:22:31 -05003322static struct drm_property *property_create_range(struct drm_device *dev,
3323 int flags, const char *name,
3324 uint64_t min, uint64_t max)
3325{
3326 struct drm_property *property;
3327
3328 property = drm_property_create(dev, flags, name, 2);
3329 if (!property)
3330 return NULL;
3331
3332 property->values[0] = min;
3333 property->values[1] = max;
3334
3335 return property;
3336}
3337
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003338/**
3339 * drm_property_create - create a new ranged property type
3340 * @dev: drm device
3341 * @flags: flags specifying the property type
3342 * @name: name of the property
3343 * @min: minimum value of the property
3344 * @max: maximum value of the property
3345 *
3346 * This creates a new generic drm property which can then be attached to a drm
3347 * object with drm_object_attach_property. The returned property object must be
3348 * freed with drm_property_destroy.
3349 *
3350 * Userspace is allowed to set any interger value in the (min, max) range
3351 * inclusive.
3352 *
3353 * Returns:
3354 * A pointer to the newly created property on success, NULL on failure.
3355 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003356struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3357 const char *name,
3358 uint64_t min, uint64_t max)
3359{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003360 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3361 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003362}
3363EXPORT_SYMBOL(drm_property_create_range);
3364
Rob Clarkebc44cf2012-09-12 22:22:31 -05003365struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3366 int flags, const char *name,
3367 int64_t min, int64_t max)
3368{
3369 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3370 name, I642U64(min), I642U64(max));
3371}
3372EXPORT_SYMBOL(drm_property_create_signed_range);
3373
Rob Clark98f75de2014-05-30 11:37:03 -04003374struct drm_property *drm_property_create_object(struct drm_device *dev,
3375 int flags, const char *name, uint32_t type)
3376{
3377 struct drm_property *property;
3378
3379 flags |= DRM_MODE_PROP_OBJECT;
3380
3381 property = drm_property_create(dev, flags, name, 1);
3382 if (!property)
3383 return NULL;
3384
3385 property->values[0] = type;
3386
3387 return property;
3388}
3389EXPORT_SYMBOL(drm_property_create_object);
3390
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003391/**
3392 * drm_property_add_enum - add a possible value to an enumeration property
3393 * @property: enumeration property to change
3394 * @index: index of the new enumeration
3395 * @value: value of the new enumeration
3396 * @name: symbolic name of the new enumeration
3397 *
3398 * This functions adds enumerations to a property.
3399 *
3400 * It's use is deprecated, drivers should use one of the more specific helpers
3401 * to directly create the property with all enumerations already attached.
3402 *
3403 * Returns:
3404 * Zero on success, error code on failure.
3405 */
Dave Airlief453ba02008-11-07 14:05:41 -08003406int drm_property_add_enum(struct drm_property *property, int index,
3407 uint64_t value, const char *name)
3408{
3409 struct drm_property_enum *prop_enum;
3410
Rob Clark5ea22f22014-05-30 11:34:01 -04003411 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3412 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003413 return -EINVAL;
3414
3415 /*
3416 * Bitmask enum properties have the additional constraint of values
3417 * from 0 to 63
3418 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003419 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3420 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003421 return -EINVAL;
3422
3423 if (!list_empty(&property->enum_blob_list)) {
3424 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3425 if (prop_enum->value == value) {
3426 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3427 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3428 return 0;
3429 }
3430 }
3431 }
3432
3433 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3434 if (!prop_enum)
3435 return -ENOMEM;
3436
3437 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3438 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3439 prop_enum->value = value;
3440
3441 property->values[index] = value;
3442 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3443 return 0;
3444}
3445EXPORT_SYMBOL(drm_property_add_enum);
3446
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003447/**
3448 * drm_property_destroy - destroy a drm property
3449 * @dev: drm device
3450 * @property: property to destry
3451 *
3452 * This function frees a property including any attached resources like
3453 * enumeration values.
3454 */
Dave Airlief453ba02008-11-07 14:05:41 -08003455void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3456{
3457 struct drm_property_enum *prop_enum, *pt;
3458
3459 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3460 list_del(&prop_enum->head);
3461 kfree(prop_enum);
3462 }
3463
3464 if (property->num_values)
3465 kfree(property->values);
3466 drm_mode_object_put(dev, &property->base);
3467 list_del(&property->head);
3468 kfree(property);
3469}
3470EXPORT_SYMBOL(drm_property_destroy);
3471
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003472/**
3473 * drm_object_attach_property - attach a property to a modeset object
3474 * @obj: drm modeset object
3475 * @property: property to attach
3476 * @init_val: initial value of the property
3477 *
3478 * This attaches the given property to the modeset object with the given initial
3479 * value. Currently this function cannot fail since the properties are stored in
3480 * a statically sized array.
3481 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003482void drm_object_attach_property(struct drm_mode_object *obj,
3483 struct drm_property *property,
3484 uint64_t init_val)
3485{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003486 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003487
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003488 if (count == DRM_OBJECT_MAX_PROPERTY) {
3489 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3490 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3491 "you see this message on the same object type.\n",
3492 obj->type);
3493 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003494 }
3495
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003496 obj->properties->ids[count] = property->base.id;
3497 obj->properties->values[count] = init_val;
3498 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003499}
3500EXPORT_SYMBOL(drm_object_attach_property);
3501
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003502/**
3503 * drm_object_property_set_value - set the value of a property
3504 * @obj: drm mode object to set property value for
3505 * @property: property to set
3506 * @val: value the property should be set to
3507 *
3508 * This functions sets a given property on a given object. This function only
3509 * changes the software state of the property, it does not call into the
3510 * driver's ->set_property callback.
3511 *
3512 * Returns:
3513 * Zero on success, error code on failure.
3514 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003515int drm_object_property_set_value(struct drm_mode_object *obj,
3516 struct drm_property *property, uint64_t val)
3517{
3518 int i;
3519
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003520 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003521 if (obj->properties->ids[i] == property->base.id) {
3522 obj->properties->values[i] = val;
3523 return 0;
3524 }
3525 }
3526
3527 return -EINVAL;
3528}
3529EXPORT_SYMBOL(drm_object_property_set_value);
3530
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003531/**
3532 * drm_object_property_get_value - retrieve the value of a property
3533 * @obj: drm mode object to get property value from
3534 * @property: property to retrieve
3535 * @val: storage for the property value
3536 *
3537 * This function retrieves the softare state of the given property for the given
3538 * property. Since there is no driver callback to retrieve the current property
3539 * value this might be out of sync with the hardware, depending upon the driver
3540 * and property.
3541 *
3542 * Returns:
3543 * Zero on success, error code on failure.
3544 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003545int drm_object_property_get_value(struct drm_mode_object *obj,
3546 struct drm_property *property, uint64_t *val)
3547{
3548 int i;
3549
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003550 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003551 if (obj->properties->ids[i] == property->base.id) {
3552 *val = obj->properties->values[i];
3553 return 0;
3554 }
3555 }
3556
3557 return -EINVAL;
3558}
3559EXPORT_SYMBOL(drm_object_property_get_value);
3560
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003561/**
3562 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3563 * @dev: DRM device
3564 * @data: ioctl data
3565 * @file_priv: DRM file info
3566 *
3567 * This function retrieves the current value for an connectors's property.
3568 *
3569 * Called by the user via ioctl.
3570 *
3571 * Returns:
3572 * Zero on success, errno on failure.
3573 */
Dave Airlief453ba02008-11-07 14:05:41 -08003574int drm_mode_getproperty_ioctl(struct drm_device *dev,
3575 void *data, struct drm_file *file_priv)
3576{
Dave Airlief453ba02008-11-07 14:05:41 -08003577 struct drm_mode_get_property *out_resp = data;
3578 struct drm_property *property;
3579 int enum_count = 0;
3580 int blob_count = 0;
3581 int value_count = 0;
3582 int ret = 0, i;
3583 int copied;
3584 struct drm_property_enum *prop_enum;
3585 struct drm_mode_property_enum __user *enum_ptr;
3586 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003587 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003588 uint64_t __user *values_ptr;
3589 uint32_t __user *blob_length_ptr;
3590
Dave Airliefb3b06c2011-02-08 13:55:21 +10003591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3592 return -EINVAL;
3593
Daniel Vetter84849902012-12-02 00:28:11 +01003594 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003595 property = drm_property_find(dev, out_resp->prop_id);
3596 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003597 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003598 goto done;
3599 }
Dave Airlief453ba02008-11-07 14:05:41 -08003600
Rob Clark5ea22f22014-05-30 11:34:01 -04003601 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3602 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003603 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3604 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003605 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003606 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3607 blob_count++;
3608 }
3609
3610 value_count = property->num_values;
3611
3612 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3613 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3614 out_resp->flags = property->flags;
3615
3616 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003617 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003618 for (i = 0; i < value_count; i++) {
3619 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3620 ret = -EFAULT;
3621 goto done;
3622 }
3623 }
3624 }
3625 out_resp->count_values = value_count;
3626
Rob Clark5ea22f22014-05-30 11:34:01 -04003627 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3628 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003629 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3630 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003631 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003632 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3633
3634 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3635 ret = -EFAULT;
3636 goto done;
3637 }
3638
3639 if (copy_to_user(&enum_ptr[copied].name,
3640 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3641 ret = -EFAULT;
3642 goto done;
3643 }
3644 copied++;
3645 }
3646 }
3647 out_resp->count_enum_blobs = enum_count;
3648 }
3649
Rob Clark5ea22f22014-05-30 11:34:01 -04003650 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003651 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3652 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003653 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3654 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003655
3656 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3657 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3658 ret = -EFAULT;
3659 goto done;
3660 }
3661
3662 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3663 ret = -EFAULT;
3664 goto done;
3665 }
3666
3667 copied++;
3668 }
3669 }
3670 out_resp->count_enum_blobs = blob_count;
3671 }
3672done:
Daniel Vetter84849902012-12-02 00:28:11 +01003673 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003674 return ret;
3675}
3676
3677static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3678 void *data)
3679{
3680 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003681 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003682
3683 if (!length || !data)
3684 return NULL;
3685
3686 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3687 if (!blob)
3688 return NULL;
3689
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003690 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3691 if (ret) {
3692 kfree(blob);
3693 return NULL;
3694 }
3695
Dave Airlief453ba02008-11-07 14:05:41 -08003696 blob->length = length;
3697
3698 memcpy(blob->data, data, length);
3699
Dave Airlief453ba02008-11-07 14:05:41 -08003700 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3701 return blob;
3702}
3703
3704static void drm_property_destroy_blob(struct drm_device *dev,
3705 struct drm_property_blob *blob)
3706{
3707 drm_mode_object_put(dev, &blob->base);
3708 list_del(&blob->head);
3709 kfree(blob);
3710}
3711
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003712/**
3713 * drm_mode_getblob_ioctl - get the contents of a blob property value
3714 * @dev: DRM device
3715 * @data: ioctl data
3716 * @file_priv: DRM file info
3717 *
3718 * This function retrieves the contents of a blob property. The value stored in
3719 * an object's blob property is just a normal modeset object id.
3720 *
3721 * Called by the user via ioctl.
3722 *
3723 * Returns:
3724 * Zero on success, errno on failure.
3725 */
Dave Airlief453ba02008-11-07 14:05:41 -08003726int drm_mode_getblob_ioctl(struct drm_device *dev,
3727 void *data, struct drm_file *file_priv)
3728{
Dave Airlief453ba02008-11-07 14:05:41 -08003729 struct drm_mode_get_blob *out_resp = data;
3730 struct drm_property_blob *blob;
3731 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003732 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003733
Dave Airliefb3b06c2011-02-08 13:55:21 +10003734 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3735 return -EINVAL;
3736
Daniel Vetter84849902012-12-02 00:28:11 +01003737 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003738 blob = drm_property_blob_find(dev, out_resp->blob_id);
3739 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003740 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003741 goto done;
3742 }
Dave Airlief453ba02008-11-07 14:05:41 -08003743
3744 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003745 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003746 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3747 ret = -EFAULT;
3748 goto done;
3749 }
3750 }
3751 out_resp->length = blob->length;
3752
3753done:
Daniel Vetter84849902012-12-02 00:28:11 +01003754 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003755 return ret;
3756}
3757
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003758/**
3759 * drm_mode_connector_update_edid_property - update the edid property of a connector
3760 * @connector: drm connector
3761 * @edid: new value of the edid property
3762 *
3763 * This function creates a new blob modeset object and assigns its id to the
3764 * connector's edid property.
3765 *
3766 * Returns:
3767 * Zero on success, errno on failure.
3768 */
Dave Airlief453ba02008-11-07 14:05:41 -08003769int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3770 struct edid *edid)
3771{
3772 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003773 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003774
Thomas Wood4cf2b282014-06-18 17:52:33 +01003775 /* ignore requests to set edid when overridden */
3776 if (connector->override_edid)
3777 return 0;
3778
Dave Airlief453ba02008-11-07 14:05:41 -08003779 if (connector->edid_blob_ptr)
3780 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3781
3782 /* Delete edid, when there is none. */
3783 if (!edid) {
3784 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003785 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003786 return ret;
3787 }
3788
Adam Jackson7466f4c2010-03-29 21:43:23 +00003789 size = EDID_LENGTH * (1 + edid->extensions);
3790 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3791 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003792 if (!connector->edid_blob_ptr)
3793 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003794
Rob Clark58495562012-10-11 20:50:56 -05003795 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003796 dev->mode_config.edid_property,
3797 connector->edid_blob_ptr->base.id);
3798
3799 return ret;
3800}
3801EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3802
Paulo Zanoni26a34812012-05-15 18:08:59 -03003803static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003804 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003805{
3806 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3807 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003808
3809 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003810 if (value < property->values[0] || value > property->values[1])
3811 return false;
3812 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003813 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3814 int64_t svalue = U642I64(value);
3815 if (svalue < U642I64(property->values[0]) ||
3816 svalue > U642I64(property->values[1]))
3817 return false;
3818 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003819 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003820 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003821 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003822 for (i = 0; i < property->num_values; i++)
3823 valid_mask |= (1ULL << property->values[i]);
3824 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003825 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003826 /* Only the driver knows */
3827 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003828 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3829 struct drm_mode_object *obj;
3830 /* a zero value for an object property translates to null: */
3831 if (value == 0)
3832 return true;
3833 /*
3834 * NOTE: use _object_find() directly to bypass restriction on
3835 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3836 * object this could race against object finalization, so it
3837 * simply tells us that the object *was* valid. Which is good
3838 * enough.
3839 */
3840 obj = _object_find(property->dev, value, property->values[0]);
3841 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003842 } else {
3843 int i;
3844 for (i = 0; i < property->num_values; i++)
3845 if (property->values[i] == value)
3846 return true;
3847 return false;
3848 }
3849}
3850
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003851/**
3852 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3853 * @dev: DRM device
3854 * @data: ioctl data
3855 * @file_priv: DRM file info
3856 *
3857 * This function sets the current value for a connectors's property. It also
3858 * calls into a driver's ->set_property callback to update the hardware state
3859 *
3860 * Called by the user via ioctl.
3861 *
3862 * Returns:
3863 * Zero on success, errno on failure.
3864 */
Dave Airlief453ba02008-11-07 14:05:41 -08003865int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3866 void *data, struct drm_file *file_priv)
3867{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003868 struct drm_mode_connector_set_property *conn_set_prop = data;
3869 struct drm_mode_obj_set_property obj_set_prop = {
3870 .value = conn_set_prop->value,
3871 .prop_id = conn_set_prop->prop_id,
3872 .obj_id = conn_set_prop->connector_id,
3873 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3874 };
Dave Airlief453ba02008-11-07 14:05:41 -08003875
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003876 /* It does all the locking and checking we need */
3877 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003878}
3879
Paulo Zanonic5431882012-05-15 18:09:02 -03003880static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3881 struct drm_property *property,
3882 uint64_t value)
3883{
3884 int ret = -EINVAL;
3885 struct drm_connector *connector = obj_to_connector(obj);
3886
3887 /* Do DPMS ourselves */
3888 if (property == connector->dev->mode_config.dpms_property) {
3889 if (connector->funcs->dpms)
3890 (*connector->funcs->dpms)(connector, (int)value);
3891 ret = 0;
3892 } else if (connector->funcs->set_property)
3893 ret = connector->funcs->set_property(connector, property, value);
3894
3895 /* store the property value if successful */
3896 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003897 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003898 return ret;
3899}
3900
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003901static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3902 struct drm_property *property,
3903 uint64_t value)
3904{
3905 int ret = -EINVAL;
3906 struct drm_crtc *crtc = obj_to_crtc(obj);
3907
3908 if (crtc->funcs->set_property)
3909 ret = crtc->funcs->set_property(crtc, property, value);
3910 if (!ret)
3911 drm_object_property_set_value(obj, property, value);
3912
3913 return ret;
3914}
3915
Rob Clark4d939142012-05-17 02:23:27 -06003916static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3917 struct drm_property *property,
3918 uint64_t value)
3919{
3920 int ret = -EINVAL;
3921 struct drm_plane *plane = obj_to_plane(obj);
3922
3923 if (plane->funcs->set_property)
3924 ret = plane->funcs->set_property(plane, property, value);
3925 if (!ret)
3926 drm_object_property_set_value(obj, property, value);
3927
3928 return ret;
3929}
3930
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003931/**
3932 * drm_mode_getproperty_ioctl - get the current value of a object's property
3933 * @dev: DRM device
3934 * @data: ioctl data
3935 * @file_priv: DRM file info
3936 *
3937 * This function retrieves the current value for an object's property. Compared
3938 * to the connector specific ioctl this one is extended to also work on crtc and
3939 * plane objects.
3940 *
3941 * Called by the user via ioctl.
3942 *
3943 * Returns:
3944 * Zero on success, errno on failure.
3945 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003946int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3947 struct drm_file *file_priv)
3948{
3949 struct drm_mode_obj_get_properties *arg = data;
3950 struct drm_mode_object *obj;
3951 int ret = 0;
3952 int i;
3953 int copied = 0;
3954 int props_count = 0;
3955 uint32_t __user *props_ptr;
3956 uint64_t __user *prop_values_ptr;
3957
3958 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3959 return -EINVAL;
3960
Daniel Vetter84849902012-12-02 00:28:11 +01003961 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003962
3963 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3964 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003965 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003966 goto out;
3967 }
3968 if (!obj->properties) {
3969 ret = -EINVAL;
3970 goto out;
3971 }
3972
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003973 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003974
3975 /* This ioctl is called twice, once to determine how much space is
3976 * needed, and the 2nd time to fill it. */
3977 if ((arg->count_props >= props_count) && props_count) {
3978 copied = 0;
3979 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3980 prop_values_ptr = (uint64_t __user *)(unsigned long)
3981 (arg->prop_values_ptr);
3982 for (i = 0; i < props_count; i++) {
3983 if (put_user(obj->properties->ids[i],
3984 props_ptr + copied)) {
3985 ret = -EFAULT;
3986 goto out;
3987 }
3988 if (put_user(obj->properties->values[i],
3989 prop_values_ptr + copied)) {
3990 ret = -EFAULT;
3991 goto out;
3992 }
3993 copied++;
3994 }
3995 }
3996 arg->count_props = props_count;
3997out:
Daniel Vetter84849902012-12-02 00:28:11 +01003998 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003999 return ret;
4000}
4001
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004002/**
4003 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4004 * @dev: DRM device
4005 * @data: ioctl data
4006 * @file_priv: DRM file info
4007 *
4008 * This function sets the current value for an object's property. It also calls
4009 * into a driver's ->set_property callback to update the hardware state.
4010 * Compared to the connector specific ioctl this one is extended to also work on
4011 * crtc and plane objects.
4012 *
4013 * Called by the user via ioctl.
4014 *
4015 * Returns:
4016 * Zero on success, errno on failure.
4017 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004018int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4019 struct drm_file *file_priv)
4020{
4021 struct drm_mode_obj_set_property *arg = data;
4022 struct drm_mode_object *arg_obj;
4023 struct drm_mode_object *prop_obj;
4024 struct drm_property *property;
4025 int ret = -EINVAL;
4026 int i;
4027
4028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4029 return -EINVAL;
4030
Daniel Vetter84849902012-12-02 00:28:11 +01004031 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004032
4033 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004034 if (!arg_obj) {
4035 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004036 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004037 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004038 if (!arg_obj->properties)
4039 goto out;
4040
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004041 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004042 if (arg_obj->properties->ids[i] == arg->prop_id)
4043 break;
4044
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004045 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004046 goto out;
4047
4048 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4049 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004050 if (!prop_obj) {
4051 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004052 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004053 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004054 property = obj_to_property(prop_obj);
4055
4056 if (!drm_property_change_is_valid(property, arg->value))
4057 goto out;
4058
4059 switch (arg_obj->type) {
4060 case DRM_MODE_OBJECT_CONNECTOR:
4061 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4062 arg->value);
4063 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004064 case DRM_MODE_OBJECT_CRTC:
4065 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4066 break;
Rob Clark4d939142012-05-17 02:23:27 -06004067 case DRM_MODE_OBJECT_PLANE:
4068 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4069 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004070 }
4071
4072out:
Daniel Vetter84849902012-12-02 00:28:11 +01004073 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004074 return ret;
4075}
4076
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004077/**
4078 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4079 * @connector: connector to attach
4080 * @encoder: encoder to attach @connector to
4081 *
4082 * This function links up a connector to an encoder. Note that the routing
4083 * restrictions between encoders and crtcs are exposed to userspace through the
4084 * possible_clones and possible_crtcs bitmasks.
4085 *
4086 * Returns:
4087 * Zero on success, errno on failure.
4088 */
Dave Airlief453ba02008-11-07 14:05:41 -08004089int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4090 struct drm_encoder *encoder)
4091{
4092 int i;
4093
4094 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4095 if (connector->encoder_ids[i] == 0) {
4096 connector->encoder_ids[i] = encoder->base.id;
4097 return 0;
4098 }
4099 }
4100 return -ENOMEM;
4101}
4102EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4103
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004104/**
4105 * drm_mode_crtc_set_gamma_size - set the gamma table size
4106 * @crtc: CRTC to set the gamma table size for
4107 * @gamma_size: size of the gamma table
4108 *
4109 * Drivers which support gamma tables should set this to the supported gamma
4110 * table size when initializing the CRTC. Currently the drm core only supports a
4111 * fixed gamma table size.
4112 *
4113 * Returns:
4114 * Zero on success, errno on failure.
4115 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004116int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004117 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004118{
4119 crtc->gamma_size = gamma_size;
4120
4121 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4122 if (!crtc->gamma_store) {
4123 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004124 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004125 }
4126
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004127 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004128}
4129EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4130
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004131/**
4132 * drm_mode_gamma_set_ioctl - set the gamma table
4133 * @dev: DRM device
4134 * @data: ioctl data
4135 * @file_priv: DRM file info
4136 *
4137 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4138 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4139 *
4140 * Called by the user via ioctl.
4141 *
4142 * Returns:
4143 * Zero on success, errno on failure.
4144 */
Dave Airlief453ba02008-11-07 14:05:41 -08004145int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4146 void *data, struct drm_file *file_priv)
4147{
4148 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004149 struct drm_crtc *crtc;
4150 void *r_base, *g_base, *b_base;
4151 int size;
4152 int ret = 0;
4153
Dave Airliefb3b06c2011-02-08 13:55:21 +10004154 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4155 return -EINVAL;
4156
Daniel Vetter84849902012-12-02 00:28:11 +01004157 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004158 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4159 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004160 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004161 goto out;
4162 }
Dave Airlief453ba02008-11-07 14:05:41 -08004163
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004164 if (crtc->funcs->gamma_set == NULL) {
4165 ret = -ENOSYS;
4166 goto out;
4167 }
4168
Dave Airlief453ba02008-11-07 14:05:41 -08004169 /* memcpy into gamma store */
4170 if (crtc_lut->gamma_size != crtc->gamma_size) {
4171 ret = -EINVAL;
4172 goto out;
4173 }
4174
4175 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4176 r_base = crtc->gamma_store;
4177 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4178 ret = -EFAULT;
4179 goto out;
4180 }
4181
4182 g_base = r_base + size;
4183 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4184 ret = -EFAULT;
4185 goto out;
4186 }
4187
4188 b_base = g_base + size;
4189 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4190 ret = -EFAULT;
4191 goto out;
4192 }
4193
James Simmons72034252010-08-03 01:33:19 +01004194 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004195
4196out:
Daniel Vetter84849902012-12-02 00:28:11 +01004197 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004198 return ret;
4199
4200}
4201
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004202/**
4203 * drm_mode_gamma_get_ioctl - get the gamma table
4204 * @dev: DRM device
4205 * @data: ioctl data
4206 * @file_priv: DRM file info
4207 *
4208 * Copy the current gamma table into the storage provided. This also provides
4209 * the gamma table size the driver expects, which can be used to size the
4210 * allocated storage.
4211 *
4212 * Called by the user via ioctl.
4213 *
4214 * Returns:
4215 * Zero on success, errno on failure.
4216 */
Dave Airlief453ba02008-11-07 14:05:41 -08004217int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4218 void *data, struct drm_file *file_priv)
4219{
4220 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004221 struct drm_crtc *crtc;
4222 void *r_base, *g_base, *b_base;
4223 int size;
4224 int ret = 0;
4225
Dave Airliefb3b06c2011-02-08 13:55:21 +10004226 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4227 return -EINVAL;
4228
Daniel Vetter84849902012-12-02 00:28:11 +01004229 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004230 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4231 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004232 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004233 goto out;
4234 }
Dave Airlief453ba02008-11-07 14:05:41 -08004235
4236 /* memcpy into gamma store */
4237 if (crtc_lut->gamma_size != crtc->gamma_size) {
4238 ret = -EINVAL;
4239 goto out;
4240 }
4241
4242 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4243 r_base = crtc->gamma_store;
4244 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4245 ret = -EFAULT;
4246 goto out;
4247 }
4248
4249 g_base = r_base + size;
4250 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4251 ret = -EFAULT;
4252 goto out;
4253 }
4254
4255 b_base = g_base + size;
4256 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4257 ret = -EFAULT;
4258 goto out;
4259 }
4260out:
Daniel Vetter84849902012-12-02 00:28:11 +01004261 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004262 return ret;
4263}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004264
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004265/**
4266 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4267 * @dev: DRM device
4268 * @data: ioctl data
4269 * @file_priv: DRM file info
4270 *
4271 * This schedules an asynchronous update on a given CRTC, called page flip.
4272 * Optionally a drm event is generated to signal the completion of the event.
4273 * Generic drivers cannot assume that a pageflip with changed framebuffer
4274 * properties (including driver specific metadata like tiling layout) will work,
4275 * but some drivers support e.g. pixel format changes through the pageflip
4276 * ioctl.
4277 *
4278 * Called by the user via ioctl.
4279 *
4280 * Returns:
4281 * Zero on success, errno on failure.
4282 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004283int drm_mode_page_flip_ioctl(struct drm_device *dev,
4284 void *data, struct drm_file *file_priv)
4285{
4286 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004287 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004288 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004289 struct drm_pending_vblank_event *e = NULL;
4290 unsigned long flags;
4291 int ret = -EINVAL;
4292
4293 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4294 page_flip->reserved != 0)
4295 return -EINVAL;
4296
Keith Packard62f21042013-07-22 18:50:00 -07004297 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4298 return -EINVAL;
4299
Rob Clarka2b34e22013-10-05 16:36:52 -04004300 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4301 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004302 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004303
Rob Clark51fd3712013-11-19 12:10:12 -05004304 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004305 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004306 /* The framebuffer is currently unbound, presumably
4307 * due to a hotplug event, that userspace has not
4308 * yet discovered.
4309 */
4310 ret = -EBUSY;
4311 goto out;
4312 }
4313
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004314 if (crtc->funcs->page_flip == NULL)
4315 goto out;
4316
Daniel Vetter786b99e2012-12-02 21:53:40 +01004317 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004318 if (!fb) {
4319 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004320 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004321 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004322
Damien Lespiauc11e9282013-09-25 16:45:30 +01004323 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4324 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004325 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004326
Matt Roperf4510a22014-04-01 15:22:40 -07004327 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004328 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4329 ret = -EINVAL;
4330 goto out;
4331 }
4332
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004333 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4334 ret = -ENOMEM;
4335 spin_lock_irqsave(&dev->event_lock, flags);
4336 if (file_priv->event_space < sizeof e->event) {
4337 spin_unlock_irqrestore(&dev->event_lock, flags);
4338 goto out;
4339 }
4340 file_priv->event_space -= sizeof e->event;
4341 spin_unlock_irqrestore(&dev->event_lock, flags);
4342
4343 e = kzalloc(sizeof *e, GFP_KERNEL);
4344 if (e == NULL) {
4345 spin_lock_irqsave(&dev->event_lock, flags);
4346 file_priv->event_space += sizeof e->event;
4347 spin_unlock_irqrestore(&dev->event_lock, flags);
4348 goto out;
4349 }
4350
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004351 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004352 e->event.base.length = sizeof e->event;
4353 e->event.user_data = page_flip->user_data;
4354 e->base.event = &e->event.base;
4355 e->base.file_priv = file_priv;
4356 e->base.destroy =
4357 (void (*) (struct drm_pending_event *)) kfree;
4358 }
4359
Matt Roperf4510a22014-04-01 15:22:40 -07004360 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004361 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004362 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004363 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4364 spin_lock_irqsave(&dev->event_lock, flags);
4365 file_priv->event_space += sizeof e->event;
4366 spin_unlock_irqrestore(&dev->event_lock, flags);
4367 kfree(e);
4368 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004369 /* Keep the old fb, don't unref it. */
4370 old_fb = NULL;
4371 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004372 /*
4373 * Warn if the driver hasn't properly updated the crtc->fb
4374 * field to reflect that the new framebuffer is now used.
4375 * Failing to do so will screw with the reference counting
4376 * on framebuffers.
4377 */
Matt Roperf4510a22014-04-01 15:22:40 -07004378 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004379 /* Unref only the old framebuffer. */
4380 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004381 }
4382
4383out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004384 if (fb)
4385 drm_framebuffer_unreference(fb);
4386 if (old_fb)
4387 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004388 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004389
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004390 return ret;
4391}
Chris Wilsoneb033552011-01-24 15:11:08 +00004392
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004393/**
4394 * drm_mode_config_reset - call ->reset callbacks
4395 * @dev: drm device
4396 *
4397 * This functions calls all the crtc's, encoder's and connector's ->reset
4398 * callback. Drivers can use this in e.g. their driver load or resume code to
4399 * reset hardware and software state.
4400 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004401void drm_mode_config_reset(struct drm_device *dev)
4402{
4403 struct drm_crtc *crtc;
4404 struct drm_encoder *encoder;
4405 struct drm_connector *connector;
4406
4407 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4408 if (crtc->funcs->reset)
4409 crtc->funcs->reset(crtc);
4410
4411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4412 if (encoder->funcs->reset)
4413 encoder->funcs->reset(encoder);
4414
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004415 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4416 connector->status = connector_status_unknown;
4417
Chris Wilsoneb033552011-01-24 15:11:08 +00004418 if (connector->funcs->reset)
4419 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004420 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004421}
4422EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004423
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004424/**
4425 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4426 * @dev: DRM device
4427 * @data: ioctl data
4428 * @file_priv: DRM file info
4429 *
4430 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4431 * TTM or something else entirely) and returns the resulting buffer handle. This
4432 * handle can then be wrapped up into a framebuffer modeset object.
4433 *
4434 * Note that userspace is not allowed to use such objects for render
4435 * acceleration - drivers must create their own private ioctls for such a use
4436 * case.
4437 *
4438 * Called by the user via ioctl.
4439 *
4440 * Returns:
4441 * Zero on success, errno on failure.
4442 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004443int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4444 void *data, struct drm_file *file_priv)
4445{
4446 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004447 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004448
4449 if (!dev->driver->dumb_create)
4450 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004451 if (!args->width || !args->height || !args->bpp)
4452 return -EINVAL;
4453
4454 /* overflow checks for 32bit size calculations */
4455 cpp = DIV_ROUND_UP(args->bpp, 8);
4456 if (cpp > 0xffffffffU / args->width)
4457 return -EINVAL;
4458 stride = cpp * args->width;
4459 if (args->height > 0xffffffffU / stride)
4460 return -EINVAL;
4461
4462 /* test for wrap-around */
4463 size = args->height * stride;
4464 if (PAGE_ALIGN(size) == 0)
4465 return -EINVAL;
4466
Dave Airlieff72145b2011-02-07 12:16:14 +10004467 return dev->driver->dumb_create(file_priv, dev, args);
4468}
4469
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004470/**
4471 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4472 * @dev: DRM device
4473 * @data: ioctl data
4474 * @file_priv: DRM file info
4475 *
4476 * Allocate an offset in the drm device node's address space to be able to
4477 * memory map a dumb buffer.
4478 *
4479 * Called by the user via ioctl.
4480 *
4481 * Returns:
4482 * Zero on success, errno on failure.
4483 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004484int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4485 void *data, struct drm_file *file_priv)
4486{
4487 struct drm_mode_map_dumb *args = data;
4488
4489 /* call driver ioctl to get mmap offset */
4490 if (!dev->driver->dumb_map_offset)
4491 return -ENOSYS;
4492
4493 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4494}
4495
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004496/**
4497 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4498 * @dev: DRM device
4499 * @data: ioctl data
4500 * @file_priv: DRM file info
4501 *
4502 * This destroys the userspace handle for the given dumb backing storage buffer.
4503 * Since buffer objects must be reference counted in the kernel a buffer object
4504 * won't be immediately freed if a framebuffer modeset object still uses it.
4505 *
4506 * Called by the user via ioctl.
4507 *
4508 * Returns:
4509 * Zero on success, errno on failure.
4510 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004511int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4512 void *data, struct drm_file *file_priv)
4513{
4514 struct drm_mode_destroy_dumb *args = data;
4515
4516 if (!dev->driver->dumb_destroy)
4517 return -ENOSYS;
4518
4519 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4520}
Dave Airlie248dbc22011-11-29 20:02:54 +00004521
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004522/**
4523 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4524 * @format: pixel format (DRM_FORMAT_*)
4525 * @depth: storage for the depth value
4526 * @bpp: storage for the bpp value
4527 *
4528 * This only supports RGB formats here for compat with code that doesn't use
4529 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004530 */
4531void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4532 int *bpp)
4533{
4534 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004535 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004536 case DRM_FORMAT_RGB332:
4537 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004538 *depth = 8;
4539 *bpp = 8;
4540 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004541 case DRM_FORMAT_XRGB1555:
4542 case DRM_FORMAT_XBGR1555:
4543 case DRM_FORMAT_RGBX5551:
4544 case DRM_FORMAT_BGRX5551:
4545 case DRM_FORMAT_ARGB1555:
4546 case DRM_FORMAT_ABGR1555:
4547 case DRM_FORMAT_RGBA5551:
4548 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004549 *depth = 15;
4550 *bpp = 16;
4551 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004552 case DRM_FORMAT_RGB565:
4553 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004554 *depth = 16;
4555 *bpp = 16;
4556 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004557 case DRM_FORMAT_RGB888:
4558 case DRM_FORMAT_BGR888:
4559 *depth = 24;
4560 *bpp = 24;
4561 break;
4562 case DRM_FORMAT_XRGB8888:
4563 case DRM_FORMAT_XBGR8888:
4564 case DRM_FORMAT_RGBX8888:
4565 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004566 *depth = 24;
4567 *bpp = 32;
4568 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004569 case DRM_FORMAT_XRGB2101010:
4570 case DRM_FORMAT_XBGR2101010:
4571 case DRM_FORMAT_RGBX1010102:
4572 case DRM_FORMAT_BGRX1010102:
4573 case DRM_FORMAT_ARGB2101010:
4574 case DRM_FORMAT_ABGR2101010:
4575 case DRM_FORMAT_RGBA1010102:
4576 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004577 *depth = 30;
4578 *bpp = 32;
4579 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004580 case DRM_FORMAT_ARGB8888:
4581 case DRM_FORMAT_ABGR8888:
4582 case DRM_FORMAT_RGBA8888:
4583 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004584 *depth = 32;
4585 *bpp = 32;
4586 break;
4587 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004588 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4589 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004590 *depth = 0;
4591 *bpp = 0;
4592 break;
4593 }
4594}
4595EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004596
4597/**
4598 * drm_format_num_planes - get the number of planes for format
4599 * @format: pixel format (DRM_FORMAT_*)
4600 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004601 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004602 * The number of planes used by the specified pixel format.
4603 */
4604int drm_format_num_planes(uint32_t format)
4605{
4606 switch (format) {
4607 case DRM_FORMAT_YUV410:
4608 case DRM_FORMAT_YVU410:
4609 case DRM_FORMAT_YUV411:
4610 case DRM_FORMAT_YVU411:
4611 case DRM_FORMAT_YUV420:
4612 case DRM_FORMAT_YVU420:
4613 case DRM_FORMAT_YUV422:
4614 case DRM_FORMAT_YVU422:
4615 case DRM_FORMAT_YUV444:
4616 case DRM_FORMAT_YVU444:
4617 return 3;
4618 case DRM_FORMAT_NV12:
4619 case DRM_FORMAT_NV21:
4620 case DRM_FORMAT_NV16:
4621 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004622 case DRM_FORMAT_NV24:
4623 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004624 return 2;
4625 default:
4626 return 1;
4627 }
4628}
4629EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004630
4631/**
4632 * drm_format_plane_cpp - determine the bytes per pixel value
4633 * @format: pixel format (DRM_FORMAT_*)
4634 * @plane: plane index
4635 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004636 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004637 * The bytes per pixel value for the specified plane.
4638 */
4639int drm_format_plane_cpp(uint32_t format, int plane)
4640{
4641 unsigned int depth;
4642 int bpp;
4643
4644 if (plane >= drm_format_num_planes(format))
4645 return 0;
4646
4647 switch (format) {
4648 case DRM_FORMAT_YUYV:
4649 case DRM_FORMAT_YVYU:
4650 case DRM_FORMAT_UYVY:
4651 case DRM_FORMAT_VYUY:
4652 return 2;
4653 case DRM_FORMAT_NV12:
4654 case DRM_FORMAT_NV21:
4655 case DRM_FORMAT_NV16:
4656 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004657 case DRM_FORMAT_NV24:
4658 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004659 return plane ? 2 : 1;
4660 case DRM_FORMAT_YUV410:
4661 case DRM_FORMAT_YVU410:
4662 case DRM_FORMAT_YUV411:
4663 case DRM_FORMAT_YVU411:
4664 case DRM_FORMAT_YUV420:
4665 case DRM_FORMAT_YVU420:
4666 case DRM_FORMAT_YUV422:
4667 case DRM_FORMAT_YVU422:
4668 case DRM_FORMAT_YUV444:
4669 case DRM_FORMAT_YVU444:
4670 return 1;
4671 default:
4672 drm_fb_get_bpp_depth(format, &depth, &bpp);
4673 return bpp >> 3;
4674 }
4675}
4676EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004677
4678/**
4679 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4680 * @format: pixel format (DRM_FORMAT_*)
4681 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004682 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004683 * The horizontal chroma subsampling factor for the
4684 * specified pixel format.
4685 */
4686int drm_format_horz_chroma_subsampling(uint32_t format)
4687{
4688 switch (format) {
4689 case DRM_FORMAT_YUV411:
4690 case DRM_FORMAT_YVU411:
4691 case DRM_FORMAT_YUV410:
4692 case DRM_FORMAT_YVU410:
4693 return 4;
4694 case DRM_FORMAT_YUYV:
4695 case DRM_FORMAT_YVYU:
4696 case DRM_FORMAT_UYVY:
4697 case DRM_FORMAT_VYUY:
4698 case DRM_FORMAT_NV12:
4699 case DRM_FORMAT_NV21:
4700 case DRM_FORMAT_NV16:
4701 case DRM_FORMAT_NV61:
4702 case DRM_FORMAT_YUV422:
4703 case DRM_FORMAT_YVU422:
4704 case DRM_FORMAT_YUV420:
4705 case DRM_FORMAT_YVU420:
4706 return 2;
4707 default:
4708 return 1;
4709 }
4710}
4711EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4712
4713/**
4714 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4715 * @format: pixel format (DRM_FORMAT_*)
4716 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004717 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004718 * The vertical chroma subsampling factor for the
4719 * specified pixel format.
4720 */
4721int drm_format_vert_chroma_subsampling(uint32_t format)
4722{
4723 switch (format) {
4724 case DRM_FORMAT_YUV410:
4725 case DRM_FORMAT_YVU410:
4726 return 4;
4727 case DRM_FORMAT_YUV420:
4728 case DRM_FORMAT_YVU420:
4729 case DRM_FORMAT_NV12:
4730 case DRM_FORMAT_NV21:
4731 return 2;
4732 default:
4733 return 1;
4734 }
4735}
4736EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004737
4738/**
4739 * drm_mode_config_init - initialize DRM mode_configuration structure
4740 * @dev: DRM device
4741 *
4742 * Initialize @dev's mode_config structure, used for tracking the graphics
4743 * configuration of @dev.
4744 *
4745 * Since this initializes the modeset locks, no locking is possible. Which is no
4746 * problem, since this should happen single threaded at init time. It is the
4747 * driver's problem to ensure this guarantee.
4748 *
4749 */
4750void drm_mode_config_init(struct drm_device *dev)
4751{
4752 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004753 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004754 mutex_init(&dev->mode_config.idr_mutex);
4755 mutex_init(&dev->mode_config.fb_lock);
4756 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4757 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4758 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004759 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004760 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4761 INIT_LIST_HEAD(&dev->mode_config.property_list);
4762 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4763 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4764 idr_init(&dev->mode_config.crtc_idr);
4765
4766 drm_modeset_lock_all(dev);
4767 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004768 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004769 drm_modeset_unlock_all(dev);
4770
4771 /* Just to be sure */
4772 dev->mode_config.num_fb = 0;
4773 dev->mode_config.num_connector = 0;
4774 dev->mode_config.num_crtc = 0;
4775 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004776 dev->mode_config.num_overlay_plane = 0;
4777 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004778}
4779EXPORT_SYMBOL(drm_mode_config_init);
4780
4781/**
4782 * drm_mode_config_cleanup - free up DRM mode_config info
4783 * @dev: DRM device
4784 *
4785 * Free up all the connectors and CRTCs associated with this DRM device, then
4786 * free up the framebuffers and associated buffer objects.
4787 *
4788 * Note that since this /should/ happen single-threaded at driver/device
4789 * teardown time, no locking is required. It's the driver's job to ensure that
4790 * this guarantee actually holds true.
4791 *
4792 * FIXME: cleanup any dangling user buffer objects too
4793 */
4794void drm_mode_config_cleanup(struct drm_device *dev)
4795{
4796 struct drm_connector *connector, *ot;
4797 struct drm_crtc *crtc, *ct;
4798 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004799 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004800 struct drm_framebuffer *fb, *fbt;
4801 struct drm_property *property, *pt;
4802 struct drm_property_blob *blob, *bt;
4803 struct drm_plane *plane, *plt;
4804
4805 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4806 head) {
4807 encoder->funcs->destroy(encoder);
4808 }
4809
Sean Paul3b336ec2013-08-14 16:47:37 -04004810 list_for_each_entry_safe(bridge, brt,
4811 &dev->mode_config.bridge_list, head) {
4812 bridge->funcs->destroy(bridge);
4813 }
4814
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004815 list_for_each_entry_safe(connector, ot,
4816 &dev->mode_config.connector_list, head) {
4817 connector->funcs->destroy(connector);
4818 }
4819
4820 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4821 head) {
4822 drm_property_destroy(dev, property);
4823 }
4824
4825 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4826 head) {
4827 drm_property_destroy_blob(dev, blob);
4828 }
4829
4830 /*
4831 * Single-threaded teardown context, so it's not required to grab the
4832 * fb_lock to protect against concurrent fb_list access. Contrary, it
4833 * would actually deadlock with the drm_framebuffer_cleanup function.
4834 *
4835 * Also, if there are any framebuffers left, that's a driver leak now,
4836 * so politely WARN about this.
4837 */
4838 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4839 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4840 drm_framebuffer_remove(fb);
4841 }
4842
4843 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4844 head) {
4845 plane->funcs->destroy(plane);
4846 }
4847
4848 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4849 crtc->funcs->destroy(crtc);
4850 }
4851
4852 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004853 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004854}
4855EXPORT_SYMBOL(drm_mode_config_cleanup);