blob: 6bd8133de3b79b7e6870961fc5bd9c64b3e4adc3 [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>
Rob Clark88a48e22014-12-18 16:01:50 -050041#include <drm/drm_atomic.h>
Dave Airlief453ba02008-11-07 14:05:41 -080042
Daniel Vetter8bd441b2014-01-23 15:35:24 +010043#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020044#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010045
Chris Wilson9a6f5132015-02-25 13:45:26 +000046static struct drm_framebuffer *
47internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020048 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +000049 struct drm_file *file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -070050
Dave Airlief453ba02008-11-07 14:05:41 -080051/* Avoid boilerplate. I'm tired of typing. */
52#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000053 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080054 { \
55 int i; \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
59 } \
60 return "(unknown)"; \
61 }
62
63/*
64 * Global properties
65 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010066static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080068 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
71};
72
73DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
74
Thierry Reding4dfd9092014-12-10 13:03:34 +010075static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040076 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
79};
80
Dave Airlief453ba02008-11-07 14:05:41 -080081/*
82 * Optional properties
83 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010084static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070085 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080089};
90
Vandana Kannanff587e42014-06-11 10:46:48 +053091static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
95};
96
Dave Airlief453ba02008-11-07 14:05:41 -080097/*
98 * Non-global properties, but "required" for certain connectors.
99 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100100static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104};
105
106DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
107
Thierry Reding4dfd9092014-12-10 13:03:34 +0100108static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
112};
113
114DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
116
Thierry Reding4dfd9092014-12-10 13:03:34 +0100117static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
126
Thierry Reding4dfd9092014-12-10 13:03:34 +0100127static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800133};
134
135DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
137
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000138static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
142};
143
Dave Airlief453ba02008-11-07 14:05:41 -0800144struct drm_conn_prop_enum_list {
145 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000146 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400147 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800148};
149
150/*
151 * Connector and encoder types.
152 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100153static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700171 { DRM_MODE_CONNECTOR_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
Thierry Reding4dfd9092014-12-10 13:03:34 +0100174static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
175 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800176 { DRM_MODE_ENCODER_DAC, "DAC" },
177 { DRM_MODE_ENCODER_TMDS, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200180 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300181 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000182 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700183 { DRM_MODE_ENCODER_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800184};
185
Thierry Reding4dfd9092014-12-10 13:03:34 +0100186static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800187 { SubPixelUnknown, "Unknown" },
188 { SubPixelHorizontalRGB, "Horizontal RGB" },
189 { SubPixelHorizontalBGR, "Horizontal BGR" },
190 { SubPixelVerticalRGB, "Vertical RGB" },
191 { SubPixelVerticalBGR, "Vertical BGR" },
192 { SubPixelNone, "None" },
193};
194
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400195void drm_connector_ida_init(void)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
200 ida_init(&drm_connector_enum_list[i].ida);
201}
202
203void drm_connector_ida_destroy(void)
204{
205 int i;
206
207 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
208 ida_destroy(&drm_connector_enum_list[i].ida);
209}
210
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100211/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
214 *
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
217 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000218const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800219{
220 if (status == connector_status_connected)
221 return "connected";
222 else if (status == connector_status_disconnected)
223 return "disconnected";
224 else
225 return "unknown";
226}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000227EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800228
Jesse Barnesac1bb362014-02-10 15:32:44 -0800229/**
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
232 *
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
235 */
236const char *drm_get_subpixel_order_name(enum subpixel_order order)
237{
238 return drm_subpixel_enum_list[order].name;
239}
240EXPORT_SYMBOL(drm_get_subpixel_order_name);
241
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300242static char printable_char(int c)
243{
244 return isascii(c) && isprint(c) ? c : '?';
245}
246
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100247/**
248 * drm_get_format_name - return a string for drm fourcc format
249 * @format: format to compute name of
250 *
251 * Note that the buffer used by this function is globally shared and owned by
252 * the function itself.
253 *
254 * FIXME: This isn't really multithreading safe.
255 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000256const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300257{
258 static char buf[32];
259
260 snprintf(buf, sizeof(buf),
261 "%c%c%c%c %s-endian (0x%08x)",
262 printable_char(format & 0xff),
263 printable_char((format >> 8) & 0xff),
264 printable_char((format >> 16) & 0xff),
265 printable_char((format >> 24) & 0x7f),
266 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
267 format);
268
269 return buf;
270}
271EXPORT_SYMBOL(drm_get_format_name);
272
Dave Airlie2ee39452014-07-24 11:53:45 +1000273/*
274 * Internal function to assign a slot in the object idr and optionally
275 * register the object into the idr.
276 */
277static int drm_mode_object_get_reg(struct drm_device *dev,
278 struct drm_mode_object *obj,
279 uint32_t obj_type,
280 bool register_obj)
281{
282 int ret;
283
284 mutex_lock(&dev->mode_config.idr_mutex);
285 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
286 if (ret >= 0) {
287 /*
288 * Set up the object linking under the protection of the idr
289 * lock so that other users can't see inconsistent state.
290 */
291 obj->id = ret;
292 obj->type = obj_type;
293 }
294 mutex_unlock(&dev->mode_config.idr_mutex);
295
296 return ret < 0 ? ret : 0;
297}
298
Dave Airlief453ba02008-11-07 14:05:41 -0800299/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100300 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800301 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100302 * @obj: object pointer, used to generate unique ID
303 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800304 *
Dave Airlief453ba02008-11-07 14:05:41 -0800305 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100306 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
307 * modeset identifiers are _not_ reference counted. Hence don't use this for
308 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800309 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100310 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200311 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800312 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100313int drm_mode_object_get(struct drm_device *dev,
314 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800315{
Dave Airlie2ee39452014-07-24 11:53:45 +1000316 return drm_mode_object_get_reg(dev, obj, obj_type, true);
317}
Dave Airlief453ba02008-11-07 14:05:41 -0800318
Dave Airlie2ee39452014-07-24 11:53:45 +1000319static void drm_mode_object_register(struct drm_device *dev,
320 struct drm_mode_object *obj)
321{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000322 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000323 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000324 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800325}
326
327/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100328 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800329 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100330 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800331 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100332 * Free @id from @dev's unique identifier pool. Note that despite the _get
333 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
334 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800335 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100336void drm_mode_object_put(struct drm_device *dev,
337 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800338{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000339 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800340 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000341 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800342}
343
Rob Clark98f75de2014-05-30 11:37:03 -0400344static struct drm_mode_object *_object_find(struct drm_device *dev,
345 uint32_t id, uint32_t type)
346{
347 struct drm_mode_object *obj = NULL;
348
349 mutex_lock(&dev->mode_config.idr_mutex);
350 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200351 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
352 obj = NULL;
353 if (obj && obj->id != id)
354 obj = NULL;
355 /* don't leak out unref'd fb's */
Daniel Stone6bcacf52015-04-20 19:22:55 +0100356 if (obj &&
357 (obj->type == DRM_MODE_OBJECT_FB ||
358 obj->type == DRM_MODE_OBJECT_BLOB))
Rob Clark98f75de2014-05-30 11:37:03 -0400359 obj = NULL;
360 mutex_unlock(&dev->mode_config.idr_mutex);
361
362 return obj;
363}
364
Daniel Vetter786b99e2012-12-02 21:53:40 +0100365/**
366 * drm_mode_object_find - look up a drm object with static lifetime
367 * @dev: drm device
368 * @id: id of the mode object
369 * @type: type of the mode object
370 *
371 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400372 * are reference counted, they need special treatment. Even with
373 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
374 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100375 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200376struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
377 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800378{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000379 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800380
Daniel Vetter786b99e2012-12-02 21:53:40 +0100381 /* Framebuffers are reference counted and need their own lookup
382 * function.*/
Daniel Stone6bcacf52015-04-20 19:22:55 +0100383 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400384 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800385 return obj;
386}
387EXPORT_SYMBOL(drm_mode_object_find);
388
389/**
Dave Airlief453ba02008-11-07 14:05:41 -0800390 * drm_framebuffer_init - initialize a framebuffer
391 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100392 * @fb: framebuffer to be initialized
393 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800394 *
Dave Airlief453ba02008-11-07 14:05:41 -0800395 * Allocates an ID for the framebuffer's parent mode object, sets its mode
396 * functions & device file and adds it to the master fd list.
397 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100398 * IMPORTANT:
399 * This functions publishes the fb and makes it available for concurrent access
400 * by other users. Which means by this point the fb _must_ be fully set up -
401 * since all the fb attributes are invariant over its lifetime, no further
402 * locking but only correct reference counting is required.
403 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100404 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200405 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800406 */
407int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
408 const struct drm_framebuffer_funcs *funcs)
409{
410 int ret;
411
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100412 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000413 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100414 INIT_LIST_HEAD(&fb->filp_head);
415 fb->dev = dev;
416 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000417
Dave Airlief453ba02008-11-07 14:05:41 -0800418 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200419 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100420 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800421
Dave Airlief453ba02008-11-07 14:05:41 -0800422 dev->mode_config.num_fb++;
423 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100424out:
425 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800426
Lukas Wunner3c67d832015-10-15 11:56:56 +0200427 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800428}
429EXPORT_SYMBOL(drm_framebuffer_init);
430
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200431/* dev->mode_config.fb_lock must be held! */
432static void __drm_framebuffer_unregister(struct drm_device *dev,
433 struct drm_framebuffer *fb)
434{
Liu Ying32709792016-02-29 11:21:10 +0800435 drm_mode_object_put(dev, &fb->base);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200436
437 fb->base.id = 0;
438}
439
Rob Clarkf7eff602012-09-05 21:48:38 +0000440static void drm_framebuffer_free(struct kref *kref)
441{
442 struct drm_framebuffer *fb =
443 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200444 struct drm_device *dev = fb->dev;
445
446 /*
447 * The lookup idr holds a weak reference, which has not necessarily been
448 * removed at this point. Check for that.
449 */
450 mutex_lock(&dev->mode_config.fb_lock);
451 if (fb->base.id) {
452 /* Mark fb as reaped and drop idr ref. */
453 __drm_framebuffer_unregister(dev, fb);
454 }
455 mutex_unlock(&dev->mode_config.fb_lock);
456
Rob Clarkf7eff602012-09-05 21:48:38 +0000457 fb->funcs->destroy(fb);
458}
459
Daniel Vetter2b677e82012-12-10 21:16:05 +0100460static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
461 uint32_t id)
462{
463 struct drm_mode_object *obj = NULL;
464 struct drm_framebuffer *fb;
465
466 mutex_lock(&dev->mode_config.idr_mutex);
467 obj = idr_find(&dev->mode_config.crtc_idr, id);
468 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
469 fb = NULL;
470 else
471 fb = obj_to_fb(obj);
472 mutex_unlock(&dev->mode_config.idr_mutex);
473
474 return fb;
475}
476
Rob Clarkf7eff602012-09-05 21:48:38 +0000477/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100478 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
479 * @dev: drm device
480 * @id: id of the fb object
481 *
482 * If successful, this grabs an additional reference to the framebuffer -
483 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100484 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100485 */
486struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
487 uint32_t id)
488{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100489 struct drm_framebuffer *fb;
490
491 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100492 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200493 if (fb) {
494 if (!kref_get_unless_zero(&fb->refcount))
495 fb = NULL;
496 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100497 mutex_unlock(&dev->mode_config.fb_lock);
498
499 return fb;
500}
501EXPORT_SYMBOL(drm_framebuffer_lookup);
502
503/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000504 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100505 * @fb: framebuffer to unref
506 *
507 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000508 */
509void drm_framebuffer_unreference(struct drm_framebuffer *fb)
510{
Rob Clark82912722014-03-18 10:07:08 -0400511 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000512 kref_put(&fb->refcount, drm_framebuffer_free);
513}
514EXPORT_SYMBOL(drm_framebuffer_unreference);
515
516/**
517 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100518 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100519 *
520 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000521 */
522void drm_framebuffer_reference(struct drm_framebuffer *fb)
523{
Rob Clark82912722014-03-18 10:07:08 -0400524 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000525 kref_get(&fb->refcount);
526}
527EXPORT_SYMBOL(drm_framebuffer_reference);
528
Dave Airlief453ba02008-11-07 14:05:41 -0800529/**
Daniel Vetter36206362012-12-10 20:42:17 +0100530 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
531 * @fb: fb to unregister
532 *
533 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
534 * those used for fbdev. Note that the caller must hold a reference of it's own,
535 * i.e. the object may not be destroyed through this call (since it'll lead to a
536 * locking inversion).
537 */
538void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
539{
Daniel Vettera39a3572015-08-25 15:45:11 +0200540 struct drm_device *dev;
541
542 if (!fb)
543 return;
544
545 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100546
547 mutex_lock(&dev->mode_config.fb_lock);
548 /* Mark fb as reaped and drop idr ref. */
549 __drm_framebuffer_unregister(dev, fb);
550 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100551}
552EXPORT_SYMBOL(drm_framebuffer_unregister_private);
553
554/**
Dave Airlief453ba02008-11-07 14:05:41 -0800555 * drm_framebuffer_cleanup - remove a framebuffer object
556 * @fb: framebuffer to remove
557 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100558 * Cleanup framebuffer. This function is intended to be used from the drivers
559 * ->destroy callback. It can also be used to clean up driver private
560 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100561 *
562 * Note that this function does not remove the fb from active usuage - if it is
563 * still used anywhere, hilarity can ensue since userspace could call getfb on
564 * the id and get back -EINVAL. Obviously no concern at driver unload time.
565 *
566 * Also, the framebuffer will not be removed from the lookup idr - for
567 * user-created framebuffers this will happen in in the rmfb ioctl. For
568 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
569 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800570 */
571void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
572{
573 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100574
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100575 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000576 list_del(&fb->head);
577 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100578 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000579}
580EXPORT_SYMBOL(drm_framebuffer_cleanup);
581
582/**
583 * drm_framebuffer_remove - remove and unreference a framebuffer object
584 * @fb: framebuffer to remove
585 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000586 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100587 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100588 * passed-in framebuffer. Might take the modeset locks.
589 *
590 * Note that this function optimizes the cleanup away if the caller holds the
591 * last reference to the framebuffer. It is also guaranteed to not take the
592 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000593 */
594void drm_framebuffer_remove(struct drm_framebuffer *fb)
595{
Daniel Vettera39a3572015-08-25 15:45:11 +0200596 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800597 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800598 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000599 struct drm_mode_set set;
600 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800601
Daniel Vettera39a3572015-08-25 15:45:11 +0200602 if (!fb)
603 return;
604
605 dev = fb->dev;
606
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100607 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100608
Daniel Vetterb62584e2012-12-11 16:51:35 +0100609 /*
610 * drm ABI mandates that we remove any deleted framebuffers from active
611 * useage. But since most sane clients only remove framebuffers they no
612 * longer need, try to optimize this away.
613 *
614 * Since we're holding a reference ourselves, observing a refcount of 1
615 * means that we're the last holder and can skip it. Also, the refcount
616 * can never increase from 1 again, so we don't need any barriers or
617 * locks.
618 *
619 * Note that userspace could try to race with use and instate a new
620 * usage _after_ we've cleared all current ones. End result will be an
621 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
622 * in this manner.
623 */
624 if (atomic_read(&fb->refcount.refcount) > 1) {
625 drm_modeset_lock_all(dev);
626 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200627 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700628 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100629 /* should turn off the crtc */
630 memset(&set, 0, sizeof(struct drm_mode_set));
631 set.crtc = crtc;
632 set.fb = NULL;
633 ret = drm_mode_set_config_internal(&set);
634 if (ret)
635 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
636 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000637 }
Dave Airlief453ba02008-11-07 14:05:41 -0800638
Daniel Vetter6295d602015-07-09 23:44:25 +0200639 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300640 if (plane->fb == fb)
641 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100643 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800644 }
645
Rob Clarkf7eff602012-09-05 21:48:38 +0000646 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800647}
Rob Clarkf7eff602012-09-05 21:48:38 +0000648EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800649
Rob Clark51fd3712013-11-19 12:10:12 -0500650DEFINE_WW_CLASS(crtc_ww_class);
651
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200652static unsigned int drm_num_crtcs(struct drm_device *dev)
653{
654 unsigned int num = 0;
655 struct drm_crtc *tmp;
656
657 drm_for_each_crtc(tmp, dev) {
658 num++;
659 }
660
661 return num;
662}
663
Dave Airlief453ba02008-11-07 14:05:41 -0800664/**
Matt Ropere13161a2014-04-01 15:22:38 -0700665 * drm_crtc_init_with_planes - Initialise a new CRTC object with
666 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800667 * @dev: DRM device
668 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700669 * @primary: Primary plane for CRTC
670 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800671 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200672 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800673 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000674 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200675 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100676 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200677 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800678 */
Matt Ropere13161a2014-04-01 15:22:38 -0700679int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
680 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700681 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200682 const struct drm_crtc_funcs *funcs,
683 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800684{
Rob Clark51fd3712013-11-19 12:10:12 -0500685 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200686 int ret;
687
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100688 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
689 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
690
Dave Airlief453ba02008-11-07 14:05:41 -0800691 crtc->dev = dev;
692 crtc->funcs = funcs;
693
Rob Clark51fd3712013-11-19 12:10:12 -0500694 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200695 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
696 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100697 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800698
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200699 if (name) {
700 va_list ap;
701
702 va_start(ap, name);
703 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
704 va_end(ap);
705 } else {
706 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
707 drm_num_crtcs(dev));
708 }
709 if (!crtc->name) {
710 drm_mode_object_put(dev, &crtc->base);
711 return -ENOMEM;
712 }
713
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300714 crtc->base.properties = &crtc->properties;
715
Rob Clark51fd3712013-11-19 12:10:12 -0500716 list_add_tail(&crtc->head, &config->crtc_list);
717 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200718
Matt Ropere13161a2014-04-01 15:22:38 -0700719 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700720 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700721 if (primary)
722 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700723 if (cursor)
724 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700725
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100726 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
727 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100728 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100729 }
730
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100731 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800732}
Matt Ropere13161a2014-04-01 15:22:38 -0700733EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800734
735/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000736 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800737 * @crtc: CRTC to cleanup
738 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000739 * This function cleans up @crtc and removes it from the DRM mode setting
740 * core. Note that the function does *not* free the crtc structure itself,
741 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800742 */
743void drm_crtc_cleanup(struct drm_crtc *crtc)
744{
745 struct drm_device *dev = crtc->dev;
746
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000747 kfree(crtc->gamma_store);
748 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800749
Rob Clark51fd3712013-11-19 12:10:12 -0500750 drm_modeset_lock_fini(&crtc->mutex);
751
Dave Airlief453ba02008-11-07 14:05:41 -0800752 drm_mode_object_put(dev, &crtc->base);
753 list_del(&crtc->head);
754 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100755
756 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
757 if (crtc->state && crtc->funcs->atomic_destroy_state)
758 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100759
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200760 kfree(crtc->name);
761
Thierry Redinga18c0af2014-12-10 11:38:49 +0100762 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800763}
764EXPORT_SYMBOL(drm_crtc_cleanup);
765
766/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000767 * drm_crtc_index - find the index of a registered CRTC
768 * @crtc: CRTC to find index for
769 *
770 * Given a registered CRTC, return the index of that CRTC within a DRM
771 * device's list of CRTCs.
772 */
773unsigned int drm_crtc_index(struct drm_crtc *crtc)
774{
775 unsigned int index = 0;
776 struct drm_crtc *tmp;
777
Daniel Vettere4f62542015-07-09 23:44:35 +0200778 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000779 if (tmp == crtc)
780 return index;
781
782 index++;
783 }
784
785 BUG();
786}
787EXPORT_SYMBOL(drm_crtc_index);
788
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100789/*
Dave Airlief453ba02008-11-07 14:05:41 -0800790 * drm_mode_remove - remove and free a mode
791 * @connector: connector list to modify
792 * @mode: mode to remove
793 *
Dave Airlief453ba02008-11-07 14:05:41 -0800794 * Remove @mode from @connector's mode list, then free it.
795 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100796static void drm_mode_remove(struct drm_connector *connector,
797 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800798{
799 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100800 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800801}
Dave Airlief453ba02008-11-07 14:05:41 -0800802
803/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200804 * drm_display_info_set_bus_formats - set the supported bus formats
805 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100806 * @formats: array containing the supported bus formats
807 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200808 *
809 * Store the supported bus formats in display info structure.
810 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
811 * a full list of available formats.
812 */
813int drm_display_info_set_bus_formats(struct drm_display_info *info,
814 const u32 *formats,
815 unsigned int num_formats)
816{
817 u32 *fmts = NULL;
818
819 if (!formats && num_formats)
820 return -EINVAL;
821
822 if (formats && num_formats) {
823 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
824 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300825 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200826 return -ENOMEM;
827 }
828
829 kfree(info->bus_formats);
830 info->bus_formats = fmts;
831 info->num_bus_formats = num_formats;
832
833 return 0;
834}
835EXPORT_SYMBOL(drm_display_info_set_bus_formats);
836
837/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200838 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
839 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200840 *
841 * The kernel supports per-connector configration of its consoles through
842 * use of the video= parameter. This function parses that option and
843 * extracts the user's specified mode (or enable/disable status) for a
844 * particular connector. This is typically only used during the early fbdev
845 * setup.
846 */
847static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
848{
849 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
850 char *option = NULL;
851
852 if (fb_get_options(connector->name, &option))
853 return;
854
855 if (!drm_mode_parse_command_line_for_connector(option,
856 connector,
857 mode))
858 return;
859
860 if (mode->force) {
861 const char *s;
862
863 switch (mode->force) {
864 case DRM_FORCE_OFF:
865 s = "OFF";
866 break;
867 case DRM_FORCE_ON_DIGITAL:
868 s = "ON - dig";
869 break;
870 default:
871 case DRM_FORCE_ON:
872 s = "ON";
873 break;
874 }
875
876 DRM_INFO("forcing %s connector %s\n", connector->name, s);
877 connector->force = mode->force;
878 }
879
880 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
881 connector->name,
882 mode->xres, mode->yres,
883 mode->refresh_specified ? mode->refresh : 60,
884 mode->rb ? " reduced blanking" : "",
885 mode->margins ? " with margins" : "",
886 mode->interlace ? " interlaced" : "");
887}
888
889/**
Dave Airlief453ba02008-11-07 14:05:41 -0800890 * drm_connector_init - Init a preallocated connector
891 * @dev: DRM device
892 * @connector: the connector to init
893 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100894 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800895 *
Dave Airlief453ba02008-11-07 14:05:41 -0800896 * Initialises a preallocated connector. Connectors should be
897 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200898 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100899 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200900 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800901 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200902int drm_connector_init(struct drm_device *dev,
903 struct drm_connector *connector,
904 const struct drm_connector_funcs *funcs,
905 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800906{
Rob Clarkae16c592014-12-18 16:01:54 -0500907 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200908 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400909 struct ida *connector_ida =
910 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200911
Daniel Vetter84849902012-12-02 00:28:11 +0100912 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800913
Dave Airlie2ee39452014-07-24 11:53:45 +1000914 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200915 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300916 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200917
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300918 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800919 connector->dev = dev;
920 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100921
922 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
923 if (connector->connector_id < 0) {
924 ret = connector->connector_id;
925 goto out_put;
926 }
927
Dave Airlief453ba02008-11-07 14:05:41 -0800928 connector->connector_type = connector_type;
929 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400930 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
931 if (connector->connector_type_id < 0) {
932 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100933 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400934 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300935 connector->name =
936 kasprintf(GFP_KERNEL, "%s-%d",
937 drm_connector_enum_list[connector_type].name,
938 connector->connector_type_id);
939 if (!connector->name) {
940 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100941 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300942 }
943
Dave Airlief453ba02008-11-07 14:05:41 -0800944 INIT_LIST_HEAD(&connector->probed_modes);
945 INIT_LIST_HEAD(&connector->modes);
946 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000947 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800948
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200949 drm_connector_get_cmdline_mode(connector);
950
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100951 /* We should add connectors at the end to avoid upsetting the connector
952 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500953 list_add_tail(&connector->head, &config->connector_list);
954 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800955
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200956 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500957 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500958 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200959 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800960
Rob Clark58495562012-10-11 20:50:56 -0500961 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500962 config->dpms_property, 0);
963
964 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
965 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
966 }
Dave Airlief453ba02008-11-07 14:05:41 -0800967
Thomas Wood30f65702014-06-18 17:52:32 +0100968 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100969out_put_type_id:
970 if (ret)
971 ida_remove(connector_ida, connector->connector_type_id);
972out_put_id:
973 if (ret)
974 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300975out_put:
976 if (ret)
977 drm_mode_object_put(dev, &connector->base);
978
979out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100980 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200981
982 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800983}
984EXPORT_SYMBOL(drm_connector_init);
985
986/**
987 * drm_connector_cleanup - cleans up an initialised connector
988 * @connector: connector to cleanup
989 *
Dave Airlief453ba02008-11-07 14:05:41 -0800990 * Cleans up the connector but doesn't free the object.
991 */
992void drm_connector_cleanup(struct drm_connector *connector)
993{
994 struct drm_device *dev = connector->dev;
995 struct drm_display_mode *mode, *t;
996
Dave Airlie40d9b042014-10-20 16:29:33 +1000997 if (connector->tile_group) {
998 drm_mode_put_tile_group(dev, connector->tile_group);
999 connector->tile_group = NULL;
1000 }
1001
Dave Airlief453ba02008-11-07 14:05:41 -08001002 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
1003 drm_mode_remove(connector, mode);
1004
1005 list_for_each_entry_safe(mode, t, &connector->modes, head)
1006 drm_mode_remove(connector, mode);
1007
Ilia Mirkinb21e3af2013-08-07 22:34:48 -04001008 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
1009 connector->connector_type_id);
1010
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001011 ida_remove(&dev->mode_config.connector_ida,
1012 connector->connector_id);
1013
Boris Brezillonb5571e92014-07-22 12:09:10 +02001014 kfree(connector->display_info.bus_formats);
Dave Airlief453ba02008-11-07 14:05:41 -08001015 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +03001016 kfree(connector->name);
1017 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001018 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001019 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001020
1021 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1022 if (connector->state && connector->funcs->atomic_destroy_state)
1023 connector->funcs->atomic_destroy_state(connector,
1024 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001025
1026 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001027}
1028EXPORT_SYMBOL(drm_connector_cleanup);
1029
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001030/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001031 * drm_connector_register - register a connector
1032 * @connector: the connector to register
1033 *
1034 * Register userspace interfaces for a connector
1035 *
1036 * Returns:
1037 * Zero on success, error code on failure.
1038 */
1039int drm_connector_register(struct drm_connector *connector)
1040{
Thomas Wood30f65702014-06-18 17:52:32 +01001041 int ret;
1042
Dave Airlie2ee39452014-07-24 11:53:45 +10001043 drm_mode_object_register(connector->dev, &connector->base);
1044
Thomas Wood30f65702014-06-18 17:52:32 +01001045 ret = drm_sysfs_connector_add(connector);
1046 if (ret)
1047 return ret;
1048
1049 ret = drm_debugfs_connector_add(connector);
1050 if (ret) {
1051 drm_sysfs_connector_remove(connector);
1052 return ret;
1053 }
1054
1055 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001056}
1057EXPORT_SYMBOL(drm_connector_register);
1058
1059/**
1060 * drm_connector_unregister - unregister a connector
1061 * @connector: the connector to unregister
1062 *
1063 * Unregister userspace interfaces for a connector
1064 */
1065void drm_connector_unregister(struct drm_connector *connector)
1066{
1067 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001068 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001069}
1070EXPORT_SYMBOL(drm_connector_unregister);
1071
1072
1073/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001074 * drm_connector_unplug_all - unregister connector userspace interfaces
1075 * @dev: drm device
1076 *
1077 * This function unregisters all connector userspace interfaces in sysfs. Should
1078 * be call when the device is disconnected, e.g. from an usb driver's
1079 * ->disconnect callback.
1080 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001081void drm_connector_unplug_all(struct drm_device *dev)
1082{
1083 struct drm_connector *connector;
1084
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001085 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Dave Airliecbc7e222012-02-20 14:16:40 +00001086 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001087 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001088
1089}
1090EXPORT_SYMBOL(drm_connector_unplug_all);
1091
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001092/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001093 * drm_encoder_init - Init a preallocated encoder
1094 * @dev: drm device
1095 * @encoder: the encoder to init
1096 * @funcs: callbacks for this encoder
1097 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001098 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001099 *
1100 * Initialises a preallocated encoder. Encoder should be
1101 * subclassed as part of driver encoder objects.
1102 *
1103 * Returns:
1104 * Zero on success, error code on failure.
1105 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001106int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001107 struct drm_encoder *encoder,
1108 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001109 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001110{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001111 int ret;
1112
Daniel Vetter84849902012-12-02 00:28:11 +01001113 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001114
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001115 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1116 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001117 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001118
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001119 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001120 encoder->encoder_type = encoder_type;
1121 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001122 if (name) {
1123 va_list ap;
1124
1125 va_start(ap, name);
1126 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1127 va_end(ap);
1128 } else {
1129 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1130 drm_encoder_enum_list[encoder_type].name,
1131 encoder->base.id);
1132 }
Jani Nikulae5748942014-05-14 16:58:20 +03001133 if (!encoder->name) {
1134 ret = -ENOMEM;
1135 goto out_put;
1136 }
Dave Airlief453ba02008-11-07 14:05:41 -08001137
1138 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1139 dev->mode_config.num_encoder++;
1140
Jani Nikulae5748942014-05-14 16:58:20 +03001141out_put:
1142 if (ret)
1143 drm_mode_object_put(dev, &encoder->base);
1144
1145out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001146 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001147
1148 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001149}
1150EXPORT_SYMBOL(drm_encoder_init);
1151
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001152/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001153 * drm_encoder_index - find the index of a registered encoder
1154 * @encoder: encoder to find index for
1155 *
1156 * Given a registered encoder, return the index of that encoder within a DRM
1157 * device's list of encoders.
1158 */
1159unsigned int drm_encoder_index(struct drm_encoder *encoder)
1160{
1161 unsigned int index = 0;
1162 struct drm_encoder *tmp;
1163
1164 drm_for_each_encoder(tmp, encoder->dev) {
1165 if (tmp == encoder)
1166 return index;
1167
1168 index++;
1169 }
1170
1171 BUG();
1172}
1173EXPORT_SYMBOL(drm_encoder_index);
1174
1175/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001176 * drm_encoder_cleanup - cleans up an initialised encoder
1177 * @encoder: encoder to cleanup
1178 *
1179 * Cleans up the encoder but doesn't free the object.
1180 */
Dave Airlief453ba02008-11-07 14:05:41 -08001181void drm_encoder_cleanup(struct drm_encoder *encoder)
1182{
1183 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001184
Daniel Vetter84849902012-12-02 00:28:11 +01001185 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001186 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001187 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001188 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001189 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001190 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001191
1192 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001193}
1194EXPORT_SYMBOL(drm_encoder_cleanup);
1195
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001196static unsigned int drm_num_planes(struct drm_device *dev)
1197{
1198 unsigned int num = 0;
1199 struct drm_plane *tmp;
1200
1201 drm_for_each_plane(tmp, dev) {
1202 num++;
1203 }
1204
1205 return num;
1206}
1207
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001208/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001209 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001210 * @dev: DRM device
1211 * @plane: plane object to init
1212 * @possible_crtcs: bitmask of possible CRTCs
1213 * @funcs: callbacks for the new plane
1214 * @formats: array of supported formats (%DRM_FORMAT_*)
1215 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001216 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001217 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001218 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001219 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001220 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001221 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001222 * Zero on success, error code on failure.
1223 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001224int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1225 unsigned long possible_crtcs,
1226 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001227 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001228 enum drm_plane_type type,
1229 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001230{
Rob Clark6b4959f2014-12-18 16:01:53 -05001231 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001232 int ret;
1233
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001234 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1235 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001236 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001237
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001238 drm_modeset_lock_init(&plane->mutex);
1239
Rob Clark4d939142012-05-17 02:23:27 -06001240 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001241 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001242 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001243 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1244 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001245 if (!plane->format_types) {
1246 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1247 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001248 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001249 }
1250
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001251 if (name) {
1252 va_list ap;
1253
1254 va_start(ap, name);
1255 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1256 va_end(ap);
1257 } else {
1258 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1259 drm_num_planes(dev));
1260 }
1261 if (!plane->name) {
1262 kfree(plane->format_types);
1263 drm_mode_object_put(dev, &plane->base);
1264 return -ENOMEM;
1265 }
1266
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001267 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001268 plane->format_count = format_count;
1269 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001270 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001271
Rob Clark6b4959f2014-12-18 16:01:53 -05001272 list_add_tail(&plane->head, &config->plane_list);
1273 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001274 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001275 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001276
Rob Clark9922ab52014-04-01 20:16:57 -04001277 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001278 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001279 plane->type);
1280
Rob Clark6b4959f2014-12-18 16:01:53 -05001281 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1282 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1283 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1284 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1285 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1286 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1287 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1288 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1289 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1290 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1291 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1292 }
1293
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001294 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001295}
Matt Roperdc415ff2014-04-01 15:22:36 -07001296EXPORT_SYMBOL(drm_universal_plane_init);
1297
1298/**
1299 * drm_plane_init - Initialize a legacy plane
1300 * @dev: DRM device
1301 * @plane: plane object to init
1302 * @possible_crtcs: bitmask of possible CRTCs
1303 * @funcs: callbacks for the new plane
1304 * @formats: array of supported formats (%DRM_FORMAT_*)
1305 * @format_count: number of elements in @formats
1306 * @is_primary: plane type (primary vs overlay)
1307 *
1308 * Legacy API to initialize a DRM plane.
1309 *
1310 * New drivers should call drm_universal_plane_init() instead.
1311 *
1312 * Returns:
1313 * Zero on success, error code on failure.
1314 */
1315int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1316 unsigned long possible_crtcs,
1317 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001318 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001319 bool is_primary)
1320{
1321 enum drm_plane_type type;
1322
1323 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1324 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001325 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001326}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001327EXPORT_SYMBOL(drm_plane_init);
1328
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001329/**
1330 * drm_plane_cleanup - Clean up the core plane usage
1331 * @plane: plane to cleanup
1332 *
1333 * This function cleans up @plane and removes it from the DRM mode setting
1334 * core. Note that the function does *not* free the plane structure itself,
1335 * this is the responsibility of the caller.
1336 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001337void drm_plane_cleanup(struct drm_plane *plane)
1338{
1339 struct drm_device *dev = plane->dev;
1340
Daniel Vetter84849902012-12-02 00:28:11 +01001341 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001342 kfree(plane->format_types);
1343 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001344
1345 BUG_ON(list_empty(&plane->head));
1346
1347 list_del(&plane->head);
1348 dev->mode_config.num_total_plane--;
1349 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1350 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001351 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001352
1353 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1354 if (plane->state && plane->funcs->atomic_destroy_state)
1355 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001356
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001357 kfree(plane->name);
1358
Thierry Redinga18c0af2014-12-10 11:38:49 +01001359 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001360}
1361EXPORT_SYMBOL(drm_plane_cleanup);
1362
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001363/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001364 * drm_plane_index - find the index of a registered plane
1365 * @plane: plane to find index for
1366 *
1367 * Given a registered plane, return the index of that CRTC within a DRM
1368 * device's list of planes.
1369 */
1370unsigned int drm_plane_index(struct drm_plane *plane)
1371{
1372 unsigned int index = 0;
1373 struct drm_plane *tmp;
1374
Daniel Vettere4f62542015-07-09 23:44:35 +02001375 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001376 if (tmp == plane)
1377 return index;
1378
1379 index++;
1380 }
1381
1382 BUG();
1383}
1384EXPORT_SYMBOL(drm_plane_index);
1385
1386/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001387 * drm_plane_from_index - find the registered plane at an index
1388 * @dev: DRM device
1389 * @idx: index of registered plane to find for
1390 *
1391 * Given a plane index, return the registered plane from DRM device's
1392 * list of planes with matching index.
1393 */
1394struct drm_plane *
1395drm_plane_from_index(struct drm_device *dev, int idx)
1396{
1397 struct drm_plane *plane;
1398 unsigned int i = 0;
1399
Daniel Vetter6295d602015-07-09 23:44:25 +02001400 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001401 if (i == idx)
1402 return plane;
1403 i++;
1404 }
1405 return NULL;
1406}
1407EXPORT_SYMBOL(drm_plane_from_index);
1408
1409/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001410 * drm_plane_force_disable - Forcibly disable a plane
1411 * @plane: plane to disable
1412 *
1413 * Forces the plane to be disabled.
1414 *
1415 * Used when the plane's current framebuffer is destroyed,
1416 * and when restoring fbdev mode.
1417 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001418void drm_plane_force_disable(struct drm_plane *plane)
1419{
1420 int ret;
1421
Daniel Vetter3d30a592014-07-27 13:42:42 +02001422 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001423 return;
1424
Daniel Vetter3d30a592014-07-27 13:42:42 +02001425 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001426 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001427 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001428 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001429 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001430 return;
1431 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001432 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001433 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001434 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001435 plane->fb = NULL;
1436 plane->crtc = NULL;
1437}
1438EXPORT_SYMBOL(drm_plane_force_disable);
1439
Rob Clark6b4959f2014-12-18 16:01:53 -05001440static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001441{
Rob Clark356af0e2014-12-18 16:01:52 -05001442 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001443
1444 /*
1445 * Standard properties (apply to all connectors)
1446 */
Rob Clark356af0e2014-12-18 16:01:52 -05001447 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001448 DRM_MODE_PROP_IMMUTABLE,
1449 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001450 if (!prop)
1451 return -ENOMEM;
1452 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001453
Rob Clark356af0e2014-12-18 16:01:52 -05001454 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001455 "DPMS", drm_dpms_enum_list,
1456 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001457 if (!prop)
1458 return -ENOMEM;
1459 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001460
Rob Clark356af0e2014-12-18 16:01:52 -05001461 prop = drm_property_create(dev,
1462 DRM_MODE_PROP_BLOB |
1463 DRM_MODE_PROP_IMMUTABLE,
1464 "PATH", 0);
1465 if (!prop)
1466 return -ENOMEM;
1467 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001468
Rob Clark356af0e2014-12-18 16:01:52 -05001469 prop = drm_property_create(dev,
1470 DRM_MODE_PROP_BLOB |
1471 DRM_MODE_PROP_IMMUTABLE,
1472 "TILE", 0);
1473 if (!prop)
1474 return -ENOMEM;
1475 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001476
Rob Clark6b4959f2014-12-18 16:01:53 -05001477 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001478 "type", drm_plane_type_enum_list,
1479 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001480 if (!prop)
1481 return -ENOMEM;
1482 dev->mode_config.plane_type_property = prop;
1483
1484 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1485 "SRC_X", 0, UINT_MAX);
1486 if (!prop)
1487 return -ENOMEM;
1488 dev->mode_config.prop_src_x = prop;
1489
1490 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1491 "SRC_Y", 0, UINT_MAX);
1492 if (!prop)
1493 return -ENOMEM;
1494 dev->mode_config.prop_src_y = prop;
1495
1496 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1497 "SRC_W", 0, UINT_MAX);
1498 if (!prop)
1499 return -ENOMEM;
1500 dev->mode_config.prop_src_w = prop;
1501
1502 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1503 "SRC_H", 0, UINT_MAX);
1504 if (!prop)
1505 return -ENOMEM;
1506 dev->mode_config.prop_src_h = prop;
1507
1508 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1509 "CRTC_X", INT_MIN, INT_MAX);
1510 if (!prop)
1511 return -ENOMEM;
1512 dev->mode_config.prop_crtc_x = prop;
1513
1514 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1515 "CRTC_Y", INT_MIN, INT_MAX);
1516 if (!prop)
1517 return -ENOMEM;
1518 dev->mode_config.prop_crtc_y = prop;
1519
1520 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1521 "CRTC_W", 0, INT_MAX);
1522 if (!prop)
1523 return -ENOMEM;
1524 dev->mode_config.prop_crtc_w = prop;
1525
1526 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1527 "CRTC_H", 0, INT_MAX);
1528 if (!prop)
1529 return -ENOMEM;
1530 dev->mode_config.prop_crtc_h = prop;
1531
1532 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1533 "FB_ID", DRM_MODE_OBJECT_FB);
1534 if (!prop)
1535 return -ENOMEM;
1536 dev->mode_config.prop_fb_id = prop;
1537
1538 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1539 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1540 if (!prop)
1541 return -ENOMEM;
1542 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001543
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001544 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1545 "ACTIVE");
1546 if (!prop)
1547 return -ENOMEM;
1548 dev->mode_config.prop_active = prop;
1549
Daniel Stone955f3c32015-05-25 19:11:52 +01001550 prop = drm_property_create(dev,
1551 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1552 "MODE_ID", 0);
1553 if (!prop)
1554 return -ENOMEM;
1555 dev->mode_config.prop_mode_id = prop;
1556
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001557 prop = drm_property_create(dev,
1558 DRM_MODE_PROP_BLOB,
1559 "DEGAMMA_LUT", 0);
1560 if (!prop)
1561 return -ENOMEM;
1562 dev->mode_config.degamma_lut_property = prop;
1563
1564 prop = drm_property_create_range(dev,
1565 DRM_MODE_PROP_IMMUTABLE,
1566 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1567 if (!prop)
1568 return -ENOMEM;
1569 dev->mode_config.degamma_lut_size_property = prop;
1570
1571 prop = drm_property_create(dev,
1572 DRM_MODE_PROP_BLOB,
1573 "CTM", 0);
1574 if (!prop)
1575 return -ENOMEM;
1576 dev->mode_config.ctm_property = prop;
1577
1578 prop = drm_property_create(dev,
1579 DRM_MODE_PROP_BLOB,
1580 "GAMMA_LUT", 0);
1581 if (!prop)
1582 return -ENOMEM;
1583 dev->mode_config.gamma_lut_property = prop;
1584
1585 prop = drm_property_create_range(dev,
1586 DRM_MODE_PROP_IMMUTABLE,
1587 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1588 if (!prop)
1589 return -ENOMEM;
1590 dev->mode_config.gamma_lut_size_property = prop;
1591
Rob Clark9922ab52014-04-01 20:16:57 -04001592 return 0;
1593}
1594
Dave Airlief453ba02008-11-07 14:05:41 -08001595/**
1596 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1597 * @dev: DRM device
1598 *
1599 * Called by a driver the first time a DVI-I connector is made.
1600 */
1601int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1602{
1603 struct drm_property *dvi_i_selector;
1604 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001605
1606 if (dev->mode_config.dvi_i_select_subconnector_property)
1607 return 0;
1608
1609 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001610 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001611 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001612 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001613 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001614 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1615
Sascha Hauer4a67d392012-02-06 10:58:17 +01001616 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001617 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001618 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001619 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001620 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1621
1622 return 0;
1623}
1624EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1625
1626/**
1627 * drm_create_tv_properties - create TV specific connector properties
1628 * @dev: DRM device
1629 * @num_modes: number of different TV formats (modes) supported
1630 * @modes: array of pointers to strings containing name of each format
1631 *
1632 * Called by a driver's TV initialization routine, this function creates
1633 * the TV specific connector properties for a given device. Caller is
1634 * responsible for allocating a list of format names and passing them to
1635 * this routine.
1636 */
Thierry Reding2f763312014-10-13 12:45:57 +02001637int drm_mode_create_tv_properties(struct drm_device *dev,
1638 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001639 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001640{
1641 struct drm_property *tv_selector;
1642 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001643 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001644
1645 if (dev->mode_config.tv_select_subconnector_property)
1646 return 0;
1647
1648 /*
1649 * Basic connector properties
1650 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001651 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001652 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001653 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001654 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001655 if (!tv_selector)
1656 goto nomem;
1657
Dave Airlief453ba02008-11-07 14:05:41 -08001658 dev->mode_config.tv_select_subconnector_property = tv_selector;
1659
1660 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001661 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1662 "subconnector",
1663 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001664 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001665 if (!tv_subconnector)
1666 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001667 dev->mode_config.tv_subconnector_property = tv_subconnector;
1668
1669 /*
1670 * Other, TV specific properties: margins & TV modes.
1671 */
1672 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001673 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001674 if (!dev->mode_config.tv_left_margin_property)
1675 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001676
1677 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001678 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001679 if (!dev->mode_config.tv_right_margin_property)
1680 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001681
1682 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001683 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001684 if (!dev->mode_config.tv_top_margin_property)
1685 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001686
1687 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001688 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001689 if (!dev->mode_config.tv_bottom_margin_property)
1690 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001691
1692 dev->mode_config.tv_mode_property =
1693 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1694 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001695 if (!dev->mode_config.tv_mode_property)
1696 goto nomem;
1697
Dave Airlief453ba02008-11-07 14:05:41 -08001698 for (i = 0; i < num_modes; i++)
1699 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1700 i, modes[i]);
1701
Francisco Jerezb6b79022009-08-02 04:19:20 +02001702 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001703 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001704 if (!dev->mode_config.tv_brightness_property)
1705 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001706
1707 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001708 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001709 if (!dev->mode_config.tv_contrast_property)
1710 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001711
1712 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001713 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001714 if (!dev->mode_config.tv_flicker_reduction_property)
1715 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001716
Francisco Jereza75f0232009-08-12 02:30:10 +02001717 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001718 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001719 if (!dev->mode_config.tv_overscan_property)
1720 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001721
1722 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001723 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001724 if (!dev->mode_config.tv_saturation_property)
1725 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001726
1727 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001728 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001729 if (!dev->mode_config.tv_hue_property)
1730 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001731
Dave Airlief453ba02008-11-07 14:05:41 -08001732 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001733nomem:
1734 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001735}
1736EXPORT_SYMBOL(drm_mode_create_tv_properties);
1737
1738/**
1739 * drm_mode_create_scaling_mode_property - create scaling mode property
1740 * @dev: DRM device
1741 *
1742 * Called by a driver the first time it's needed, must be attached to desired
1743 * connectors.
1744 */
1745int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1746{
1747 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001748
1749 if (dev->mode_config.scaling_mode_property)
1750 return 0;
1751
1752 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001753 drm_property_create_enum(dev, 0, "scaling mode",
1754 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001755 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001756
1757 dev->mode_config.scaling_mode_property = scaling_mode;
1758
1759 return 0;
1760}
1761EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1762
1763/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301764 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1765 * @dev: DRM device
1766 *
1767 * Called by a driver the first time it's needed, must be attached to desired
1768 * connectors.
1769 *
1770 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001771 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301772 */
1773int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1774{
1775 if (dev->mode_config.aspect_ratio_property)
1776 return 0;
1777
1778 dev->mode_config.aspect_ratio_property =
1779 drm_property_create_enum(dev, 0, "aspect ratio",
1780 drm_aspect_ratio_enum_list,
1781 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1782
1783 if (dev->mode_config.aspect_ratio_property == NULL)
1784 return -ENOMEM;
1785
1786 return 0;
1787}
1788EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1789
1790/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001791 * drm_mode_create_dirty_property - create dirty property
1792 * @dev: DRM device
1793 *
1794 * Called by a driver the first time it's needed, must be attached to desired
1795 * connectors.
1796 */
1797int drm_mode_create_dirty_info_property(struct drm_device *dev)
1798{
1799 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001800
1801 if (dev->mode_config.dirty_info_property)
1802 return 0;
1803
1804 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001805 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001806 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001807 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001808 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001809 dev->mode_config.dirty_info_property = dirty_info;
1810
1811 return 0;
1812}
1813EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1814
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001815/**
1816 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1817 * @dev: DRM device
1818 *
1819 * Create the the suggested x/y offset property for connectors.
1820 */
1821int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1822{
1823 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1824 return 0;
1825
1826 dev->mode_config.suggested_x_property =
1827 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1828
1829 dev->mode_config.suggested_y_property =
1830 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1831
1832 if (dev->mode_config.suggested_x_property == NULL ||
1833 dev->mode_config.suggested_y_property == NULL)
1834 return -ENOMEM;
1835 return 0;
1836}
1837EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1838
Dave Airlief453ba02008-11-07 14:05:41 -08001839/**
Dave Airlief453ba02008-11-07 14:05:41 -08001840 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001841 * @dev: drm device for the ioctl
1842 * @data: data pointer for the ioctl
1843 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001844 *
Dave Airlief453ba02008-11-07 14:05:41 -08001845 * Construct a set of configuration description structures and return
1846 * them to the user, including CRTC, connector and framebuffer configuration.
1847 *
1848 * Called by the user via ioctl.
1849 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001850 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001851 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001852 */
1853int drm_mode_getresources(struct drm_device *dev, void *data,
1854 struct drm_file *file_priv)
1855{
1856 struct drm_mode_card_res *card_res = data;
1857 struct list_head *lh;
1858 struct drm_framebuffer *fb;
1859 struct drm_connector *connector;
1860 struct drm_crtc *crtc;
1861 struct drm_encoder *encoder;
1862 int ret = 0;
1863 int connector_count = 0;
1864 int crtc_count = 0;
1865 int fb_count = 0;
1866 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001867 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001868 uint32_t __user *fb_id;
1869 uint32_t __user *crtc_id;
1870 uint32_t __user *connector_id;
1871 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001872
Dave Airliefb3b06c2011-02-08 13:55:21 +10001873 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1874 return -EINVAL;
1875
Dave Airlief453ba02008-11-07 14:05:41 -08001876
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001877 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001878 /*
1879 * For the non-control nodes we need to limit the list of resources
1880 * by IDs in the group list for this node
1881 */
1882 list_for_each(lh, &file_priv->fbs)
1883 fb_count++;
1884
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001885 /* handle this in 4 parts */
1886 /* FBs */
1887 if (card_res->count_fbs >= fb_count) {
1888 copied = 0;
1889 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1890 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1891 if (put_user(fb->base.id, fb_id + copied)) {
1892 mutex_unlock(&file_priv->fbs_lock);
1893 return -EFAULT;
1894 }
1895 copied++;
1896 }
1897 }
1898 card_res->count_fbs = fb_count;
1899 mutex_unlock(&file_priv->fbs_lock);
1900
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001901 /* mode_config.mutex protects the connector list against e.g. DP MST
1902 * connector hot-adding. CRTC/Plane lists are invariant. */
1903 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001904 drm_for_each_crtc(crtc, dev)
1905 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001906
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001907 drm_for_each_connector(connector, dev)
1908 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001909
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001910 drm_for_each_encoder(encoder, dev)
1911 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001912
1913 card_res->max_height = dev->mode_config.max_height;
1914 card_res->min_height = dev->mode_config.min_height;
1915 card_res->max_width = dev->mode_config.max_width;
1916 card_res->min_width = dev->mode_config.min_width;
1917
Dave Airlief453ba02008-11-07 14:05:41 -08001918 /* CRTCs */
1919 if (card_res->count_crtcs >= crtc_count) {
1920 copied = 0;
1921 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001922 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001923 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1924 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001925 if (put_user(crtc->base.id, crtc_id + copied)) {
1926 ret = -EFAULT;
1927 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001928 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001929 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001930 }
1931 }
1932 card_res->count_crtcs = crtc_count;
1933
1934 /* Encoders */
1935 if (card_res->count_encoders >= encoder_count) {
1936 copied = 0;
1937 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001938 drm_for_each_encoder(encoder, dev) {
1939 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1940 encoder->name);
1941 if (put_user(encoder->base.id, encoder_id +
1942 copied)) {
1943 ret = -EFAULT;
1944 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001945 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001946 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001947 }
1948 }
1949 card_res->count_encoders = encoder_count;
1950
1951 /* Connectors */
1952 if (card_res->count_connectors >= connector_count) {
1953 copied = 0;
1954 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001955 drm_for_each_connector(connector, dev) {
1956 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1957 connector->base.id,
1958 connector->name);
1959 if (put_user(connector->base.id,
1960 connector_id + copied)) {
1961 ret = -EFAULT;
1962 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001963 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001964 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001965 }
1966 }
1967 card_res->count_connectors = connector_count;
1968
Jerome Glisse94401062010-07-15 15:43:25 -04001969 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001970 card_res->count_connectors, card_res->count_encoders);
1971
1972out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001973 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001974 return ret;
1975}
1976
1977/**
1978 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001979 * @dev: drm device for the ioctl
1980 * @data: data pointer for the ioctl
1981 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001982 *
Dave Airlief453ba02008-11-07 14:05:41 -08001983 * Construct a CRTC configuration structure to return to the user.
1984 *
1985 * Called by the user via ioctl.
1986 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001987 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001988 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001989 */
1990int drm_mode_getcrtc(struct drm_device *dev,
1991 void *data, struct drm_file *file_priv)
1992{
1993 struct drm_mode_crtc *crtc_resp = data;
1994 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001995
Dave Airliefb3b06c2011-02-08 13:55:21 +10001996 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1997 return -EINVAL;
1998
Rob Clarka2b34e22013-10-05 16:36:52 -04001999 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002000 if (!crtc)
2001 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002002
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002003 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002004 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002005 if (crtc->primary->fb)
2006 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002007 else
2008 crtc_resp->fb_id = 0;
2009
Daniel Vetter31c946e2015-02-22 12:24:17 +01002010 if (crtc->state) {
2011 crtc_resp->x = crtc->primary->state->src_x >> 16;
2012 crtc_resp->y = crtc->primary->state->src_y >> 16;
2013 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002014 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002015 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002016
Daniel Vetter31c946e2015-02-22 12:24:17 +01002017 } else {
2018 crtc_resp->mode_valid = 0;
2019 }
Dave Airlief453ba02008-11-07 14:05:41 -08002020 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002021 crtc_resp->x = crtc->x;
2022 crtc_resp->y = crtc->y;
2023 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002024 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002025 crtc_resp->mode_valid = 1;
2026
2027 } else {
2028 crtc_resp->mode_valid = 0;
2029 }
Dave Airlief453ba02008-11-07 14:05:41 -08002030 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002031 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002032
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002033 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002034}
2035
Damien Lespiau61d8e322013-09-25 16:45:22 +01002036static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2037 const struct drm_file *file_priv)
2038{
2039 /*
2040 * If user-space hasn't configured the driver to expose the stereo 3D
2041 * modes, don't expose them.
2042 */
2043 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2044 return false;
2045
2046 return true;
2047}
2048
Daniel Vetterabd69c52014-11-25 23:50:05 +01002049static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2050{
2051 /* For atomic drivers only state objects are synchronously updated and
2052 * protected by modeset locks, so check those first. */
2053 if (connector->state)
2054 return connector->state->best_encoder;
2055 return connector->encoder;
2056}
2057
Rob Clark95cbf112014-12-18 16:01:49 -05002058/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002059static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002060 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2061 uint32_t *arg_count_props)
2062{
Rob Clark88a48e22014-12-18 16:01:50 -05002063 int props_count;
2064 int i, ret, copied;
2065
2066 props_count = obj->properties->count;
2067 if (!atomic)
2068 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002069
2070 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002071 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002072 struct drm_property *prop = obj->properties->properties[i];
2073 uint64_t val;
2074
Rob Clark88a48e22014-12-18 16:01:50 -05002075 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2076 continue;
2077
Rob Clark95cbf112014-12-18 16:01:49 -05002078 ret = drm_object_property_get_value(obj, prop, &val);
2079 if (ret)
2080 return ret;
2081
2082 if (put_user(prop->base.id, prop_ptr + copied))
2083 return -EFAULT;
2084
2085 if (put_user(val, prop_values + copied))
2086 return -EFAULT;
2087
2088 copied++;
2089 }
2090 }
2091 *arg_count_props = props_count;
2092
2093 return 0;
2094}
2095
Dave Airlief453ba02008-11-07 14:05:41 -08002096/**
2097 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002098 * @dev: drm device for the ioctl
2099 * @data: data pointer for the ioctl
2100 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002101 *
Dave Airlief453ba02008-11-07 14:05:41 -08002102 * Construct a connector configuration structure to return to the user.
2103 *
2104 * Called by the user via ioctl.
2105 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002106 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002107 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002108 */
2109int drm_mode_getconnector(struct drm_device *dev, void *data,
2110 struct drm_file *file_priv)
2111{
2112 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002113 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002114 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 struct drm_display_mode *mode;
2116 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002117 int encoders_count = 0;
2118 int ret = 0;
2119 int copied = 0;
2120 int i;
2121 struct drm_mode_modeinfo u_mode;
2122 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002123 uint32_t __user *encoder_ptr;
2124
Dave Airliefb3b06c2011-02-08 13:55:21 +10002125 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2126 return -EINVAL;
2127
Dave Airlief453ba02008-11-07 14:05:41 -08002128 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2129
Jerome Glisse94401062010-07-15 15:43:25 -04002130 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002131
Daniel Vetter7b240562012-12-12 00:35:33 +01002132 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002133
Rob Clarka2b34e22013-10-05 16:36:52 -04002134 connector = drm_connector_find(dev, out_resp->connector_id);
2135 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002136 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002137 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002138 }
Dave Airlief453ba02008-11-07 14:05:41 -08002139
Thierry Reding01073b02014-12-10 13:03:38 +01002140 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2141 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002142 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002143
2144 if (out_resp->count_modes == 0) {
2145 connector->funcs->fill_modes(connector,
2146 dev->mode_config.max_width,
2147 dev->mode_config.max_height);
2148 }
2149
2150 /* delayed so we get modes regardless of pre-fill_modes state */
2151 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002152 if (drm_mode_expose_to_userspace(mode, file_priv))
2153 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002154
2155 out_resp->connector_id = connector->base.id;
2156 out_resp->connector_type = connector->connector_type;
2157 out_resp->connector_type_id = connector->connector_type_id;
2158 out_resp->mm_width = connector->display_info.width_mm;
2159 out_resp->mm_height = connector->display_info.height_mm;
2160 out_resp->subpixel = connector->display_info.subpixel_order;
2161 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002162
2163 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002164 encoder = drm_connector_get_encoder(connector);
2165 if (encoder)
2166 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002167 else
2168 out_resp->encoder_id = 0;
2169
2170 /*
2171 * This ioctl is called twice, once to determine how much space is
2172 * needed, and the 2nd time to fill it.
2173 */
2174 if ((out_resp->count_modes >= mode_count) && mode_count) {
2175 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002176 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002177 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002178 if (!drm_mode_expose_to_userspace(mode, file_priv))
2179 continue;
2180
Daniel Stone934a8a82015-05-22 13:34:48 +01002181 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002182 if (copy_to_user(mode_ptr + copied,
2183 &u_mode, sizeof(u_mode))) {
2184 ret = -EFAULT;
2185 goto out;
2186 }
2187 copied++;
2188 }
2189 }
2190 out_resp->count_modes = mode_count;
2191
Rob Clark88a48e22014-12-18 16:01:50 -05002192 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002193 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2194 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2195 &out_resp->count_props);
2196 if (ret)
2197 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002198
2199 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2200 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002201 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002202 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2203 if (connector->encoder_ids[i] != 0) {
2204 if (put_user(connector->encoder_ids[i],
2205 encoder_ptr + copied)) {
2206 ret = -EFAULT;
2207 goto out;
2208 }
2209 copied++;
2210 }
2211 }
2212 }
2213 out_resp->count_encoders = encoders_count;
2214
2215out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002216 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002217
2218out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002219 mutex_unlock(&dev->mode_config.mutex);
2220
Dave Airlief453ba02008-11-07 14:05:41 -08002221 return ret;
2222}
2223
Daniel Vetterabd69c52014-11-25 23:50:05 +01002224static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2225{
2226 struct drm_connector *connector;
2227 struct drm_device *dev = encoder->dev;
2228 bool uses_atomic = false;
2229
2230 /* For atomic drivers only state objects are synchronously updated and
2231 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002232 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002233 if (!connector->state)
2234 continue;
2235
2236 uses_atomic = true;
2237
2238 if (connector->state->best_encoder != encoder)
2239 continue;
2240
2241 return connector->state->crtc;
2242 }
2243
2244 /* Don't return stale data (e.g. pending async disable). */
2245 if (uses_atomic)
2246 return NULL;
2247
2248 return encoder->crtc;
2249}
2250
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002251/**
2252 * drm_mode_getencoder - get encoder configuration
2253 * @dev: drm device for the ioctl
2254 * @data: data pointer for the ioctl
2255 * @file_priv: drm file for the ioctl call
2256 *
2257 * Construct a encoder configuration structure to return to the user.
2258 *
2259 * Called by the user via ioctl.
2260 *
2261 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002262 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002263 */
Dave Airlief453ba02008-11-07 14:05:41 -08002264int drm_mode_getencoder(struct drm_device *dev, void *data,
2265 struct drm_file *file_priv)
2266{
2267 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002268 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002269 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002270
Dave Airliefb3b06c2011-02-08 13:55:21 +10002271 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2272 return -EINVAL;
2273
Rob Clarka2b34e22013-10-05 16:36:52 -04002274 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002275 if (!encoder)
2276 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002277
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002278 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002279 crtc = drm_encoder_get_crtc(encoder);
2280 if (crtc)
2281 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002282 else
2283 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002284 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2285
Dave Airlief453ba02008-11-07 14:05:41 -08002286 enc_resp->encoder_type = encoder->encoder_type;
2287 enc_resp->encoder_id = encoder->base.id;
2288 enc_resp->possible_crtcs = encoder->possible_crtcs;
2289 enc_resp->possible_clones = encoder->possible_clones;
2290
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002291 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002292}
2293
2294/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002295 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002296 * @dev: DRM device
2297 * @data: ioctl data
2298 * @file_priv: DRM file info
2299 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002300 * Construct a list of plane ids to return to the user.
2301 *
2302 * Called by the user via ioctl.
2303 *
2304 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002305 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002306 */
2307int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002308 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002309{
2310 struct drm_mode_get_plane_res *plane_resp = data;
2311 struct drm_mode_config *config;
2312 struct drm_plane *plane;
2313 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002314 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002315 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002316
2317 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2318 return -EINVAL;
2319
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002320 config = &dev->mode_config;
2321
Matt Roper681e7ec2014-04-01 15:22:42 -07002322 if (file_priv->universal_planes)
2323 num_planes = config->num_total_plane;
2324 else
2325 num_planes = config->num_overlay_plane;
2326
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002327 /*
2328 * This ioctl is called twice, once to determine how much space is
2329 * needed, and the 2nd time to fill it.
2330 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002331 if (num_planes &&
2332 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002333 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002334
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002335 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002336 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002337 /*
2338 * Unless userspace set the 'universal planes'
2339 * capability bit, only advertise overlays.
2340 */
2341 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2342 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002343 continue;
2344
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002345 if (put_user(plane->base.id, plane_ptr + copied))
2346 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002347 copied++;
2348 }
2349 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002350 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002351
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002352 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002353}
2354
2355/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002356 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002357 * @dev: DRM device
2358 * @data: ioctl data
2359 * @file_priv: DRM file info
2360 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002361 * Construct a plane configuration structure to return to the user.
2362 *
2363 * Called by the user via ioctl.
2364 *
2365 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002366 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002367 */
2368int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002369 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002370{
2371 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372 struct drm_plane *plane;
2373 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002374
2375 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2376 return -EINVAL;
2377
Rob Clarka2b34e22013-10-05 16:36:52 -04002378 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002379 if (!plane)
2380 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002381
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002382 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002383 if (plane->crtc)
2384 plane_resp->crtc_id = plane->crtc->base.id;
2385 else
2386 plane_resp->crtc_id = 0;
2387
2388 if (plane->fb)
2389 plane_resp->fb_id = plane->fb->base.id;
2390 else
2391 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002392 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002393
2394 plane_resp->plane_id = plane->base.id;
2395 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002396 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002397
2398 /*
2399 * This ioctl is called twice, once to determine how much space is
2400 * needed, and the 2nd time to fill it.
2401 */
2402 if (plane->format_count &&
2403 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002404 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002405 if (copy_to_user(format_ptr,
2406 plane->format_types,
2407 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002408 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002409 }
2410 }
2411 plane_resp->count_format_types = plane->format_count;
2412
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002413 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414}
2415
Laurent Pinchartead86102015-03-05 02:25:43 +02002416/**
2417 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2418 * @plane: plane to check for format support
2419 * @format: the pixel format
2420 *
2421 * Returns:
2422 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2423 * otherwise.
2424 */
2425int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2426{
2427 unsigned int i;
2428
2429 for (i = 0; i < plane->format_count; i++) {
2430 if (format == plane->format_types[i])
2431 return 0;
2432 }
2433
2434 return -EINVAL;
2435}
2436
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002437static int check_src_coords(uint32_t src_x, uint32_t src_y,
2438 uint32_t src_w, uint32_t src_h,
2439 const struct drm_framebuffer *fb)
2440{
2441 unsigned int fb_width, fb_height;
2442
2443 fb_width = fb->width << 16;
2444 fb_height = fb->height << 16;
2445
2446 /* Make sure source coordinates are inside the fb. */
2447 if (src_w > fb_width ||
2448 src_x > fb_width - src_w ||
2449 src_h > fb_height ||
2450 src_y > fb_height - src_h) {
2451 DRM_DEBUG_KMS("Invalid source coordinates "
2452 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2453 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2454 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2455 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2456 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2457 return -ENOSPC;
2458 }
2459
2460 return 0;
2461}
2462
Matt Roperb36552b2014-06-10 08:28:09 -07002463/*
2464 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002465 *
Matt Roperb36552b2014-06-10 08:28:09 -07002466 * Note that we assume an extra reference has already been taken on fb. If the
2467 * update fails, this reference will be dropped before return; if it succeeds,
2468 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002469 *
Matt Roperb36552b2014-06-10 08:28:09 -07002470 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002471 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002472static int __setplane_internal(struct drm_plane *plane,
2473 struct drm_crtc *crtc,
2474 struct drm_framebuffer *fb,
2475 int32_t crtc_x, int32_t crtc_y,
2476 uint32_t crtc_w, uint32_t crtc_h,
2477 /* src_{x,y,w,h} values are 16.16 fixed point */
2478 uint32_t src_x, uint32_t src_y,
2479 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002480{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002481 int ret = 0;
2482
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002483 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002484 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002485 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002486 ret = plane->funcs->disable_plane(plane);
2487 if (!ret) {
2488 plane->crtc = NULL;
2489 plane->fb = NULL;
2490 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002491 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002492 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002493 goto out;
2494 }
2495
Matt Roper7f994f32014-05-29 08:06:51 -07002496 /* Check whether this plane is usable on this CRTC */
2497 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2498 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2499 ret = -EINVAL;
2500 goto out;
2501 }
2502
Ville Syrjälä62443be2011-12-20 00:06:47 +02002503 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002504 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2505 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002506 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2507 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002508 goto out;
2509 }
2510
Matt Roper3968be92015-04-13 11:06:13 -07002511 /* Give drivers some help against integer overflows */
2512 if (crtc_w > INT_MAX ||
2513 crtc_x > INT_MAX - (int32_t) crtc_w ||
2514 crtc_h > INT_MAX ||
2515 crtc_y > INT_MAX - (int32_t) crtc_h) {
2516 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2517 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002518 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002519 goto out;
2520 }
2521
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002522 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2523 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002524 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002525
Daniel Vetter3d30a592014-07-27 13:42:42 +02002526 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002527 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002528 crtc_x, crtc_y, crtc_w, crtc_h,
2529 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002530 if (!ret) {
2531 plane->crtc = crtc;
2532 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002533 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002534 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002535 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002536 }
2537
2538out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002539 if (fb)
2540 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002541 if (plane->old_fb)
2542 drm_framebuffer_unreference(plane->old_fb);
2543 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002544
2545 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002546}
Matt Roperb36552b2014-06-10 08:28:09 -07002547
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002548static int setplane_internal(struct drm_plane *plane,
2549 struct drm_crtc *crtc,
2550 struct drm_framebuffer *fb,
2551 int32_t crtc_x, int32_t crtc_y,
2552 uint32_t crtc_w, uint32_t crtc_h,
2553 /* src_{x,y,w,h} values are 16.16 fixed point */
2554 uint32_t src_x, uint32_t src_y,
2555 uint32_t src_w, uint32_t src_h)
2556{
2557 int ret;
2558
2559 drm_modeset_lock_all(plane->dev);
2560 ret = __setplane_internal(plane, crtc, fb,
2561 crtc_x, crtc_y, crtc_w, crtc_h,
2562 src_x, src_y, src_w, src_h);
2563 drm_modeset_unlock_all(plane->dev);
2564
2565 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002566}
2567
2568/**
2569 * drm_mode_setplane - configure a plane's configuration
2570 * @dev: DRM device
2571 * @data: ioctl data*
2572 * @file_priv: DRM file info
2573 *
2574 * Set plane configuration, including placement, fb, scaling, and other factors.
2575 * Or pass a NULL fb to disable (planes may be disabled without providing a
2576 * valid crtc).
2577 *
2578 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002579 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002580 */
2581int drm_mode_setplane(struct drm_device *dev, void *data,
2582 struct drm_file *file_priv)
2583{
2584 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002585 struct drm_plane *plane;
2586 struct drm_crtc *crtc = NULL;
2587 struct drm_framebuffer *fb = NULL;
2588
2589 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2590 return -EINVAL;
2591
Matt Roperb36552b2014-06-10 08:28:09 -07002592 /*
2593 * First, find the plane, crtc, and fb objects. If not available,
2594 * we don't bother to call the driver.
2595 */
Rob Clark933f6222014-11-25 20:33:11 -05002596 plane = drm_plane_find(dev, plane_req->plane_id);
2597 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002598 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2599 plane_req->plane_id);
2600 return -ENOENT;
2601 }
Matt Roperb36552b2014-06-10 08:28:09 -07002602
2603 if (plane_req->fb_id) {
2604 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2605 if (!fb) {
2606 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2607 plane_req->fb_id);
2608 return -ENOENT;
2609 }
2610
Rob Clark933f6222014-11-25 20:33:11 -05002611 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2612 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002613 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2614 plane_req->crtc_id);
2615 return -ENOENT;
2616 }
Matt Roperb36552b2014-06-10 08:28:09 -07002617 }
2618
Matt Roper161d0dc2014-06-10 08:28:10 -07002619 /*
2620 * setplane_internal will take care of deref'ing either the old or new
2621 * framebuffer depending on success.
2622 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002623 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002624 plane_req->crtc_x, plane_req->crtc_y,
2625 plane_req->crtc_w, plane_req->crtc_h,
2626 plane_req->src_x, plane_req->src_y,
2627 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002628}
2629
2630/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002631 * drm_mode_set_config_internal - helper to call ->set_config
2632 * @set: modeset config to set
2633 *
2634 * This is a little helper to wrap internal calls to the ->set_config driver
2635 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002636 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002637 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002638 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002639 */
2640int drm_mode_set_config_internal(struct drm_mode_set *set)
2641{
2642 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002643 struct drm_framebuffer *fb;
2644 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002645 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002646
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002647 /*
2648 * NOTE: ->set_config can also disable other crtcs (if we steal all
2649 * connectors from it), hence we need to refcount the fbs across all
2650 * crtcs. Atomic modeset will have saner semantics ...
2651 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002652 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002653 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002654
Daniel Vetterb0d12322012-12-11 01:07:12 +01002655 fb = set->fb;
2656
2657 ret = crtc->funcs->set_config(set);
2658 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002659 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002660 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002661 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002662
Daniel Vettere4f62542015-07-09 23:44:35 +02002663 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002664 if (tmp->primary->fb)
2665 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002666 if (tmp->primary->old_fb)
2667 drm_framebuffer_unreference(tmp->primary->old_fb);
2668 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002669 }
2670
2671 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002672}
2673EXPORT_SYMBOL(drm_mode_set_config_internal);
2674
Matt Roperaf936292014-04-01 15:22:34 -07002675/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002676 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2677 * @mode: mode to query
2678 * @hdisplay: hdisplay value to fill in
2679 * @vdisplay: vdisplay value to fill in
2680 *
2681 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2682 * the appropriate layout.
2683 */
2684void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2685 int *hdisplay, int *vdisplay)
2686{
2687 struct drm_display_mode adjusted;
2688
2689 drm_mode_copy(&adjusted, mode);
2690 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2691 *hdisplay = adjusted.crtc_hdisplay;
2692 *vdisplay = adjusted.crtc_vdisplay;
2693}
2694EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2695
2696/**
Matt Roperaf936292014-04-01 15:22:34 -07002697 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2698 * CRTC viewport
2699 * @crtc: CRTC that framebuffer will be displayed on
2700 * @x: x panning
2701 * @y: y panning
2702 * @mode: mode that framebuffer will be displayed under
2703 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002704 */
Matt Roperaf936292014-04-01 15:22:34 -07002705int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2706 int x, int y,
2707 const struct drm_display_mode *mode,
2708 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002709
2710{
2711 int hdisplay, vdisplay;
2712
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002713 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002714
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002715 if (crtc->state &&
2716 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2717 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002718 swap(hdisplay, vdisplay);
2719
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002720 return check_src_coords(x << 16, y << 16,
2721 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002722}
Matt Roperaf936292014-04-01 15:22:34 -07002723EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002724
Daniel Vetter2d13b672012-12-11 13:47:23 +01002725/**
Dave Airlief453ba02008-11-07 14:05:41 -08002726 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002727 * @dev: drm device for the ioctl
2728 * @data: data pointer for the ioctl
2729 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002730 *
Dave Airlief453ba02008-11-07 14:05:41 -08002731 * Build a new CRTC configuration based on user request.
2732 *
2733 * Called by the user via ioctl.
2734 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002735 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002736 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002737 */
2738int drm_mode_setcrtc(struct drm_device *dev, void *data,
2739 struct drm_file *file_priv)
2740{
2741 struct drm_mode_config *config = &dev->mode_config;
2742 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002743 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002744 struct drm_connector **connector_set = NULL, *connector;
2745 struct drm_framebuffer *fb = NULL;
2746 struct drm_display_mode *mode = NULL;
2747 struct drm_mode_set set;
2748 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002749 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002750 int i;
2751
Dave Airliefb3b06c2011-02-08 13:55:21 +10002752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2753 return -EINVAL;
2754
Zhao Junwang01447e92015-07-07 17:08:35 +08002755 /*
2756 * Universal plane src offsets are only 16.16, prevent havoc for
2757 * drivers using universal plane code internally.
2758 */
2759 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002760 return -ERANGE;
2761
Daniel Vetter84849902012-12-02 00:28:11 +01002762 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002763 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2764 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002765 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002766 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002767 goto out;
2768 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002769 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002770
2771 if (crtc_req->mode_valid) {
2772 /* If we have a mode we need a framebuffer. */
2773 /* If we pass -1, set the mode with the currently bound fb */
2774 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002775 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002776 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2777 ret = -EINVAL;
2778 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002779 }
Matt Roperf4510a22014-04-01 15:22:40 -07002780 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002781 /* Make refcounting symmetric with the lookup path. */
2782 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002783 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002784 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2785 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002786 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2787 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002788 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002789 goto out;
2790 }
Dave Airlief453ba02008-11-07 14:05:41 -08002791 }
2792
2793 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002794 if (!mode) {
2795 ret = -ENOMEM;
2796 goto out;
2797 }
2798
Daniel Stone934a8a82015-05-22 13:34:48 +01002799 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002800 if (ret) {
2801 DRM_DEBUG_KMS("Invalid mode\n");
2802 goto out;
2803 }
2804
Dave Airlief453ba02008-11-07 14:05:41 -08002805 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002806
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002807 /*
2808 * Check whether the primary plane supports the fb pixel format.
2809 * Drivers not implementing the universal planes API use a
2810 * default formats list provided by the DRM core which doesn't
2811 * match real hardware capabilities. Skip the check in that
2812 * case.
2813 */
2814 if (!crtc->primary->format_default) {
2815 ret = drm_plane_check_pixel_format(crtc->primary,
2816 fb->pixel_format);
2817 if (ret) {
2818 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2819 drm_get_format_name(fb->pixel_format));
2820 goto out;
2821 }
2822 }
2823
Damien Lespiauc11e9282013-09-25 16:45:30 +01002824 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2825 mode, fb);
2826 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002827 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002828
Dave Airlief453ba02008-11-07 14:05:41 -08002829 }
2830
2831 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002832 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002833 ret = -EINVAL;
2834 goto out;
2835 }
2836
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002837 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002838 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002839 crtc_req->count_connectors);
2840 ret = -EINVAL;
2841 goto out;
2842 }
2843
2844 if (crtc_req->count_connectors > 0) {
2845 u32 out_id;
2846
2847 /* Avoid unbounded kernel memory allocation */
2848 if (crtc_req->count_connectors > config->num_connector) {
2849 ret = -EINVAL;
2850 goto out;
2851 }
2852
Thierry Reding2f6c5382014-12-10 13:03:36 +01002853 connector_set = kmalloc_array(crtc_req->count_connectors,
2854 sizeof(struct drm_connector *),
2855 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002856 if (!connector_set) {
2857 ret = -ENOMEM;
2858 goto out;
2859 }
2860
2861 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002862 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002863 if (get_user(out_id, &set_connectors_ptr[i])) {
2864 ret = -EFAULT;
2865 goto out;
2866 }
2867
Rob Clarka2b34e22013-10-05 16:36:52 -04002868 connector = drm_connector_find(dev, out_id);
2869 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002870 DRM_DEBUG_KMS("Connector id %d unknown\n",
2871 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002872 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002873 goto out;
2874 }
Jerome Glisse94401062010-07-15 15:43:25 -04002875 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2876 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002877 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002878
2879 connector_set[i] = connector;
2880 }
2881 }
2882
2883 set.crtc = crtc;
2884 set.x = crtc_req->x;
2885 set.y = crtc_req->y;
2886 set.mode = mode;
2887 set.connectors = connector_set;
2888 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002889 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002890 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002891
2892out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002893 if (fb)
2894 drm_framebuffer_unreference(fb);
2895
Dave Airlief453ba02008-11-07 14:05:41 -08002896 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002897 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002898 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002899 return ret;
2900}
2901
Matt Roper161d0dc2014-06-10 08:28:10 -07002902/**
2903 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2904 * universal plane handler call
2905 * @crtc: crtc to update cursor for
2906 * @req: data pointer for the ioctl
2907 * @file_priv: drm file for the ioctl call
2908 *
2909 * Legacy cursor ioctl's work directly with driver buffer handles. To
2910 * translate legacy ioctl calls into universal plane handler calls, we need to
2911 * wrap the native buffer handle in a drm_framebuffer.
2912 *
2913 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2914 * buffer with a pitch of 4*width; the universal plane interface should be used
2915 * directly in cases where the hardware can support other buffer settings and
2916 * userspace wants to make use of these capabilities.
2917 *
2918 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002919 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002920 */
2921static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2922 struct drm_mode_cursor2 *req,
2923 struct drm_file *file_priv)
2924{
2925 struct drm_device *dev = crtc->dev;
2926 struct drm_framebuffer *fb = NULL;
2927 struct drm_mode_fb_cmd2 fbreq = {
2928 .width = req->width,
2929 .height = req->height,
2930 .pixel_format = DRM_FORMAT_ARGB8888,
2931 .pitches = { req->width * 4 },
2932 .handles = { req->handle },
2933 };
2934 int32_t crtc_x, crtc_y;
2935 uint32_t crtc_w = 0, crtc_h = 0;
2936 uint32_t src_w = 0, src_h = 0;
2937 int ret = 0;
2938
2939 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002940 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002941
2942 /*
2943 * Obtain fb we'll be using (either new or existing) and take an extra
2944 * reference to it if fb != null. setplane will take care of dropping
2945 * the reference if the plane update fails.
2946 */
2947 if (req->flags & DRM_MODE_CURSOR_BO) {
2948 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002949 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002950 if (IS_ERR(fb)) {
2951 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2952 return PTR_ERR(fb);
2953 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002954 } else {
2955 fb = NULL;
2956 }
2957 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002958 fb = crtc->cursor->fb;
2959 if (fb)
2960 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002961 }
2962
2963 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2964 crtc_x = req->x;
2965 crtc_y = req->y;
2966 } else {
2967 crtc_x = crtc->cursor_x;
2968 crtc_y = crtc->cursor_y;
2969 }
2970
2971 if (fb) {
2972 crtc_w = fb->width;
2973 crtc_h = fb->height;
2974 src_w = fb->width << 16;
2975 src_h = fb->height << 16;
2976 }
2977
2978 /*
2979 * setplane_internal will take care of deref'ing either the old or new
2980 * framebuffer depending on success.
2981 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002982 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002983 crtc_x, crtc_y, crtc_w, crtc_h,
2984 0, 0, src_w, src_h);
2985
2986 /* Update successful; save new cursor position, if necessary */
2987 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2988 crtc->cursor_x = req->x;
2989 crtc->cursor_y = req->y;
2990 }
2991
2992 return ret;
2993}
2994
Dave Airlie4c813d42013-06-20 11:48:52 +10002995static int drm_mode_cursor_common(struct drm_device *dev,
2996 struct drm_mode_cursor2 *req,
2997 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002998{
Dave Airlief453ba02008-11-07 14:05:41 -08002999 struct drm_crtc *crtc;
3000 int ret = 0;
3001
Dave Airliefb3b06c2011-02-08 13:55:21 +10003002 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3003 return -EINVAL;
3004
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003005 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003006 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003007
Rob Clarka2b34e22013-10-05 16:36:52 -04003008 crtc = drm_crtc_find(dev, req->crtc_id);
3009 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003010 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003011 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003012 }
Dave Airlief453ba02008-11-07 14:05:41 -08003013
Matt Roper161d0dc2014-06-10 08:28:10 -07003014 /*
3015 * If this crtc has a universal cursor plane, call that plane's update
3016 * handler rather than using legacy cursor handlers.
3017 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003018 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003019 if (crtc->cursor) {
3020 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3021 goto out;
3022 }
3023
Dave Airlief453ba02008-11-07 14:05:41 -08003024 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003025 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003026 ret = -ENXIO;
3027 goto out;
3028 }
3029 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003030 if (crtc->funcs->cursor_set2)
3031 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3032 req->width, req->height, req->hot_x, req->hot_y);
3033 else
3034 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3035 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003036 }
3037
3038 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3039 if (crtc->funcs->cursor_move) {
3040 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3041 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003042 ret = -EFAULT;
3043 goto out;
3044 }
3045 }
3046out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003047 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003048
Dave Airlief453ba02008-11-07 14:05:41 -08003049 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003050
3051}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003052
3053
3054/**
3055 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3056 * @dev: drm device for the ioctl
3057 * @data: data pointer for the ioctl
3058 * @file_priv: drm file for the ioctl call
3059 *
3060 * Set the cursor configuration based on user request.
3061 *
3062 * Called by the user via ioctl.
3063 *
3064 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003065 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003066 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003067int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003068 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003069{
3070 struct drm_mode_cursor *req = data;
3071 struct drm_mode_cursor2 new_req;
3072
3073 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3074 new_req.hot_x = new_req.hot_y = 0;
3075
3076 return drm_mode_cursor_common(dev, &new_req, file_priv);
3077}
3078
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003079/**
3080 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3081 * @dev: drm device for the ioctl
3082 * @data: data pointer for the ioctl
3083 * @file_priv: drm file for the ioctl call
3084 *
3085 * Set the cursor configuration based on user request. This implements the 2nd
3086 * version of the cursor ioctl, which allows userspace to additionally specify
3087 * the hotspot of the pointer.
3088 *
3089 * Called by the user via ioctl.
3090 *
3091 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003092 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003093 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003094int drm_mode_cursor2_ioctl(struct drm_device *dev,
3095 void *data, struct drm_file *file_priv)
3096{
3097 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003098
Dave Airlie4c813d42013-06-20 11:48:52 +10003099 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003100}
3101
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003102/**
3103 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3104 * @bpp: bits per pixels
3105 * @depth: bit depth per pixel
3106 *
3107 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3108 * Useful in fbdev emulation code, since that deals in those values.
3109 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003110uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3111{
3112 uint32_t fmt;
3113
3114 switch (bpp) {
3115 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003116 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003117 break;
3118 case 16:
3119 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003120 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003121 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003122 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003123 break;
3124 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003125 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003126 break;
3127 case 32:
3128 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003129 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003130 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003131 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003132 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003133 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003134 break;
3135 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003136 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3137 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003138 break;
3139 }
3140
3141 return fmt;
3142}
3143EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3144
Dave Airlief453ba02008-11-07 14:05:41 -08003145/**
3146 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003147 * @dev: drm device for the ioctl
3148 * @data: data pointer for the ioctl
3149 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003150 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003151 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003152 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003153 *
3154 * Called by the user via ioctl.
3155 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003156 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003157 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003158 */
3159int drm_mode_addfb(struct drm_device *dev,
3160 void *data, struct drm_file *file_priv)
3161{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003162 struct drm_mode_fb_cmd *or = data;
3163 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003164 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003165
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003166 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003167 r.fb_id = or->fb_id;
3168 r.width = or->width;
3169 r.height = or->height;
3170 r.pitches[0] = or->pitch;
3171 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3172 r.handles[0] = or->handle;
3173
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003174 ret = drm_mode_addfb2(dev, &r, file_priv);
3175 if (ret)
3176 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003177
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003178 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003179
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003180 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003181}
3182
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003183static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003184{
3185 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3186
3187 switch (format) {
3188 case DRM_FORMAT_C8:
3189 case DRM_FORMAT_RGB332:
3190 case DRM_FORMAT_BGR233:
3191 case DRM_FORMAT_XRGB4444:
3192 case DRM_FORMAT_XBGR4444:
3193 case DRM_FORMAT_RGBX4444:
3194 case DRM_FORMAT_BGRX4444:
3195 case DRM_FORMAT_ARGB4444:
3196 case DRM_FORMAT_ABGR4444:
3197 case DRM_FORMAT_RGBA4444:
3198 case DRM_FORMAT_BGRA4444:
3199 case DRM_FORMAT_XRGB1555:
3200 case DRM_FORMAT_XBGR1555:
3201 case DRM_FORMAT_RGBX5551:
3202 case DRM_FORMAT_BGRX5551:
3203 case DRM_FORMAT_ARGB1555:
3204 case DRM_FORMAT_ABGR1555:
3205 case DRM_FORMAT_RGBA5551:
3206 case DRM_FORMAT_BGRA5551:
3207 case DRM_FORMAT_RGB565:
3208 case DRM_FORMAT_BGR565:
3209 case DRM_FORMAT_RGB888:
3210 case DRM_FORMAT_BGR888:
3211 case DRM_FORMAT_XRGB8888:
3212 case DRM_FORMAT_XBGR8888:
3213 case DRM_FORMAT_RGBX8888:
3214 case DRM_FORMAT_BGRX8888:
3215 case DRM_FORMAT_ARGB8888:
3216 case DRM_FORMAT_ABGR8888:
3217 case DRM_FORMAT_RGBA8888:
3218 case DRM_FORMAT_BGRA8888:
3219 case DRM_FORMAT_XRGB2101010:
3220 case DRM_FORMAT_XBGR2101010:
3221 case DRM_FORMAT_RGBX1010102:
3222 case DRM_FORMAT_BGRX1010102:
3223 case DRM_FORMAT_ARGB2101010:
3224 case DRM_FORMAT_ABGR2101010:
3225 case DRM_FORMAT_RGBA1010102:
3226 case DRM_FORMAT_BGRA1010102:
3227 case DRM_FORMAT_YUYV:
3228 case DRM_FORMAT_YVYU:
3229 case DRM_FORMAT_UYVY:
3230 case DRM_FORMAT_VYUY:
3231 case DRM_FORMAT_AYUV:
3232 case DRM_FORMAT_NV12:
3233 case DRM_FORMAT_NV21:
3234 case DRM_FORMAT_NV16:
3235 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003236 case DRM_FORMAT_NV24:
3237 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003238 case DRM_FORMAT_YUV410:
3239 case DRM_FORMAT_YVU410:
3240 case DRM_FORMAT_YUV411:
3241 case DRM_FORMAT_YVU411:
3242 case DRM_FORMAT_YUV420:
3243 case DRM_FORMAT_YVU420:
3244 case DRM_FORMAT_YUV422:
3245 case DRM_FORMAT_YVU422:
3246 case DRM_FORMAT_YUV444:
3247 case DRM_FORMAT_YVU444:
3248 return 0;
3249 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003250 DRM_DEBUG_KMS("invalid pixel format %s\n",
3251 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003252 return -EINVAL;
3253 }
3254}
3255
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003256static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003257{
3258 int ret, hsub, vsub, num_planes, i;
3259
3260 ret = format_check(r);
3261 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003262 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3263 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003264 return ret;
3265 }
3266
3267 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3268 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3269 num_planes = drm_format_num_planes(r->pixel_format);
3270
3271 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003272 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003273 return -EINVAL;
3274 }
3275
3276 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003277 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003278 return -EINVAL;
3279 }
3280
3281 for (i = 0; i < num_planes; i++) {
3282 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003283 unsigned int height = r->height / (i != 0 ? vsub : 1);
3284 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003285
3286 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003287 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003288 return -EINVAL;
3289 }
3290
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003291 if ((uint64_t) width * cpp > UINT_MAX)
3292 return -ERANGE;
3293
3294 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3295 return -ERANGE;
3296
3297 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003298 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003299 return -EINVAL;
3300 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003301
3302 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3303 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3304 r->modifier[i], i);
3305 return -EINVAL;
3306 }
Rob Clark570655b2015-01-30 20:18:11 +05303307
3308 /* modifier specific checks: */
3309 switch (r->modifier[i]) {
3310 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3311 /* NOTE: the pitch restriction may be lifted later if it turns
3312 * out that no hw has this restriction:
3313 */
3314 if (r->pixel_format != DRM_FORMAT_NV12 ||
3315 width % 128 || height % 32 ||
3316 r->pitches[i] % 128) {
3317 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3318 return -EINVAL;
3319 }
3320 break;
3321
3322 default:
3323 break;
3324 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003325 }
3326
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003327 for (i = num_planes; i < 4; i++) {
3328 if (r->modifier[i]) {
3329 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3330 return -EINVAL;
3331 }
3332
3333 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3334 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3335 continue;
3336
3337 if (r->handles[i]) {
3338 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3339 return -EINVAL;
3340 }
3341
3342 if (r->pitches[i]) {
3343 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3344 return -EINVAL;
3345 }
3346
3347 if (r->offsets[i]) {
3348 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3349 return -EINVAL;
3350 }
3351 }
3352
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003353 return 0;
3354}
3355
Chris Wilson9a6f5132015-02-25 13:45:26 +00003356static struct drm_framebuffer *
3357internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003358 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003359 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003360{
3361 struct drm_mode_config *config = &dev->mode_config;
3362 struct drm_framebuffer *fb;
3363 int ret;
3364
Rob Clarke3eb3252015-02-05 14:41:52 +00003365 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003366 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3367 return ERR_PTR(-EINVAL);
3368 }
3369
3370 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3371 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3372 r->width, config->min_width, config->max_width);
3373 return ERR_PTR(-EINVAL);
3374 }
3375 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3376 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3377 r->height, config->min_height, config->max_height);
3378 return ERR_PTR(-EINVAL);
3379 }
3380
Rob Clarke3eb3252015-02-05 14:41:52 +00003381 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3382 !dev->mode_config.allow_fb_modifiers) {
3383 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3384 return ERR_PTR(-EINVAL);
3385 }
3386
Matt Roperc394c2b2014-06-10 08:28:08 -07003387 ret = framebuffer_check(r);
3388 if (ret)
3389 return ERR_PTR(ret);
3390
3391 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3392 if (IS_ERR(fb)) {
3393 DRM_DEBUG_KMS("could not create framebuffer\n");
3394 return fb;
3395 }
3396
Matt Roperc394c2b2014-06-10 08:28:08 -07003397 return fb;
3398}
3399
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003400/**
3401 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003402 * @dev: drm device for the ioctl
3403 * @data: data pointer for the ioctl
3404 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003405 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003406 * Add a new FB to the specified CRTC, given a user request with format. This is
3407 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3408 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003409 *
3410 * Called by the user via ioctl.
3411 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003412 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003413 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003414 */
3415int drm_mode_addfb2(struct drm_device *dev,
3416 void *data, struct drm_file *file_priv)
3417{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003418 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003419 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003420
Dave Airliefb3b06c2011-02-08 13:55:21 +10003421 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3422 return -EINVAL;
3423
Chris Wilson9a6f5132015-02-25 13:45:26 +00003424 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003425 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003426 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003427
Chris Wilson9a6f5132015-02-25 13:45:26 +00003428 /* Transfer ownership to the filp for reaping on close */
3429
3430 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3431 mutex_lock(&file_priv->fbs_lock);
3432 r->fb_id = fb->base.id;
3433 list_add(&fb->filp_head, &file_priv->fbs);
3434 mutex_unlock(&file_priv->fbs_lock);
3435
Matt Roperc394c2b2014-06-10 08:28:08 -07003436 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003437}
3438
3439/**
3440 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003441 * @dev: drm device for the ioctl
3442 * @data: data pointer for the ioctl
3443 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003444 *
Dave Airlief453ba02008-11-07 14:05:41 -08003445 * Remove the FB specified by the user.
3446 *
3447 * Called by the user via ioctl.
3448 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003449 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003450 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003451 */
3452int drm_mode_rmfb(struct drm_device *dev,
3453 void *data, struct drm_file *file_priv)
3454{
Dave Airlief453ba02008-11-07 14:05:41 -08003455 struct drm_framebuffer *fb = NULL;
3456 struct drm_framebuffer *fbl = NULL;
3457 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003458 int found = 0;
3459
Dave Airliefb3b06c2011-02-08 13:55:21 +10003460 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3461 return -EINVAL;
3462
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003463 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003464 mutex_lock(&dev->mode_config.fb_lock);
3465 fb = __drm_framebuffer_lookup(dev, *id);
3466 if (!fb)
3467 goto fail_lookup;
3468
Dave Airlief453ba02008-11-07 14:05:41 -08003469 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3470 if (fb == fbl)
3471 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003472 if (!found)
3473 goto fail_lookup;
3474
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003475 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003476 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003477 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003478
Maarten Lankhorst13803132015-09-09 16:40:56 +02003479 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003480
Daniel Vetter2b677e82012-12-10 21:16:05 +01003481 return 0;
3482
3483fail_lookup:
3484 mutex_unlock(&dev->mode_config.fb_lock);
3485 mutex_unlock(&file_priv->fbs_lock);
3486
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003487 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003488}
3489
3490/**
3491 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003492 * @dev: drm device for the ioctl
3493 * @data: data pointer for the ioctl
3494 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003495 *
Dave Airlief453ba02008-11-07 14:05:41 -08003496 * Lookup the FB given its ID and return info about it.
3497 *
3498 * Called by the user via ioctl.
3499 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003500 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003501 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003502 */
3503int drm_mode_getfb(struct drm_device *dev,
3504 void *data, struct drm_file *file_priv)
3505{
3506 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003507 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003508 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003509
Dave Airliefb3b06c2011-02-08 13:55:21 +10003510 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3511 return -EINVAL;
3512
Daniel Vetter786b99e2012-12-02 21:53:40 +01003513 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003514 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003515 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003516
3517 r->height = fb->height;
3518 r->width = fb->width;
3519 r->depth = fb->depth;
3520 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003521 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003522 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003523 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003524 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003525 ret = fb->funcs->create_handle(fb, file_priv,
3526 &r->handle);
3527 } else {
3528 /* GET_FB() is an unprivileged ioctl so we must not
3529 * return a buffer-handle to non-master processes! For
3530 * backwards-compatibility reasons, we cannot make
3531 * GET_FB() privileged, so just return an invalid handle
3532 * for non-masters. */
3533 r->handle = 0;
3534 ret = 0;
3535 }
3536 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003537 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003538 }
Dave Airlief453ba02008-11-07 14:05:41 -08003539
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003540 drm_framebuffer_unreference(fb);
3541
Dave Airlief453ba02008-11-07 14:05:41 -08003542 return ret;
3543}
3544
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003545/**
3546 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3547 * @dev: drm device for the ioctl
3548 * @data: data pointer for the ioctl
3549 * @file_priv: drm file for the ioctl call
3550 *
3551 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3552 * rectangle list. Generic userspace which does frontbuffer rendering must call
3553 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3554 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3555 *
3556 * Modesetting drivers which always update the frontbuffer do not need to
3557 * implement the corresponding ->dirty framebuffer callback.
3558 *
3559 * Called by the user via ioctl.
3560 *
3561 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003562 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003563 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003564int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3565 void *data, struct drm_file *file_priv)
3566{
3567 struct drm_clip_rect __user *clips_ptr;
3568 struct drm_clip_rect *clips = NULL;
3569 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003570 struct drm_framebuffer *fb;
3571 unsigned flags;
3572 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003573 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003574
Dave Airliefb3b06c2011-02-08 13:55:21 +10003575 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3576 return -EINVAL;
3577
Daniel Vetter786b99e2012-12-02 21:53:40 +01003578 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003579 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003580 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003581
3582 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003583 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003584
3585 if (!num_clips != !clips_ptr) {
3586 ret = -EINVAL;
3587 goto out_err1;
3588 }
3589
3590 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3591
3592 /* If userspace annotates copy, clips must come in pairs */
3593 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3594 ret = -EINVAL;
3595 goto out_err1;
3596 }
3597
3598 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003599 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3600 ret = -EINVAL;
3601 goto out_err1;
3602 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003603 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003604 if (!clips) {
3605 ret = -ENOMEM;
3606 goto out_err1;
3607 }
3608
3609 ret = copy_from_user(clips, clips_ptr,
3610 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003611 if (ret) {
3612 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003613 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003614 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003615 }
3616
3617 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003618 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3619 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003620 } else {
3621 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003622 }
3623
3624out_err2:
3625 kfree(clips);
3626out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003627 drm_framebuffer_unreference(fb);
3628
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003629 return ret;
3630}
3631
3632
Dave Airlief453ba02008-11-07 14:05:41 -08003633/**
3634 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003635 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003636 *
Dave Airlief453ba02008-11-07 14:05:41 -08003637 * Destroy all the FBs associated with @filp.
3638 *
3639 * Called by the user via ioctl.
3640 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003641 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003642 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003643 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003644void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003645{
Dave Airlief453ba02008-11-07 14:05:41 -08003646 struct drm_framebuffer *fb, *tfb;
3647
Daniel Vetter1b116292014-09-24 21:51:06 +02003648 /*
3649 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003650 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003651 * avoid upsetting lockdep since the universal cursor code adds a
3652 * framebuffer while holding mutex locks.
3653 *
3654 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3655 * locks is impossible here since no one else but this function can get
3656 * at it any more.
3657 */
Dave Airlief453ba02008-11-07 14:05:41 -08003658 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003659 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003660
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003661 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003662 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003663 }
Dave Airlief453ba02008-11-07 14:05:41 -08003664}
3665
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003666/**
3667 * drm_property_create - create a new property type
3668 * @dev: drm device
3669 * @flags: flags specifying the property type
3670 * @name: name of the property
3671 * @num_values: number of pre-defined values
3672 *
3673 * This creates a new generic drm property which can then be attached to a drm
3674 * object with drm_object_attach_property. The returned property object must be
3675 * freed with drm_property_destroy.
3676 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003677 * Note that the DRM core keeps a per-device list of properties and that, if
3678 * drm_mode_config_cleanup() is called, it will destroy all properties created
3679 * by the driver.
3680 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003681 * Returns:
3682 * A pointer to the newly created property on success, NULL on failure.
3683 */
Dave Airlief453ba02008-11-07 14:05:41 -08003684struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3685 const char *name, int num_values)
3686{
3687 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003688 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003689
3690 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3691 if (!property)
3692 return NULL;
3693
Rob Clark98f75de2014-05-30 11:37:03 -04003694 property->dev = dev;
3695
Dave Airlief453ba02008-11-07 14:05:41 -08003696 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003697 property->values = kcalloc(num_values, sizeof(uint64_t),
3698 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003699 if (!property->values)
3700 goto fail;
3701 }
3702
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003703 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3704 if (ret)
3705 goto fail;
3706
Dave Airlief453ba02008-11-07 14:05:41 -08003707 property->flags = flags;
3708 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003709 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003710
Vinson Lee471dd2e2011-11-10 11:55:40 -08003711 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003712 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003713 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3714 }
Dave Airlief453ba02008-11-07 14:05:41 -08003715
3716 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003717
3718 WARN_ON(!drm_property_type_valid(property));
3719
Dave Airlief453ba02008-11-07 14:05:41 -08003720 return property;
3721fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003722 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003723 kfree(property);
3724 return NULL;
3725}
3726EXPORT_SYMBOL(drm_property_create);
3727
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003728/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003729 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003730 * @dev: drm device
3731 * @flags: flags specifying the property type
3732 * @name: name of the property
3733 * @props: enumeration lists with property values
3734 * @num_values: number of pre-defined values
3735 *
3736 * This creates a new generic drm property which can then be attached to a drm
3737 * object with drm_object_attach_property. The returned property object must be
3738 * freed with drm_property_destroy.
3739 *
3740 * Userspace is only allowed to set one of the predefined values for enumeration
3741 * properties.
3742 *
3743 * Returns:
3744 * A pointer to the newly created property on success, NULL on failure.
3745 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003746struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3747 const char *name,
3748 const struct drm_prop_enum_list *props,
3749 int num_values)
3750{
3751 struct drm_property *property;
3752 int i, ret;
3753
3754 flags |= DRM_MODE_PROP_ENUM;
3755
3756 property = drm_property_create(dev, flags, name, num_values);
3757 if (!property)
3758 return NULL;
3759
3760 for (i = 0; i < num_values; i++) {
3761 ret = drm_property_add_enum(property, i,
3762 props[i].type,
3763 props[i].name);
3764 if (ret) {
3765 drm_property_destroy(dev, property);
3766 return NULL;
3767 }
3768 }
3769
3770 return property;
3771}
3772EXPORT_SYMBOL(drm_property_create_enum);
3773
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003774/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003775 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003776 * @dev: drm device
3777 * @flags: flags specifying the property type
3778 * @name: name of the property
3779 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003780 * @num_props: size of the @props array
3781 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003782 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003783 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003784 * object with drm_object_attach_property. The returned property object must be
3785 * freed with drm_property_destroy.
3786 *
3787 * Compared to plain enumeration properties userspace is allowed to set any
3788 * or'ed together combination of the predefined property bitflag values
3789 *
3790 * Returns:
3791 * A pointer to the newly created property on success, NULL on failure.
3792 */
Rob Clark49e27542012-05-17 02:23:26 -06003793struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3794 int flags, const char *name,
3795 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303796 int num_props,
3797 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003798{
3799 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303800 int i, ret, index = 0;
3801 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003802
3803 flags |= DRM_MODE_PROP_BITMASK;
3804
3805 property = drm_property_create(dev, flags, name, num_values);
3806 if (!property)
3807 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303808 for (i = 0; i < num_props; i++) {
3809 if (!(supported_bits & (1ULL << props[i].type)))
3810 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003811
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303812 if (WARN_ON(index >= num_values)) {
3813 drm_property_destroy(dev, property);
3814 return NULL;
3815 }
3816
3817 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003818 props[i].type,
3819 props[i].name);
3820 if (ret) {
3821 drm_property_destroy(dev, property);
3822 return NULL;
3823 }
3824 }
3825
3826 return property;
3827}
3828EXPORT_SYMBOL(drm_property_create_bitmask);
3829
Rob Clarkebc44cf2012-09-12 22:22:31 -05003830static struct drm_property *property_create_range(struct drm_device *dev,
3831 int flags, const char *name,
3832 uint64_t min, uint64_t max)
3833{
3834 struct drm_property *property;
3835
3836 property = drm_property_create(dev, flags, name, 2);
3837 if (!property)
3838 return NULL;
3839
3840 property->values[0] = min;
3841 property->values[1] = max;
3842
3843 return property;
3844}
3845
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003846/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003847 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003848 * @dev: drm device
3849 * @flags: flags specifying the property type
3850 * @name: name of the property
3851 * @min: minimum value of the property
3852 * @max: maximum value of the property
3853 *
3854 * This creates a new generic drm property which can then be attached to a drm
3855 * object with drm_object_attach_property. The returned property object must be
3856 * freed with drm_property_destroy.
3857 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003858 * Userspace is allowed to set any unsigned integer value in the (min, max)
3859 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003860 *
3861 * Returns:
3862 * A pointer to the newly created property on success, NULL on failure.
3863 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003864struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3865 const char *name,
3866 uint64_t min, uint64_t max)
3867{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003868 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3869 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003870}
3871EXPORT_SYMBOL(drm_property_create_range);
3872
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003873/**
3874 * drm_property_create_signed_range - create a new signed ranged property type
3875 * @dev: drm device
3876 * @flags: flags specifying the property type
3877 * @name: name of the property
3878 * @min: minimum value of the property
3879 * @max: maximum value of the property
3880 *
3881 * This creates a new generic drm property which can then be attached to a drm
3882 * object with drm_object_attach_property. The returned property object must be
3883 * freed with drm_property_destroy.
3884 *
3885 * Userspace is allowed to set any signed integer value in the (min, max)
3886 * range inclusive.
3887 *
3888 * Returns:
3889 * A pointer to the newly created property on success, NULL on failure.
3890 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003891struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3892 int flags, const char *name,
3893 int64_t min, int64_t max)
3894{
3895 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3896 name, I642U64(min), I642U64(max));
3897}
3898EXPORT_SYMBOL(drm_property_create_signed_range);
3899
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003900/**
3901 * drm_property_create_object - create a new object property type
3902 * @dev: drm device
3903 * @flags: flags specifying the property type
3904 * @name: name of the property
3905 * @type: object type from DRM_MODE_OBJECT_* defines
3906 *
3907 * This creates a new generic drm property which can then be attached to a drm
3908 * object with drm_object_attach_property. The returned property object must be
3909 * freed with drm_property_destroy.
3910 *
3911 * Userspace is only allowed to set this to any property value of the given
3912 * @type. Only useful for atomic properties, which is enforced.
3913 *
3914 * Returns:
3915 * A pointer to the newly created property on success, NULL on failure.
3916 */
Rob Clark98f75de2014-05-30 11:37:03 -04003917struct drm_property *drm_property_create_object(struct drm_device *dev,
3918 int flags, const char *name, uint32_t type)
3919{
3920 struct drm_property *property;
3921
3922 flags |= DRM_MODE_PROP_OBJECT;
3923
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003924 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3925 return NULL;
3926
Rob Clark98f75de2014-05-30 11:37:03 -04003927 property = drm_property_create(dev, flags, name, 1);
3928 if (!property)
3929 return NULL;
3930
3931 property->values[0] = type;
3932
3933 return property;
3934}
3935EXPORT_SYMBOL(drm_property_create_object);
3936
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003937/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003938 * drm_property_create_bool - create a new boolean property type
3939 * @dev: drm device
3940 * @flags: flags specifying the property type
3941 * @name: name of the property
3942 *
3943 * This creates a new generic drm property which can then be attached to a drm
3944 * object with drm_object_attach_property. The returned property object must be
3945 * freed with drm_property_destroy.
3946 *
3947 * This is implemented as a ranged property with only {0, 1} as valid values.
3948 *
3949 * Returns:
3950 * A pointer to the newly created property on success, NULL on failure.
3951 */
3952struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3953 const char *name)
3954{
3955 return drm_property_create_range(dev, flags, name, 0, 1);
3956}
3957EXPORT_SYMBOL(drm_property_create_bool);
3958
3959/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003960 * drm_property_add_enum - add a possible value to an enumeration property
3961 * @property: enumeration property to change
3962 * @index: index of the new enumeration
3963 * @value: value of the new enumeration
3964 * @name: symbolic name of the new enumeration
3965 *
3966 * This functions adds enumerations to a property.
3967 *
3968 * It's use is deprecated, drivers should use one of the more specific helpers
3969 * to directly create the property with all enumerations already attached.
3970 *
3971 * Returns:
3972 * Zero on success, error code on failure.
3973 */
Dave Airlief453ba02008-11-07 14:05:41 -08003974int drm_property_add_enum(struct drm_property *property, int index,
3975 uint64_t value, const char *name)
3976{
3977 struct drm_property_enum *prop_enum;
3978
Rob Clark5ea22f22014-05-30 11:34:01 -04003979 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3980 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003981 return -EINVAL;
3982
3983 /*
3984 * Bitmask enum properties have the additional constraint of values
3985 * from 0 to 63
3986 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003987 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3988 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003989 return -EINVAL;
3990
Daniel Vetter3758b342014-11-19 18:38:10 +01003991 if (!list_empty(&property->enum_list)) {
3992 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003993 if (prop_enum->value == value) {
3994 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3995 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3996 return 0;
3997 }
3998 }
3999 }
4000
4001 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4002 if (!prop_enum)
4003 return -ENOMEM;
4004
4005 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4006 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4007 prop_enum->value = value;
4008
4009 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004010 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004011 return 0;
4012}
4013EXPORT_SYMBOL(drm_property_add_enum);
4014
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004015/**
4016 * drm_property_destroy - destroy a drm property
4017 * @dev: drm device
4018 * @property: property to destry
4019 *
4020 * This function frees a property including any attached resources like
4021 * enumeration values.
4022 */
Dave Airlief453ba02008-11-07 14:05:41 -08004023void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4024{
4025 struct drm_property_enum *prop_enum, *pt;
4026
Daniel Vetter3758b342014-11-19 18:38:10 +01004027 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004028 list_del(&prop_enum->head);
4029 kfree(prop_enum);
4030 }
4031
4032 if (property->num_values)
4033 kfree(property->values);
4034 drm_mode_object_put(dev, &property->base);
4035 list_del(&property->head);
4036 kfree(property);
4037}
4038EXPORT_SYMBOL(drm_property_destroy);
4039
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004040/**
4041 * drm_object_attach_property - attach a property to a modeset object
4042 * @obj: drm modeset object
4043 * @property: property to attach
4044 * @init_val: initial value of the property
4045 *
4046 * This attaches the given property to the modeset object with the given initial
4047 * value. Currently this function cannot fail since the properties are stored in
4048 * a statically sized array.
4049 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004050void drm_object_attach_property(struct drm_mode_object *obj,
4051 struct drm_property *property,
4052 uint64_t init_val)
4053{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004054 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004055
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004056 if (count == DRM_OBJECT_MAX_PROPERTY) {
4057 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4058 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4059 "you see this message on the same object type.\n",
4060 obj->type);
4061 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004062 }
4063
Rob Clarkb17cd752014-12-16 18:05:30 -05004064 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004065 obj->properties->values[count] = init_val;
4066 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004067 if (property->flags & DRM_MODE_PROP_ATOMIC)
4068 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004069}
4070EXPORT_SYMBOL(drm_object_attach_property);
4071
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004072/**
4073 * drm_object_property_set_value - set the value of a property
4074 * @obj: drm mode object to set property value for
4075 * @property: property to set
4076 * @val: value the property should be set to
4077 *
4078 * This functions sets a given property on a given object. This function only
4079 * changes the software state of the property, it does not call into the
4080 * driver's ->set_property callback.
4081 *
4082 * Returns:
4083 * Zero on success, error code on failure.
4084 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004085int drm_object_property_set_value(struct drm_mode_object *obj,
4086 struct drm_property *property, uint64_t val)
4087{
4088 int i;
4089
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004090 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004091 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004092 obj->properties->values[i] = val;
4093 return 0;
4094 }
4095 }
4096
4097 return -EINVAL;
4098}
4099EXPORT_SYMBOL(drm_object_property_set_value);
4100
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004101/**
4102 * drm_object_property_get_value - retrieve the value of a property
4103 * @obj: drm mode object to get property value from
4104 * @property: property to retrieve
4105 * @val: storage for the property value
4106 *
4107 * This function retrieves the softare state of the given property for the given
4108 * property. Since there is no driver callback to retrieve the current property
4109 * value this might be out of sync with the hardware, depending upon the driver
4110 * and property.
4111 *
4112 * Returns:
4113 * Zero on success, error code on failure.
4114 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004115int drm_object_property_get_value(struct drm_mode_object *obj,
4116 struct drm_property *property, uint64_t *val)
4117{
4118 int i;
4119
Rob Clark88a48e22014-12-18 16:01:50 -05004120 /* read-only properties bypass atomic mechanism and still store
4121 * their value in obj->properties->values[].. mostly to avoid
4122 * having to deal w/ EDID and similar props in atomic paths:
4123 */
4124 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4125 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4126 return drm_atomic_get_property(obj, property, val);
4127
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004128 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004129 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004130 *val = obj->properties->values[i];
4131 return 0;
4132 }
4133 }
4134
4135 return -EINVAL;
4136}
4137EXPORT_SYMBOL(drm_object_property_get_value);
4138
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004139/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004140 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004141 * @dev: DRM device
4142 * @data: ioctl data
4143 * @file_priv: DRM file info
4144 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004145 * This function retrieves the metadata for a given property, like the different
4146 * possible values for an enum property or the limits for a range property.
4147 *
4148 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004149 *
4150 * Called by the user via ioctl.
4151 *
4152 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004153 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004154 */
Dave Airlief453ba02008-11-07 14:05:41 -08004155int drm_mode_getproperty_ioctl(struct drm_device *dev,
4156 void *data, struct drm_file *file_priv)
4157{
Dave Airlief453ba02008-11-07 14:05:41 -08004158 struct drm_mode_get_property *out_resp = data;
4159 struct drm_property *property;
4160 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004161 int value_count = 0;
4162 int ret = 0, i;
4163 int copied;
4164 struct drm_property_enum *prop_enum;
4165 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004166 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004167
Dave Airliefb3b06c2011-02-08 13:55:21 +10004168 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4169 return -EINVAL;
4170
Daniel Vetter84849902012-12-02 00:28:11 +01004171 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004172 property = drm_property_find(dev, out_resp->prop_id);
4173 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004174 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004175 goto done;
4176 }
Dave Airlief453ba02008-11-07 14:05:41 -08004177
Rob Clark5ea22f22014-05-30 11:34:01 -04004178 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4179 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004180 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004181 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004182 }
4183
4184 value_count = property->num_values;
4185
4186 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4187 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4188 out_resp->flags = property->flags;
4189
4190 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004191 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004192 for (i = 0; i < value_count; i++) {
4193 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4194 ret = -EFAULT;
4195 goto done;
4196 }
4197 }
4198 }
4199 out_resp->count_values = value_count;
4200
Rob Clark5ea22f22014-05-30 11:34:01 -04004201 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4202 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004203 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4204 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004205 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004206 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004207
4208 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4209 ret = -EFAULT;
4210 goto done;
4211 }
4212
4213 if (copy_to_user(&enum_ptr[copied].name,
4214 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4215 ret = -EFAULT;
4216 goto done;
4217 }
4218 copied++;
4219 }
4220 }
4221 out_resp->count_enum_blobs = enum_count;
4222 }
4223
Daniel Vetter3758b342014-11-19 18:38:10 +01004224 /*
4225 * NOTE: The idea seems to have been to use this to read all the blob
4226 * property values. But nothing ever added them to the corresponding
4227 * list, userspace always used the special-purpose get_blob ioctl to
4228 * read the value for a blob property. It also doesn't make a lot of
4229 * sense to return values here when everything else is just metadata for
4230 * the property itself.
4231 */
4232 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4233 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004234done:
Daniel Vetter84849902012-12-02 00:28:11 +01004235 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004236 return ret;
4237}
4238
Daniel Stone99531d92015-05-22 13:34:49 +01004239/**
4240 * drm_property_create_blob - Create new blob property
4241 *
4242 * Creates a new blob property for a specified DRM device, optionally
4243 * copying data.
4244 *
4245 * @dev: DRM device to create property for
4246 * @length: Length to allocate for blob data
4247 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004248 *
4249 * Returns:
4250 * New blob property with a single reference on success, or an ERR_PTR
4251 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004252 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004253struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004254drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004255 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004256{
4257 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004258 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004259
Dan Carpenter9ac09342015-10-29 16:37:54 +03004260 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004261 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004262
4263 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4264 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004265 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004266
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004267 /* This must be explicitly initialised, so we can safely call list_del
4268 * on it in the removal handler, even if it isn't in a file list. */
4269 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004270 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004271 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004272
Daniel Stone99531d92015-05-22 13:34:49 +01004273 if (data)
4274 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004275
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004276 mutex_lock(&dev->mode_config.blob_lock);
4277
4278 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4279 if (ret) {
4280 kfree(blob);
4281 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004282 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004283 }
4284
Daniel Stone6bcacf52015-04-20 19:22:55 +01004285 kref_init(&blob->refcount);
4286
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004287 list_add_tail(&blob->head_global,
4288 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004289
4290 mutex_unlock(&dev->mode_config.blob_lock);
4291
Dave Airlief453ba02008-11-07 14:05:41 -08004292 return blob;
4293}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004294EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004295
Daniel Stone6bcacf52015-04-20 19:22:55 +01004296/**
4297 * drm_property_free_blob - Blob property destructor
4298 *
4299 * Internal free function for blob properties; must not be used directly.
4300 *
Daniel Stonef102c162015-05-22 13:34:44 +01004301 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004302 */
4303static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004304{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004305 struct drm_property_blob *blob =
4306 container_of(kref, struct drm_property_blob, refcount);
4307
4308 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4309
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004310 list_del(&blob->head_global);
4311 list_del(&blob->head_file);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004312 drm_mode_object_put(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004313
Dave Airlief453ba02008-11-07 14:05:41 -08004314 kfree(blob);
4315}
4316
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004317/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004318 * drm_property_unreference_blob - Unreference a blob property
4319 *
4320 * Drop a reference on a blob property. May free the object.
4321 *
Daniel Stonef102c162015-05-22 13:34:44 +01004322 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004323 */
4324void drm_property_unreference_blob(struct drm_property_blob *blob)
4325{
4326 struct drm_device *dev;
4327
4328 if (!blob)
4329 return;
4330
4331 dev = blob->dev;
4332
4333 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4334
4335 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4336 &dev->mode_config.blob_lock))
4337 mutex_unlock(&dev->mode_config.blob_lock);
4338 else
4339 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004340}
4341EXPORT_SYMBOL(drm_property_unreference_blob);
4342
4343/**
4344 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4345 *
4346 * Drop a reference on a blob property. May free the object. This must be
4347 * called with blob_lock held.
4348 *
Daniel Stonef102c162015-05-22 13:34:44 +01004349 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004350 */
4351static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4352{
4353 if (!blob)
4354 return;
4355
4356 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4357
4358 kref_put(&blob->refcount, drm_property_free_blob);
4359}
4360
4361/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004362 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4363 * @dev: DRM device
4364 * @file_priv: destroy all blobs owned by this file handle
4365 */
4366void drm_property_destroy_user_blobs(struct drm_device *dev,
4367 struct drm_file *file_priv)
4368{
4369 struct drm_property_blob *blob, *bt;
4370
4371 mutex_lock(&dev->mode_config.blob_lock);
4372
4373 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4374 list_del_init(&blob->head_file);
4375 drm_property_unreference_blob_locked(blob);
4376 }
4377
4378 mutex_unlock(&dev->mode_config.blob_lock);
4379}
4380
4381/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004382 * drm_property_reference_blob - Take a reference on an existing property
4383 *
4384 * Take a new reference on an existing blob property.
4385 *
Daniel Stonef102c162015-05-22 13:34:44 +01004386 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004387 */
4388struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4389{
4390 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4391 kref_get(&blob->refcount);
4392 return blob;
4393}
4394EXPORT_SYMBOL(drm_property_reference_blob);
4395
4396/*
4397 * Like drm_property_lookup_blob, but does not return an additional reference.
4398 * Must be called with blob_lock held.
4399 */
4400static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4401 uint32_t id)
4402{
4403 struct drm_mode_object *obj = NULL;
4404 struct drm_property_blob *blob;
4405
4406 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4407
4408 mutex_lock(&dev->mode_config.idr_mutex);
4409 obj = idr_find(&dev->mode_config.crtc_idr, id);
4410 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4411 blob = NULL;
4412 else
4413 blob = obj_to_blob(obj);
4414 mutex_unlock(&dev->mode_config.idr_mutex);
4415
Dave Airlief453ba02008-11-07 14:05:41 -08004416 return blob;
4417}
4418
Daniel Stone6bcacf52015-04-20 19:22:55 +01004419/**
4420 * drm_property_lookup_blob - look up a blob property and take a reference
4421 * @dev: drm device
4422 * @id: id of the blob property
4423 *
4424 * If successful, this takes an additional reference to the blob property.
4425 * callers need to make sure to eventually unreference the returned property
4426 * again, using @drm_property_unreference_blob.
4427 */
4428struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4429 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004430{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004431 struct drm_property_blob *blob;
4432
4433 mutex_lock(&dev->mode_config.blob_lock);
4434 blob = __drm_property_lookup_blob(dev, id);
4435 if (blob) {
4436 if (!kref_get_unless_zero(&blob->refcount))
4437 blob = NULL;
4438 }
4439 mutex_unlock(&dev->mode_config.blob_lock);
4440
4441 return blob;
4442}
4443EXPORT_SYMBOL(drm_property_lookup_blob);
4444
4445/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004446 * drm_property_replace_global_blob - atomically replace existing blob property
4447 * @dev: drm device
4448 * @replace: location of blob property pointer to be replaced
4449 * @length: length of data for new blob, or 0 for no data
4450 * @data: content for new blob, or NULL for no data
4451 * @obj_holds_id: optional object for property holding blob ID
4452 * @prop_holds_id: optional property holding blob ID
4453 * @return 0 on success or error on failure
4454 *
4455 * This function will atomically replace a global property in the blob list,
4456 * optionally updating a property which holds the ID of that property. It is
4457 * guaranteed to be atomic: no caller will be allowed to see intermediate
4458 * results, and either the entire operation will succeed and clean up the
4459 * previous property, or it will fail and the state will be unchanged.
4460 *
4461 * If length is 0 or data is NULL, no new blob will be created, and the holding
4462 * property, if specified, will be set to 0.
4463 *
4464 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4465 * by holding the relevant modesetting object lock for its parent.
4466 *
4467 * For example, a drm_connector has a 'PATH' property, which contains the ID
4468 * of a blob property with the value of the MST path information. Calling this
4469 * function with replace pointing to the connector's path_blob_ptr, length and
4470 * data set for the new path information, obj_holds_id set to the connector's
4471 * base object, and prop_holds_id set to the path property name, will perform
4472 * a completely atomic update. The access to path_blob_ptr is protected by the
4473 * caller holding a lock on the connector.
4474 */
4475static int drm_property_replace_global_blob(struct drm_device *dev,
4476 struct drm_property_blob **replace,
4477 size_t length,
4478 const void *data,
4479 struct drm_mode_object *obj_holds_id,
4480 struct drm_property *prop_holds_id)
4481{
4482 struct drm_property_blob *new_blob = NULL;
4483 struct drm_property_blob *old_blob = NULL;
4484 int ret;
4485
4486 WARN_ON(replace == NULL);
4487
4488 old_blob = *replace;
4489
4490 if (length && data) {
4491 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004492 if (IS_ERR(new_blob))
4493 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004494 }
4495
4496 /* This does not need to be synchronised with blob_lock, as the
4497 * get_properties ioctl locks all modesetting objects, and
4498 * obj_holds_id must be locked before calling here, so we cannot
4499 * have its value out of sync with the list membership modified
4500 * below under blob_lock. */
4501 if (obj_holds_id) {
4502 ret = drm_object_property_set_value(obj_holds_id,
4503 prop_holds_id,
4504 new_blob ?
4505 new_blob->base.id : 0);
4506 if (ret != 0)
4507 goto err_created;
4508 }
4509
Markus Elfringec530822015-07-05 21:55:10 +02004510 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004511 *replace = new_blob;
4512
4513 return 0;
4514
4515err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004516 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004517 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004518}
4519
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004520/**
4521 * drm_mode_getblob_ioctl - get the contents of a blob property value
4522 * @dev: DRM device
4523 * @data: ioctl data
4524 * @file_priv: DRM file info
4525 *
4526 * This function retrieves the contents of a blob property. The value stored in
4527 * an object's blob property is just a normal modeset object id.
4528 *
4529 * Called by the user via ioctl.
4530 *
4531 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004532 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004533 */
Dave Airlief453ba02008-11-07 14:05:41 -08004534int drm_mode_getblob_ioctl(struct drm_device *dev,
4535 void *data, struct drm_file *file_priv)
4536{
Dave Airlief453ba02008-11-07 14:05:41 -08004537 struct drm_mode_get_blob *out_resp = data;
4538 struct drm_property_blob *blob;
4539 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004540 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004541
Dave Airliefb3b06c2011-02-08 13:55:21 +10004542 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4543 return -EINVAL;
4544
Daniel Vetter84849902012-12-02 00:28:11 +01004545 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004546 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004547 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004548 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004549 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004550 goto done;
4551 }
Dave Airlief453ba02008-11-07 14:05:41 -08004552
4553 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004554 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004555 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004556 ret = -EFAULT;
4557 goto done;
4558 }
4559 }
4560 out_resp->length = blob->length;
4561
4562done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004563 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004564 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004565 return ret;
4566}
4567
Dave Airliecc7096f2014-10-22 12:03:04 +10004568/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004569 * drm_mode_createblob_ioctl - create a new blob property
4570 * @dev: DRM device
4571 * @data: ioctl data
4572 * @file_priv: DRM file info
4573 *
4574 * This function creates a new blob property with user-defined values. In order
4575 * to give us sensible validation and checking when creating, rather than at
4576 * every potential use, we also require a type to be provided upfront.
4577 *
4578 * Called by the user via ioctl.
4579 *
4580 * Returns:
4581 * Zero on success, negative errno on failure.
4582 */
4583int drm_mode_createblob_ioctl(struct drm_device *dev,
4584 void *data, struct drm_file *file_priv)
4585{
4586 struct drm_mode_create_blob *out_resp = data;
4587 struct drm_property_blob *blob;
4588 void __user *blob_ptr;
4589 int ret = 0;
4590
4591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4592 return -EINVAL;
4593
4594 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4595 if (IS_ERR(blob))
4596 return PTR_ERR(blob);
4597
4598 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4599 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4600 ret = -EFAULT;
4601 goto out_blob;
4602 }
4603
4604 /* Dropping the lock between create_blob and our access here is safe
4605 * as only the same file_priv can remove the blob; at this point, it is
4606 * not associated with any file_priv. */
4607 mutex_lock(&dev->mode_config.blob_lock);
4608 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004609 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004610 mutex_unlock(&dev->mode_config.blob_lock);
4611
4612 return 0;
4613
4614out_blob:
4615 drm_property_unreference_blob(blob);
4616 return ret;
4617}
4618
4619/**
4620 * drm_mode_destroyblob_ioctl - destroy a user blob property
4621 * @dev: DRM device
4622 * @data: ioctl data
4623 * @file_priv: DRM file info
4624 *
4625 * Destroy an existing user-defined blob property.
4626 *
4627 * Called by the user via ioctl.
4628 *
4629 * Returns:
4630 * Zero on success, negative errno on failure.
4631 */
4632int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4633 void *data, struct drm_file *file_priv)
4634{
4635 struct drm_mode_destroy_blob *out_resp = data;
4636 struct drm_property_blob *blob = NULL, *bt;
4637 bool found = false;
4638 int ret = 0;
4639
4640 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4641 return -EINVAL;
4642
4643 mutex_lock(&dev->mode_config.blob_lock);
4644 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4645 if (!blob) {
4646 ret = -ENOENT;
4647 goto err;
4648 }
4649
4650 /* Ensure the property was actually created by this user. */
4651 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4652 if (bt == blob) {
4653 found = true;
4654 break;
4655 }
4656 }
4657
4658 if (!found) {
4659 ret = -EPERM;
4660 goto err;
4661 }
4662
4663 /* We must drop head_file here, because we may not be the last
4664 * reference on the blob. */
4665 list_del_init(&blob->head_file);
4666 drm_property_unreference_blob_locked(blob);
4667 mutex_unlock(&dev->mode_config.blob_lock);
4668
4669 return 0;
4670
4671err:
4672 mutex_unlock(&dev->mode_config.blob_lock);
4673 return ret;
4674}
4675
4676/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004677 * drm_mode_connector_set_path_property - set tile property on connector
4678 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004679 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004680 *
4681 * This creates a property to expose to userspace to specify a
4682 * connector path. This is mainly used for DisplayPort MST where
4683 * connectors have a topology and we want to allow userspace to give
4684 * them more meaningful names.
4685 *
4686 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004687 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004688 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004689int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004690 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004691{
4692 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004693 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004694
Daniel Stoned2ed3432015-04-20 19:22:53 +01004695 ret = drm_property_replace_global_blob(dev,
4696 &connector->path_blob_ptr,
4697 strlen(path) + 1,
4698 path,
4699 &connector->base,
4700 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004701 return ret;
4702}
4703EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4704
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004705/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004706 * drm_mode_connector_set_tile_property - set tile property on connector
4707 * @connector: connector to set property on.
4708 *
4709 * This looks up the tile information for a connector, and creates a
4710 * property for userspace to parse if it exists. The property is of
4711 * the form of 8 integers using ':' as a separator.
4712 *
4713 * Returns:
4714 * Zero on success, errno on failure.
4715 */
4716int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4717{
4718 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004719 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004720 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004721
4722 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004723 ret = drm_property_replace_global_blob(dev,
4724 &connector->tile_blob_ptr,
4725 0,
4726 NULL,
4727 &connector->base,
4728 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004729 return ret;
4730 }
4731
4732 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4733 connector->tile_group->id, connector->tile_is_single_monitor,
4734 connector->num_h_tile, connector->num_v_tile,
4735 connector->tile_h_loc, connector->tile_v_loc,
4736 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004737
Daniel Stoned2ed3432015-04-20 19:22:53 +01004738 ret = drm_property_replace_global_blob(dev,
4739 &connector->tile_blob_ptr,
4740 strlen(tile) + 1,
4741 tile,
4742 &connector->base,
4743 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004744 return ret;
4745}
4746EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4747
4748/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004749 * drm_mode_connector_update_edid_property - update the edid property of a connector
4750 * @connector: drm connector
4751 * @edid: new value of the edid property
4752 *
4753 * This function creates a new blob modeset object and assigns its id to the
4754 * connector's edid property.
4755 *
4756 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004757 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004758 */
Dave Airlief453ba02008-11-07 14:05:41 -08004759int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004760 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004761{
4762 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004763 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004764 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004765
Thomas Wood4cf2b282014-06-18 17:52:33 +01004766 /* ignore requests to set edid when overridden */
4767 if (connector->override_edid)
4768 return 0;
4769
Daniel Stoned2ed3432015-04-20 19:22:53 +01004770 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004771 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004772
Daniel Stoned2ed3432015-04-20 19:22:53 +01004773 ret = drm_property_replace_global_blob(dev,
4774 &connector->edid_blob_ptr,
4775 size,
4776 edid,
4777 &connector->base,
4778 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004779 return ret;
4780}
4781EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4782
Rob Clark3843e712014-12-16 18:05:29 -05004783/* Some properties could refer to dynamic refcnt'd objects, or things that
4784 * need special locking to handle lifetime issues (ie. to ensure the prop
4785 * value doesn't become invalid part way through the property update due to
4786 * race). The value returned by reference via 'obj' should be passed back
4787 * to drm_property_change_valid_put() after the property is set (and the
4788 * object to which the property is attached has a chance to take it's own
4789 * reference).
4790 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004791bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004792 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004793{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004794 int i;
4795
Paulo Zanoni26a34812012-05-15 18:08:59 -03004796 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4797 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004798
Rob Clark3843e712014-12-16 18:05:29 -05004799 *ref = NULL;
4800
Rob Clark5ea22f22014-05-30 11:34:01 -04004801 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004802 if (value < property->values[0] || value > property->values[1])
4803 return false;
4804 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004805 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4806 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004807
Rob Clarkebc44cf2012-09-12 22:22:31 -05004808 if (svalue < U642I64(property->values[0]) ||
4809 svalue > U642I64(property->values[1]))
4810 return false;
4811 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004812 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004813 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004814
Rob Clark49e27542012-05-17 02:23:26 -06004815 for (i = 0; i < property->num_values; i++)
4816 valid_mask |= (1ULL << property->values[i]);
4817 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004818 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004819 struct drm_property_blob *blob;
4820
4821 if (value == 0)
4822 return true;
4823
4824 blob = drm_property_lookup_blob(property->dev, value);
4825 if (blob) {
4826 *ref = &blob->base;
4827 return true;
4828 } else {
4829 return false;
4830 }
Rob Clark98f75de2014-05-30 11:37:03 -04004831 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004832 /* a zero value for an object property translates to null: */
4833 if (value == 0)
4834 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004835
4836 /* handle refcnt'd objects specially: */
4837 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4838 struct drm_framebuffer *fb;
4839 fb = drm_framebuffer_lookup(property->dev, value);
4840 if (fb) {
4841 *ref = &fb->base;
4842 return true;
4843 } else {
4844 return false;
4845 }
4846 } else {
4847 return _object_find(property->dev, value, property->values[0]) != NULL;
4848 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004849 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004850
4851 for (i = 0; i < property->num_values; i++)
4852 if (property->values[i] == value)
4853 return true;
4854 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004855}
4856
Rob Clarkd34f20d2014-12-18 16:01:56 -05004857void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004858 struct drm_mode_object *ref)
4859{
4860 if (!ref)
4861 return;
4862
4863 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4864 if (property->values[0] == DRM_MODE_OBJECT_FB)
4865 drm_framebuffer_unreference(obj_to_fb(ref));
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004866 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4867 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004868}
4869
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004870/**
4871 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4872 * @dev: DRM device
4873 * @data: ioctl data
4874 * @file_priv: DRM file info
4875 *
4876 * This function sets the current value for a connectors's property. It also
4877 * calls into a driver's ->set_property callback to update the hardware state
4878 *
4879 * Called by the user via ioctl.
4880 *
4881 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004882 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004883 */
Dave Airlief453ba02008-11-07 14:05:41 -08004884int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4885 void *data, struct drm_file *file_priv)
4886{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004887 struct drm_mode_connector_set_property *conn_set_prop = data;
4888 struct drm_mode_obj_set_property obj_set_prop = {
4889 .value = conn_set_prop->value,
4890 .prop_id = conn_set_prop->prop_id,
4891 .obj_id = conn_set_prop->connector_id,
4892 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4893 };
Dave Airlief453ba02008-11-07 14:05:41 -08004894
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004895 /* It does all the locking and checking we need */
4896 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004897}
4898
Paulo Zanonic5431882012-05-15 18:09:02 -03004899static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4900 struct drm_property *property,
4901 uint64_t value)
4902{
4903 int ret = -EINVAL;
4904 struct drm_connector *connector = obj_to_connector(obj);
4905
4906 /* Do DPMS ourselves */
4907 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004908 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004909 } else if (connector->funcs->set_property)
4910 ret = connector->funcs->set_property(connector, property, value);
4911
4912 /* store the property value if successful */
4913 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004914 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004915 return ret;
4916}
4917
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004918static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4919 struct drm_property *property,
4920 uint64_t value)
4921{
4922 int ret = -EINVAL;
4923 struct drm_crtc *crtc = obj_to_crtc(obj);
4924
4925 if (crtc->funcs->set_property)
4926 ret = crtc->funcs->set_property(crtc, property, value);
4927 if (!ret)
4928 drm_object_property_set_value(obj, property, value);
4929
4930 return ret;
4931}
4932
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004933/**
4934 * drm_mode_plane_set_obj_prop - set the value of a property
4935 * @plane: drm plane object to set property value for
4936 * @property: property to set
4937 * @value: value the property should be set to
4938 *
4939 * This functions sets a given property on a given plane object. This function
4940 * calls the driver's ->set_property callback and changes the software state of
4941 * the property if the callback succeeds.
4942 *
4943 * Returns:
4944 * Zero on success, error code on failure.
4945 */
4946int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4947 struct drm_property *property,
4948 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004949{
4950 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004951 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004952
4953 if (plane->funcs->set_property)
4954 ret = plane->funcs->set_property(plane, property, value);
4955 if (!ret)
4956 drm_object_property_set_value(obj, property, value);
4957
4958 return ret;
4959}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004960EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004961
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004962/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004963 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004964 * @dev: DRM device
4965 * @data: ioctl data
4966 * @file_priv: DRM file info
4967 *
4968 * This function retrieves the current value for an object's property. Compared
4969 * to the connector specific ioctl this one is extended to also work on crtc and
4970 * plane objects.
4971 *
4972 * Called by the user via ioctl.
4973 *
4974 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004975 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004976 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004977int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4978 struct drm_file *file_priv)
4979{
4980 struct drm_mode_obj_get_properties *arg = data;
4981 struct drm_mode_object *obj;
4982 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004983
4984 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4985 return -EINVAL;
4986
Daniel Vetter84849902012-12-02 00:28:11 +01004987 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004988
4989 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4990 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004991 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004992 goto out;
4993 }
4994 if (!obj->properties) {
4995 ret = -EINVAL;
4996 goto out;
4997 }
4998
Rob Clark88a48e22014-12-18 16:01:50 -05004999 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005000 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5001 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5002 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005003
Paulo Zanonic5431882012-05-15 18:09:02 -03005004out:
Daniel Vetter84849902012-12-02 00:28:11 +01005005 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005006 return ret;
5007}
5008
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005009/**
5010 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5011 * @dev: DRM device
5012 * @data: ioctl data
5013 * @file_priv: DRM file info
5014 *
5015 * This function sets the current value for an object's property. It also calls
5016 * into a driver's ->set_property callback to update the hardware state.
5017 * Compared to the connector specific ioctl this one is extended to also work on
5018 * crtc and plane objects.
5019 *
5020 * Called by the user via ioctl.
5021 *
5022 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005023 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005024 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005025int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5026 struct drm_file *file_priv)
5027{
5028 struct drm_mode_obj_set_property *arg = data;
5029 struct drm_mode_object *arg_obj;
5030 struct drm_mode_object *prop_obj;
5031 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005032 int i, ret = -EINVAL;
5033 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005034
5035 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5036 return -EINVAL;
5037
Daniel Vetter84849902012-12-02 00:28:11 +01005038 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005039
5040 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005041 if (!arg_obj) {
5042 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005043 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005044 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005045 if (!arg_obj->properties)
5046 goto out;
5047
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005048 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005049 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005050 break;
5051
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005052 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005053 goto out;
5054
5055 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5056 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005057 if (!prop_obj) {
5058 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005059 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005060 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005061 property = obj_to_property(prop_obj);
5062
Rob Clark3843e712014-12-16 18:05:29 -05005063 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005064 goto out;
5065
5066 switch (arg_obj->type) {
5067 case DRM_MODE_OBJECT_CONNECTOR:
5068 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5069 arg->value);
5070 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005071 case DRM_MODE_OBJECT_CRTC:
5072 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5073 break;
Rob Clark4d939142012-05-17 02:23:27 -06005074 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005075 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5076 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005077 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005078 }
5079
Rob Clark3843e712014-12-16 18:05:29 -05005080 drm_property_change_valid_put(property, ref);
5081
Paulo Zanonic5431882012-05-15 18:09:02 -03005082out:
Daniel Vetter84849902012-12-02 00:28:11 +01005083 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005084 return ret;
5085}
5086
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005087/**
5088 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5089 * @connector: connector to attach
5090 * @encoder: encoder to attach @connector to
5091 *
5092 * This function links up a connector to an encoder. Note that the routing
5093 * restrictions between encoders and crtcs are exposed to userspace through the
5094 * possible_clones and possible_crtcs bitmasks.
5095 *
5096 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005097 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005098 */
Dave Airlief453ba02008-11-07 14:05:41 -08005099int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5100 struct drm_encoder *encoder)
5101{
5102 int i;
5103
Thierry Redingeb47fe82015-11-16 18:19:53 +01005104 /*
5105 * In the past, drivers have attempted to model the static association
5106 * of connector to encoder in simple connector/encoder devices using a
5107 * direct assignment of connector->encoder = encoder. This connection
5108 * is a logical one and the responsibility of the core, so drivers are
5109 * expected not to mess with this.
5110 *
5111 * Note that the error return should've been enough here, but a large
5112 * majority of drivers ignores the return value, so add in a big WARN
5113 * to get people's attention.
5114 */
5115 if (WARN_ON(connector->encoder))
5116 return -EINVAL;
5117
Dave Airlief453ba02008-11-07 14:05:41 -08005118 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5119 if (connector->encoder_ids[i] == 0) {
5120 connector->encoder_ids[i] = encoder->base.id;
5121 return 0;
5122 }
5123 }
5124 return -ENOMEM;
5125}
5126EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5127
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005128/**
5129 * drm_mode_crtc_set_gamma_size - set the gamma table size
5130 * @crtc: CRTC to set the gamma table size for
5131 * @gamma_size: size of the gamma table
5132 *
5133 * Drivers which support gamma tables should set this to the supported gamma
5134 * table size when initializing the CRTC. Currently the drm core only supports a
5135 * fixed gamma table size.
5136 *
5137 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005138 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005139 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005140int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005141 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005142{
5143 crtc->gamma_size = gamma_size;
5144
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005145 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5146 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005147 if (!crtc->gamma_store) {
5148 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005149 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005150 }
5151
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005152 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005153}
5154EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5155
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005156/**
5157 * drm_mode_gamma_set_ioctl - set the gamma table
5158 * @dev: DRM device
5159 * @data: ioctl data
5160 * @file_priv: DRM file info
5161 *
5162 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5163 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5164 *
5165 * Called by the user via ioctl.
5166 *
5167 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005168 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005169 */
Dave Airlief453ba02008-11-07 14:05:41 -08005170int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5171 void *data, struct drm_file *file_priv)
5172{
5173 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005174 struct drm_crtc *crtc;
5175 void *r_base, *g_base, *b_base;
5176 int size;
5177 int ret = 0;
5178
Dave Airliefb3b06c2011-02-08 13:55:21 +10005179 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5180 return -EINVAL;
5181
Daniel Vetter84849902012-12-02 00:28:11 +01005182 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005183 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5184 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005185 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005186 goto out;
5187 }
Dave Airlief453ba02008-11-07 14:05:41 -08005188
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005189 if (crtc->funcs->gamma_set == NULL) {
5190 ret = -ENOSYS;
5191 goto out;
5192 }
5193
Dave Airlief453ba02008-11-07 14:05:41 -08005194 /* memcpy into gamma store */
5195 if (crtc_lut->gamma_size != crtc->gamma_size) {
5196 ret = -EINVAL;
5197 goto out;
5198 }
5199
5200 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5201 r_base = crtc->gamma_store;
5202 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5203 ret = -EFAULT;
5204 goto out;
5205 }
5206
5207 g_base = r_base + size;
5208 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5209 ret = -EFAULT;
5210 goto out;
5211 }
5212
5213 b_base = g_base + size;
5214 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5215 ret = -EFAULT;
5216 goto out;
5217 }
5218
James Simmons72034252010-08-03 01:33:19 +01005219 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005220
5221out:
Daniel Vetter84849902012-12-02 00:28:11 +01005222 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005223 return ret;
5224
5225}
5226
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005227/**
5228 * drm_mode_gamma_get_ioctl - get the gamma table
5229 * @dev: DRM device
5230 * @data: ioctl data
5231 * @file_priv: DRM file info
5232 *
5233 * Copy the current gamma table into the storage provided. This also provides
5234 * the gamma table size the driver expects, which can be used to size the
5235 * allocated storage.
5236 *
5237 * Called by the user via ioctl.
5238 *
5239 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005240 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005241 */
Dave Airlief453ba02008-11-07 14:05:41 -08005242int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5243 void *data, struct drm_file *file_priv)
5244{
5245 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005246 struct drm_crtc *crtc;
5247 void *r_base, *g_base, *b_base;
5248 int size;
5249 int ret = 0;
5250
Dave Airliefb3b06c2011-02-08 13:55:21 +10005251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5252 return -EINVAL;
5253
Daniel Vetter84849902012-12-02 00:28:11 +01005254 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005255 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5256 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005257 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005258 goto out;
5259 }
Dave Airlief453ba02008-11-07 14:05:41 -08005260
5261 /* memcpy into gamma store */
5262 if (crtc_lut->gamma_size != crtc->gamma_size) {
5263 ret = -EINVAL;
5264 goto out;
5265 }
5266
5267 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5268 r_base = crtc->gamma_store;
5269 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5270 ret = -EFAULT;
5271 goto out;
5272 }
5273
5274 g_base = r_base + size;
5275 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5276 ret = -EFAULT;
5277 goto out;
5278 }
5279
5280 b_base = g_base + size;
5281 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5282 ret = -EFAULT;
5283 goto out;
5284 }
5285out:
Daniel Vetter84849902012-12-02 00:28:11 +01005286 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005287 return ret;
5288}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005289
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005290/**
5291 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5292 * @dev: DRM device
5293 * @data: ioctl data
5294 * @file_priv: DRM file info
5295 *
5296 * This schedules an asynchronous update on a given CRTC, called page flip.
5297 * Optionally a drm event is generated to signal the completion of the event.
5298 * Generic drivers cannot assume that a pageflip with changed framebuffer
5299 * properties (including driver specific metadata like tiling layout) will work,
5300 * but some drivers support e.g. pixel format changes through the pageflip
5301 * ioctl.
5302 *
5303 * Called by the user via ioctl.
5304 *
5305 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005306 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005307 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005308int drm_mode_page_flip_ioctl(struct drm_device *dev,
5309 void *data, struct drm_file *file_priv)
5310{
5311 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005312 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005313 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005314 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005315 int ret = -EINVAL;
5316
5317 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5318 page_flip->reserved != 0)
5319 return -EINVAL;
5320
Keith Packard62f21042013-07-22 18:50:00 -07005321 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5322 return -EINVAL;
5323
Rob Clarka2b34e22013-10-05 16:36:52 -04005324 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5325 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005326 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005327
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005328 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005329 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005330 /* The framebuffer is currently unbound, presumably
5331 * due to a hotplug event, that userspace has not
5332 * yet discovered.
5333 */
5334 ret = -EBUSY;
5335 goto out;
5336 }
5337
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005338 if (crtc->funcs->page_flip == NULL)
5339 goto out;
5340
Daniel Vetter786b99e2012-12-02 21:53:40 +01005341 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005342 if (!fb) {
5343 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005344 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005345 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005346
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005347 if (crtc->state) {
5348 const struct drm_plane_state *state = crtc->primary->state;
5349
5350 ret = check_src_coords(state->src_x, state->src_y,
5351 state->src_w, state->src_h, fb);
5352 } else {
5353 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5354 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005355 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005356 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005357
Matt Roperf4510a22014-04-01 15:22:40 -07005358 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005359 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5360 ret = -EINVAL;
5361 goto out;
5362 }
5363
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005364 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005365 e = kzalloc(sizeof *e, GFP_KERNEL);
5366 if (!e) {
5367 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005368 goto out;
5369 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005370 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005371 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005372 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005373 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5374 if (ret) {
5375 kfree(e);
5376 goto out;
5377 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005378 }
5379
Daniel Vetter3d30a592014-07-27 13:42:42 +02005380 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005381 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005382 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005383 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5384 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005385 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005386 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005387 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005388 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005389 /* Unref only the old framebuffer. */
5390 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005391 }
5392
5393out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005394 if (fb)
5395 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005396 if (crtc->primary->old_fb)
5397 drm_framebuffer_unreference(crtc->primary->old_fb);
5398 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005399 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005400
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005401 return ret;
5402}
Chris Wilsoneb033552011-01-24 15:11:08 +00005403
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005404/**
5405 * drm_mode_config_reset - call ->reset callbacks
5406 * @dev: drm device
5407 *
5408 * This functions calls all the crtc's, encoder's and connector's ->reset
5409 * callback. Drivers can use this in e.g. their driver load or resume code to
5410 * reset hardware and software state.
5411 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005412void drm_mode_config_reset(struct drm_device *dev)
5413{
5414 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005415 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005416 struct drm_encoder *encoder;
5417 struct drm_connector *connector;
5418
Daniel Vettere4f62542015-07-09 23:44:35 +02005419 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005420 if (plane->funcs->reset)
5421 plane->funcs->reset(plane);
5422
Daniel Vettere4f62542015-07-09 23:44:35 +02005423 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005424 if (crtc->funcs->reset)
5425 crtc->funcs->reset(crtc);
5426
Daniel Vettere4f62542015-07-09 23:44:35 +02005427 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005428 if (encoder->funcs->reset)
5429 encoder->funcs->reset(encoder);
5430
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005431 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005432 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005433 if (connector->funcs->reset)
5434 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005435 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005436}
5437EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005438
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005439/**
5440 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5441 * @dev: DRM device
5442 * @data: ioctl data
5443 * @file_priv: DRM file info
5444 *
5445 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5446 * TTM or something else entirely) and returns the resulting buffer handle. This
5447 * handle can then be wrapped up into a framebuffer modeset object.
5448 *
5449 * Note that userspace is not allowed to use such objects for render
5450 * acceleration - drivers must create their own private ioctls for such a use
5451 * case.
5452 *
5453 * Called by the user via ioctl.
5454 *
5455 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005456 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005457 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005458int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5459 void *data, struct drm_file *file_priv)
5460{
5461 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005462 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005463
5464 if (!dev->driver->dumb_create)
5465 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005466 if (!args->width || !args->height || !args->bpp)
5467 return -EINVAL;
5468
5469 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005470 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005471 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005472 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005473 return -EINVAL;
5474 stride = cpp * args->width;
5475 if (args->height > 0xffffffffU / stride)
5476 return -EINVAL;
5477
5478 /* test for wrap-around */
5479 size = args->height * stride;
5480 if (PAGE_ALIGN(size) == 0)
5481 return -EINVAL;
5482
Thierry Redingf6085952014-11-03 11:14:14 +01005483 /*
5484 * handle, pitch and size are output parameters. Zero them out to
5485 * prevent drivers from accidentally using uninitialized data. Since
5486 * not all existing userspace is clearing these fields properly we
5487 * cannot reject IOCTL with garbage in them.
5488 */
5489 args->handle = 0;
5490 args->pitch = 0;
5491 args->size = 0;
5492
Dave Airlieff72145b2011-02-07 12:16:14 +10005493 return dev->driver->dumb_create(file_priv, dev, args);
5494}
5495
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005496/**
5497 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5498 * @dev: DRM device
5499 * @data: ioctl data
5500 * @file_priv: DRM file info
5501 *
5502 * Allocate an offset in the drm device node's address space to be able to
5503 * memory map a dumb buffer.
5504 *
5505 * Called by the user via ioctl.
5506 *
5507 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005508 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005509 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005510int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5511 void *data, struct drm_file *file_priv)
5512{
5513 struct drm_mode_map_dumb *args = data;
5514
5515 /* call driver ioctl to get mmap offset */
5516 if (!dev->driver->dumb_map_offset)
5517 return -ENOSYS;
5518
5519 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5520}
5521
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005522/**
5523 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5524 * @dev: DRM device
5525 * @data: ioctl data
5526 * @file_priv: DRM file info
5527 *
5528 * This destroys the userspace handle for the given dumb backing storage buffer.
5529 * Since buffer objects must be reference counted in the kernel a buffer object
5530 * won't be immediately freed if a framebuffer modeset object still uses it.
5531 *
5532 * Called by the user via ioctl.
5533 *
5534 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005535 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005536 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005537int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5538 void *data, struct drm_file *file_priv)
5539{
5540 struct drm_mode_destroy_dumb *args = data;
5541
5542 if (!dev->driver->dumb_destroy)
5543 return -ENOSYS;
5544
5545 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5546}
Dave Airlie248dbc22011-11-29 20:02:54 +00005547
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005548/**
5549 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5550 * @format: pixel format (DRM_FORMAT_*)
5551 * @depth: storage for the depth value
5552 * @bpp: storage for the bpp value
5553 *
5554 * This only supports RGB formats here for compat with code that doesn't use
5555 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005556 */
5557void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5558 int *bpp)
5559{
5560 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005561 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005562 case DRM_FORMAT_RGB332:
5563 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005564 *depth = 8;
5565 *bpp = 8;
5566 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005567 case DRM_FORMAT_XRGB1555:
5568 case DRM_FORMAT_XBGR1555:
5569 case DRM_FORMAT_RGBX5551:
5570 case DRM_FORMAT_BGRX5551:
5571 case DRM_FORMAT_ARGB1555:
5572 case DRM_FORMAT_ABGR1555:
5573 case DRM_FORMAT_RGBA5551:
5574 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005575 *depth = 15;
5576 *bpp = 16;
5577 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005578 case DRM_FORMAT_RGB565:
5579 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005580 *depth = 16;
5581 *bpp = 16;
5582 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005583 case DRM_FORMAT_RGB888:
5584 case DRM_FORMAT_BGR888:
5585 *depth = 24;
5586 *bpp = 24;
5587 break;
5588 case DRM_FORMAT_XRGB8888:
5589 case DRM_FORMAT_XBGR8888:
5590 case DRM_FORMAT_RGBX8888:
5591 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005592 *depth = 24;
5593 *bpp = 32;
5594 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005595 case DRM_FORMAT_XRGB2101010:
5596 case DRM_FORMAT_XBGR2101010:
5597 case DRM_FORMAT_RGBX1010102:
5598 case DRM_FORMAT_BGRX1010102:
5599 case DRM_FORMAT_ARGB2101010:
5600 case DRM_FORMAT_ABGR2101010:
5601 case DRM_FORMAT_RGBA1010102:
5602 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005603 *depth = 30;
5604 *bpp = 32;
5605 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005606 case DRM_FORMAT_ARGB8888:
5607 case DRM_FORMAT_ABGR8888:
5608 case DRM_FORMAT_RGBA8888:
5609 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005610 *depth = 32;
5611 *bpp = 32;
5612 break;
5613 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005614 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5615 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005616 *depth = 0;
5617 *bpp = 0;
5618 break;
5619 }
5620}
5621EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005622
5623/**
5624 * drm_format_num_planes - get the number of planes for format
5625 * @format: pixel format (DRM_FORMAT_*)
5626 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005627 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005628 * The number of planes used by the specified pixel format.
5629 */
5630int drm_format_num_planes(uint32_t format)
5631{
5632 switch (format) {
5633 case DRM_FORMAT_YUV410:
5634 case DRM_FORMAT_YVU410:
5635 case DRM_FORMAT_YUV411:
5636 case DRM_FORMAT_YVU411:
5637 case DRM_FORMAT_YUV420:
5638 case DRM_FORMAT_YVU420:
5639 case DRM_FORMAT_YUV422:
5640 case DRM_FORMAT_YVU422:
5641 case DRM_FORMAT_YUV444:
5642 case DRM_FORMAT_YVU444:
5643 return 3;
5644 case DRM_FORMAT_NV12:
5645 case DRM_FORMAT_NV21:
5646 case DRM_FORMAT_NV16:
5647 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005648 case DRM_FORMAT_NV24:
5649 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005650 return 2;
5651 default:
5652 return 1;
5653 }
5654}
5655EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005656
5657/**
5658 * drm_format_plane_cpp - determine the bytes per pixel value
5659 * @format: pixel format (DRM_FORMAT_*)
5660 * @plane: plane index
5661 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005662 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005663 * The bytes per pixel value for the specified plane.
5664 */
5665int drm_format_plane_cpp(uint32_t format, int plane)
5666{
5667 unsigned int depth;
5668 int bpp;
5669
5670 if (plane >= drm_format_num_planes(format))
5671 return 0;
5672
5673 switch (format) {
5674 case DRM_FORMAT_YUYV:
5675 case DRM_FORMAT_YVYU:
5676 case DRM_FORMAT_UYVY:
5677 case DRM_FORMAT_VYUY:
5678 return 2;
5679 case DRM_FORMAT_NV12:
5680 case DRM_FORMAT_NV21:
5681 case DRM_FORMAT_NV16:
5682 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005683 case DRM_FORMAT_NV24:
5684 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005685 return plane ? 2 : 1;
5686 case DRM_FORMAT_YUV410:
5687 case DRM_FORMAT_YVU410:
5688 case DRM_FORMAT_YUV411:
5689 case DRM_FORMAT_YVU411:
5690 case DRM_FORMAT_YUV420:
5691 case DRM_FORMAT_YVU420:
5692 case DRM_FORMAT_YUV422:
5693 case DRM_FORMAT_YVU422:
5694 case DRM_FORMAT_YUV444:
5695 case DRM_FORMAT_YVU444:
5696 return 1;
5697 default:
5698 drm_fb_get_bpp_depth(format, &depth, &bpp);
5699 return bpp >> 3;
5700 }
5701}
5702EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005703
5704/**
5705 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5706 * @format: pixel format (DRM_FORMAT_*)
5707 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005708 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005709 * The horizontal chroma subsampling factor for the
5710 * specified pixel format.
5711 */
5712int drm_format_horz_chroma_subsampling(uint32_t format)
5713{
5714 switch (format) {
5715 case DRM_FORMAT_YUV411:
5716 case DRM_FORMAT_YVU411:
5717 case DRM_FORMAT_YUV410:
5718 case DRM_FORMAT_YVU410:
5719 return 4;
5720 case DRM_FORMAT_YUYV:
5721 case DRM_FORMAT_YVYU:
5722 case DRM_FORMAT_UYVY:
5723 case DRM_FORMAT_VYUY:
5724 case DRM_FORMAT_NV12:
5725 case DRM_FORMAT_NV21:
5726 case DRM_FORMAT_NV16:
5727 case DRM_FORMAT_NV61:
5728 case DRM_FORMAT_YUV422:
5729 case DRM_FORMAT_YVU422:
5730 case DRM_FORMAT_YUV420:
5731 case DRM_FORMAT_YVU420:
5732 return 2;
5733 default:
5734 return 1;
5735 }
5736}
5737EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5738
5739/**
5740 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5741 * @format: pixel format (DRM_FORMAT_*)
5742 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005743 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005744 * The vertical chroma subsampling factor for the
5745 * specified pixel format.
5746 */
5747int drm_format_vert_chroma_subsampling(uint32_t format)
5748{
5749 switch (format) {
5750 case DRM_FORMAT_YUV410:
5751 case DRM_FORMAT_YVU410:
5752 return 4;
5753 case DRM_FORMAT_YUV420:
5754 case DRM_FORMAT_YVU420:
5755 case DRM_FORMAT_NV12:
5756 case DRM_FORMAT_NV21:
5757 return 2;
5758 default:
5759 return 1;
5760 }
5761}
5762EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005763
5764/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005765 * drm_format_plane_width - width of the plane given the first plane
5766 * @width: width of the first plane
5767 * @format: pixel format
5768 * @plane: plane index
5769 *
5770 * Returns:
5771 * The width of @plane, given that the width of the first plane is @width.
5772 */
5773int drm_format_plane_width(int width, uint32_t format, int plane)
5774{
5775 if (plane >= drm_format_num_planes(format))
5776 return 0;
5777
5778 if (plane == 0)
5779 return width;
5780
5781 return width / drm_format_horz_chroma_subsampling(format);
5782}
5783EXPORT_SYMBOL(drm_format_plane_width);
5784
5785/**
5786 * drm_format_plane_height - height of the plane given the first plane
5787 * @height: height of the first plane
5788 * @format: pixel format
5789 * @plane: plane index
5790 *
5791 * Returns:
5792 * The height of @plane, given that the height of the first plane is @height.
5793 */
5794int drm_format_plane_height(int height, uint32_t format, int plane)
5795{
5796 if (plane >= drm_format_num_planes(format))
5797 return 0;
5798
5799 if (plane == 0)
5800 return height;
5801
5802 return height / drm_format_vert_chroma_subsampling(format);
5803}
5804EXPORT_SYMBOL(drm_format_plane_height);
5805
5806/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305807 * drm_rotation_simplify() - Try to simplify the rotation
5808 * @rotation: Rotation to be simplified
5809 * @supported_rotations: Supported rotations
5810 *
5811 * Attempt to simplify the rotation to a form that is supported.
5812 * Eg. if the hardware supports everything except DRM_REFLECT_X
5813 * one could call this function like this:
5814 *
5815 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5816 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5817 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5818 *
5819 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5820 * transforms the hardware supports, this function may not
5821 * be able to produce a supported transform, so the caller should
5822 * check the result afterwards.
5823 */
5824unsigned int drm_rotation_simplify(unsigned int rotation,
5825 unsigned int supported_rotations)
5826{
5827 if (rotation & ~supported_rotations) {
5828 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005829 rotation = (rotation & DRM_REFLECT_MASK) |
5830 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305831 }
5832
5833 return rotation;
5834}
5835EXPORT_SYMBOL(drm_rotation_simplify);
5836
5837/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005838 * drm_mode_config_init - initialize DRM mode_configuration structure
5839 * @dev: DRM device
5840 *
5841 * Initialize @dev's mode_config structure, used for tracking the graphics
5842 * configuration of @dev.
5843 *
5844 * Since this initializes the modeset locks, no locking is possible. Which is no
5845 * problem, since this should happen single threaded at init time. It is the
5846 * driver's problem to ensure this guarantee.
5847 *
5848 */
5849void drm_mode_config_init(struct drm_device *dev)
5850{
5851 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005852 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005853 mutex_init(&dev->mode_config.idr_mutex);
5854 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005855 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005856 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5857 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5858 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5859 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5860 INIT_LIST_HEAD(&dev->mode_config.property_list);
5861 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5862 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5863 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005864 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005865 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005866
5867 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005868 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005869 drm_modeset_unlock_all(dev);
5870
5871 /* Just to be sure */
5872 dev->mode_config.num_fb = 0;
5873 dev->mode_config.num_connector = 0;
5874 dev->mode_config.num_crtc = 0;
5875 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005876 dev->mode_config.num_overlay_plane = 0;
5877 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005878}
5879EXPORT_SYMBOL(drm_mode_config_init);
5880
5881/**
5882 * drm_mode_config_cleanup - free up DRM mode_config info
5883 * @dev: DRM device
5884 *
5885 * Free up all the connectors and CRTCs associated with this DRM device, then
5886 * free up the framebuffers and associated buffer objects.
5887 *
5888 * Note that since this /should/ happen single-threaded at driver/device
5889 * teardown time, no locking is required. It's the driver's job to ensure that
5890 * this guarantee actually holds true.
5891 *
5892 * FIXME: cleanup any dangling user buffer objects too
5893 */
5894void drm_mode_config_cleanup(struct drm_device *dev)
5895{
5896 struct drm_connector *connector, *ot;
5897 struct drm_crtc *crtc, *ct;
5898 struct drm_encoder *encoder, *enct;
5899 struct drm_framebuffer *fb, *fbt;
5900 struct drm_property *property, *pt;
5901 struct drm_property_blob *blob, *bt;
5902 struct drm_plane *plane, *plt;
5903
5904 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5905 head) {
5906 encoder->funcs->destroy(encoder);
5907 }
5908
5909 list_for_each_entry_safe(connector, ot,
5910 &dev->mode_config.connector_list, head) {
5911 connector->funcs->destroy(connector);
5912 }
5913
5914 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5915 head) {
5916 drm_property_destroy(dev, property);
5917 }
5918
5919 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005920 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005921 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005922 }
5923
5924 /*
5925 * Single-threaded teardown context, so it's not required to grab the
5926 * fb_lock to protect against concurrent fb_list access. Contrary, it
5927 * would actually deadlock with the drm_framebuffer_cleanup function.
5928 *
5929 * Also, if there are any framebuffers left, that's a driver leak now,
5930 * so politely WARN about this.
5931 */
5932 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5933 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Maarten Lankhorstc099b552015-09-09 13:46:21 +02005934 drm_framebuffer_free(&fb->refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005935 }
5936
5937 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5938 head) {
5939 plane->funcs->destroy(plane);
5940 }
5941
5942 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5943 crtc->funcs->destroy(crtc);
5944 }
5945
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005946 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005947 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005948 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005949 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005950}
5951EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305952
5953struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5954 unsigned int supported_rotations)
5955{
5956 static const struct drm_prop_enum_list props[] = {
5957 { DRM_ROTATE_0, "rotate-0" },
5958 { DRM_ROTATE_90, "rotate-90" },
5959 { DRM_ROTATE_180, "rotate-180" },
5960 { DRM_ROTATE_270, "rotate-270" },
5961 { DRM_REFLECT_X, "reflect-x" },
5962 { DRM_REFLECT_Y, "reflect-y" },
5963 };
5964
5965 return drm_property_create_bitmask(dev, 0, "rotation",
5966 props, ARRAY_SIZE(props),
5967 supported_rotations);
5968}
5969EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005970
5971/**
5972 * DOC: Tile group
5973 *
5974 * Tile groups are used to represent tiled monitors with a unique
5975 * integer identifier. Tiled monitors using DisplayID v1.3 have
5976 * a unique 8-byte handle, we store this in a tile group, so we
5977 * have a common identifier for all tiles in a monitor group.
5978 */
5979static void drm_tile_group_free(struct kref *kref)
5980{
5981 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5982 struct drm_device *dev = tg->dev;
5983 mutex_lock(&dev->mode_config.idr_mutex);
5984 idr_remove(&dev->mode_config.tile_idr, tg->id);
5985 mutex_unlock(&dev->mode_config.idr_mutex);
5986 kfree(tg);
5987}
5988
5989/**
5990 * drm_mode_put_tile_group - drop a reference to a tile group.
5991 * @dev: DRM device
5992 * @tg: tile group to drop reference to.
5993 *
5994 * drop reference to tile group and free if 0.
5995 */
5996void drm_mode_put_tile_group(struct drm_device *dev,
5997 struct drm_tile_group *tg)
5998{
5999 kref_put(&tg->refcount, drm_tile_group_free);
6000}
6001
6002/**
6003 * drm_mode_get_tile_group - get a reference to an existing tile group
6004 * @dev: DRM device
6005 * @topology: 8-bytes unique per monitor.
6006 *
6007 * Use the unique bytes to get a reference to an existing tile group.
6008 *
6009 * RETURNS:
6010 * tile group or NULL if not found.
6011 */
6012struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6013 char topology[8])
6014{
6015 struct drm_tile_group *tg;
6016 int id;
6017 mutex_lock(&dev->mode_config.idr_mutex);
6018 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6019 if (!memcmp(tg->group_data, topology, 8)) {
6020 if (!kref_get_unless_zero(&tg->refcount))
6021 tg = NULL;
6022 mutex_unlock(&dev->mode_config.idr_mutex);
6023 return tg;
6024 }
6025 }
6026 mutex_unlock(&dev->mode_config.idr_mutex);
6027 return NULL;
6028}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006029EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006030
6031/**
6032 * drm_mode_create_tile_group - create a tile group from a displayid description
6033 * @dev: DRM device
6034 * @topology: 8-bytes unique per monitor.
6035 *
6036 * Create a tile group for the unique monitor, and get a unique
6037 * identifier for the tile group.
6038 *
6039 * RETURNS:
6040 * new tile group or error.
6041 */
6042struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6043 char topology[8])
6044{
6045 struct drm_tile_group *tg;
6046 int ret;
6047
6048 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6049 if (!tg)
6050 return ERR_PTR(-ENOMEM);
6051
6052 kref_init(&tg->refcount);
6053 memcpy(tg->group_data, topology, 8);
6054 tg->dev = dev;
6055
6056 mutex_lock(&dev->mode_config.idr_mutex);
6057 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6058 if (ret >= 0) {
6059 tg->id = ret;
6060 } else {
6061 kfree(tg);
6062 tg = ERR_PTR(ret);
6063 }
6064
6065 mutex_unlock(&dev->mode_config.idr_mutex);
6066 return tg;
6067}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006068EXPORT_SYMBOL(drm_mode_create_tile_group);