blob: c50c827cefb6c93eed0217e48e11c747fe17aa3b [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
Jani Nikula2abdd312014-05-14 16:58:19 +0300884out_put:
885 if (ret)
886 drm_mode_object_put(dev, &connector->base);
887
888out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100889 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890
891 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800892}
893EXPORT_SYMBOL(drm_connector_init);
894
895/**
896 * drm_connector_cleanup - cleans up an initialised connector
897 * @connector: connector to cleanup
898 *
Dave Airlief453ba02008-11-07 14:05:41 -0800899 * Cleans up the connector but doesn't free the object.
900 */
901void drm_connector_cleanup(struct drm_connector *connector)
902{
903 struct drm_device *dev = connector->dev;
904 struct drm_display_mode *mode, *t;
905
906 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
907 drm_mode_remove(connector, mode);
908
909 list_for_each_entry_safe(mode, t, &connector->modes, head)
910 drm_mode_remove(connector, mode);
911
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400912 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
913 connector->connector_type_id);
914
Dave Airlief453ba02008-11-07 14:05:41 -0800915 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300916 kfree(connector->name);
917 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800918 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000919 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800920}
921EXPORT_SYMBOL(drm_connector_cleanup);
922
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100923/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100924 * drm_connector_register - register a connector
925 * @connector: the connector to register
926 *
927 * Register userspace interfaces for a connector
928 *
929 * Returns:
930 * Zero on success, error code on failure.
931 */
932int drm_connector_register(struct drm_connector *connector)
933{
934 return drm_sysfs_connector_add(connector);
935}
936EXPORT_SYMBOL(drm_connector_register);
937
938/**
939 * drm_connector_unregister - unregister a connector
940 * @connector: the connector to unregister
941 *
942 * Unregister userspace interfaces for a connector
943 */
944void drm_connector_unregister(struct drm_connector *connector)
945{
946 drm_sysfs_connector_remove(connector);
947}
948EXPORT_SYMBOL(drm_connector_unregister);
949
950
951/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100952 * drm_connector_unplug_all - unregister connector userspace interfaces
953 * @dev: drm device
954 *
955 * This function unregisters all connector userspace interfaces in sysfs. Should
956 * be call when the device is disconnected, e.g. from an usb driver's
957 * ->disconnect callback.
958 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000959void drm_connector_unplug_all(struct drm_device *dev)
960{
961 struct drm_connector *connector;
962
963 /* taking the mode config mutex ends up in a clash with sysfs */
964 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +0100965 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +0000966
967}
968EXPORT_SYMBOL(drm_connector_unplug_all);
969
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100970/**
971 * drm_bridge_init - initialize a drm transcoder/bridge
972 * @dev: drm device
973 * @bridge: transcoder/bridge to set up
974 * @funcs: bridge function table
975 *
976 * Initialises a preallocated bridge. Bridges should be
977 * subclassed as part of driver connector objects.
978 *
979 * Returns:
980 * Zero on success, error code on failure.
981 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400982int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
983 const struct drm_bridge_funcs *funcs)
984{
985 int ret;
986
987 drm_modeset_lock_all(dev);
988
989 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
990 if (ret)
991 goto out;
992
993 bridge->dev = dev;
994 bridge->funcs = funcs;
995
996 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
997 dev->mode_config.num_bridge++;
998
999 out:
1000 drm_modeset_unlock_all(dev);
1001 return ret;
1002}
1003EXPORT_SYMBOL(drm_bridge_init);
1004
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001005/**
1006 * drm_bridge_cleanup - cleans up an initialised bridge
1007 * @bridge: bridge to cleanup
1008 *
1009 * Cleans up the bridge but doesn't free the object.
1010 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001011void drm_bridge_cleanup(struct drm_bridge *bridge)
1012{
1013 struct drm_device *dev = bridge->dev;
1014
1015 drm_modeset_lock_all(dev);
1016 drm_mode_object_put(dev, &bridge->base);
1017 list_del(&bridge->head);
1018 dev->mode_config.num_bridge--;
1019 drm_modeset_unlock_all(dev);
1020}
1021EXPORT_SYMBOL(drm_bridge_cleanup);
1022
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001023/**
1024 * drm_encoder_init - Init a preallocated encoder
1025 * @dev: drm device
1026 * @encoder: the encoder to init
1027 * @funcs: callbacks for this encoder
1028 * @encoder_type: user visible type of the encoder
1029 *
1030 * Initialises a preallocated encoder. Encoder should be
1031 * subclassed as part of driver encoder objects.
1032 *
1033 * Returns:
1034 * Zero on success, error code on failure.
1035 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001036int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001037 struct drm_encoder *encoder,
1038 const struct drm_encoder_funcs *funcs,
1039 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001040{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001041 int ret;
1042
Daniel Vetter84849902012-12-02 00:28:11 +01001043 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001044
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001045 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1046 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001047 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001048
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001049 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001050 encoder->encoder_type = encoder_type;
1051 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001052 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1053 drm_encoder_enum_list[encoder_type].name,
1054 encoder->base.id);
1055 if (!encoder->name) {
1056 ret = -ENOMEM;
1057 goto out_put;
1058 }
Dave Airlief453ba02008-11-07 14:05:41 -08001059
1060 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1061 dev->mode_config.num_encoder++;
1062
Jani Nikulae5748942014-05-14 16:58:20 +03001063out_put:
1064 if (ret)
1065 drm_mode_object_put(dev, &encoder->base);
1066
1067out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001068 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001069
1070 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001071}
1072EXPORT_SYMBOL(drm_encoder_init);
1073
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001074/**
1075 * drm_encoder_cleanup - cleans up an initialised encoder
1076 * @encoder: encoder to cleanup
1077 *
1078 * Cleans up the encoder but doesn't free the object.
1079 */
Dave Airlief453ba02008-11-07 14:05:41 -08001080void drm_encoder_cleanup(struct drm_encoder *encoder)
1081{
1082 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001083 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001084 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001085 kfree(encoder->name);
1086 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001087 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001088 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001089 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001090}
1091EXPORT_SYMBOL(drm_encoder_cleanup);
1092
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001093/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001094 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001095 * @dev: DRM device
1096 * @plane: plane object to init
1097 * @possible_crtcs: bitmask of possible CRTCs
1098 * @funcs: callbacks for the new plane
1099 * @formats: array of supported formats (%DRM_FORMAT_*)
1100 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001101 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001102 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001103 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001104 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001105 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001106 * Zero on success, error code on failure.
1107 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001108int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1109 unsigned long possible_crtcs,
1110 const struct drm_plane_funcs *funcs,
1111 const uint32_t *formats, uint32_t format_count,
1112 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001113{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001114 int ret;
1115
Daniel Vetter84849902012-12-02 00:28:11 +01001116 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001117
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001118 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1119 if (ret)
1120 goto out;
1121
Rob Clark4d939142012-05-17 02:23:27 -06001122 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001123 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001124 plane->funcs = funcs;
1125 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1126 GFP_KERNEL);
1127 if (!plane->format_types) {
1128 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1129 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001130 ret = -ENOMEM;
1131 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001132 }
1133
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001134 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001135 plane->format_count = format_count;
1136 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001137 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001138
Matt Roperdc415ff2014-04-01 15:22:36 -07001139 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1140 dev->mode_config.num_total_plane++;
1141 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1142 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001143
Rob Clark9922ab52014-04-01 20:16:57 -04001144 drm_object_attach_property(&plane->base,
1145 dev->mode_config.plane_type_property,
1146 plane->type);
1147
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001148 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001149 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001150
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001151 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001152}
Matt Roperdc415ff2014-04-01 15:22:36 -07001153EXPORT_SYMBOL(drm_universal_plane_init);
1154
1155/**
1156 * drm_plane_init - Initialize a legacy plane
1157 * @dev: DRM device
1158 * @plane: plane object to init
1159 * @possible_crtcs: bitmask of possible CRTCs
1160 * @funcs: callbacks for the new plane
1161 * @formats: array of supported formats (%DRM_FORMAT_*)
1162 * @format_count: number of elements in @formats
1163 * @is_primary: plane type (primary vs overlay)
1164 *
1165 * Legacy API to initialize a DRM plane.
1166 *
1167 * New drivers should call drm_universal_plane_init() instead.
1168 *
1169 * Returns:
1170 * Zero on success, error code on failure.
1171 */
1172int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1173 unsigned long possible_crtcs,
1174 const struct drm_plane_funcs *funcs,
1175 const uint32_t *formats, uint32_t format_count,
1176 bool is_primary)
1177{
1178 enum drm_plane_type type;
1179
1180 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1181 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1182 formats, format_count, type);
1183}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001184EXPORT_SYMBOL(drm_plane_init);
1185
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001186/**
1187 * drm_plane_cleanup - Clean up the core plane usage
1188 * @plane: plane to cleanup
1189 *
1190 * This function cleans up @plane and removes it from the DRM mode setting
1191 * core. Note that the function does *not* free the plane structure itself,
1192 * this is the responsibility of the caller.
1193 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194void drm_plane_cleanup(struct drm_plane *plane)
1195{
1196 struct drm_device *dev = plane->dev;
1197
Daniel Vetter84849902012-12-02 00:28:11 +01001198 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001199 kfree(plane->format_types);
1200 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001201
1202 BUG_ON(list_empty(&plane->head));
1203
1204 list_del(&plane->head);
1205 dev->mode_config.num_total_plane--;
1206 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1207 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001208 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001209}
1210EXPORT_SYMBOL(drm_plane_cleanup);
1211
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001212/**
1213 * drm_plane_force_disable - Forcibly disable a plane
1214 * @plane: plane to disable
1215 *
1216 * Forces the plane to be disabled.
1217 *
1218 * Used when the plane's current framebuffer is destroyed,
1219 * and when restoring fbdev mode.
1220 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001221void drm_plane_force_disable(struct drm_plane *plane)
1222{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001223 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001224 int ret;
1225
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001226 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001227 return;
1228
1229 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001230 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001231 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001232 return;
1233 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001234 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001235 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001236 plane->fb = NULL;
1237 plane->crtc = NULL;
1238}
1239EXPORT_SYMBOL(drm_plane_force_disable);
1240
Dave Airlief453ba02008-11-07 14:05:41 -08001241static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1242{
1243 struct drm_property *edid;
1244 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001245
1246 /*
1247 * Standard properties (apply to all connectors)
1248 */
1249 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1250 DRM_MODE_PROP_IMMUTABLE,
1251 "EDID", 0);
1252 dev->mode_config.edid_property = edid;
1253
Sascha Hauer4a67d392012-02-06 10:58:17 +01001254 dpms = drm_property_create_enum(dev, 0,
1255 "DPMS", drm_dpms_enum_list,
1256 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001257 dev->mode_config.dpms_property = dpms;
1258
1259 return 0;
1260}
1261
Rob Clark9922ab52014-04-01 20:16:57 -04001262static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1263{
1264 struct drm_property *type;
1265
1266 /*
1267 * Standard properties (apply to all planes)
1268 */
1269 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1270 "type", drm_plane_type_enum_list,
1271 ARRAY_SIZE(drm_plane_type_enum_list));
1272 dev->mode_config.plane_type_property = type;
1273
1274 return 0;
1275}
1276
Dave Airlief453ba02008-11-07 14:05:41 -08001277/**
1278 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1279 * @dev: DRM device
1280 *
1281 * Called by a driver the first time a DVI-I connector is made.
1282 */
1283int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1284{
1285 struct drm_property *dvi_i_selector;
1286 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001287
1288 if (dev->mode_config.dvi_i_select_subconnector_property)
1289 return 0;
1290
1291 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001292 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001293 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001294 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001295 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001296 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1297
Sascha Hauer4a67d392012-02-06 10:58:17 +01001298 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001299 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001300 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001301 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001302 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1303
1304 return 0;
1305}
1306EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1307
1308/**
1309 * drm_create_tv_properties - create TV specific connector properties
1310 * @dev: DRM device
1311 * @num_modes: number of different TV formats (modes) supported
1312 * @modes: array of pointers to strings containing name of each format
1313 *
1314 * Called by a driver's TV initialization routine, this function creates
1315 * the TV specific connector properties for a given device. Caller is
1316 * responsible for allocating a list of format names and passing them to
1317 * this routine.
1318 */
1319int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1320 char *modes[])
1321{
1322 struct drm_property *tv_selector;
1323 struct drm_property *tv_subconnector;
1324 int i;
1325
1326 if (dev->mode_config.tv_select_subconnector_property)
1327 return 0;
1328
1329 /*
1330 * Basic connector properties
1331 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001332 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001333 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001334 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001335 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001336 dev->mode_config.tv_select_subconnector_property = tv_selector;
1337
1338 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001339 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1340 "subconnector",
1341 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001342 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001343 dev->mode_config.tv_subconnector_property = tv_subconnector;
1344
1345 /*
1346 * Other, TV specific properties: margins & TV modes.
1347 */
1348 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001349 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001350
1351 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001352 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001353
1354 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001355 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001356
1357 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001358 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001359
1360 dev->mode_config.tv_mode_property =
1361 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1362 "mode", num_modes);
1363 for (i = 0; i < num_modes; i++)
1364 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1365 i, modes[i]);
1366
Francisco Jerezb6b79022009-08-02 04:19:20 +02001367 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001368 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001369
1370 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001371 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001372
1373 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001374 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001375
Francisco Jereza75f0232009-08-12 02:30:10 +02001376 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001377 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001378
1379 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001380 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001381
1382 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001383 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001384
Dave Airlief453ba02008-11-07 14:05:41 -08001385 return 0;
1386}
1387EXPORT_SYMBOL(drm_mode_create_tv_properties);
1388
1389/**
1390 * drm_mode_create_scaling_mode_property - create scaling mode property
1391 * @dev: DRM device
1392 *
1393 * Called by a driver the first time it's needed, must be attached to desired
1394 * connectors.
1395 */
1396int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1397{
1398 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001399
1400 if (dev->mode_config.scaling_mode_property)
1401 return 0;
1402
1403 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001404 drm_property_create_enum(dev, 0, "scaling mode",
1405 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001406 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001407
1408 dev->mode_config.scaling_mode_property = scaling_mode;
1409
1410 return 0;
1411}
1412EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1413
1414/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001415 * drm_mode_create_dirty_property - create dirty property
1416 * @dev: DRM device
1417 *
1418 * Called by a driver the first time it's needed, must be attached to desired
1419 * connectors.
1420 */
1421int drm_mode_create_dirty_info_property(struct drm_device *dev)
1422{
1423 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001424
1425 if (dev->mode_config.dirty_info_property)
1426 return 0;
1427
1428 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001429 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001430 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001431 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001432 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001433 dev->mode_config.dirty_info_property = dirty_info;
1434
1435 return 0;
1436}
1437EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1438
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001439static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001440{
1441 uint32_t total_objects = 0;
1442
1443 total_objects += dev->mode_config.num_crtc;
1444 total_objects += dev->mode_config.num_connector;
1445 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001446 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001447
Dave Airlief453ba02008-11-07 14:05:41 -08001448 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1449 if (!group->id_list)
1450 return -ENOMEM;
1451
1452 group->num_crtcs = 0;
1453 group->num_connectors = 0;
1454 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001455 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001456 return 0;
1457}
1458
Dave Airliead222792014-05-02 13:22:19 +10001459void drm_mode_group_destroy(struct drm_mode_group *group)
1460{
1461 kfree(group->id_list);
1462 group->id_list = NULL;
1463}
1464
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001465/*
1466 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1467 * the drm core's responsibility to set up mode control groups.
1468 */
Dave Airlief453ba02008-11-07 14:05:41 -08001469int drm_mode_group_init_legacy_group(struct drm_device *dev,
1470 struct drm_mode_group *group)
1471{
1472 struct drm_crtc *crtc;
1473 struct drm_encoder *encoder;
1474 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001475 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001476 int ret;
1477
1478 if ((ret = drm_mode_group_init(dev, group)))
1479 return ret;
1480
1481 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1482 group->id_list[group->num_crtcs++] = crtc->base.id;
1483
1484 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1485 group->id_list[group->num_crtcs + group->num_encoders++] =
1486 encoder->base.id;
1487
1488 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1489 group->id_list[group->num_crtcs + group->num_encoders +
1490 group->num_connectors++] = connector->base.id;
1491
Sean Paul3b336ec2013-08-14 16:47:37 -04001492 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1493 group->id_list[group->num_crtcs + group->num_encoders +
1494 group->num_connectors + group->num_bridges++] =
1495 bridge->base.id;
1496
Dave Airlief453ba02008-11-07 14:05:41 -08001497 return 0;
1498}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001499EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001500
1501/**
Dave Airlief453ba02008-11-07 14:05:41 -08001502 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1503 * @out: drm_mode_modeinfo struct to return to the user
1504 * @in: drm_display_mode to use
1505 *
Dave Airlief453ba02008-11-07 14:05:41 -08001506 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1507 * the user.
1508 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001509static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1510 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001511{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001512 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1513 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1514 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1515 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1516 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1517 "timing values too large for mode info\n");
1518
Dave Airlief453ba02008-11-07 14:05:41 -08001519 out->clock = in->clock;
1520 out->hdisplay = in->hdisplay;
1521 out->hsync_start = in->hsync_start;
1522 out->hsync_end = in->hsync_end;
1523 out->htotal = in->htotal;
1524 out->hskew = in->hskew;
1525 out->vdisplay = in->vdisplay;
1526 out->vsync_start = in->vsync_start;
1527 out->vsync_end = in->vsync_end;
1528 out->vtotal = in->vtotal;
1529 out->vscan = in->vscan;
1530 out->vrefresh = in->vrefresh;
1531 out->flags = in->flags;
1532 out->type = in->type;
1533 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1534 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1535}
1536
1537/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001538 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001539 * @out: drm_display_mode to return to the user
1540 * @in: drm_mode_modeinfo to use
1541 *
Dave Airlief453ba02008-11-07 14:05:41 -08001542 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1543 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001544 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001545 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001546 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001547 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001548static int drm_crtc_convert_umode(struct drm_display_mode *out,
1549 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001550{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001551 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1552 return -ERANGE;
1553
Damien Lespiau5848ad42013-09-27 12:11:50 +01001554 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1555 return -EINVAL;
1556
Dave Airlief453ba02008-11-07 14:05:41 -08001557 out->clock = in->clock;
1558 out->hdisplay = in->hdisplay;
1559 out->hsync_start = in->hsync_start;
1560 out->hsync_end = in->hsync_end;
1561 out->htotal = in->htotal;
1562 out->hskew = in->hskew;
1563 out->vdisplay = in->vdisplay;
1564 out->vsync_start = in->vsync_start;
1565 out->vsync_end = in->vsync_end;
1566 out->vtotal = in->vtotal;
1567 out->vscan = in->vscan;
1568 out->vrefresh = in->vrefresh;
1569 out->flags = in->flags;
1570 out->type = in->type;
1571 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1572 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001573
1574 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001575}
1576
1577/**
1578 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001579 * @dev: drm device for the ioctl
1580 * @data: data pointer for the ioctl
1581 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001582 *
Dave Airlief453ba02008-11-07 14:05:41 -08001583 * Construct a set of configuration description structures and return
1584 * them to the user, including CRTC, connector and framebuffer configuration.
1585 *
1586 * Called by the user via ioctl.
1587 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001588 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001589 * Zero on success, errno on failure.
1590 */
1591int drm_mode_getresources(struct drm_device *dev, void *data,
1592 struct drm_file *file_priv)
1593{
1594 struct drm_mode_card_res *card_res = data;
1595 struct list_head *lh;
1596 struct drm_framebuffer *fb;
1597 struct drm_connector *connector;
1598 struct drm_crtc *crtc;
1599 struct drm_encoder *encoder;
1600 int ret = 0;
1601 int connector_count = 0;
1602 int crtc_count = 0;
1603 int fb_count = 0;
1604 int encoder_count = 0;
1605 int copied = 0, i;
1606 uint32_t __user *fb_id;
1607 uint32_t __user *crtc_id;
1608 uint32_t __user *connector_id;
1609 uint32_t __user *encoder_id;
1610 struct drm_mode_group *mode_group;
1611
Dave Airliefb3b06c2011-02-08 13:55:21 +10001612 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1613 return -EINVAL;
1614
Dave Airlief453ba02008-11-07 14:05:41 -08001615
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001616 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001617 /*
1618 * For the non-control nodes we need to limit the list of resources
1619 * by IDs in the group list for this node
1620 */
1621 list_for_each(lh, &file_priv->fbs)
1622 fb_count++;
1623
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001624 /* handle this in 4 parts */
1625 /* FBs */
1626 if (card_res->count_fbs >= fb_count) {
1627 copied = 0;
1628 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1629 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1630 if (put_user(fb->base.id, fb_id + copied)) {
1631 mutex_unlock(&file_priv->fbs_lock);
1632 return -EFAULT;
1633 }
1634 copied++;
1635 }
1636 }
1637 card_res->count_fbs = fb_count;
1638 mutex_unlock(&file_priv->fbs_lock);
1639
1640 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001641 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001642
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001643 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001644 list_for_each(lh, &dev->mode_config.crtc_list)
1645 crtc_count++;
1646
1647 list_for_each(lh, &dev->mode_config.connector_list)
1648 connector_count++;
1649
1650 list_for_each(lh, &dev->mode_config.encoder_list)
1651 encoder_count++;
1652 } else {
1653
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001654 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001655 crtc_count = mode_group->num_crtcs;
1656 connector_count = mode_group->num_connectors;
1657 encoder_count = mode_group->num_encoders;
1658 }
1659
1660 card_res->max_height = dev->mode_config.max_height;
1661 card_res->min_height = dev->mode_config.min_height;
1662 card_res->max_width = dev->mode_config.max_width;
1663 card_res->min_width = dev->mode_config.min_width;
1664
Dave Airlief453ba02008-11-07 14:05:41 -08001665 /* CRTCs */
1666 if (card_res->count_crtcs >= crtc_count) {
1667 copied = 0;
1668 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001669 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001670 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1671 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001672 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001673 if (put_user(crtc->base.id, crtc_id + copied)) {
1674 ret = -EFAULT;
1675 goto out;
1676 }
1677 copied++;
1678 }
1679 } else {
1680 for (i = 0; i < mode_group->num_crtcs; i++) {
1681 if (put_user(mode_group->id_list[i],
1682 crtc_id + copied)) {
1683 ret = -EFAULT;
1684 goto out;
1685 }
1686 copied++;
1687 }
1688 }
1689 }
1690 card_res->count_crtcs = crtc_count;
1691
1692 /* Encoders */
1693 if (card_res->count_encoders >= encoder_count) {
1694 copied = 0;
1695 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001696 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001697 list_for_each_entry(encoder,
1698 &dev->mode_config.encoder_list,
1699 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001700 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001701 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001702 if (put_user(encoder->base.id, encoder_id +
1703 copied)) {
1704 ret = -EFAULT;
1705 goto out;
1706 }
1707 copied++;
1708 }
1709 } else {
1710 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1711 if (put_user(mode_group->id_list[i],
1712 encoder_id + copied)) {
1713 ret = -EFAULT;
1714 goto out;
1715 }
1716 copied++;
1717 }
1718
1719 }
1720 }
1721 card_res->count_encoders = encoder_count;
1722
1723 /* Connectors */
1724 if (card_res->count_connectors >= connector_count) {
1725 copied = 0;
1726 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001727 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001728 list_for_each_entry(connector,
1729 &dev->mode_config.connector_list,
1730 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001731 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1732 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001733 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001734 if (put_user(connector->base.id,
1735 connector_id + copied)) {
1736 ret = -EFAULT;
1737 goto out;
1738 }
1739 copied++;
1740 }
1741 } else {
1742 int start = mode_group->num_crtcs +
1743 mode_group->num_encoders;
1744 for (i = start; i < start + mode_group->num_connectors; i++) {
1745 if (put_user(mode_group->id_list[i],
1746 connector_id + copied)) {
1747 ret = -EFAULT;
1748 goto out;
1749 }
1750 copied++;
1751 }
1752 }
1753 }
1754 card_res->count_connectors = connector_count;
1755
Jerome Glisse94401062010-07-15 15:43:25 -04001756 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001757 card_res->count_connectors, card_res->count_encoders);
1758
1759out:
Daniel Vetter84849902012-12-02 00:28:11 +01001760 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001761 return ret;
1762}
1763
1764/**
1765 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001766 * @dev: drm device for the ioctl
1767 * @data: data pointer for the ioctl
1768 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001769 *
Dave Airlief453ba02008-11-07 14:05:41 -08001770 * Construct a CRTC configuration structure to return to the user.
1771 *
1772 * Called by the user via ioctl.
1773 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001774 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001775 * Zero on success, errno on failure.
1776 */
1777int drm_mode_getcrtc(struct drm_device *dev,
1778 void *data, struct drm_file *file_priv)
1779{
1780 struct drm_mode_crtc *crtc_resp = data;
1781 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001782 int ret = 0;
1783
Dave Airliefb3b06c2011-02-08 13:55:21 +10001784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1785 return -EINVAL;
1786
Daniel Vetter84849902012-12-02 00:28:11 +01001787 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001788
Rob Clarka2b34e22013-10-05 16:36:52 -04001789 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1790 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001791 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001792 goto out;
1793 }
Dave Airlief453ba02008-11-07 14:05:41 -08001794
1795 crtc_resp->x = crtc->x;
1796 crtc_resp->y = crtc->y;
1797 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001798 if (crtc->primary->fb)
1799 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001800 else
1801 crtc_resp->fb_id = 0;
1802
1803 if (crtc->enabled) {
1804
1805 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1806 crtc_resp->mode_valid = 1;
1807
1808 } else {
1809 crtc_resp->mode_valid = 0;
1810 }
1811
1812out:
Daniel Vetter84849902012-12-02 00:28:11 +01001813 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001814 return ret;
1815}
1816
Damien Lespiau61d8e322013-09-25 16:45:22 +01001817static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1818 const struct drm_file *file_priv)
1819{
1820 /*
1821 * If user-space hasn't configured the driver to expose the stereo 3D
1822 * modes, don't expose them.
1823 */
1824 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1825 return false;
1826
1827 return true;
1828}
1829
Dave Airlief453ba02008-11-07 14:05:41 -08001830/**
1831 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001832 * @dev: drm device for the ioctl
1833 * @data: data pointer for the ioctl
1834 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001835 *
Dave Airlief453ba02008-11-07 14:05:41 -08001836 * Construct a connector configuration structure to return to the user.
1837 *
1838 * Called by the user via ioctl.
1839 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001840 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001841 * Zero on success, errno on failure.
1842 */
1843int drm_mode_getconnector(struct drm_device *dev, void *data,
1844 struct drm_file *file_priv)
1845{
1846 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001847 struct drm_connector *connector;
1848 struct drm_display_mode *mode;
1849 int mode_count = 0;
1850 int props_count = 0;
1851 int encoders_count = 0;
1852 int ret = 0;
1853 int copied = 0;
1854 int i;
1855 struct drm_mode_modeinfo u_mode;
1856 struct drm_mode_modeinfo __user *mode_ptr;
1857 uint32_t __user *prop_ptr;
1858 uint64_t __user *prop_values;
1859 uint32_t __user *encoder_ptr;
1860
Dave Airliefb3b06c2011-02-08 13:55:21 +10001861 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1862 return -EINVAL;
1863
Dave Airlief453ba02008-11-07 14:05:41 -08001864 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1865
Jerome Glisse94401062010-07-15 15:43:25 -04001866 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001867
Daniel Vetter7b240562012-12-12 00:35:33 +01001868 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001869
Rob Clarka2b34e22013-10-05 16:36:52 -04001870 connector = drm_connector_find(dev, out_resp->connector_id);
1871 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001872 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001873 goto out;
1874 }
Dave Airlief453ba02008-11-07 14:05:41 -08001875
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001876 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001877
1878 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1879 if (connector->encoder_ids[i] != 0) {
1880 encoders_count++;
1881 }
1882 }
1883
1884 if (out_resp->count_modes == 0) {
1885 connector->funcs->fill_modes(connector,
1886 dev->mode_config.max_width,
1887 dev->mode_config.max_height);
1888 }
1889
1890 /* delayed so we get modes regardless of pre-fill_modes state */
1891 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001892 if (drm_mode_expose_to_userspace(mode, file_priv))
1893 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001894
1895 out_resp->connector_id = connector->base.id;
1896 out_resp->connector_type = connector->connector_type;
1897 out_resp->connector_type_id = connector->connector_type_id;
1898 out_resp->mm_width = connector->display_info.width_mm;
1899 out_resp->mm_height = connector->display_info.height_mm;
1900 out_resp->subpixel = connector->display_info.subpixel_order;
1901 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001902 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001903 if (connector->encoder)
1904 out_resp->encoder_id = connector->encoder->base.id;
1905 else
1906 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001907 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001908
1909 /*
1910 * This ioctl is called twice, once to determine how much space is
1911 * needed, and the 2nd time to fill it.
1912 */
1913 if ((out_resp->count_modes >= mode_count) && mode_count) {
1914 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001915 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001916 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001917 if (!drm_mode_expose_to_userspace(mode, file_priv))
1918 continue;
1919
Dave Airlief453ba02008-11-07 14:05:41 -08001920 drm_crtc_convert_to_umode(&u_mode, mode);
1921 if (copy_to_user(mode_ptr + copied,
1922 &u_mode, sizeof(u_mode))) {
1923 ret = -EFAULT;
1924 goto out;
1925 }
1926 copied++;
1927 }
1928 }
1929 out_resp->count_modes = mode_count;
1930
1931 if ((out_resp->count_props >= props_count) && props_count) {
1932 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001933 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1934 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001935 for (i = 0; i < connector->properties.count; i++) {
1936 if (put_user(connector->properties.ids[i],
1937 prop_ptr + copied)) {
1938 ret = -EFAULT;
1939 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001940 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001941
1942 if (put_user(connector->properties.values[i],
1943 prop_values + copied)) {
1944 ret = -EFAULT;
1945 goto out;
1946 }
1947 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001948 }
1949 }
1950 out_resp->count_props = props_count;
1951
1952 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1953 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001954 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001955 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1956 if (connector->encoder_ids[i] != 0) {
1957 if (put_user(connector->encoder_ids[i],
1958 encoder_ptr + copied)) {
1959 ret = -EFAULT;
1960 goto out;
1961 }
1962 copied++;
1963 }
1964 }
1965 }
1966 out_resp->count_encoders = encoders_count;
1967
1968out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001969 mutex_unlock(&dev->mode_config.mutex);
1970
Dave Airlief453ba02008-11-07 14:05:41 -08001971 return ret;
1972}
1973
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001974/**
1975 * drm_mode_getencoder - get encoder configuration
1976 * @dev: drm device for the ioctl
1977 * @data: data pointer for the ioctl
1978 * @file_priv: drm file for the ioctl call
1979 *
1980 * Construct a encoder configuration structure to return to the user.
1981 *
1982 * Called by the user via ioctl.
1983 *
1984 * Returns:
1985 * Zero on success, errno on failure.
1986 */
Dave Airlief453ba02008-11-07 14:05:41 -08001987int drm_mode_getencoder(struct drm_device *dev, void *data,
1988 struct drm_file *file_priv)
1989{
1990 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001991 struct drm_encoder *encoder;
1992 int ret = 0;
1993
Dave Airliefb3b06c2011-02-08 13:55:21 +10001994 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1995 return -EINVAL;
1996
Daniel Vetter84849902012-12-02 00:28:11 +01001997 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04001998 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1999 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002000 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002001 goto out;
2002 }
Dave Airlief453ba02008-11-07 14:05:41 -08002003
2004 if (encoder->crtc)
2005 enc_resp->crtc_id = encoder->crtc->base.id;
2006 else
2007 enc_resp->crtc_id = 0;
2008 enc_resp->encoder_type = encoder->encoder_type;
2009 enc_resp->encoder_id = encoder->base.id;
2010 enc_resp->possible_crtcs = encoder->possible_crtcs;
2011 enc_resp->possible_clones = encoder->possible_clones;
2012
2013out:
Daniel Vetter84849902012-12-02 00:28:11 +01002014 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002015 return ret;
2016}
2017
2018/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002019 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002020 * @dev: DRM device
2021 * @data: ioctl data
2022 * @file_priv: DRM file info
2023 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002024 * Construct a list of plane ids to return to the user.
2025 *
2026 * Called by the user via ioctl.
2027 *
2028 * Returns:
2029 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002030 */
2031int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002032 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002033{
2034 struct drm_mode_get_plane_res *plane_resp = data;
2035 struct drm_mode_config *config;
2036 struct drm_plane *plane;
2037 uint32_t __user *plane_ptr;
2038 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002039 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002040
2041 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2042 return -EINVAL;
2043
Daniel Vetter84849902012-12-02 00:28:11 +01002044 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002045 config = &dev->mode_config;
2046
Matt Roper681e7ec2014-04-01 15:22:42 -07002047 if (file_priv->universal_planes)
2048 num_planes = config->num_total_plane;
2049 else
2050 num_planes = config->num_overlay_plane;
2051
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002052 /*
2053 * This ioctl is called twice, once to determine how much space is
2054 * needed, and the 2nd time to fill it.
2055 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002056 if (num_planes &&
2057 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002058 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002059
2060 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002061 /*
2062 * Unless userspace set the 'universal planes'
2063 * capability bit, only advertise overlays.
2064 */
2065 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2066 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002067 continue;
2068
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002069 if (put_user(plane->base.id, plane_ptr + copied)) {
2070 ret = -EFAULT;
2071 goto out;
2072 }
2073 copied++;
2074 }
2075 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002076 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002077
2078out:
Daniel Vetter84849902012-12-02 00:28:11 +01002079 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002080 return ret;
2081}
2082
2083/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002084 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002085 * @dev: DRM device
2086 * @data: ioctl data
2087 * @file_priv: DRM file info
2088 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002089 * Construct a plane configuration structure to return to the user.
2090 *
2091 * Called by the user via ioctl.
2092 *
2093 * Returns:
2094 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002095 */
2096int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002097 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002098{
2099 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002100 struct drm_plane *plane;
2101 uint32_t __user *format_ptr;
2102 int ret = 0;
2103
2104 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2105 return -EINVAL;
2106
Daniel Vetter84849902012-12-02 00:28:11 +01002107 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002108 plane = drm_plane_find(dev, plane_resp->plane_id);
2109 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002110 ret = -ENOENT;
2111 goto out;
2112 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002113
2114 if (plane->crtc)
2115 plane_resp->crtc_id = plane->crtc->base.id;
2116 else
2117 plane_resp->crtc_id = 0;
2118
2119 if (plane->fb)
2120 plane_resp->fb_id = plane->fb->base.id;
2121 else
2122 plane_resp->fb_id = 0;
2123
2124 plane_resp->plane_id = plane->base.id;
2125 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002126 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002127
2128 /*
2129 * This ioctl is called twice, once to determine how much space is
2130 * needed, and the 2nd time to fill it.
2131 */
2132 if (plane->format_count &&
2133 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002134 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002135 if (copy_to_user(format_ptr,
2136 plane->format_types,
2137 sizeof(uint32_t) * plane->format_count)) {
2138 ret = -EFAULT;
2139 goto out;
2140 }
2141 }
2142 plane_resp->count_format_types = plane->format_count;
2143
2144out:
Daniel Vetter84849902012-12-02 00:28:11 +01002145 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002146 return ret;
2147}
2148
2149/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002150 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002151 * @dev: DRM device
2152 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002153 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002154 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002155 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002156 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002157 *
2158 * Returns:
2159 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002160 */
2161int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002162 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002163{
2164 struct drm_mode_set_plane *plane_req = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002165 struct drm_plane *plane;
2166 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002167 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002168 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002169 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002170 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002171
2172 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2173 return -EINVAL;
2174
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002175 /*
2176 * First, find the plane, crtc, and fb objects. If not available,
2177 * we don't bother to call the driver.
2178 */
Rob Clarka2b34e22013-10-05 16:36:52 -04002179 plane = drm_plane_find(dev, plane_req->plane_id);
2180 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002181 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2182 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002183 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002184 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002185
2186 /* No fb means shut it down */
2187 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002188 drm_modeset_lock_all(dev);
2189 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002190 ret = plane->funcs->disable_plane(plane);
2191 if (!ret) {
2192 plane->crtc = NULL;
2193 plane->fb = NULL;
2194 } else {
2195 old_fb = NULL;
2196 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002197 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002198 goto out;
2199 }
2200
Rob Clarka2b34e22013-10-05 16:36:52 -04002201 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2202 if (!crtc) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002203 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2204 plane_req->crtc_id);
2205 ret = -ENOENT;
2206 goto out;
2207 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002208
Matt Roper7f994f32014-05-29 08:06:51 -07002209 /* Check whether this plane is usable on this CRTC */
2210 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2211 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2212 ret = -EINVAL;
2213 goto out;
2214 }
2215
Daniel Vetter786b99e2012-12-02 21:53:40 +01002216 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2217 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002218 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2219 plane_req->fb_id);
2220 ret = -ENOENT;
2221 goto out;
2222 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002223
Ville Syrjälä62443be2011-12-20 00:06:47 +02002224 /* Check whether this plane supports the fb pixel format. */
2225 for (i = 0; i < plane->format_count; i++)
2226 if (fb->pixel_format == plane->format_types[i])
2227 break;
2228 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002229 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2230 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002231 ret = -EINVAL;
2232 goto out;
2233 }
2234
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002235 fb_width = fb->width << 16;
2236 fb_height = fb->height << 16;
2237
2238 /* Make sure source coordinates are inside the fb. */
2239 if (plane_req->src_w > fb_width ||
2240 plane_req->src_x > fb_width - plane_req->src_w ||
2241 plane_req->src_h > fb_height ||
2242 plane_req->src_y > fb_height - plane_req->src_h) {
2243 DRM_DEBUG_KMS("Invalid source coordinates "
2244 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2245 plane_req->src_w >> 16,
2246 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2247 plane_req->src_h >> 16,
2248 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2249 plane_req->src_x >> 16,
2250 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2251 plane_req->src_y >> 16,
2252 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2253 ret = -ENOSPC;
2254 goto out;
2255 }
2256
Ville Syrjälä687a0402011-12-20 00:06:45 +02002257 /* Give drivers some help against integer overflows */
2258 if (plane_req->crtc_w > INT_MAX ||
2259 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2260 plane_req->crtc_h > INT_MAX ||
2261 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2262 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2263 plane_req->crtc_w, plane_req->crtc_h,
2264 plane_req->crtc_x, plane_req->crtc_y);
2265 ret = -ERANGE;
2266 goto out;
2267 }
2268
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002269 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002270 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002271 ret = plane->funcs->update_plane(plane, crtc, fb,
2272 plane_req->crtc_x, plane_req->crtc_y,
2273 plane_req->crtc_w, plane_req->crtc_h,
2274 plane_req->src_x, plane_req->src_y,
2275 plane_req->src_w, plane_req->src_h);
2276 if (!ret) {
2277 plane->crtc = crtc;
2278 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002279 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002280 } else {
2281 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002282 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002283 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002284
2285out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002286 if (fb)
2287 drm_framebuffer_unreference(fb);
2288 if (old_fb)
2289 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002290
2291 return ret;
2292}
2293
2294/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002295 * drm_mode_set_config_internal - helper to call ->set_config
2296 * @set: modeset config to set
2297 *
2298 * This is a little helper to wrap internal calls to the ->set_config driver
2299 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002300 *
2301 * Returns:
2302 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002303 */
2304int drm_mode_set_config_internal(struct drm_mode_set *set)
2305{
2306 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002307 struct drm_framebuffer *fb;
2308 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002309 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002310
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002311 /*
2312 * NOTE: ->set_config can also disable other crtcs (if we steal all
2313 * connectors from it), hence we need to refcount the fbs across all
2314 * crtcs. Atomic modeset will have saner semantics ...
2315 */
2316 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002317 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002318
Daniel Vetterb0d12322012-12-11 01:07:12 +01002319 fb = set->fb;
2320
2321 ret = crtc->funcs->set_config(set);
2322 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002323 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002324 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002325 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002326
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002327 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002328 if (tmp->primary->fb)
2329 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002330 if (tmp->old_fb)
2331 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002332 }
2333
2334 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002335}
2336EXPORT_SYMBOL(drm_mode_set_config_internal);
2337
Matt Roperaf936292014-04-01 15:22:34 -07002338/**
2339 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2340 * CRTC viewport
2341 * @crtc: CRTC that framebuffer will be displayed on
2342 * @x: x panning
2343 * @y: y panning
2344 * @mode: mode that framebuffer will be displayed under
2345 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002346 */
Matt Roperaf936292014-04-01 15:22:34 -07002347int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2348 int x, int y,
2349 const struct drm_display_mode *mode,
2350 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002351
2352{
2353 int hdisplay, vdisplay;
2354
2355 hdisplay = mode->hdisplay;
2356 vdisplay = mode->vdisplay;
2357
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002358 if (drm_mode_is_stereo(mode)) {
2359 struct drm_display_mode adjusted = *mode;
2360
2361 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2362 hdisplay = adjusted.crtc_hdisplay;
2363 vdisplay = adjusted.crtc_vdisplay;
2364 }
2365
Damien Lespiauc11e9282013-09-25 16:45:30 +01002366 if (crtc->invert_dimensions)
2367 swap(hdisplay, vdisplay);
2368
2369 if (hdisplay > fb->width ||
2370 vdisplay > fb->height ||
2371 x > fb->width - hdisplay ||
2372 y > fb->height - vdisplay) {
2373 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2374 fb->width, fb->height, hdisplay, vdisplay, x, y,
2375 crtc->invert_dimensions ? " (inverted)" : "");
2376 return -ENOSPC;
2377 }
2378
2379 return 0;
2380}
Matt Roperaf936292014-04-01 15:22:34 -07002381EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002382
Daniel Vetter2d13b672012-12-11 13:47:23 +01002383/**
Dave Airlief453ba02008-11-07 14:05:41 -08002384 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002385 * @dev: drm device for the ioctl
2386 * @data: data pointer for the ioctl
2387 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002388 *
Dave Airlief453ba02008-11-07 14:05:41 -08002389 * Build a new CRTC configuration based on user request.
2390 *
2391 * Called by the user via ioctl.
2392 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002393 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002394 * Zero on success, errno on failure.
2395 */
2396int drm_mode_setcrtc(struct drm_device *dev, void *data,
2397 struct drm_file *file_priv)
2398{
2399 struct drm_mode_config *config = &dev->mode_config;
2400 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002401 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002402 struct drm_connector **connector_set = NULL, *connector;
2403 struct drm_framebuffer *fb = NULL;
2404 struct drm_display_mode *mode = NULL;
2405 struct drm_mode_set set;
2406 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002407 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002408 int i;
2409
Dave Airliefb3b06c2011-02-08 13:55:21 +10002410 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2411 return -EINVAL;
2412
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002413 /* For some reason crtc x/y offsets are signed internally. */
2414 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2415 return -ERANGE;
2416
Daniel Vetter84849902012-12-02 00:28:11 +01002417 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002418 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2419 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002420 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002421 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002422 goto out;
2423 }
Jerome Glisse94401062010-07-15 15:43:25 -04002424 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002425
2426 if (crtc_req->mode_valid) {
2427 /* If we have a mode we need a framebuffer. */
2428 /* If we pass -1, set the mode with the currently bound fb */
2429 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002430 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002431 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2432 ret = -EINVAL;
2433 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002434 }
Matt Roperf4510a22014-04-01 15:22:40 -07002435 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002436 /* Make refcounting symmetric with the lookup path. */
2437 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002438 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002439 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2440 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002441 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2442 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002443 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002444 goto out;
2445 }
Dave Airlief453ba02008-11-07 14:05:41 -08002446 }
2447
2448 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002449 if (!mode) {
2450 ret = -ENOMEM;
2451 goto out;
2452 }
2453
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002454 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2455 if (ret) {
2456 DRM_DEBUG_KMS("Invalid mode\n");
2457 goto out;
2458 }
2459
Dave Airlief453ba02008-11-07 14:05:41 -08002460 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002461
Damien Lespiauc11e9282013-09-25 16:45:30 +01002462 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2463 mode, fb);
2464 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002465 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002466
Dave Airlief453ba02008-11-07 14:05:41 -08002467 }
2468
2469 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002470 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002471 ret = -EINVAL;
2472 goto out;
2473 }
2474
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002475 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002476 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002477 crtc_req->count_connectors);
2478 ret = -EINVAL;
2479 goto out;
2480 }
2481
2482 if (crtc_req->count_connectors > 0) {
2483 u32 out_id;
2484
2485 /* Avoid unbounded kernel memory allocation */
2486 if (crtc_req->count_connectors > config->num_connector) {
2487 ret = -EINVAL;
2488 goto out;
2489 }
2490
2491 connector_set = kmalloc(crtc_req->count_connectors *
2492 sizeof(struct drm_connector *),
2493 GFP_KERNEL);
2494 if (!connector_set) {
2495 ret = -ENOMEM;
2496 goto out;
2497 }
2498
2499 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002500 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002501 if (get_user(out_id, &set_connectors_ptr[i])) {
2502 ret = -EFAULT;
2503 goto out;
2504 }
2505
Rob Clarka2b34e22013-10-05 16:36:52 -04002506 connector = drm_connector_find(dev, out_id);
2507 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002508 DRM_DEBUG_KMS("Connector id %d unknown\n",
2509 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002510 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002511 goto out;
2512 }
Jerome Glisse94401062010-07-15 15:43:25 -04002513 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2514 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002515 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002516
2517 connector_set[i] = connector;
2518 }
2519 }
2520
2521 set.crtc = crtc;
2522 set.x = crtc_req->x;
2523 set.y = crtc_req->y;
2524 set.mode = mode;
2525 set.connectors = connector_set;
2526 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002527 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002528 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002529
2530out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002531 if (fb)
2532 drm_framebuffer_unreference(fb);
2533
Dave Airlief453ba02008-11-07 14:05:41 -08002534 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002535 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002536 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002537 return ret;
2538}
2539
Dave Airlie4c813d42013-06-20 11:48:52 +10002540static int drm_mode_cursor_common(struct drm_device *dev,
2541 struct drm_mode_cursor2 *req,
2542 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002543{
Dave Airlief453ba02008-11-07 14:05:41 -08002544 struct drm_crtc *crtc;
2545 int ret = 0;
2546
Dave Airliefb3b06c2011-02-08 13:55:21 +10002547 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2548 return -EINVAL;
2549
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002550 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002551 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002552
Rob Clarka2b34e22013-10-05 16:36:52 -04002553 crtc = drm_crtc_find(dev, req->crtc_id);
2554 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002555 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002556 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002557 }
Dave Airlief453ba02008-11-07 14:05:41 -08002558
Rob Clark51fd3712013-11-19 12:10:12 -05002559 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002560 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002561 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002562 ret = -ENXIO;
2563 goto out;
2564 }
2565 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002566 if (crtc->funcs->cursor_set2)
2567 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2568 req->width, req->height, req->hot_x, req->hot_y);
2569 else
2570 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2571 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002572 }
2573
2574 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2575 if (crtc->funcs->cursor_move) {
2576 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2577 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002578 ret = -EFAULT;
2579 goto out;
2580 }
2581 }
2582out:
Rob Clark51fd3712013-11-19 12:10:12 -05002583 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002584
Dave Airlief453ba02008-11-07 14:05:41 -08002585 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002586
2587}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002588
2589
2590/**
2591 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2592 * @dev: drm device for the ioctl
2593 * @data: data pointer for the ioctl
2594 * @file_priv: drm file for the ioctl call
2595 *
2596 * Set the cursor configuration based on user request.
2597 *
2598 * Called by the user via ioctl.
2599 *
2600 * Returns:
2601 * Zero on success, errno on failure.
2602 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002603int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002604 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002605{
2606 struct drm_mode_cursor *req = data;
2607 struct drm_mode_cursor2 new_req;
2608
2609 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2610 new_req.hot_x = new_req.hot_y = 0;
2611
2612 return drm_mode_cursor_common(dev, &new_req, file_priv);
2613}
2614
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002615/**
2616 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2617 * @dev: drm device for the ioctl
2618 * @data: data pointer for the ioctl
2619 * @file_priv: drm file for the ioctl call
2620 *
2621 * Set the cursor configuration based on user request. This implements the 2nd
2622 * version of the cursor ioctl, which allows userspace to additionally specify
2623 * the hotspot of the pointer.
2624 *
2625 * Called by the user via ioctl.
2626 *
2627 * Returns:
2628 * Zero on success, errno on failure.
2629 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002630int drm_mode_cursor2_ioctl(struct drm_device *dev,
2631 void *data, struct drm_file *file_priv)
2632{
2633 struct drm_mode_cursor2 *req = data;
2634 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002635}
2636
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002637/**
2638 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2639 * @bpp: bits per pixels
2640 * @depth: bit depth per pixel
2641 *
2642 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2643 * Useful in fbdev emulation code, since that deals in those values.
2644 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002645uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2646{
2647 uint32_t fmt;
2648
2649 switch (bpp) {
2650 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002651 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002652 break;
2653 case 16:
2654 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002655 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002656 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002657 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002658 break;
2659 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002660 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002661 break;
2662 case 32:
2663 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002664 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002665 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002666 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002667 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002668 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002669 break;
2670 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002671 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2672 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002673 break;
2674 }
2675
2676 return fmt;
2677}
2678EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2679
Dave Airlief453ba02008-11-07 14:05:41 -08002680/**
2681 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002682 * @dev: drm device for the ioctl
2683 * @data: data pointer for the ioctl
2684 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002685 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002686 * Add a new FB to the specified CRTC, given a user request. This is the
2687 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002688 *
2689 * Called by the user via ioctl.
2690 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002691 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002692 * Zero on success, errno on failure.
2693 */
2694int drm_mode_addfb(struct drm_device *dev,
2695 void *data, struct drm_file *file_priv)
2696{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002697 struct drm_mode_fb_cmd *or = data;
2698 struct drm_mode_fb_cmd2 r = {};
2699 struct drm_mode_config *config = &dev->mode_config;
2700 struct drm_framebuffer *fb;
2701 int ret = 0;
2702
2703 /* Use new struct with format internally */
2704 r.fb_id = or->fb_id;
2705 r.width = or->width;
2706 r.height = or->height;
2707 r.pitches[0] = or->pitch;
2708 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2709 r.handles[0] = or->handle;
2710
2711 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2712 return -EINVAL;
2713
Jesse Barnesacb4b992011-11-07 12:03:23 -08002714 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002715 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002716
2717 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002718 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002719
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002720 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2721 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002722 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002723 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002724 }
2725
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002726 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002727 or->fb_id = fb->base.id;
2728 list_add(&fb->filp_head, &file_priv->fbs);
2729 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002730 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002731
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002732 return ret;
2733}
2734
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002735static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002736{
2737 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2738
2739 switch (format) {
2740 case DRM_FORMAT_C8:
2741 case DRM_FORMAT_RGB332:
2742 case DRM_FORMAT_BGR233:
2743 case DRM_FORMAT_XRGB4444:
2744 case DRM_FORMAT_XBGR4444:
2745 case DRM_FORMAT_RGBX4444:
2746 case DRM_FORMAT_BGRX4444:
2747 case DRM_FORMAT_ARGB4444:
2748 case DRM_FORMAT_ABGR4444:
2749 case DRM_FORMAT_RGBA4444:
2750 case DRM_FORMAT_BGRA4444:
2751 case DRM_FORMAT_XRGB1555:
2752 case DRM_FORMAT_XBGR1555:
2753 case DRM_FORMAT_RGBX5551:
2754 case DRM_FORMAT_BGRX5551:
2755 case DRM_FORMAT_ARGB1555:
2756 case DRM_FORMAT_ABGR1555:
2757 case DRM_FORMAT_RGBA5551:
2758 case DRM_FORMAT_BGRA5551:
2759 case DRM_FORMAT_RGB565:
2760 case DRM_FORMAT_BGR565:
2761 case DRM_FORMAT_RGB888:
2762 case DRM_FORMAT_BGR888:
2763 case DRM_FORMAT_XRGB8888:
2764 case DRM_FORMAT_XBGR8888:
2765 case DRM_FORMAT_RGBX8888:
2766 case DRM_FORMAT_BGRX8888:
2767 case DRM_FORMAT_ARGB8888:
2768 case DRM_FORMAT_ABGR8888:
2769 case DRM_FORMAT_RGBA8888:
2770 case DRM_FORMAT_BGRA8888:
2771 case DRM_FORMAT_XRGB2101010:
2772 case DRM_FORMAT_XBGR2101010:
2773 case DRM_FORMAT_RGBX1010102:
2774 case DRM_FORMAT_BGRX1010102:
2775 case DRM_FORMAT_ARGB2101010:
2776 case DRM_FORMAT_ABGR2101010:
2777 case DRM_FORMAT_RGBA1010102:
2778 case DRM_FORMAT_BGRA1010102:
2779 case DRM_FORMAT_YUYV:
2780 case DRM_FORMAT_YVYU:
2781 case DRM_FORMAT_UYVY:
2782 case DRM_FORMAT_VYUY:
2783 case DRM_FORMAT_AYUV:
2784 case DRM_FORMAT_NV12:
2785 case DRM_FORMAT_NV21:
2786 case DRM_FORMAT_NV16:
2787 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002788 case DRM_FORMAT_NV24:
2789 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002790 case DRM_FORMAT_YUV410:
2791 case DRM_FORMAT_YVU410:
2792 case DRM_FORMAT_YUV411:
2793 case DRM_FORMAT_YVU411:
2794 case DRM_FORMAT_YUV420:
2795 case DRM_FORMAT_YVU420:
2796 case DRM_FORMAT_YUV422:
2797 case DRM_FORMAT_YVU422:
2798 case DRM_FORMAT_YUV444:
2799 case DRM_FORMAT_YVU444:
2800 return 0;
2801 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002802 DRM_DEBUG_KMS("invalid pixel format %s\n",
2803 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002804 return -EINVAL;
2805 }
2806}
2807
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002808static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002809{
2810 int ret, hsub, vsub, num_planes, i;
2811
2812 ret = format_check(r);
2813 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002814 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2815 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002816 return ret;
2817 }
2818
2819 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2820 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2821 num_planes = drm_format_num_planes(r->pixel_format);
2822
2823 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002824 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002825 return -EINVAL;
2826 }
2827
2828 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002829 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002830 return -EINVAL;
2831 }
2832
2833 for (i = 0; i < num_planes; i++) {
2834 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002835 unsigned int height = r->height / (i != 0 ? vsub : 1);
2836 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002837
2838 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002839 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002840 return -EINVAL;
2841 }
2842
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002843 if ((uint64_t) width * cpp > UINT_MAX)
2844 return -ERANGE;
2845
2846 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2847 return -ERANGE;
2848
2849 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002850 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002851 return -EINVAL;
2852 }
2853 }
2854
2855 return 0;
2856}
2857
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002858/**
2859 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002860 * @dev: drm device for the ioctl
2861 * @data: data pointer for the ioctl
2862 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002863 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002864 * Add a new FB to the specified CRTC, given a user request with format. This is
2865 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2866 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002867 *
2868 * Called by the user via ioctl.
2869 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002870 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002871 * Zero on success, errno on failure.
2872 */
2873int drm_mode_addfb2(struct drm_device *dev,
2874 void *data, struct drm_file *file_priv)
2875{
2876 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002877 struct drm_mode_config *config = &dev->mode_config;
2878 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002879 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002880
Dave Airliefb3b06c2011-02-08 13:55:21 +10002881 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2882 return -EINVAL;
2883
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002884 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2885 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2886 return -EINVAL;
2887 }
2888
Dave Airlief453ba02008-11-07 14:05:41 -08002889 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002890 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002891 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002892 return -EINVAL;
2893 }
2894 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002895 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002896 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002897 return -EINVAL;
2898 }
2899
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002900 ret = framebuffer_check(r);
2901 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002902 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002903
Dave Airlief453ba02008-11-07 14:05:41 -08002904 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002905 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002906 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002907 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002908 }
2909
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002910 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002911 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002912 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002913 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002914 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002915
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002916
Dave Airlief453ba02008-11-07 14:05:41 -08002917 return ret;
2918}
2919
2920/**
2921 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002922 * @dev: drm device for the ioctl
2923 * @data: data pointer for the ioctl
2924 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002925 *
Dave Airlief453ba02008-11-07 14:05:41 -08002926 * Remove the FB specified by the user.
2927 *
2928 * Called by the user via ioctl.
2929 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002930 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002931 * Zero on success, errno on failure.
2932 */
2933int drm_mode_rmfb(struct drm_device *dev,
2934 void *data, struct drm_file *file_priv)
2935{
Dave Airlief453ba02008-11-07 14:05:41 -08002936 struct drm_framebuffer *fb = NULL;
2937 struct drm_framebuffer *fbl = NULL;
2938 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002939 int found = 0;
2940
Dave Airliefb3b06c2011-02-08 13:55:21 +10002941 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2942 return -EINVAL;
2943
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002944 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002945 mutex_lock(&dev->mode_config.fb_lock);
2946 fb = __drm_framebuffer_lookup(dev, *id);
2947 if (!fb)
2948 goto fail_lookup;
2949
Dave Airlief453ba02008-11-07 14:05:41 -08002950 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2951 if (fb == fbl)
2952 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002953 if (!found)
2954 goto fail_lookup;
2955
2956 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2957 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002958
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002959 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002960 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002961 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002962
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002963 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002964
Daniel Vetter2b677e82012-12-10 21:16:05 +01002965 return 0;
2966
2967fail_lookup:
2968 mutex_unlock(&dev->mode_config.fb_lock);
2969 mutex_unlock(&file_priv->fbs_lock);
2970
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002971 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002972}
2973
2974/**
2975 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002976 * @dev: drm device for the ioctl
2977 * @data: data pointer for the ioctl
2978 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002979 *
Dave Airlief453ba02008-11-07 14:05:41 -08002980 * Lookup the FB given its ID and return info about it.
2981 *
2982 * Called by the user via ioctl.
2983 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002984 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002985 * Zero on success, errno on failure.
2986 */
2987int drm_mode_getfb(struct drm_device *dev,
2988 void *data, struct drm_file *file_priv)
2989{
2990 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002991 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002992 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002993
Dave Airliefb3b06c2011-02-08 13:55:21 +10002994 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2995 return -EINVAL;
2996
Daniel Vetter786b99e2012-12-02 21:53:40 +01002997 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002998 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002999 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003000
3001 r->height = fb->height;
3002 r->width = fb->width;
3003 r->depth = fb->depth;
3004 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003005 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003006 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003007 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003008 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003009 ret = fb->funcs->create_handle(fb, file_priv,
3010 &r->handle);
3011 } else {
3012 /* GET_FB() is an unprivileged ioctl so we must not
3013 * return a buffer-handle to non-master processes! For
3014 * backwards-compatibility reasons, we cannot make
3015 * GET_FB() privileged, so just return an invalid handle
3016 * for non-masters. */
3017 r->handle = 0;
3018 ret = 0;
3019 }
3020 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003021 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003022 }
Dave Airlief453ba02008-11-07 14:05:41 -08003023
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003024 drm_framebuffer_unreference(fb);
3025
Dave Airlief453ba02008-11-07 14:05:41 -08003026 return ret;
3027}
3028
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003029/**
3030 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3031 * @dev: drm device for the ioctl
3032 * @data: data pointer for the ioctl
3033 * @file_priv: drm file for the ioctl call
3034 *
3035 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3036 * rectangle list. Generic userspace which does frontbuffer rendering must call
3037 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3038 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3039 *
3040 * Modesetting drivers which always update the frontbuffer do not need to
3041 * implement the corresponding ->dirty framebuffer callback.
3042 *
3043 * Called by the user via ioctl.
3044 *
3045 * Returns:
3046 * Zero on success, errno on failure.
3047 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003048int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3049 void *data, struct drm_file *file_priv)
3050{
3051 struct drm_clip_rect __user *clips_ptr;
3052 struct drm_clip_rect *clips = NULL;
3053 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003054 struct drm_framebuffer *fb;
3055 unsigned flags;
3056 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003057 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003058
Dave Airliefb3b06c2011-02-08 13:55:21 +10003059 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3060 return -EINVAL;
3061
Daniel Vetter786b99e2012-12-02 21:53:40 +01003062 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003063 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003064 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003065
3066 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003067 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003068
3069 if (!num_clips != !clips_ptr) {
3070 ret = -EINVAL;
3071 goto out_err1;
3072 }
3073
3074 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3075
3076 /* If userspace annotates copy, clips must come in pairs */
3077 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3078 ret = -EINVAL;
3079 goto out_err1;
3080 }
3081
3082 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003083 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3084 ret = -EINVAL;
3085 goto out_err1;
3086 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003087 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3088 if (!clips) {
3089 ret = -ENOMEM;
3090 goto out_err1;
3091 }
3092
3093 ret = copy_from_user(clips, clips_ptr,
3094 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003095 if (ret) {
3096 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003097 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003098 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003099 }
3100
3101 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003102 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3103 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003104 } else {
3105 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003106 }
3107
3108out_err2:
3109 kfree(clips);
3110out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003111 drm_framebuffer_unreference(fb);
3112
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003113 return ret;
3114}
3115
3116
Dave Airlief453ba02008-11-07 14:05:41 -08003117/**
3118 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003119 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003120 *
Dave Airlief453ba02008-11-07 14:05:41 -08003121 * Destroy all the FBs associated with @filp.
3122 *
3123 * Called by the user via ioctl.
3124 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003125 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003126 * Zero on success, errno on failure.
3127 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003128void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003129{
Dave Airlief453ba02008-11-07 14:05:41 -08003130 struct drm_device *dev = priv->minor->dev;
3131 struct drm_framebuffer *fb, *tfb;
3132
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003133 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003134 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003135
3136 mutex_lock(&dev->mode_config.fb_lock);
3137 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3138 __drm_framebuffer_unregister(dev, fb);
3139 mutex_unlock(&dev->mode_config.fb_lock);
3140
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003141 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003142
3143 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003144 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003145 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003146 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003147}
3148
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003149/**
3150 * drm_property_create - create a new property type
3151 * @dev: drm device
3152 * @flags: flags specifying the property type
3153 * @name: name of the property
3154 * @num_values: number of pre-defined values
3155 *
3156 * This creates a new generic drm property which can then be attached to a drm
3157 * object with drm_object_attach_property. The returned property object must be
3158 * freed with drm_property_destroy.
3159 *
3160 * Returns:
3161 * A pointer to the newly created property on success, NULL on failure.
3162 */
Dave Airlief453ba02008-11-07 14:05:41 -08003163struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3164 const char *name, int num_values)
3165{
3166 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003167 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003168
3169 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3170 if (!property)
3171 return NULL;
3172
Rob Clark98f75de2014-05-30 11:37:03 -04003173 property->dev = dev;
3174
Dave Airlief453ba02008-11-07 14:05:41 -08003175 if (num_values) {
3176 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3177 if (!property->values)
3178 goto fail;
3179 }
3180
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003181 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3182 if (ret)
3183 goto fail;
3184
Dave Airlief453ba02008-11-07 14:05:41 -08003185 property->flags = flags;
3186 property->num_values = num_values;
3187 INIT_LIST_HEAD(&property->enum_blob_list);
3188
Vinson Lee471dd2e2011-11-10 11:55:40 -08003189 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003190 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003191 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3192 }
Dave Airlief453ba02008-11-07 14:05:41 -08003193
3194 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003195
3196 WARN_ON(!drm_property_type_valid(property));
3197
Dave Airlief453ba02008-11-07 14:05:41 -08003198 return property;
3199fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003200 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003201 kfree(property);
3202 return NULL;
3203}
3204EXPORT_SYMBOL(drm_property_create);
3205
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003206/**
3207 * drm_property_create - create a new enumeration property type
3208 * @dev: drm device
3209 * @flags: flags specifying the property type
3210 * @name: name of the property
3211 * @props: enumeration lists with property values
3212 * @num_values: number of pre-defined values
3213 *
3214 * This creates a new generic drm property which can then be attached to a drm
3215 * object with drm_object_attach_property. The returned property object must be
3216 * freed with drm_property_destroy.
3217 *
3218 * Userspace is only allowed to set one of the predefined values for enumeration
3219 * properties.
3220 *
3221 * Returns:
3222 * A pointer to the newly created property on success, NULL on failure.
3223 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003224struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3225 const char *name,
3226 const struct drm_prop_enum_list *props,
3227 int num_values)
3228{
3229 struct drm_property *property;
3230 int i, ret;
3231
3232 flags |= DRM_MODE_PROP_ENUM;
3233
3234 property = drm_property_create(dev, flags, name, num_values);
3235 if (!property)
3236 return NULL;
3237
3238 for (i = 0; i < num_values; i++) {
3239 ret = drm_property_add_enum(property, i,
3240 props[i].type,
3241 props[i].name);
3242 if (ret) {
3243 drm_property_destroy(dev, property);
3244 return NULL;
3245 }
3246 }
3247
3248 return property;
3249}
3250EXPORT_SYMBOL(drm_property_create_enum);
3251
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003252/**
3253 * drm_property_create - create a new bitmask property type
3254 * @dev: drm device
3255 * @flags: flags specifying the property type
3256 * @name: name of the property
3257 * @props: enumeration lists with property bitflags
3258 * @num_values: number of pre-defined values
3259 *
3260 * This creates a new generic drm property which can then be attached to a drm
3261 * object with drm_object_attach_property. The returned property object must be
3262 * freed with drm_property_destroy.
3263 *
3264 * Compared to plain enumeration properties userspace is allowed to set any
3265 * or'ed together combination of the predefined property bitflag values
3266 *
3267 * Returns:
3268 * A pointer to the newly created property on success, NULL on failure.
3269 */
Rob Clark49e27542012-05-17 02:23:26 -06003270struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3271 int flags, const char *name,
3272 const struct drm_prop_enum_list *props,
3273 int num_values)
3274{
3275 struct drm_property *property;
3276 int i, ret;
3277
3278 flags |= DRM_MODE_PROP_BITMASK;
3279
3280 property = drm_property_create(dev, flags, name, num_values);
3281 if (!property)
3282 return NULL;
3283
3284 for (i = 0; i < num_values; i++) {
3285 ret = drm_property_add_enum(property, i,
3286 props[i].type,
3287 props[i].name);
3288 if (ret) {
3289 drm_property_destroy(dev, property);
3290 return NULL;
3291 }
3292 }
3293
3294 return property;
3295}
3296EXPORT_SYMBOL(drm_property_create_bitmask);
3297
Rob Clarkebc44cf2012-09-12 22:22:31 -05003298static struct drm_property *property_create_range(struct drm_device *dev,
3299 int flags, const char *name,
3300 uint64_t min, uint64_t max)
3301{
3302 struct drm_property *property;
3303
3304 property = drm_property_create(dev, flags, name, 2);
3305 if (!property)
3306 return NULL;
3307
3308 property->values[0] = min;
3309 property->values[1] = max;
3310
3311 return property;
3312}
3313
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003314/**
3315 * drm_property_create - create a new ranged property type
3316 * @dev: drm device
3317 * @flags: flags specifying the property type
3318 * @name: name of the property
3319 * @min: minimum value of the property
3320 * @max: maximum value of the property
3321 *
3322 * This creates a new generic drm property which can then be attached to a drm
3323 * object with drm_object_attach_property. The returned property object must be
3324 * freed with drm_property_destroy.
3325 *
3326 * Userspace is allowed to set any interger value in the (min, max) range
3327 * inclusive.
3328 *
3329 * Returns:
3330 * A pointer to the newly created property on success, NULL on failure.
3331 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003332struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3333 const char *name,
3334 uint64_t min, uint64_t max)
3335{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003336 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3337 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003338}
3339EXPORT_SYMBOL(drm_property_create_range);
3340
Rob Clarkebc44cf2012-09-12 22:22:31 -05003341struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3342 int flags, const char *name,
3343 int64_t min, int64_t max)
3344{
3345 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3346 name, I642U64(min), I642U64(max));
3347}
3348EXPORT_SYMBOL(drm_property_create_signed_range);
3349
Rob Clark98f75de2014-05-30 11:37:03 -04003350struct drm_property *drm_property_create_object(struct drm_device *dev,
3351 int flags, const char *name, uint32_t type)
3352{
3353 struct drm_property *property;
3354
3355 flags |= DRM_MODE_PROP_OBJECT;
3356
3357 property = drm_property_create(dev, flags, name, 1);
3358 if (!property)
3359 return NULL;
3360
3361 property->values[0] = type;
3362
3363 return property;
3364}
3365EXPORT_SYMBOL(drm_property_create_object);
3366
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003367/**
3368 * drm_property_add_enum - add a possible value to an enumeration property
3369 * @property: enumeration property to change
3370 * @index: index of the new enumeration
3371 * @value: value of the new enumeration
3372 * @name: symbolic name of the new enumeration
3373 *
3374 * This functions adds enumerations to a property.
3375 *
3376 * It's use is deprecated, drivers should use one of the more specific helpers
3377 * to directly create the property with all enumerations already attached.
3378 *
3379 * Returns:
3380 * Zero on success, error code on failure.
3381 */
Dave Airlief453ba02008-11-07 14:05:41 -08003382int drm_property_add_enum(struct drm_property *property, int index,
3383 uint64_t value, const char *name)
3384{
3385 struct drm_property_enum *prop_enum;
3386
Rob Clark5ea22f22014-05-30 11:34:01 -04003387 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3388 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003389 return -EINVAL;
3390
3391 /*
3392 * Bitmask enum properties have the additional constraint of values
3393 * from 0 to 63
3394 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003395 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3396 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003397 return -EINVAL;
3398
3399 if (!list_empty(&property->enum_blob_list)) {
3400 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3401 if (prop_enum->value == value) {
3402 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3403 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3404 return 0;
3405 }
3406 }
3407 }
3408
3409 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3410 if (!prop_enum)
3411 return -ENOMEM;
3412
3413 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3414 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3415 prop_enum->value = value;
3416
3417 property->values[index] = value;
3418 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3419 return 0;
3420}
3421EXPORT_SYMBOL(drm_property_add_enum);
3422
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003423/**
3424 * drm_property_destroy - destroy a drm property
3425 * @dev: drm device
3426 * @property: property to destry
3427 *
3428 * This function frees a property including any attached resources like
3429 * enumeration values.
3430 */
Dave Airlief453ba02008-11-07 14:05:41 -08003431void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3432{
3433 struct drm_property_enum *prop_enum, *pt;
3434
3435 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3436 list_del(&prop_enum->head);
3437 kfree(prop_enum);
3438 }
3439
3440 if (property->num_values)
3441 kfree(property->values);
3442 drm_mode_object_put(dev, &property->base);
3443 list_del(&property->head);
3444 kfree(property);
3445}
3446EXPORT_SYMBOL(drm_property_destroy);
3447
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003448/**
3449 * drm_object_attach_property - attach a property to a modeset object
3450 * @obj: drm modeset object
3451 * @property: property to attach
3452 * @init_val: initial value of the property
3453 *
3454 * This attaches the given property to the modeset object with the given initial
3455 * value. Currently this function cannot fail since the properties are stored in
3456 * a statically sized array.
3457 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003458void drm_object_attach_property(struct drm_mode_object *obj,
3459 struct drm_property *property,
3460 uint64_t init_val)
3461{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003462 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003463
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003464 if (count == DRM_OBJECT_MAX_PROPERTY) {
3465 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3466 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3467 "you see this message on the same object type.\n",
3468 obj->type);
3469 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003470 }
3471
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003472 obj->properties->ids[count] = property->base.id;
3473 obj->properties->values[count] = init_val;
3474 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003475}
3476EXPORT_SYMBOL(drm_object_attach_property);
3477
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003478/**
3479 * drm_object_property_set_value - set the value of a property
3480 * @obj: drm mode object to set property value for
3481 * @property: property to set
3482 * @val: value the property should be set to
3483 *
3484 * This functions sets a given property on a given object. This function only
3485 * changes the software state of the property, it does not call into the
3486 * driver's ->set_property callback.
3487 *
3488 * Returns:
3489 * Zero on success, error code on failure.
3490 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003491int drm_object_property_set_value(struct drm_mode_object *obj,
3492 struct drm_property *property, uint64_t val)
3493{
3494 int i;
3495
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003496 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003497 if (obj->properties->ids[i] == property->base.id) {
3498 obj->properties->values[i] = val;
3499 return 0;
3500 }
3501 }
3502
3503 return -EINVAL;
3504}
3505EXPORT_SYMBOL(drm_object_property_set_value);
3506
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003507/**
3508 * drm_object_property_get_value - retrieve the value of a property
3509 * @obj: drm mode object to get property value from
3510 * @property: property to retrieve
3511 * @val: storage for the property value
3512 *
3513 * This function retrieves the softare state of the given property for the given
3514 * property. Since there is no driver callback to retrieve the current property
3515 * value this might be out of sync with the hardware, depending upon the driver
3516 * and property.
3517 *
3518 * Returns:
3519 * Zero on success, error code on failure.
3520 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003521int drm_object_property_get_value(struct drm_mode_object *obj,
3522 struct drm_property *property, uint64_t *val)
3523{
3524 int i;
3525
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003526 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003527 if (obj->properties->ids[i] == property->base.id) {
3528 *val = obj->properties->values[i];
3529 return 0;
3530 }
3531 }
3532
3533 return -EINVAL;
3534}
3535EXPORT_SYMBOL(drm_object_property_get_value);
3536
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003537/**
3538 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3539 * @dev: DRM device
3540 * @data: ioctl data
3541 * @file_priv: DRM file info
3542 *
3543 * This function retrieves the current value for an connectors's property.
3544 *
3545 * Called by the user via ioctl.
3546 *
3547 * Returns:
3548 * Zero on success, errno on failure.
3549 */
Dave Airlief453ba02008-11-07 14:05:41 -08003550int drm_mode_getproperty_ioctl(struct drm_device *dev,
3551 void *data, struct drm_file *file_priv)
3552{
Dave Airlief453ba02008-11-07 14:05:41 -08003553 struct drm_mode_get_property *out_resp = data;
3554 struct drm_property *property;
3555 int enum_count = 0;
3556 int blob_count = 0;
3557 int value_count = 0;
3558 int ret = 0, i;
3559 int copied;
3560 struct drm_property_enum *prop_enum;
3561 struct drm_mode_property_enum __user *enum_ptr;
3562 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003563 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003564 uint64_t __user *values_ptr;
3565 uint32_t __user *blob_length_ptr;
3566
Dave Airliefb3b06c2011-02-08 13:55:21 +10003567 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3568 return -EINVAL;
3569
Daniel Vetter84849902012-12-02 00:28:11 +01003570 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003571 property = drm_property_find(dev, out_resp->prop_id);
3572 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003573 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003574 goto done;
3575 }
Dave Airlief453ba02008-11-07 14:05:41 -08003576
Rob Clark5ea22f22014-05-30 11:34:01 -04003577 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3578 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003579 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3580 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003581 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003582 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3583 blob_count++;
3584 }
3585
3586 value_count = property->num_values;
3587
3588 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3589 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3590 out_resp->flags = property->flags;
3591
3592 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003593 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003594 for (i = 0; i < value_count; i++) {
3595 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3596 ret = -EFAULT;
3597 goto done;
3598 }
3599 }
3600 }
3601 out_resp->count_values = value_count;
3602
Rob Clark5ea22f22014-05-30 11:34:01 -04003603 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3604 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003605 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3606 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003607 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003608 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3609
3610 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3611 ret = -EFAULT;
3612 goto done;
3613 }
3614
3615 if (copy_to_user(&enum_ptr[copied].name,
3616 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3617 ret = -EFAULT;
3618 goto done;
3619 }
3620 copied++;
3621 }
3622 }
3623 out_resp->count_enum_blobs = enum_count;
3624 }
3625
Rob Clark5ea22f22014-05-30 11:34:01 -04003626 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003627 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3628 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003629 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3630 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003631
3632 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3633 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3634 ret = -EFAULT;
3635 goto done;
3636 }
3637
3638 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3639 ret = -EFAULT;
3640 goto done;
3641 }
3642
3643 copied++;
3644 }
3645 }
3646 out_resp->count_enum_blobs = blob_count;
3647 }
3648done:
Daniel Vetter84849902012-12-02 00:28:11 +01003649 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003650 return ret;
3651}
3652
3653static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3654 void *data)
3655{
3656 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003657 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003658
3659 if (!length || !data)
3660 return NULL;
3661
3662 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3663 if (!blob)
3664 return NULL;
3665
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003666 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3667 if (ret) {
3668 kfree(blob);
3669 return NULL;
3670 }
3671
Dave Airlief453ba02008-11-07 14:05:41 -08003672 blob->length = length;
3673
3674 memcpy(blob->data, data, length);
3675
Dave Airlief453ba02008-11-07 14:05:41 -08003676 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3677 return blob;
3678}
3679
3680static void drm_property_destroy_blob(struct drm_device *dev,
3681 struct drm_property_blob *blob)
3682{
3683 drm_mode_object_put(dev, &blob->base);
3684 list_del(&blob->head);
3685 kfree(blob);
3686}
3687
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003688/**
3689 * drm_mode_getblob_ioctl - get the contents of a blob property value
3690 * @dev: DRM device
3691 * @data: ioctl data
3692 * @file_priv: DRM file info
3693 *
3694 * This function retrieves the contents of a blob property. The value stored in
3695 * an object's blob property is just a normal modeset object id.
3696 *
3697 * Called by the user via ioctl.
3698 *
3699 * Returns:
3700 * Zero on success, errno on failure.
3701 */
Dave Airlief453ba02008-11-07 14:05:41 -08003702int drm_mode_getblob_ioctl(struct drm_device *dev,
3703 void *data, struct drm_file *file_priv)
3704{
Dave Airlief453ba02008-11-07 14:05:41 -08003705 struct drm_mode_get_blob *out_resp = data;
3706 struct drm_property_blob *blob;
3707 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003708 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003709
Dave Airliefb3b06c2011-02-08 13:55:21 +10003710 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3711 return -EINVAL;
3712
Daniel Vetter84849902012-12-02 00:28:11 +01003713 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003714 blob = drm_property_blob_find(dev, out_resp->blob_id);
3715 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003716 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003717 goto done;
3718 }
Dave Airlief453ba02008-11-07 14:05:41 -08003719
3720 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003721 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003722 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3723 ret = -EFAULT;
3724 goto done;
3725 }
3726 }
3727 out_resp->length = blob->length;
3728
3729done:
Daniel Vetter84849902012-12-02 00:28:11 +01003730 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003731 return ret;
3732}
3733
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003734/**
3735 * drm_mode_connector_update_edid_property - update the edid property of a connector
3736 * @connector: drm connector
3737 * @edid: new value of the edid property
3738 *
3739 * This function creates a new blob modeset object and assigns its id to the
3740 * connector's edid property.
3741 *
3742 * Returns:
3743 * Zero on success, errno on failure.
3744 */
Dave Airlief453ba02008-11-07 14:05:41 -08003745int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3746 struct edid *edid)
3747{
3748 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003749 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003750
3751 if (connector->edid_blob_ptr)
3752 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3753
3754 /* Delete edid, when there is none. */
3755 if (!edid) {
3756 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003757 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003758 return ret;
3759 }
3760
Adam Jackson7466f4c2010-03-29 21:43:23 +00003761 size = EDID_LENGTH * (1 + edid->extensions);
3762 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3763 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003764 if (!connector->edid_blob_ptr)
3765 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003766
Rob Clark58495562012-10-11 20:50:56 -05003767 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003768 dev->mode_config.edid_property,
3769 connector->edid_blob_ptr->base.id);
3770
3771 return ret;
3772}
3773EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3774
Paulo Zanoni26a34812012-05-15 18:08:59 -03003775static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003776 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003777{
3778 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3779 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003780
3781 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003782 if (value < property->values[0] || value > property->values[1])
3783 return false;
3784 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003785 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3786 int64_t svalue = U642I64(value);
3787 if (svalue < U642I64(property->values[0]) ||
3788 svalue > U642I64(property->values[1]))
3789 return false;
3790 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003791 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003792 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003793 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003794 for (i = 0; i < property->num_values; i++)
3795 valid_mask |= (1ULL << property->values[i]);
3796 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003797 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003798 /* Only the driver knows */
3799 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003800 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3801 struct drm_mode_object *obj;
3802 /* a zero value for an object property translates to null: */
3803 if (value == 0)
3804 return true;
3805 /*
3806 * NOTE: use _object_find() directly to bypass restriction on
3807 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3808 * object this could race against object finalization, so it
3809 * simply tells us that the object *was* valid. Which is good
3810 * enough.
3811 */
3812 obj = _object_find(property->dev, value, property->values[0]);
3813 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003814 } else {
3815 int i;
3816 for (i = 0; i < property->num_values; i++)
3817 if (property->values[i] == value)
3818 return true;
3819 return false;
3820 }
3821}
3822
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003823/**
3824 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3825 * @dev: DRM device
3826 * @data: ioctl data
3827 * @file_priv: DRM file info
3828 *
3829 * This function sets the current value for a connectors's property. It also
3830 * calls into a driver's ->set_property callback to update the hardware state
3831 *
3832 * Called by the user via ioctl.
3833 *
3834 * Returns:
3835 * Zero on success, errno on failure.
3836 */
Dave Airlief453ba02008-11-07 14:05:41 -08003837int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3838 void *data, struct drm_file *file_priv)
3839{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003840 struct drm_mode_connector_set_property *conn_set_prop = data;
3841 struct drm_mode_obj_set_property obj_set_prop = {
3842 .value = conn_set_prop->value,
3843 .prop_id = conn_set_prop->prop_id,
3844 .obj_id = conn_set_prop->connector_id,
3845 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3846 };
Dave Airlief453ba02008-11-07 14:05:41 -08003847
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003848 /* It does all the locking and checking we need */
3849 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003850}
3851
Paulo Zanonic5431882012-05-15 18:09:02 -03003852static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3853 struct drm_property *property,
3854 uint64_t value)
3855{
3856 int ret = -EINVAL;
3857 struct drm_connector *connector = obj_to_connector(obj);
3858
3859 /* Do DPMS ourselves */
3860 if (property == connector->dev->mode_config.dpms_property) {
3861 if (connector->funcs->dpms)
3862 (*connector->funcs->dpms)(connector, (int)value);
3863 ret = 0;
3864 } else if (connector->funcs->set_property)
3865 ret = connector->funcs->set_property(connector, property, value);
3866
3867 /* store the property value if successful */
3868 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003869 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003870 return ret;
3871}
3872
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003873static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3874 struct drm_property *property,
3875 uint64_t value)
3876{
3877 int ret = -EINVAL;
3878 struct drm_crtc *crtc = obj_to_crtc(obj);
3879
3880 if (crtc->funcs->set_property)
3881 ret = crtc->funcs->set_property(crtc, property, value);
3882 if (!ret)
3883 drm_object_property_set_value(obj, property, value);
3884
3885 return ret;
3886}
3887
Rob Clark4d939142012-05-17 02:23:27 -06003888static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3889 struct drm_property *property,
3890 uint64_t value)
3891{
3892 int ret = -EINVAL;
3893 struct drm_plane *plane = obj_to_plane(obj);
3894
3895 if (plane->funcs->set_property)
3896 ret = plane->funcs->set_property(plane, property, value);
3897 if (!ret)
3898 drm_object_property_set_value(obj, property, value);
3899
3900 return ret;
3901}
3902
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003903/**
3904 * drm_mode_getproperty_ioctl - get the current value of a object's property
3905 * @dev: DRM device
3906 * @data: ioctl data
3907 * @file_priv: DRM file info
3908 *
3909 * This function retrieves the current value for an object's property. Compared
3910 * to the connector specific ioctl this one is extended to also work on crtc and
3911 * plane objects.
3912 *
3913 * Called by the user via ioctl.
3914 *
3915 * Returns:
3916 * Zero on success, errno on failure.
3917 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003918int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3919 struct drm_file *file_priv)
3920{
3921 struct drm_mode_obj_get_properties *arg = data;
3922 struct drm_mode_object *obj;
3923 int ret = 0;
3924 int i;
3925 int copied = 0;
3926 int props_count = 0;
3927 uint32_t __user *props_ptr;
3928 uint64_t __user *prop_values_ptr;
3929
3930 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3931 return -EINVAL;
3932
Daniel Vetter84849902012-12-02 00:28:11 +01003933 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003934
3935 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3936 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003937 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003938 goto out;
3939 }
3940 if (!obj->properties) {
3941 ret = -EINVAL;
3942 goto out;
3943 }
3944
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003945 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003946
3947 /* This ioctl is called twice, once to determine how much space is
3948 * needed, and the 2nd time to fill it. */
3949 if ((arg->count_props >= props_count) && props_count) {
3950 copied = 0;
3951 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3952 prop_values_ptr = (uint64_t __user *)(unsigned long)
3953 (arg->prop_values_ptr);
3954 for (i = 0; i < props_count; i++) {
3955 if (put_user(obj->properties->ids[i],
3956 props_ptr + copied)) {
3957 ret = -EFAULT;
3958 goto out;
3959 }
3960 if (put_user(obj->properties->values[i],
3961 prop_values_ptr + copied)) {
3962 ret = -EFAULT;
3963 goto out;
3964 }
3965 copied++;
3966 }
3967 }
3968 arg->count_props = props_count;
3969out:
Daniel Vetter84849902012-12-02 00:28:11 +01003970 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003971 return ret;
3972}
3973
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003974/**
3975 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3976 * @dev: DRM device
3977 * @data: ioctl data
3978 * @file_priv: DRM file info
3979 *
3980 * This function sets the current value for an object's property. It also calls
3981 * into a driver's ->set_property callback to update the hardware state.
3982 * Compared to the connector specific ioctl this one is extended to also work on
3983 * crtc and plane objects.
3984 *
3985 * Called by the user via ioctl.
3986 *
3987 * Returns:
3988 * Zero on success, errno on failure.
3989 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003990int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3991 struct drm_file *file_priv)
3992{
3993 struct drm_mode_obj_set_property *arg = data;
3994 struct drm_mode_object *arg_obj;
3995 struct drm_mode_object *prop_obj;
3996 struct drm_property *property;
3997 int ret = -EINVAL;
3998 int i;
3999
4000 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4001 return -EINVAL;
4002
Daniel Vetter84849902012-12-02 00:28:11 +01004003 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004004
4005 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004006 if (!arg_obj) {
4007 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004008 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004009 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004010 if (!arg_obj->properties)
4011 goto out;
4012
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004013 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004014 if (arg_obj->properties->ids[i] == arg->prop_id)
4015 break;
4016
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004017 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004018 goto out;
4019
4020 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4021 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004022 if (!prop_obj) {
4023 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004024 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004025 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004026 property = obj_to_property(prop_obj);
4027
4028 if (!drm_property_change_is_valid(property, arg->value))
4029 goto out;
4030
4031 switch (arg_obj->type) {
4032 case DRM_MODE_OBJECT_CONNECTOR:
4033 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4034 arg->value);
4035 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004036 case DRM_MODE_OBJECT_CRTC:
4037 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4038 break;
Rob Clark4d939142012-05-17 02:23:27 -06004039 case DRM_MODE_OBJECT_PLANE:
4040 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4041 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004042 }
4043
4044out:
Daniel Vetter84849902012-12-02 00:28:11 +01004045 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004046 return ret;
4047}
4048
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004049/**
4050 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4051 * @connector: connector to attach
4052 * @encoder: encoder to attach @connector to
4053 *
4054 * This function links up a connector to an encoder. Note that the routing
4055 * restrictions between encoders and crtcs are exposed to userspace through the
4056 * possible_clones and possible_crtcs bitmasks.
4057 *
4058 * Returns:
4059 * Zero on success, errno on failure.
4060 */
Dave Airlief453ba02008-11-07 14:05:41 -08004061int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4062 struct drm_encoder *encoder)
4063{
4064 int i;
4065
4066 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4067 if (connector->encoder_ids[i] == 0) {
4068 connector->encoder_ids[i] = encoder->base.id;
4069 return 0;
4070 }
4071 }
4072 return -ENOMEM;
4073}
4074EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4075
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004076/**
4077 * drm_mode_crtc_set_gamma_size - set the gamma table size
4078 * @crtc: CRTC to set the gamma table size for
4079 * @gamma_size: size of the gamma table
4080 *
4081 * Drivers which support gamma tables should set this to the supported gamma
4082 * table size when initializing the CRTC. Currently the drm core only supports a
4083 * fixed gamma table size.
4084 *
4085 * Returns:
4086 * Zero on success, errno on failure.
4087 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004088int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004089 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004090{
4091 crtc->gamma_size = gamma_size;
4092
4093 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4094 if (!crtc->gamma_store) {
4095 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004096 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004097 }
4098
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004099 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004100}
4101EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4102
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004103/**
4104 * drm_mode_gamma_set_ioctl - set the gamma table
4105 * @dev: DRM device
4106 * @data: ioctl data
4107 * @file_priv: DRM file info
4108 *
4109 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4110 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4111 *
4112 * Called by the user via ioctl.
4113 *
4114 * Returns:
4115 * Zero on success, errno on failure.
4116 */
Dave Airlief453ba02008-11-07 14:05:41 -08004117int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4118 void *data, struct drm_file *file_priv)
4119{
4120 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004121 struct drm_crtc *crtc;
4122 void *r_base, *g_base, *b_base;
4123 int size;
4124 int ret = 0;
4125
Dave Airliefb3b06c2011-02-08 13:55:21 +10004126 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4127 return -EINVAL;
4128
Daniel Vetter84849902012-12-02 00:28:11 +01004129 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004130 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4131 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004132 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004133 goto out;
4134 }
Dave Airlief453ba02008-11-07 14:05:41 -08004135
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004136 if (crtc->funcs->gamma_set == NULL) {
4137 ret = -ENOSYS;
4138 goto out;
4139 }
4140
Dave Airlief453ba02008-11-07 14:05:41 -08004141 /* memcpy into gamma store */
4142 if (crtc_lut->gamma_size != crtc->gamma_size) {
4143 ret = -EINVAL;
4144 goto out;
4145 }
4146
4147 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4148 r_base = crtc->gamma_store;
4149 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4150 ret = -EFAULT;
4151 goto out;
4152 }
4153
4154 g_base = r_base + size;
4155 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4156 ret = -EFAULT;
4157 goto out;
4158 }
4159
4160 b_base = g_base + size;
4161 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4162 ret = -EFAULT;
4163 goto out;
4164 }
4165
James Simmons72034252010-08-03 01:33:19 +01004166 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004167
4168out:
Daniel Vetter84849902012-12-02 00:28:11 +01004169 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004170 return ret;
4171
4172}
4173
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004174/**
4175 * drm_mode_gamma_get_ioctl - get the gamma table
4176 * @dev: DRM device
4177 * @data: ioctl data
4178 * @file_priv: DRM file info
4179 *
4180 * Copy the current gamma table into the storage provided. This also provides
4181 * the gamma table size the driver expects, which can be used to size the
4182 * allocated storage.
4183 *
4184 * Called by the user via ioctl.
4185 *
4186 * Returns:
4187 * Zero on success, errno on failure.
4188 */
Dave Airlief453ba02008-11-07 14:05:41 -08004189int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4190 void *data, struct drm_file *file_priv)
4191{
4192 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004193 struct drm_crtc *crtc;
4194 void *r_base, *g_base, *b_base;
4195 int size;
4196 int ret = 0;
4197
Dave Airliefb3b06c2011-02-08 13:55:21 +10004198 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4199 return -EINVAL;
4200
Daniel Vetter84849902012-12-02 00:28:11 +01004201 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004202 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4203 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004204 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004205 goto out;
4206 }
Dave Airlief453ba02008-11-07 14:05:41 -08004207
4208 /* memcpy into gamma store */
4209 if (crtc_lut->gamma_size != crtc->gamma_size) {
4210 ret = -EINVAL;
4211 goto out;
4212 }
4213
4214 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4215 r_base = crtc->gamma_store;
4216 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4217 ret = -EFAULT;
4218 goto out;
4219 }
4220
4221 g_base = r_base + size;
4222 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4223 ret = -EFAULT;
4224 goto out;
4225 }
4226
4227 b_base = g_base + size;
4228 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4229 ret = -EFAULT;
4230 goto out;
4231 }
4232out:
Daniel Vetter84849902012-12-02 00:28:11 +01004233 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004234 return ret;
4235}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004236
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004237/**
4238 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4239 * @dev: DRM device
4240 * @data: ioctl data
4241 * @file_priv: DRM file info
4242 *
4243 * This schedules an asynchronous update on a given CRTC, called page flip.
4244 * Optionally a drm event is generated to signal the completion of the event.
4245 * Generic drivers cannot assume that a pageflip with changed framebuffer
4246 * properties (including driver specific metadata like tiling layout) will work,
4247 * but some drivers support e.g. pixel format changes through the pageflip
4248 * ioctl.
4249 *
4250 * Called by the user via ioctl.
4251 *
4252 * Returns:
4253 * Zero on success, errno on failure.
4254 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004255int drm_mode_page_flip_ioctl(struct drm_device *dev,
4256 void *data, struct drm_file *file_priv)
4257{
4258 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004259 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004260 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004261 struct drm_pending_vblank_event *e = NULL;
4262 unsigned long flags;
4263 int ret = -EINVAL;
4264
4265 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4266 page_flip->reserved != 0)
4267 return -EINVAL;
4268
Keith Packard62f21042013-07-22 18:50:00 -07004269 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4270 return -EINVAL;
4271
Rob Clarka2b34e22013-10-05 16:36:52 -04004272 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4273 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004274 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004275
Rob Clark51fd3712013-11-19 12:10:12 -05004276 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004277 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004278 /* The framebuffer is currently unbound, presumably
4279 * due to a hotplug event, that userspace has not
4280 * yet discovered.
4281 */
4282 ret = -EBUSY;
4283 goto out;
4284 }
4285
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004286 if (crtc->funcs->page_flip == NULL)
4287 goto out;
4288
Daniel Vetter786b99e2012-12-02 21:53:40 +01004289 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004290 if (!fb) {
4291 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004292 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004293 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004294
Damien Lespiauc11e9282013-09-25 16:45:30 +01004295 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4296 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004297 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004298
Matt Roperf4510a22014-04-01 15:22:40 -07004299 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004300 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4301 ret = -EINVAL;
4302 goto out;
4303 }
4304
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004305 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4306 ret = -ENOMEM;
4307 spin_lock_irqsave(&dev->event_lock, flags);
4308 if (file_priv->event_space < sizeof e->event) {
4309 spin_unlock_irqrestore(&dev->event_lock, flags);
4310 goto out;
4311 }
4312 file_priv->event_space -= sizeof e->event;
4313 spin_unlock_irqrestore(&dev->event_lock, flags);
4314
4315 e = kzalloc(sizeof *e, GFP_KERNEL);
4316 if (e == NULL) {
4317 spin_lock_irqsave(&dev->event_lock, flags);
4318 file_priv->event_space += sizeof e->event;
4319 spin_unlock_irqrestore(&dev->event_lock, flags);
4320 goto out;
4321 }
4322
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004323 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004324 e->event.base.length = sizeof e->event;
4325 e->event.user_data = page_flip->user_data;
4326 e->base.event = &e->event.base;
4327 e->base.file_priv = file_priv;
4328 e->base.destroy =
4329 (void (*) (struct drm_pending_event *)) kfree;
4330 }
4331
Matt Roperf4510a22014-04-01 15:22:40 -07004332 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004333 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004334 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004335 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4336 spin_lock_irqsave(&dev->event_lock, flags);
4337 file_priv->event_space += sizeof e->event;
4338 spin_unlock_irqrestore(&dev->event_lock, flags);
4339 kfree(e);
4340 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004341 /* Keep the old fb, don't unref it. */
4342 old_fb = NULL;
4343 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004344 /*
4345 * Warn if the driver hasn't properly updated the crtc->fb
4346 * field to reflect that the new framebuffer is now used.
4347 * Failing to do so will screw with the reference counting
4348 * on framebuffers.
4349 */
Matt Roperf4510a22014-04-01 15:22:40 -07004350 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004351 /* Unref only the old framebuffer. */
4352 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004353 }
4354
4355out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004356 if (fb)
4357 drm_framebuffer_unreference(fb);
4358 if (old_fb)
4359 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004360 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004361
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004362 return ret;
4363}
Chris Wilsoneb033552011-01-24 15:11:08 +00004364
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004365/**
4366 * drm_mode_config_reset - call ->reset callbacks
4367 * @dev: drm device
4368 *
4369 * This functions calls all the crtc's, encoder's and connector's ->reset
4370 * callback. Drivers can use this in e.g. their driver load or resume code to
4371 * reset hardware and software state.
4372 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004373void drm_mode_config_reset(struct drm_device *dev)
4374{
4375 struct drm_crtc *crtc;
4376 struct drm_encoder *encoder;
4377 struct drm_connector *connector;
4378
4379 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4380 if (crtc->funcs->reset)
4381 crtc->funcs->reset(crtc);
4382
4383 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4384 if (encoder->funcs->reset)
4385 encoder->funcs->reset(encoder);
4386
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004387 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4388 connector->status = connector_status_unknown;
4389
Chris Wilsoneb033552011-01-24 15:11:08 +00004390 if (connector->funcs->reset)
4391 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004392 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004393}
4394EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004395
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004396/**
4397 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4398 * @dev: DRM device
4399 * @data: ioctl data
4400 * @file_priv: DRM file info
4401 *
4402 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4403 * TTM or something else entirely) and returns the resulting buffer handle. This
4404 * handle can then be wrapped up into a framebuffer modeset object.
4405 *
4406 * Note that userspace is not allowed to use such objects for render
4407 * acceleration - drivers must create their own private ioctls for such a use
4408 * case.
4409 *
4410 * Called by the user via ioctl.
4411 *
4412 * Returns:
4413 * Zero on success, errno on failure.
4414 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004415int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4416 void *data, struct drm_file *file_priv)
4417{
4418 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004419 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004420
4421 if (!dev->driver->dumb_create)
4422 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004423 if (!args->width || !args->height || !args->bpp)
4424 return -EINVAL;
4425
4426 /* overflow checks for 32bit size calculations */
4427 cpp = DIV_ROUND_UP(args->bpp, 8);
4428 if (cpp > 0xffffffffU / args->width)
4429 return -EINVAL;
4430 stride = cpp * args->width;
4431 if (args->height > 0xffffffffU / stride)
4432 return -EINVAL;
4433
4434 /* test for wrap-around */
4435 size = args->height * stride;
4436 if (PAGE_ALIGN(size) == 0)
4437 return -EINVAL;
4438
Dave Airlieff72145b2011-02-07 12:16:14 +10004439 return dev->driver->dumb_create(file_priv, dev, args);
4440}
4441
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004442/**
4443 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4444 * @dev: DRM device
4445 * @data: ioctl data
4446 * @file_priv: DRM file info
4447 *
4448 * Allocate an offset in the drm device node's address space to be able to
4449 * memory map a dumb buffer.
4450 *
4451 * Called by the user via ioctl.
4452 *
4453 * Returns:
4454 * Zero on success, errno on failure.
4455 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004456int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4457 void *data, struct drm_file *file_priv)
4458{
4459 struct drm_mode_map_dumb *args = data;
4460
4461 /* call driver ioctl to get mmap offset */
4462 if (!dev->driver->dumb_map_offset)
4463 return -ENOSYS;
4464
4465 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4466}
4467
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004468/**
4469 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4470 * @dev: DRM device
4471 * @data: ioctl data
4472 * @file_priv: DRM file info
4473 *
4474 * This destroys the userspace handle for the given dumb backing storage buffer.
4475 * Since buffer objects must be reference counted in the kernel a buffer object
4476 * won't be immediately freed if a framebuffer modeset object still uses it.
4477 *
4478 * Called by the user via ioctl.
4479 *
4480 * Returns:
4481 * Zero on success, errno on failure.
4482 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004483int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4484 void *data, struct drm_file *file_priv)
4485{
4486 struct drm_mode_destroy_dumb *args = data;
4487
4488 if (!dev->driver->dumb_destroy)
4489 return -ENOSYS;
4490
4491 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4492}
Dave Airlie248dbc22011-11-29 20:02:54 +00004493
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004494/**
4495 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4496 * @format: pixel format (DRM_FORMAT_*)
4497 * @depth: storage for the depth value
4498 * @bpp: storage for the bpp value
4499 *
4500 * This only supports RGB formats here for compat with code that doesn't use
4501 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004502 */
4503void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4504 int *bpp)
4505{
4506 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004507 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004508 case DRM_FORMAT_RGB332:
4509 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004510 *depth = 8;
4511 *bpp = 8;
4512 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004513 case DRM_FORMAT_XRGB1555:
4514 case DRM_FORMAT_XBGR1555:
4515 case DRM_FORMAT_RGBX5551:
4516 case DRM_FORMAT_BGRX5551:
4517 case DRM_FORMAT_ARGB1555:
4518 case DRM_FORMAT_ABGR1555:
4519 case DRM_FORMAT_RGBA5551:
4520 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004521 *depth = 15;
4522 *bpp = 16;
4523 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004524 case DRM_FORMAT_RGB565:
4525 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004526 *depth = 16;
4527 *bpp = 16;
4528 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004529 case DRM_FORMAT_RGB888:
4530 case DRM_FORMAT_BGR888:
4531 *depth = 24;
4532 *bpp = 24;
4533 break;
4534 case DRM_FORMAT_XRGB8888:
4535 case DRM_FORMAT_XBGR8888:
4536 case DRM_FORMAT_RGBX8888:
4537 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004538 *depth = 24;
4539 *bpp = 32;
4540 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004541 case DRM_FORMAT_XRGB2101010:
4542 case DRM_FORMAT_XBGR2101010:
4543 case DRM_FORMAT_RGBX1010102:
4544 case DRM_FORMAT_BGRX1010102:
4545 case DRM_FORMAT_ARGB2101010:
4546 case DRM_FORMAT_ABGR2101010:
4547 case DRM_FORMAT_RGBA1010102:
4548 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004549 *depth = 30;
4550 *bpp = 32;
4551 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004552 case DRM_FORMAT_ARGB8888:
4553 case DRM_FORMAT_ABGR8888:
4554 case DRM_FORMAT_RGBA8888:
4555 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004556 *depth = 32;
4557 *bpp = 32;
4558 break;
4559 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004560 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4561 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004562 *depth = 0;
4563 *bpp = 0;
4564 break;
4565 }
4566}
4567EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004568
4569/**
4570 * drm_format_num_planes - get the number of planes for format
4571 * @format: pixel format (DRM_FORMAT_*)
4572 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004573 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004574 * The number of planes used by the specified pixel format.
4575 */
4576int drm_format_num_planes(uint32_t format)
4577{
4578 switch (format) {
4579 case DRM_FORMAT_YUV410:
4580 case DRM_FORMAT_YVU410:
4581 case DRM_FORMAT_YUV411:
4582 case DRM_FORMAT_YVU411:
4583 case DRM_FORMAT_YUV420:
4584 case DRM_FORMAT_YVU420:
4585 case DRM_FORMAT_YUV422:
4586 case DRM_FORMAT_YVU422:
4587 case DRM_FORMAT_YUV444:
4588 case DRM_FORMAT_YVU444:
4589 return 3;
4590 case DRM_FORMAT_NV12:
4591 case DRM_FORMAT_NV21:
4592 case DRM_FORMAT_NV16:
4593 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004594 case DRM_FORMAT_NV24:
4595 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004596 return 2;
4597 default:
4598 return 1;
4599 }
4600}
4601EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004602
4603/**
4604 * drm_format_plane_cpp - determine the bytes per pixel value
4605 * @format: pixel format (DRM_FORMAT_*)
4606 * @plane: plane index
4607 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004608 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004609 * The bytes per pixel value for the specified plane.
4610 */
4611int drm_format_plane_cpp(uint32_t format, int plane)
4612{
4613 unsigned int depth;
4614 int bpp;
4615
4616 if (plane >= drm_format_num_planes(format))
4617 return 0;
4618
4619 switch (format) {
4620 case DRM_FORMAT_YUYV:
4621 case DRM_FORMAT_YVYU:
4622 case DRM_FORMAT_UYVY:
4623 case DRM_FORMAT_VYUY:
4624 return 2;
4625 case DRM_FORMAT_NV12:
4626 case DRM_FORMAT_NV21:
4627 case DRM_FORMAT_NV16:
4628 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004629 case DRM_FORMAT_NV24:
4630 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004631 return plane ? 2 : 1;
4632 case DRM_FORMAT_YUV410:
4633 case DRM_FORMAT_YVU410:
4634 case DRM_FORMAT_YUV411:
4635 case DRM_FORMAT_YVU411:
4636 case DRM_FORMAT_YUV420:
4637 case DRM_FORMAT_YVU420:
4638 case DRM_FORMAT_YUV422:
4639 case DRM_FORMAT_YVU422:
4640 case DRM_FORMAT_YUV444:
4641 case DRM_FORMAT_YVU444:
4642 return 1;
4643 default:
4644 drm_fb_get_bpp_depth(format, &depth, &bpp);
4645 return bpp >> 3;
4646 }
4647}
4648EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004649
4650/**
4651 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4652 * @format: pixel format (DRM_FORMAT_*)
4653 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004654 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004655 * The horizontal chroma subsampling factor for the
4656 * specified pixel format.
4657 */
4658int drm_format_horz_chroma_subsampling(uint32_t format)
4659{
4660 switch (format) {
4661 case DRM_FORMAT_YUV411:
4662 case DRM_FORMAT_YVU411:
4663 case DRM_FORMAT_YUV410:
4664 case DRM_FORMAT_YVU410:
4665 return 4;
4666 case DRM_FORMAT_YUYV:
4667 case DRM_FORMAT_YVYU:
4668 case DRM_FORMAT_UYVY:
4669 case DRM_FORMAT_VYUY:
4670 case DRM_FORMAT_NV12:
4671 case DRM_FORMAT_NV21:
4672 case DRM_FORMAT_NV16:
4673 case DRM_FORMAT_NV61:
4674 case DRM_FORMAT_YUV422:
4675 case DRM_FORMAT_YVU422:
4676 case DRM_FORMAT_YUV420:
4677 case DRM_FORMAT_YVU420:
4678 return 2;
4679 default:
4680 return 1;
4681 }
4682}
4683EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4684
4685/**
4686 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4687 * @format: pixel format (DRM_FORMAT_*)
4688 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004689 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004690 * The vertical chroma subsampling factor for the
4691 * specified pixel format.
4692 */
4693int drm_format_vert_chroma_subsampling(uint32_t format)
4694{
4695 switch (format) {
4696 case DRM_FORMAT_YUV410:
4697 case DRM_FORMAT_YVU410:
4698 return 4;
4699 case DRM_FORMAT_YUV420:
4700 case DRM_FORMAT_YVU420:
4701 case DRM_FORMAT_NV12:
4702 case DRM_FORMAT_NV21:
4703 return 2;
4704 default:
4705 return 1;
4706 }
4707}
4708EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004709
4710/**
4711 * drm_mode_config_init - initialize DRM mode_configuration structure
4712 * @dev: DRM device
4713 *
4714 * Initialize @dev's mode_config structure, used for tracking the graphics
4715 * configuration of @dev.
4716 *
4717 * Since this initializes the modeset locks, no locking is possible. Which is no
4718 * problem, since this should happen single threaded at init time. It is the
4719 * driver's problem to ensure this guarantee.
4720 *
4721 */
4722void drm_mode_config_init(struct drm_device *dev)
4723{
4724 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004725 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004726 mutex_init(&dev->mode_config.idr_mutex);
4727 mutex_init(&dev->mode_config.fb_lock);
4728 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4729 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4730 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004731 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004732 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4733 INIT_LIST_HEAD(&dev->mode_config.property_list);
4734 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4735 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4736 idr_init(&dev->mode_config.crtc_idr);
4737
4738 drm_modeset_lock_all(dev);
4739 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004740 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004741 drm_modeset_unlock_all(dev);
4742
4743 /* Just to be sure */
4744 dev->mode_config.num_fb = 0;
4745 dev->mode_config.num_connector = 0;
4746 dev->mode_config.num_crtc = 0;
4747 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004748 dev->mode_config.num_overlay_plane = 0;
4749 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004750}
4751EXPORT_SYMBOL(drm_mode_config_init);
4752
4753/**
4754 * drm_mode_config_cleanup - free up DRM mode_config info
4755 * @dev: DRM device
4756 *
4757 * Free up all the connectors and CRTCs associated with this DRM device, then
4758 * free up the framebuffers and associated buffer objects.
4759 *
4760 * Note that since this /should/ happen single-threaded at driver/device
4761 * teardown time, no locking is required. It's the driver's job to ensure that
4762 * this guarantee actually holds true.
4763 *
4764 * FIXME: cleanup any dangling user buffer objects too
4765 */
4766void drm_mode_config_cleanup(struct drm_device *dev)
4767{
4768 struct drm_connector *connector, *ot;
4769 struct drm_crtc *crtc, *ct;
4770 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004771 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004772 struct drm_framebuffer *fb, *fbt;
4773 struct drm_property *property, *pt;
4774 struct drm_property_blob *blob, *bt;
4775 struct drm_plane *plane, *plt;
4776
4777 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4778 head) {
4779 encoder->funcs->destroy(encoder);
4780 }
4781
Sean Paul3b336ec2013-08-14 16:47:37 -04004782 list_for_each_entry_safe(bridge, brt,
4783 &dev->mode_config.bridge_list, head) {
4784 bridge->funcs->destroy(bridge);
4785 }
4786
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004787 list_for_each_entry_safe(connector, ot,
4788 &dev->mode_config.connector_list, head) {
4789 connector->funcs->destroy(connector);
4790 }
4791
4792 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4793 head) {
4794 drm_property_destroy(dev, property);
4795 }
4796
4797 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4798 head) {
4799 drm_property_destroy_blob(dev, blob);
4800 }
4801
4802 /*
4803 * Single-threaded teardown context, so it's not required to grab the
4804 * fb_lock to protect against concurrent fb_list access. Contrary, it
4805 * would actually deadlock with the drm_framebuffer_cleanup function.
4806 *
4807 * Also, if there are any framebuffers left, that's a driver leak now,
4808 * so politely WARN about this.
4809 */
4810 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4811 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4812 drm_framebuffer_remove(fb);
4813 }
4814
4815 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4816 head) {
4817 plane->funcs->destroy(plane);
4818 }
4819
4820 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4821 crtc->funcs->destroy(crtc);
4822 }
4823
4824 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004825 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004826}
4827EXPORT_SYMBOL(drm_mode_config_cleanup);