blob: d7618bf18b4591afb4fb3619b1fb5202ee2ba823 [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,
Dave Airlied0f37cf2016-04-15 15:10:36 +1000280 bool register_obj,
281 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000282{
283 int ret;
284
285 mutex_lock(&dev->mode_config.idr_mutex);
286 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
287 if (ret >= 0) {
288 /*
289 * Set up the object linking under the protection of the idr
290 * lock so that other users can't see inconsistent state.
291 */
292 obj->id = ret;
293 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +1000294 if (obj_free_cb) {
295 obj->free_cb = obj_free_cb;
296 kref_init(&obj->refcount);
297 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000298 }
299 mutex_unlock(&dev->mode_config.idr_mutex);
300
301 return ret < 0 ? ret : 0;
302}
303
Dave Airlief453ba02008-11-07 14:05:41 -0800304/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100305 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800306 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100307 * @obj: object pointer, used to generate unique ID
308 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800309 *
Dave Airlief453ba02008-11-07 14:05:41 -0800310 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100311 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
312 * modeset identifiers are _not_ reference counted. Hence don't use this for
313 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800314 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100315 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200316 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800317 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100318int drm_mode_object_get(struct drm_device *dev,
319 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800320{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000321 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000322}
Dave Airlief453ba02008-11-07 14:05:41 -0800323
Dave Airlie2ee39452014-07-24 11:53:45 +1000324static void drm_mode_object_register(struct drm_device *dev,
325 struct drm_mode_object *obj)
326{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000327 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000328 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000329 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800330}
331
332/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000333 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800334 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100335 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800336 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000337 * Free @id from @dev's unique identifier pool.
338 * This function can be called multiple times, and guards against
339 * multiple removals.
340 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100341 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800342 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000343void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100344 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800345{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000346 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000347 if (object->id) {
348 idr_remove(&dev->mode_config.crtc_idr, object->id);
349 object->id = 0;
350 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000351 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800352}
353
Rob Clark98f75de2014-05-30 11:37:03 -0400354static struct drm_mode_object *_object_find(struct drm_device *dev,
355 uint32_t id, uint32_t type)
356{
357 struct drm_mode_object *obj = NULL;
358
359 mutex_lock(&dev->mode_config.idr_mutex);
360 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200361 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
362 obj = NULL;
363 if (obj && obj->id != id)
364 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000365
366 if (obj && obj->free_cb) {
367 if (!kref_get_unless_zero(&obj->refcount))
368 obj = NULL;
369 }
Rob Clark98f75de2014-05-30 11:37:03 -0400370 mutex_unlock(&dev->mode_config.idr_mutex);
371
372 return obj;
373}
374
Daniel Vetter786b99e2012-12-02 21:53:40 +0100375/**
376 * drm_mode_object_find - look up a drm object with static lifetime
377 * @dev: drm device
378 * @id: id of the mode object
379 * @type: type of the mode object
380 *
Daniel Vetter059814222016-04-22 22:10:27 +0200381 * This function is used to look up a modeset object. It will acquire a
382 * reference for reference counted objects. This reference must be dropped again
383 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100384 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200385struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
386 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800387{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000388 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800389
Rob Clark98f75de2014-05-30 11:37:03 -0400390 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800391 return obj;
392}
393EXPORT_SYMBOL(drm_mode_object_find);
394
Daniel Vetter059814222016-04-22 22:10:27 +0200395/**
396 * drm_mode_object_unreference - decr the object refcnt
397 * @obj: mode_object
398 *
399 * This functions decrements the object's refcount if it is a refcounted modeset
400 * object. It is a no-op on any other object. This is used to drop references
401 * acquired with drm_mode_object_reference().
402 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000403void drm_mode_object_unreference(struct drm_mode_object *obj)
404{
405 if (obj->free_cb) {
406 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
407 kref_put(&obj->refcount, obj->free_cb);
408 }
409}
410EXPORT_SYMBOL(drm_mode_object_unreference);
411
412/**
Daniel Vetter059814222016-04-22 22:10:27 +0200413 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000414 * @obj: mode_object
415 *
Daniel Vetter059814222016-04-22 22:10:27 +0200416 * This functions increments the object's refcount if it is a refcounted modeset
417 * object. It is a no-op on any other object. References should be dropped again
418 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000419 */
420void drm_mode_object_reference(struct drm_mode_object *obj)
421{
422 if (obj->free_cb) {
423 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
424 kref_get(&obj->refcount);
425 }
426}
427EXPORT_SYMBOL(drm_mode_object_reference);
428
Dave Airlief55f1f92016-04-15 15:10:33 +1000429static void drm_framebuffer_free(struct kref *kref)
430{
431 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000432 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000433 struct drm_device *dev = fb->dev;
434
435 /*
436 * The lookup idr holds a weak reference, which has not necessarily been
437 * removed at this point. Check for that.
438 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000439 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000440
441 fb->funcs->destroy(fb);
442}
443
Dave Airlief453ba02008-11-07 14:05:41 -0800444/**
Dave Airlief453ba02008-11-07 14:05:41 -0800445 * drm_framebuffer_init - initialize a framebuffer
446 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100447 * @fb: framebuffer to be initialized
448 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800449 *
Dave Airlief453ba02008-11-07 14:05:41 -0800450 * Allocates an ID for the framebuffer's parent mode object, sets its mode
451 * functions & device file and adds it to the master fd list.
452 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100453 * IMPORTANT:
454 * This functions publishes the fb and makes it available for concurrent access
455 * by other users. Which means by this point the fb _must_ be fully set up -
456 * since all the fb attributes are invariant over its lifetime, no further
457 * locking but only correct reference counting is required.
458 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100459 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200460 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800461 */
462int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
463 const struct drm_framebuffer_funcs *funcs)
464{
465 int ret;
466
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100467 INIT_LIST_HEAD(&fb->filp_head);
468 fb->dev = dev;
469 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000470
Dave Airlied0f37cf2016-04-15 15:10:36 +1000471 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000472 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200473 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100474 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800475
Dave Airlie9cd47422016-04-15 15:10:38 +1000476 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800477 dev->mode_config.num_fb++;
478 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000479 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800480
Dave Airlie9cd47422016-04-15 15:10:38 +1000481 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000482out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200483 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800484}
485EXPORT_SYMBOL(drm_framebuffer_init);
486
Rob Clarkf7eff602012-09-05 21:48:38 +0000487/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100488 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
489 * @dev: drm device
490 * @id: id of the fb object
491 *
492 * If successful, this grabs an additional reference to the framebuffer -
493 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100494 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100495 */
496struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
497 uint32_t id)
498{
Dave Airliecee26ac2016-04-15 15:10:37 +1000499 struct drm_mode_object *obj;
500 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100501
Dave Airliecee26ac2016-04-15 15:10:37 +1000502 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000503 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000504 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100505 return fb;
506}
507EXPORT_SYMBOL(drm_framebuffer_lookup);
508
509/**
Daniel Vetter36206362012-12-10 20:42:17 +0100510 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
511 * @fb: fb to unregister
512 *
513 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
514 * those used for fbdev. Note that the caller must hold a reference of it's own,
515 * i.e. the object may not be destroyed through this call (since it'll lead to a
516 * locking inversion).
517 */
518void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
519{
Daniel Vettera39a3572015-08-25 15:45:11 +0200520 struct drm_device *dev;
521
522 if (!fb)
523 return;
524
525 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100526
Daniel Vetter2b677e82012-12-10 21:16:05 +0100527 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000528 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100529}
530EXPORT_SYMBOL(drm_framebuffer_unregister_private);
531
532/**
Dave Airlief453ba02008-11-07 14:05:41 -0800533 * drm_framebuffer_cleanup - remove a framebuffer object
534 * @fb: framebuffer to remove
535 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100536 * Cleanup framebuffer. This function is intended to be used from the drivers
537 * ->destroy callback. It can also be used to clean up driver private
538 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100539 *
540 * Note that this function does not remove the fb from active usuage - if it is
541 * still used anywhere, hilarity can ensue since userspace could call getfb on
542 * the id and get back -EINVAL. Obviously no concern at driver unload time.
543 *
544 * Also, the framebuffer will not be removed from the lookup idr - for
545 * user-created framebuffers this will happen in in the rmfb ioctl. For
546 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
547 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800548 */
549void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
550{
551 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100552
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100553 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000554 list_del(&fb->head);
555 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100556 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000557}
558EXPORT_SYMBOL(drm_framebuffer_cleanup);
559
560/**
561 * drm_framebuffer_remove - remove and unreference a framebuffer object
562 * @fb: framebuffer to remove
563 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000564 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100565 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100566 * passed-in framebuffer. Might take the modeset locks.
567 *
568 * Note that this function optimizes the cleanup away if the caller holds the
569 * last reference to the framebuffer. It is also guaranteed to not take the
570 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000571 */
572void drm_framebuffer_remove(struct drm_framebuffer *fb)
573{
Daniel Vettera39a3572015-08-25 15:45:11 +0200574 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800575 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800576 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000577 struct drm_mode_set set;
578 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800579
Daniel Vettera39a3572015-08-25 15:45:11 +0200580 if (!fb)
581 return;
582
583 dev = fb->dev;
584
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100585 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100586
Daniel Vetterb62584e2012-12-11 16:51:35 +0100587 /*
588 * drm ABI mandates that we remove any deleted framebuffers from active
589 * useage. But since most sane clients only remove framebuffers they no
590 * longer need, try to optimize this away.
591 *
592 * Since we're holding a reference ourselves, observing a refcount of 1
593 * means that we're the last holder and can skip it. Also, the refcount
594 * can never increase from 1 again, so we don't need any barriers or
595 * locks.
596 *
597 * Note that userspace could try to race with use and instate a new
598 * usage _after_ we've cleared all current ones. End result will be an
599 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
600 * in this manner.
601 */
Dave Airlie747a5982016-04-15 15:10:35 +1000602 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100603 drm_modeset_lock_all(dev);
604 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200605 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700606 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100607 /* should turn off the crtc */
608 memset(&set, 0, sizeof(struct drm_mode_set));
609 set.crtc = crtc;
610 set.fb = NULL;
611 ret = drm_mode_set_config_internal(&set);
612 if (ret)
613 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
614 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000615 }
Dave Airlief453ba02008-11-07 14:05:41 -0800616
Daniel Vetter6295d602015-07-09 23:44:25 +0200617 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300618 if (plane->fb == fb)
619 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800620 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100621 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800622 }
623
Rob Clarkf7eff602012-09-05 21:48:38 +0000624 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800625}
Rob Clarkf7eff602012-09-05 21:48:38 +0000626EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800627
Rob Clark51fd3712013-11-19 12:10:12 -0500628DEFINE_WW_CLASS(crtc_ww_class);
629
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200630static unsigned int drm_num_crtcs(struct drm_device *dev)
631{
632 unsigned int num = 0;
633 struct drm_crtc *tmp;
634
635 drm_for_each_crtc(tmp, dev) {
636 num++;
637 }
638
639 return num;
640}
641
Dave Airlief453ba02008-11-07 14:05:41 -0800642/**
Matt Ropere13161a2014-04-01 15:22:38 -0700643 * drm_crtc_init_with_planes - Initialise a new CRTC object with
644 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800645 * @dev: DRM device
646 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700647 * @primary: Primary plane for CRTC
648 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800649 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200650 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800651 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000652 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200653 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100654 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200655 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800656 */
Matt Ropere13161a2014-04-01 15:22:38 -0700657int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
658 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700659 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200660 const struct drm_crtc_funcs *funcs,
661 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800662{
Rob Clark51fd3712013-11-19 12:10:12 -0500663 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200664 int ret;
665
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100666 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
667 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
668
Dave Airlief453ba02008-11-07 14:05:41 -0800669 crtc->dev = dev;
670 crtc->funcs = funcs;
671
Rob Clark51fd3712013-11-19 12:10:12 -0500672 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200673 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
674 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100675 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800676
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200677 if (name) {
678 va_list ap;
679
680 va_start(ap, name);
681 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
682 va_end(ap);
683 } else {
684 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
685 drm_num_crtcs(dev));
686 }
687 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000688 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200689 return -ENOMEM;
690 }
691
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300692 crtc->base.properties = &crtc->properties;
693
Rob Clark51fd3712013-11-19 12:10:12 -0500694 list_add_tail(&crtc->head, &config->crtc_list);
695 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200696
Matt Ropere13161a2014-04-01 15:22:38 -0700697 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700698 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700699 if (primary)
700 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700701 if (cursor)
702 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700703
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100704 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
705 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100706 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100707 }
708
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100709 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800710}
Matt Ropere13161a2014-04-01 15:22:38 -0700711EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800712
713/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000714 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800715 * @crtc: CRTC to cleanup
716 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000717 * This function cleans up @crtc and removes it from the DRM mode setting
718 * core. Note that the function does *not* free the crtc structure itself,
719 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800720 */
721void drm_crtc_cleanup(struct drm_crtc *crtc)
722{
723 struct drm_device *dev = crtc->dev;
724
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000725 kfree(crtc->gamma_store);
726 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800727
Rob Clark51fd3712013-11-19 12:10:12 -0500728 drm_modeset_lock_fini(&crtc->mutex);
729
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000730 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800731 list_del(&crtc->head);
732 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100733
734 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
735 if (crtc->state && crtc->funcs->atomic_destroy_state)
736 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100737
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200738 kfree(crtc->name);
739
Thierry Redinga18c0af2014-12-10 11:38:49 +0100740 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800741}
742EXPORT_SYMBOL(drm_crtc_cleanup);
743
744/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000745 * drm_crtc_index - find the index of a registered CRTC
746 * @crtc: CRTC to find index for
747 *
748 * Given a registered CRTC, return the index of that CRTC within a DRM
749 * device's list of CRTCs.
750 */
751unsigned int drm_crtc_index(struct drm_crtc *crtc)
752{
753 unsigned int index = 0;
754 struct drm_crtc *tmp;
755
Daniel Vettere4f62542015-07-09 23:44:35 +0200756 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000757 if (tmp == crtc)
758 return index;
759
760 index++;
761 }
762
763 BUG();
764}
765EXPORT_SYMBOL(drm_crtc_index);
766
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100767/*
Dave Airlief453ba02008-11-07 14:05:41 -0800768 * drm_mode_remove - remove and free a mode
769 * @connector: connector list to modify
770 * @mode: mode to remove
771 *
Dave Airlief453ba02008-11-07 14:05:41 -0800772 * Remove @mode from @connector's mode list, then free it.
773 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100774static void drm_mode_remove(struct drm_connector *connector,
775 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800776{
777 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100778 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800779}
Dave Airlief453ba02008-11-07 14:05:41 -0800780
781/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200782 * drm_display_info_set_bus_formats - set the supported bus formats
783 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100784 * @formats: array containing the supported bus formats
785 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200786 *
787 * Store the supported bus formats in display info structure.
788 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
789 * a full list of available formats.
790 */
791int drm_display_info_set_bus_formats(struct drm_display_info *info,
792 const u32 *formats,
793 unsigned int num_formats)
794{
795 u32 *fmts = NULL;
796
797 if (!formats && num_formats)
798 return -EINVAL;
799
800 if (formats && num_formats) {
801 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
802 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300803 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200804 return -ENOMEM;
805 }
806
807 kfree(info->bus_formats);
808 info->bus_formats = fmts;
809 info->num_bus_formats = num_formats;
810
811 return 0;
812}
813EXPORT_SYMBOL(drm_display_info_set_bus_formats);
814
815/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200816 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
817 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200818 *
819 * The kernel supports per-connector configration of its consoles through
820 * use of the video= parameter. This function parses that option and
821 * extracts the user's specified mode (or enable/disable status) for a
822 * particular connector. This is typically only used during the early fbdev
823 * setup.
824 */
825static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
826{
827 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
828 char *option = NULL;
829
830 if (fb_get_options(connector->name, &option))
831 return;
832
833 if (!drm_mode_parse_command_line_for_connector(option,
834 connector,
835 mode))
836 return;
837
838 if (mode->force) {
839 const char *s;
840
841 switch (mode->force) {
842 case DRM_FORCE_OFF:
843 s = "OFF";
844 break;
845 case DRM_FORCE_ON_DIGITAL:
846 s = "ON - dig";
847 break;
848 default:
849 case DRM_FORCE_ON:
850 s = "ON";
851 break;
852 }
853
854 DRM_INFO("forcing %s connector %s\n", connector->name, s);
855 connector->force = mode->force;
856 }
857
858 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
859 connector->name,
860 mode->xres, mode->yres,
861 mode->refresh_specified ? mode->refresh : 60,
862 mode->rb ? " reduced blanking" : "",
863 mode->margins ? " with margins" : "",
864 mode->interlace ? " interlaced" : "");
865}
866
Dave Airlieb164d312016-04-27 11:10:09 +1000867static void drm_connector_free(struct kref *kref)
868{
869 struct drm_connector *connector =
870 container_of(kref, struct drm_connector, base.refcount);
871 struct drm_device *dev = connector->dev;
872
873 drm_mode_object_unregister(dev, &connector->base);
874 connector->funcs->destroy(connector);
875}
876
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200877/**
Dave Airlief453ba02008-11-07 14:05:41 -0800878 * drm_connector_init - Init a preallocated connector
879 * @dev: DRM device
880 * @connector: the connector to init
881 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100882 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800883 *
Dave Airlief453ba02008-11-07 14:05:41 -0800884 * Initialises a preallocated connector. Connectors should be
885 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200886 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100887 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200888 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800889 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890int drm_connector_init(struct drm_device *dev,
891 struct drm_connector *connector,
892 const struct drm_connector_funcs *funcs,
893 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800894{
Rob Clarkae16c592014-12-18 16:01:54 -0500895 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400897 struct ida *connector_ida =
898 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200899
Daniel Vetter84849902012-12-02 00:28:11 +0100900 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800901
Dave Airlieb164d312016-04-27 11:10:09 +1000902 ret = drm_mode_object_get_reg(dev, &connector->base,
903 DRM_MODE_OBJECT_CONNECTOR,
904 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200905 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300906 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200907
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300908 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800909 connector->dev = dev;
910 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100911
912 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
913 if (connector->connector_id < 0) {
914 ret = connector->connector_id;
915 goto out_put;
916 }
917
Dave Airlief453ba02008-11-07 14:05:41 -0800918 connector->connector_type = connector_type;
919 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400920 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
921 if (connector->connector_type_id < 0) {
922 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100923 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400924 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300925 connector->name =
926 kasprintf(GFP_KERNEL, "%s-%d",
927 drm_connector_enum_list[connector_type].name,
928 connector->connector_type_id);
929 if (!connector->name) {
930 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100931 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300932 }
933
Dave Airlief453ba02008-11-07 14:05:41 -0800934 INIT_LIST_HEAD(&connector->probed_modes);
935 INIT_LIST_HEAD(&connector->modes);
936 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000937 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800938
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200939 drm_connector_get_cmdline_mode(connector);
940
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100941 /* We should add connectors at the end to avoid upsetting the connector
942 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500943 list_add_tail(&connector->head, &config->connector_list);
944 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800945
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200946 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500947 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500948 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200949 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800950
Rob Clark58495562012-10-11 20:50:56 -0500951 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500952 config->dpms_property, 0);
953
954 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
955 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
956 }
Dave Airlief453ba02008-11-07 14:05:41 -0800957
Thomas Wood30f65702014-06-18 17:52:32 +0100958 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100959out_put_type_id:
960 if (ret)
961 ida_remove(connector_ida, connector->connector_type_id);
962out_put_id:
963 if (ret)
964 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300965out_put:
966 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000967 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300968
969out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100970 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200971
972 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800973}
974EXPORT_SYMBOL(drm_connector_init);
975
976/**
977 * drm_connector_cleanup - cleans up an initialised connector
978 * @connector: connector to cleanup
979 *
Dave Airlief453ba02008-11-07 14:05:41 -0800980 * Cleans up the connector but doesn't free the object.
981 */
982void drm_connector_cleanup(struct drm_connector *connector)
983{
984 struct drm_device *dev = connector->dev;
985 struct drm_display_mode *mode, *t;
986
Dave Airlie40d9b042014-10-20 16:29:33 +1000987 if (connector->tile_group) {
988 drm_mode_put_tile_group(dev, connector->tile_group);
989 connector->tile_group = NULL;
990 }
991
Dave Airlief453ba02008-11-07 14:05:41 -0800992 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
993 drm_mode_remove(connector, mode);
994
995 list_for_each_entry_safe(mode, t, &connector->modes, head)
996 drm_mode_remove(connector, mode);
997
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400998 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
999 connector->connector_type_id);
1000
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001001 ida_remove(&dev->mode_config.connector_ida,
1002 connector->connector_id);
1003
Boris Brezillonb5571e92014-07-22 12:09:10 +02001004 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001005 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +03001006 kfree(connector->name);
1007 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001008 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001009 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001010
1011 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1012 if (connector->state && connector->funcs->atomic_destroy_state)
1013 connector->funcs->atomic_destroy_state(connector,
1014 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001015
1016 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001017}
1018EXPORT_SYMBOL(drm_connector_cleanup);
1019
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001020/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001021 * drm_connector_register - register a connector
1022 * @connector: the connector to register
1023 *
1024 * Register userspace interfaces for a connector
1025 *
1026 * Returns:
1027 * Zero on success, error code on failure.
1028 */
1029int drm_connector_register(struct drm_connector *connector)
1030{
Thomas Wood30f65702014-06-18 17:52:32 +01001031 int ret;
1032
1033 ret = drm_sysfs_connector_add(connector);
1034 if (ret)
1035 return ret;
1036
1037 ret = drm_debugfs_connector_add(connector);
1038 if (ret) {
1039 drm_sysfs_connector_remove(connector);
1040 return ret;
1041 }
1042
Daniel Vetterfdf2c852016-05-06 14:55:02 +02001043 drm_mode_object_register(connector->dev, &connector->base);
1044
Thomas Wood30f65702014-06-18 17:52:32 +01001045 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001046}
1047EXPORT_SYMBOL(drm_connector_register);
1048
1049/**
1050 * drm_connector_unregister - unregister a connector
1051 * @connector: the connector to unregister
1052 *
1053 * Unregister userspace interfaces for a connector
1054 */
1055void drm_connector_unregister(struct drm_connector *connector)
1056{
1057 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001058 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001059}
1060EXPORT_SYMBOL(drm_connector_unregister);
1061
Thomas Wood34ea3d32014-05-29 16:57:41 +01001062/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001063 * drm_connector_register_all - register all connectors
1064 * @dev: drm device
1065 *
1066 * This function registers all connectors in sysfs and other places so that
1067 * userspace can start to access them. Drivers can call it after calling
1068 * drm_dev_register() to complete the device registration, if they don't call
1069 * drm_connector_register() on each connector individually.
1070 *
1071 * When a device is unplugged and should be removed from userspace access,
1072 * call drm_connector_unregister_all(), which is the inverse of this
1073 * function.
1074 *
1075 * Returns:
1076 * Zero on success, error code on failure.
1077 */
1078int drm_connector_register_all(struct drm_device *dev)
1079{
1080 struct drm_connector *connector;
1081 int ret;
1082
1083 mutex_lock(&dev->mode_config.mutex);
1084
1085 drm_for_each_connector(connector, dev) {
1086 ret = drm_connector_register(connector);
1087 if (ret)
1088 goto err;
1089 }
1090
1091 mutex_unlock(&dev->mode_config.mutex);
1092
1093 return 0;
1094
1095err:
1096 mutex_unlock(&dev->mode_config.mutex);
1097 drm_connector_unregister_all(dev);
1098 return ret;
1099}
1100EXPORT_SYMBOL(drm_connector_register_all);
1101
1102/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001103 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001104 * @dev: drm device
1105 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001106 * This functions unregisters all connectors from sysfs and other places so
1107 * that userspace can no longer access them. Drivers should call this as the
1108 * first step tearing down the device instace, or when the underlying
1109 * physical device disappeared (e.g. USB unplug), right before calling
1110 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001111 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001112void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001113{
1114 struct drm_connector *connector;
1115
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001116 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001117 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001118 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001119}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001120EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001121
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001122/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001123 * drm_encoder_init - Init a preallocated encoder
1124 * @dev: drm device
1125 * @encoder: the encoder to init
1126 * @funcs: callbacks for this encoder
1127 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001128 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001129 *
1130 * Initialises a preallocated encoder. Encoder should be
1131 * subclassed as part of driver encoder objects.
1132 *
1133 * Returns:
1134 * Zero on success, error code on failure.
1135 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001136int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001137 struct drm_encoder *encoder,
1138 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001139 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001140{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001141 int ret;
1142
Daniel Vetter84849902012-12-02 00:28:11 +01001143 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001144
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001145 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1146 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001147 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001148
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001149 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001150 encoder->encoder_type = encoder_type;
1151 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001152 if (name) {
1153 va_list ap;
1154
1155 va_start(ap, name);
1156 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1157 va_end(ap);
1158 } else {
1159 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1160 drm_encoder_enum_list[encoder_type].name,
1161 encoder->base.id);
1162 }
Jani Nikulae5748942014-05-14 16:58:20 +03001163 if (!encoder->name) {
1164 ret = -ENOMEM;
1165 goto out_put;
1166 }
Dave Airlief453ba02008-11-07 14:05:41 -08001167
1168 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1169 dev->mode_config.num_encoder++;
1170
Jani Nikulae5748942014-05-14 16:58:20 +03001171out_put:
1172 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001173 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001174
1175out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001176 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001177
1178 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001179}
1180EXPORT_SYMBOL(drm_encoder_init);
1181
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001182/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001183 * drm_encoder_index - find the index of a registered encoder
1184 * @encoder: encoder to find index for
1185 *
1186 * Given a registered encoder, return the index of that encoder within a DRM
1187 * device's list of encoders.
1188 */
1189unsigned int drm_encoder_index(struct drm_encoder *encoder)
1190{
1191 unsigned int index = 0;
1192 struct drm_encoder *tmp;
1193
1194 drm_for_each_encoder(tmp, encoder->dev) {
1195 if (tmp == encoder)
1196 return index;
1197
1198 index++;
1199 }
1200
1201 BUG();
1202}
1203EXPORT_SYMBOL(drm_encoder_index);
1204
1205/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001206 * drm_encoder_cleanup - cleans up an initialised encoder
1207 * @encoder: encoder to cleanup
1208 *
1209 * Cleans up the encoder but doesn't free the object.
1210 */
Dave Airlief453ba02008-11-07 14:05:41 -08001211void drm_encoder_cleanup(struct drm_encoder *encoder)
1212{
1213 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001214
Daniel Vetter84849902012-12-02 00:28:11 +01001215 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001216 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001217 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001218 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001219 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001220 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001221
1222 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001223}
1224EXPORT_SYMBOL(drm_encoder_cleanup);
1225
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001226static unsigned int drm_num_planes(struct drm_device *dev)
1227{
1228 unsigned int num = 0;
1229 struct drm_plane *tmp;
1230
1231 drm_for_each_plane(tmp, dev) {
1232 num++;
1233 }
1234
1235 return num;
1236}
1237
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001238/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001239 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001240 * @dev: DRM device
1241 * @plane: plane object to init
1242 * @possible_crtcs: bitmask of possible CRTCs
1243 * @funcs: callbacks for the new plane
1244 * @formats: array of supported formats (%DRM_FORMAT_*)
1245 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001246 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001247 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001248 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001249 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001250 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001251 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001252 * Zero on success, error code on failure.
1253 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001254int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1255 unsigned long possible_crtcs,
1256 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001257 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001258 enum drm_plane_type type,
1259 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001260{
Rob Clark6b4959f2014-12-18 16:01:53 -05001261 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001262 int ret;
1263
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001264 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1265 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001266 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001267
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001268 drm_modeset_lock_init(&plane->mutex);
1269
Rob Clark4d939142012-05-17 02:23:27 -06001270 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001271 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001272 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001273 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1274 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001275 if (!plane->format_types) {
1276 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001277 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001278 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001279 }
1280
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001281 if (name) {
1282 va_list ap;
1283
1284 va_start(ap, name);
1285 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1286 va_end(ap);
1287 } else {
1288 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1289 drm_num_planes(dev));
1290 }
1291 if (!plane->name) {
1292 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001293 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001294 return -ENOMEM;
1295 }
1296
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001297 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001298 plane->format_count = format_count;
1299 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001300 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001301
Rob Clark6b4959f2014-12-18 16:01:53 -05001302 list_add_tail(&plane->head, &config->plane_list);
1303 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001304 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001305 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001306
Rob Clark9922ab52014-04-01 20:16:57 -04001307 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001308 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001309 plane->type);
1310
Rob Clark6b4959f2014-12-18 16:01:53 -05001311 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1312 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1313 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1314 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1315 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1316 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1317 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1318 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1319 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1320 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1321 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1322 }
1323
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001324 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001325}
Matt Roperdc415ff2014-04-01 15:22:36 -07001326EXPORT_SYMBOL(drm_universal_plane_init);
1327
1328/**
1329 * drm_plane_init - Initialize a legacy plane
1330 * @dev: DRM device
1331 * @plane: plane object to init
1332 * @possible_crtcs: bitmask of possible CRTCs
1333 * @funcs: callbacks for the new plane
1334 * @formats: array of supported formats (%DRM_FORMAT_*)
1335 * @format_count: number of elements in @formats
1336 * @is_primary: plane type (primary vs overlay)
1337 *
1338 * Legacy API to initialize a DRM plane.
1339 *
1340 * New drivers should call drm_universal_plane_init() instead.
1341 *
1342 * Returns:
1343 * Zero on success, error code on failure.
1344 */
1345int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1346 unsigned long possible_crtcs,
1347 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001348 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001349 bool is_primary)
1350{
1351 enum drm_plane_type type;
1352
1353 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1354 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001355 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001356}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001357EXPORT_SYMBOL(drm_plane_init);
1358
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001359/**
1360 * drm_plane_cleanup - Clean up the core plane usage
1361 * @plane: plane to cleanup
1362 *
1363 * This function cleans up @plane and removes it from the DRM mode setting
1364 * core. Note that the function does *not* free the plane structure itself,
1365 * this is the responsibility of the caller.
1366 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001367void drm_plane_cleanup(struct drm_plane *plane)
1368{
1369 struct drm_device *dev = plane->dev;
1370
Daniel Vetter84849902012-12-02 00:28:11 +01001371 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001372 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001373 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001374
1375 BUG_ON(list_empty(&plane->head));
1376
1377 list_del(&plane->head);
1378 dev->mode_config.num_total_plane--;
1379 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1380 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001381 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001382
1383 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1384 if (plane->state && plane->funcs->atomic_destroy_state)
1385 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001386
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001387 kfree(plane->name);
1388
Thierry Redinga18c0af2014-12-10 11:38:49 +01001389 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001390}
1391EXPORT_SYMBOL(drm_plane_cleanup);
1392
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001393/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001394 * drm_plane_index - find the index of a registered plane
1395 * @plane: plane to find index for
1396 *
1397 * Given a registered plane, return the index of that CRTC within a DRM
1398 * device's list of planes.
1399 */
1400unsigned int drm_plane_index(struct drm_plane *plane)
1401{
1402 unsigned int index = 0;
1403 struct drm_plane *tmp;
1404
Daniel Vettere4f62542015-07-09 23:44:35 +02001405 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001406 if (tmp == plane)
1407 return index;
1408
1409 index++;
1410 }
1411
1412 BUG();
1413}
1414EXPORT_SYMBOL(drm_plane_index);
1415
1416/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001417 * drm_plane_from_index - find the registered plane at an index
1418 * @dev: DRM device
1419 * @idx: index of registered plane to find for
1420 *
1421 * Given a plane index, return the registered plane from DRM device's
1422 * list of planes with matching index.
1423 */
1424struct drm_plane *
1425drm_plane_from_index(struct drm_device *dev, int idx)
1426{
1427 struct drm_plane *plane;
1428 unsigned int i = 0;
1429
Daniel Vetter6295d602015-07-09 23:44:25 +02001430 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001431 if (i == idx)
1432 return plane;
1433 i++;
1434 }
1435 return NULL;
1436}
1437EXPORT_SYMBOL(drm_plane_from_index);
1438
1439/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001440 * drm_plane_force_disable - Forcibly disable a plane
1441 * @plane: plane to disable
1442 *
1443 * Forces the plane to be disabled.
1444 *
1445 * Used when the plane's current framebuffer is destroyed,
1446 * and when restoring fbdev mode.
1447 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001448void drm_plane_force_disable(struct drm_plane *plane)
1449{
1450 int ret;
1451
Daniel Vetter3d30a592014-07-27 13:42:42 +02001452 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001453 return;
1454
Daniel Vetter3d30a592014-07-27 13:42:42 +02001455 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001456 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001457 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001458 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001459 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001460 return;
1461 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001462 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001463 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001464 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001465 plane->fb = NULL;
1466 plane->crtc = NULL;
1467}
1468EXPORT_SYMBOL(drm_plane_force_disable);
1469
Rob Clark6b4959f2014-12-18 16:01:53 -05001470static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001471{
Rob Clark356af0e2014-12-18 16:01:52 -05001472 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001473
1474 /*
1475 * Standard properties (apply to all connectors)
1476 */
Rob Clark356af0e2014-12-18 16:01:52 -05001477 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001478 DRM_MODE_PROP_IMMUTABLE,
1479 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001480 if (!prop)
1481 return -ENOMEM;
1482 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001483
Rob Clark356af0e2014-12-18 16:01:52 -05001484 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001485 "DPMS", drm_dpms_enum_list,
1486 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001487 if (!prop)
1488 return -ENOMEM;
1489 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001490
Rob Clark356af0e2014-12-18 16:01:52 -05001491 prop = drm_property_create(dev,
1492 DRM_MODE_PROP_BLOB |
1493 DRM_MODE_PROP_IMMUTABLE,
1494 "PATH", 0);
1495 if (!prop)
1496 return -ENOMEM;
1497 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001498
Rob Clark356af0e2014-12-18 16:01:52 -05001499 prop = drm_property_create(dev,
1500 DRM_MODE_PROP_BLOB |
1501 DRM_MODE_PROP_IMMUTABLE,
1502 "TILE", 0);
1503 if (!prop)
1504 return -ENOMEM;
1505 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001506
Rob Clark6b4959f2014-12-18 16:01:53 -05001507 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001508 "type", drm_plane_type_enum_list,
1509 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001510 if (!prop)
1511 return -ENOMEM;
1512 dev->mode_config.plane_type_property = prop;
1513
1514 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1515 "SRC_X", 0, UINT_MAX);
1516 if (!prop)
1517 return -ENOMEM;
1518 dev->mode_config.prop_src_x = prop;
1519
1520 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1521 "SRC_Y", 0, UINT_MAX);
1522 if (!prop)
1523 return -ENOMEM;
1524 dev->mode_config.prop_src_y = prop;
1525
1526 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1527 "SRC_W", 0, UINT_MAX);
1528 if (!prop)
1529 return -ENOMEM;
1530 dev->mode_config.prop_src_w = prop;
1531
1532 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1533 "SRC_H", 0, UINT_MAX);
1534 if (!prop)
1535 return -ENOMEM;
1536 dev->mode_config.prop_src_h = prop;
1537
1538 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1539 "CRTC_X", INT_MIN, INT_MAX);
1540 if (!prop)
1541 return -ENOMEM;
1542 dev->mode_config.prop_crtc_x = prop;
1543
1544 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1545 "CRTC_Y", INT_MIN, INT_MAX);
1546 if (!prop)
1547 return -ENOMEM;
1548 dev->mode_config.prop_crtc_y = prop;
1549
1550 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1551 "CRTC_W", 0, INT_MAX);
1552 if (!prop)
1553 return -ENOMEM;
1554 dev->mode_config.prop_crtc_w = prop;
1555
1556 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1557 "CRTC_H", 0, INT_MAX);
1558 if (!prop)
1559 return -ENOMEM;
1560 dev->mode_config.prop_crtc_h = prop;
1561
1562 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1563 "FB_ID", DRM_MODE_OBJECT_FB);
1564 if (!prop)
1565 return -ENOMEM;
1566 dev->mode_config.prop_fb_id = prop;
1567
1568 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1569 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1570 if (!prop)
1571 return -ENOMEM;
1572 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001573
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001574 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1575 "ACTIVE");
1576 if (!prop)
1577 return -ENOMEM;
1578 dev->mode_config.prop_active = prop;
1579
Daniel Stone955f3c32015-05-25 19:11:52 +01001580 prop = drm_property_create(dev,
1581 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1582 "MODE_ID", 0);
1583 if (!prop)
1584 return -ENOMEM;
1585 dev->mode_config.prop_mode_id = prop;
1586
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001587 prop = drm_property_create(dev,
1588 DRM_MODE_PROP_BLOB,
1589 "DEGAMMA_LUT", 0);
1590 if (!prop)
1591 return -ENOMEM;
1592 dev->mode_config.degamma_lut_property = prop;
1593
1594 prop = drm_property_create_range(dev,
1595 DRM_MODE_PROP_IMMUTABLE,
1596 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1597 if (!prop)
1598 return -ENOMEM;
1599 dev->mode_config.degamma_lut_size_property = prop;
1600
1601 prop = drm_property_create(dev,
1602 DRM_MODE_PROP_BLOB,
1603 "CTM", 0);
1604 if (!prop)
1605 return -ENOMEM;
1606 dev->mode_config.ctm_property = prop;
1607
1608 prop = drm_property_create(dev,
1609 DRM_MODE_PROP_BLOB,
1610 "GAMMA_LUT", 0);
1611 if (!prop)
1612 return -ENOMEM;
1613 dev->mode_config.gamma_lut_property = prop;
1614
1615 prop = drm_property_create_range(dev,
1616 DRM_MODE_PROP_IMMUTABLE,
1617 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1618 if (!prop)
1619 return -ENOMEM;
1620 dev->mode_config.gamma_lut_size_property = prop;
1621
Rob Clark9922ab52014-04-01 20:16:57 -04001622 return 0;
1623}
1624
Dave Airlief453ba02008-11-07 14:05:41 -08001625/**
1626 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1627 * @dev: DRM device
1628 *
1629 * Called by a driver the first time a DVI-I connector is made.
1630 */
1631int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1632{
1633 struct drm_property *dvi_i_selector;
1634 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001635
1636 if (dev->mode_config.dvi_i_select_subconnector_property)
1637 return 0;
1638
1639 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001640 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001641 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001642 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001643 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001644 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1645
Sascha Hauer4a67d392012-02-06 10:58:17 +01001646 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001647 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001648 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001649 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001650 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1651
1652 return 0;
1653}
1654EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1655
1656/**
1657 * drm_create_tv_properties - create TV specific connector properties
1658 * @dev: DRM device
1659 * @num_modes: number of different TV formats (modes) supported
1660 * @modes: array of pointers to strings containing name of each format
1661 *
1662 * Called by a driver's TV initialization routine, this function creates
1663 * the TV specific connector properties for a given device. Caller is
1664 * responsible for allocating a list of format names and passing them to
1665 * this routine.
1666 */
Thierry Reding2f763312014-10-13 12:45:57 +02001667int drm_mode_create_tv_properties(struct drm_device *dev,
1668 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001669 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001670{
1671 struct drm_property *tv_selector;
1672 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001673 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001674
1675 if (dev->mode_config.tv_select_subconnector_property)
1676 return 0;
1677
1678 /*
1679 * Basic connector properties
1680 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001681 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001682 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001683 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001684 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001685 if (!tv_selector)
1686 goto nomem;
1687
Dave Airlief453ba02008-11-07 14:05:41 -08001688 dev->mode_config.tv_select_subconnector_property = tv_selector;
1689
1690 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001691 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1692 "subconnector",
1693 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001694 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001695 if (!tv_subconnector)
1696 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001697 dev->mode_config.tv_subconnector_property = tv_subconnector;
1698
1699 /*
1700 * Other, TV specific properties: margins & TV modes.
1701 */
1702 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001703 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001704 if (!dev->mode_config.tv_left_margin_property)
1705 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001706
1707 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001708 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001709 if (!dev->mode_config.tv_right_margin_property)
1710 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001711
1712 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001713 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001714 if (!dev->mode_config.tv_top_margin_property)
1715 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001716
1717 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001718 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001719 if (!dev->mode_config.tv_bottom_margin_property)
1720 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001721
1722 dev->mode_config.tv_mode_property =
1723 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1724 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001725 if (!dev->mode_config.tv_mode_property)
1726 goto nomem;
1727
Dave Airlief453ba02008-11-07 14:05:41 -08001728 for (i = 0; i < num_modes; i++)
1729 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1730 i, modes[i]);
1731
Francisco Jerezb6b79022009-08-02 04:19:20 +02001732 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001733 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001734 if (!dev->mode_config.tv_brightness_property)
1735 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001736
1737 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001738 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001739 if (!dev->mode_config.tv_contrast_property)
1740 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001741
1742 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001743 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001744 if (!dev->mode_config.tv_flicker_reduction_property)
1745 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001746
Francisco Jereza75f0232009-08-12 02:30:10 +02001747 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001748 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001749 if (!dev->mode_config.tv_overscan_property)
1750 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001751
1752 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001753 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001754 if (!dev->mode_config.tv_saturation_property)
1755 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001756
1757 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001758 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001759 if (!dev->mode_config.tv_hue_property)
1760 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001761
Dave Airlief453ba02008-11-07 14:05:41 -08001762 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001763nomem:
1764 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001765}
1766EXPORT_SYMBOL(drm_mode_create_tv_properties);
1767
1768/**
1769 * drm_mode_create_scaling_mode_property - create scaling mode property
1770 * @dev: DRM device
1771 *
1772 * Called by a driver the first time it's needed, must be attached to desired
1773 * connectors.
1774 */
1775int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1776{
1777 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001778
1779 if (dev->mode_config.scaling_mode_property)
1780 return 0;
1781
1782 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001783 drm_property_create_enum(dev, 0, "scaling mode",
1784 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001785 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001786
1787 dev->mode_config.scaling_mode_property = scaling_mode;
1788
1789 return 0;
1790}
1791EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1792
1793/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301794 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1795 * @dev: DRM device
1796 *
1797 * Called by a driver the first time it's needed, must be attached to desired
1798 * connectors.
1799 *
1800 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001801 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301802 */
1803int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1804{
1805 if (dev->mode_config.aspect_ratio_property)
1806 return 0;
1807
1808 dev->mode_config.aspect_ratio_property =
1809 drm_property_create_enum(dev, 0, "aspect ratio",
1810 drm_aspect_ratio_enum_list,
1811 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1812
1813 if (dev->mode_config.aspect_ratio_property == NULL)
1814 return -ENOMEM;
1815
1816 return 0;
1817}
1818EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1819
1820/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001821 * drm_mode_create_dirty_property - create dirty property
1822 * @dev: DRM device
1823 *
1824 * Called by a driver the first time it's needed, must be attached to desired
1825 * connectors.
1826 */
1827int drm_mode_create_dirty_info_property(struct drm_device *dev)
1828{
1829 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001830
1831 if (dev->mode_config.dirty_info_property)
1832 return 0;
1833
1834 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001835 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001836 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001837 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001838 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001839 dev->mode_config.dirty_info_property = dirty_info;
1840
1841 return 0;
1842}
1843EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1844
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001845/**
1846 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1847 * @dev: DRM device
1848 *
1849 * Create the the suggested x/y offset property for connectors.
1850 */
1851int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1852{
1853 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1854 return 0;
1855
1856 dev->mode_config.suggested_x_property =
1857 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1858
1859 dev->mode_config.suggested_y_property =
1860 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1861
1862 if (dev->mode_config.suggested_x_property == NULL ||
1863 dev->mode_config.suggested_y_property == NULL)
1864 return -ENOMEM;
1865 return 0;
1866}
1867EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1868
Dave Airlief453ba02008-11-07 14:05:41 -08001869/**
Dave Airlief453ba02008-11-07 14:05:41 -08001870 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001871 * @dev: drm device for the ioctl
1872 * @data: data pointer for the ioctl
1873 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001874 *
Dave Airlief453ba02008-11-07 14:05:41 -08001875 * Construct a set of configuration description structures and return
1876 * them to the user, including CRTC, connector and framebuffer configuration.
1877 *
1878 * Called by the user via ioctl.
1879 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001880 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001881 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001882 */
1883int drm_mode_getresources(struct drm_device *dev, void *data,
1884 struct drm_file *file_priv)
1885{
1886 struct drm_mode_card_res *card_res = data;
1887 struct list_head *lh;
1888 struct drm_framebuffer *fb;
1889 struct drm_connector *connector;
1890 struct drm_crtc *crtc;
1891 struct drm_encoder *encoder;
1892 int ret = 0;
1893 int connector_count = 0;
1894 int crtc_count = 0;
1895 int fb_count = 0;
1896 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001897 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001898 uint32_t __user *fb_id;
1899 uint32_t __user *crtc_id;
1900 uint32_t __user *connector_id;
1901 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001902
Dave Airliefb3b06c2011-02-08 13:55:21 +10001903 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1904 return -EINVAL;
1905
Dave Airlief453ba02008-11-07 14:05:41 -08001906
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001907 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001908 /*
1909 * For the non-control nodes we need to limit the list of resources
1910 * by IDs in the group list for this node
1911 */
1912 list_for_each(lh, &file_priv->fbs)
1913 fb_count++;
1914
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001915 /* handle this in 4 parts */
1916 /* FBs */
1917 if (card_res->count_fbs >= fb_count) {
1918 copied = 0;
1919 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1920 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1921 if (put_user(fb->base.id, fb_id + copied)) {
1922 mutex_unlock(&file_priv->fbs_lock);
1923 return -EFAULT;
1924 }
1925 copied++;
1926 }
1927 }
1928 card_res->count_fbs = fb_count;
1929 mutex_unlock(&file_priv->fbs_lock);
1930
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001931 /* mode_config.mutex protects the connector list against e.g. DP MST
1932 * connector hot-adding. CRTC/Plane lists are invariant. */
1933 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001934 drm_for_each_crtc(crtc, dev)
1935 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001936
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001937 drm_for_each_connector(connector, dev)
1938 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001939
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001940 drm_for_each_encoder(encoder, dev)
1941 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001942
1943 card_res->max_height = dev->mode_config.max_height;
1944 card_res->min_height = dev->mode_config.min_height;
1945 card_res->max_width = dev->mode_config.max_width;
1946 card_res->min_width = dev->mode_config.min_width;
1947
Dave Airlief453ba02008-11-07 14:05:41 -08001948 /* CRTCs */
1949 if (card_res->count_crtcs >= crtc_count) {
1950 copied = 0;
1951 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001952 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001953 if (put_user(crtc->base.id, crtc_id + copied)) {
1954 ret = -EFAULT;
1955 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001956 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001957 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001958 }
1959 }
1960 card_res->count_crtcs = crtc_count;
1961
1962 /* Encoders */
1963 if (card_res->count_encoders >= encoder_count) {
1964 copied = 0;
1965 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001966 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001967 if (put_user(encoder->base.id, encoder_id +
1968 copied)) {
1969 ret = -EFAULT;
1970 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001971 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001972 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001973 }
1974 }
1975 card_res->count_encoders = encoder_count;
1976
1977 /* Connectors */
1978 if (card_res->count_connectors >= connector_count) {
1979 copied = 0;
1980 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001981 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001982 if (put_user(connector->base.id,
1983 connector_id + copied)) {
1984 ret = -EFAULT;
1985 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001986 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001987 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001988 }
1989 }
1990 card_res->count_connectors = connector_count;
1991
Dave Airlief453ba02008-11-07 14:05:41 -08001992out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001993 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001994 return ret;
1995}
1996
1997/**
1998 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001999 * @dev: drm device for the ioctl
2000 * @data: data pointer for the ioctl
2001 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002002 *
Dave Airlief453ba02008-11-07 14:05:41 -08002003 * Construct a CRTC configuration structure to return to the user.
2004 *
2005 * Called by the user via ioctl.
2006 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002007 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002008 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002009 */
2010int drm_mode_getcrtc(struct drm_device *dev,
2011 void *data, struct drm_file *file_priv)
2012{
2013 struct drm_mode_crtc *crtc_resp = data;
2014 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002015
Dave Airliefb3b06c2011-02-08 13:55:21 +10002016 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2017 return -EINVAL;
2018
Rob Clarka2b34e22013-10-05 16:36:52 -04002019 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002020 if (!crtc)
2021 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002022
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002023 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002024 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002025 if (crtc->primary->fb)
2026 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002027 else
2028 crtc_resp->fb_id = 0;
2029
Daniel Vetter31c946e2015-02-22 12:24:17 +01002030 if (crtc->state) {
2031 crtc_resp->x = crtc->primary->state->src_x >> 16;
2032 crtc_resp->y = crtc->primary->state->src_y >> 16;
2033 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002034 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002035 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002036
Daniel Vetter31c946e2015-02-22 12:24:17 +01002037 } else {
2038 crtc_resp->mode_valid = 0;
2039 }
Dave Airlief453ba02008-11-07 14:05:41 -08002040 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002041 crtc_resp->x = crtc->x;
2042 crtc_resp->y = crtc->y;
2043 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002044 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002045 crtc_resp->mode_valid = 1;
2046
2047 } else {
2048 crtc_resp->mode_valid = 0;
2049 }
Dave Airlief453ba02008-11-07 14:05:41 -08002050 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002051 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002052
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002053 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002054}
2055
Damien Lespiau61d8e322013-09-25 16:45:22 +01002056static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2057 const struct drm_file *file_priv)
2058{
2059 /*
2060 * If user-space hasn't configured the driver to expose the stereo 3D
2061 * modes, don't expose them.
2062 */
2063 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2064 return false;
2065
2066 return true;
2067}
2068
Daniel Vetterabd69c52014-11-25 23:50:05 +01002069static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2070{
2071 /* For atomic drivers only state objects are synchronously updated and
2072 * protected by modeset locks, so check those first. */
2073 if (connector->state)
2074 return connector->state->best_encoder;
2075 return connector->encoder;
2076}
2077
Rob Clark95cbf112014-12-18 16:01:49 -05002078/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002079static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002080 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2081 uint32_t *arg_count_props)
2082{
Rob Clark88a48e22014-12-18 16:01:50 -05002083 int props_count;
2084 int i, ret, copied;
2085
2086 props_count = obj->properties->count;
2087 if (!atomic)
2088 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002089
2090 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002091 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002092 struct drm_property *prop = obj->properties->properties[i];
2093 uint64_t val;
2094
Rob Clark88a48e22014-12-18 16:01:50 -05002095 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2096 continue;
2097
Rob Clark95cbf112014-12-18 16:01:49 -05002098 ret = drm_object_property_get_value(obj, prop, &val);
2099 if (ret)
2100 return ret;
2101
2102 if (put_user(prop->base.id, prop_ptr + copied))
2103 return -EFAULT;
2104
2105 if (put_user(val, prop_values + copied))
2106 return -EFAULT;
2107
2108 copied++;
2109 }
2110 }
2111 *arg_count_props = props_count;
2112
2113 return 0;
2114}
2115
Dave Airlief453ba02008-11-07 14:05:41 -08002116/**
2117 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002118 * @dev: drm device for the ioctl
2119 * @data: data pointer for the ioctl
2120 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002121 *
Dave Airlief453ba02008-11-07 14:05:41 -08002122 * Construct a connector configuration structure to return to the user.
2123 *
2124 * Called by the user via ioctl.
2125 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002126 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002127 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002128 */
2129int drm_mode_getconnector(struct drm_device *dev, void *data,
2130 struct drm_file *file_priv)
2131{
2132 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002133 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002134 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002135 struct drm_display_mode *mode;
2136 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002137 int encoders_count = 0;
2138 int ret = 0;
2139 int copied = 0;
2140 int i;
2141 struct drm_mode_modeinfo u_mode;
2142 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002143 uint32_t __user *encoder_ptr;
2144
Dave Airliefb3b06c2011-02-08 13:55:21 +10002145 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2146 return -EINVAL;
2147
Dave Airlief453ba02008-11-07 14:05:41 -08002148 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2149
Daniel Vetter7b240562012-12-12 00:35:33 +01002150 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002151
Dave Airlieb164d312016-04-27 11:10:09 +10002152 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002153 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002154 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002155 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002156 }
Dave Airlief453ba02008-11-07 14:05:41 -08002157
Thierry Reding01073b02014-12-10 13:03:38 +01002158 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2159 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002160 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002161
2162 if (out_resp->count_modes == 0) {
2163 connector->funcs->fill_modes(connector,
2164 dev->mode_config.max_width,
2165 dev->mode_config.max_height);
2166 }
2167
2168 /* delayed so we get modes regardless of pre-fill_modes state */
2169 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002170 if (drm_mode_expose_to_userspace(mode, file_priv))
2171 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002172
2173 out_resp->connector_id = connector->base.id;
2174 out_resp->connector_type = connector->connector_type;
2175 out_resp->connector_type_id = connector->connector_type_id;
2176 out_resp->mm_width = connector->display_info.width_mm;
2177 out_resp->mm_height = connector->display_info.height_mm;
2178 out_resp->subpixel = connector->display_info.subpixel_order;
2179 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002180
2181 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002182 encoder = drm_connector_get_encoder(connector);
2183 if (encoder)
2184 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002185 else
2186 out_resp->encoder_id = 0;
2187
2188 /*
2189 * This ioctl is called twice, once to determine how much space is
2190 * needed, and the 2nd time to fill it.
2191 */
2192 if ((out_resp->count_modes >= mode_count) && mode_count) {
2193 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002194 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002195 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002196 if (!drm_mode_expose_to_userspace(mode, file_priv))
2197 continue;
2198
Daniel Stone934a8a82015-05-22 13:34:48 +01002199 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002200 if (copy_to_user(mode_ptr + copied,
2201 &u_mode, sizeof(u_mode))) {
2202 ret = -EFAULT;
2203 goto out;
2204 }
2205 copied++;
2206 }
2207 }
2208 out_resp->count_modes = mode_count;
2209
Rob Clark88a48e22014-12-18 16:01:50 -05002210 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002211 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2212 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2213 &out_resp->count_props);
2214 if (ret)
2215 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002216
2217 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2218 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002219 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002220 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2221 if (connector->encoder_ids[i] != 0) {
2222 if (put_user(connector->encoder_ids[i],
2223 encoder_ptr + copied)) {
2224 ret = -EFAULT;
2225 goto out;
2226 }
2227 copied++;
2228 }
2229 }
2230 }
2231 out_resp->count_encoders = encoders_count;
2232
2233out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002234 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002235
Dave Airlieb164d312016-04-27 11:10:09 +10002236 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002237out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002238 mutex_unlock(&dev->mode_config.mutex);
2239
Dave Airlief453ba02008-11-07 14:05:41 -08002240 return ret;
2241}
2242
Daniel Vetterabd69c52014-11-25 23:50:05 +01002243static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2244{
2245 struct drm_connector *connector;
2246 struct drm_device *dev = encoder->dev;
2247 bool uses_atomic = false;
2248
2249 /* For atomic drivers only state objects are synchronously updated and
2250 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002251 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002252 if (!connector->state)
2253 continue;
2254
2255 uses_atomic = true;
2256
2257 if (connector->state->best_encoder != encoder)
2258 continue;
2259
2260 return connector->state->crtc;
2261 }
2262
2263 /* Don't return stale data (e.g. pending async disable). */
2264 if (uses_atomic)
2265 return NULL;
2266
2267 return encoder->crtc;
2268}
2269
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002270/**
2271 * drm_mode_getencoder - get encoder configuration
2272 * @dev: drm device for the ioctl
2273 * @data: data pointer for the ioctl
2274 * @file_priv: drm file for the ioctl call
2275 *
2276 * Construct a encoder configuration structure to return to the user.
2277 *
2278 * Called by the user via ioctl.
2279 *
2280 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002281 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002282 */
Dave Airlief453ba02008-11-07 14:05:41 -08002283int drm_mode_getencoder(struct drm_device *dev, void *data,
2284 struct drm_file *file_priv)
2285{
2286 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002287 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002288 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002289
Dave Airliefb3b06c2011-02-08 13:55:21 +10002290 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2291 return -EINVAL;
2292
Rob Clarka2b34e22013-10-05 16:36:52 -04002293 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002294 if (!encoder)
2295 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002296
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002297 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002298 crtc = drm_encoder_get_crtc(encoder);
2299 if (crtc)
2300 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002301 else
2302 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002303 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2304
Dave Airlief453ba02008-11-07 14:05:41 -08002305 enc_resp->encoder_type = encoder->encoder_type;
2306 enc_resp->encoder_id = encoder->base.id;
2307 enc_resp->possible_crtcs = encoder->possible_crtcs;
2308 enc_resp->possible_clones = encoder->possible_clones;
2309
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002310 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002311}
2312
2313/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002314 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002315 * @dev: DRM device
2316 * @data: ioctl data
2317 * @file_priv: DRM file info
2318 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002319 * Construct a list of plane ids to return to the user.
2320 *
2321 * Called by the user via ioctl.
2322 *
2323 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002324 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325 */
2326int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002327 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328{
2329 struct drm_mode_get_plane_res *plane_resp = data;
2330 struct drm_mode_config *config;
2331 struct drm_plane *plane;
2332 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002333 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002334 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002335
2336 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2337 return -EINVAL;
2338
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002339 config = &dev->mode_config;
2340
Matt Roper681e7ec2014-04-01 15:22:42 -07002341 if (file_priv->universal_planes)
2342 num_planes = config->num_total_plane;
2343 else
2344 num_planes = config->num_overlay_plane;
2345
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002346 /*
2347 * This ioctl is called twice, once to determine how much space is
2348 * needed, and the 2nd time to fill it.
2349 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002350 if (num_planes &&
2351 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002352 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002353
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002354 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002355 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002356 /*
2357 * Unless userspace set the 'universal planes'
2358 * capability bit, only advertise overlays.
2359 */
2360 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2361 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002362 continue;
2363
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002364 if (put_user(plane->base.id, plane_ptr + copied))
2365 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002366 copied++;
2367 }
2368 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002369 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002370
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002371 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372}
2373
2374/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002375 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002376 * @dev: DRM device
2377 * @data: ioctl data
2378 * @file_priv: DRM file info
2379 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002380 * Construct a plane configuration structure to return to the user.
2381 *
2382 * Called by the user via ioctl.
2383 *
2384 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002385 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002386 */
2387int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002388 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389{
2390 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002391 struct drm_plane *plane;
2392 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002393
2394 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2395 return -EINVAL;
2396
Rob Clarka2b34e22013-10-05 16:36:52 -04002397 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002398 if (!plane)
2399 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002400
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002401 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002402 if (plane->crtc)
2403 plane_resp->crtc_id = plane->crtc->base.id;
2404 else
2405 plane_resp->crtc_id = 0;
2406
2407 if (plane->fb)
2408 plane_resp->fb_id = plane->fb->base.id;
2409 else
2410 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002411 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412
2413 plane_resp->plane_id = plane->base.id;
2414 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002415 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002416
2417 /*
2418 * This ioctl is called twice, once to determine how much space is
2419 * needed, and the 2nd time to fill it.
2420 */
2421 if (plane->format_count &&
2422 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002423 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002424 if (copy_to_user(format_ptr,
2425 plane->format_types,
2426 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002427 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002428 }
2429 }
2430 plane_resp->count_format_types = plane->format_count;
2431
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002432 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002433}
2434
Laurent Pinchartead86102015-03-05 02:25:43 +02002435/**
2436 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2437 * @plane: plane to check for format support
2438 * @format: the pixel format
2439 *
2440 * Returns:
2441 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2442 * otherwise.
2443 */
2444int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2445{
2446 unsigned int i;
2447
2448 for (i = 0; i < plane->format_count; i++) {
2449 if (format == plane->format_types[i])
2450 return 0;
2451 }
2452
2453 return -EINVAL;
2454}
2455
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002456static int check_src_coords(uint32_t src_x, uint32_t src_y,
2457 uint32_t src_w, uint32_t src_h,
2458 const struct drm_framebuffer *fb)
2459{
2460 unsigned int fb_width, fb_height;
2461
2462 fb_width = fb->width << 16;
2463 fb_height = fb->height << 16;
2464
2465 /* Make sure source coordinates are inside the fb. */
2466 if (src_w > fb_width ||
2467 src_x > fb_width - src_w ||
2468 src_h > fb_height ||
2469 src_y > fb_height - src_h) {
2470 DRM_DEBUG_KMS("Invalid source coordinates "
2471 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2472 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2473 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2474 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2475 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2476 return -ENOSPC;
2477 }
2478
2479 return 0;
2480}
2481
Matt Roperb36552b2014-06-10 08:28:09 -07002482/*
2483 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002484 *
Matt Roperb36552b2014-06-10 08:28:09 -07002485 * Note that we assume an extra reference has already been taken on fb. If the
2486 * update fails, this reference will be dropped before return; if it succeeds,
2487 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002488 *
Matt Roperb36552b2014-06-10 08:28:09 -07002489 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002490 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002491static int __setplane_internal(struct drm_plane *plane,
2492 struct drm_crtc *crtc,
2493 struct drm_framebuffer *fb,
2494 int32_t crtc_x, int32_t crtc_y,
2495 uint32_t crtc_w, uint32_t crtc_h,
2496 /* src_{x,y,w,h} values are 16.16 fixed point */
2497 uint32_t src_x, uint32_t src_y,
2498 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002499{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002500 int ret = 0;
2501
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002503 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002504 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002505 ret = plane->funcs->disable_plane(plane);
2506 if (!ret) {
2507 plane->crtc = NULL;
2508 plane->fb = NULL;
2509 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002510 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002511 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002512 goto out;
2513 }
2514
Matt Roper7f994f32014-05-29 08:06:51 -07002515 /* Check whether this plane is usable on this CRTC */
2516 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2517 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521
Ville Syrjälä62443be2011-12-20 00:06:47 +02002522 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002523 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2524 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002525 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2526 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002527 goto out;
2528 }
2529
Matt Roper3968be92015-04-13 11:06:13 -07002530 /* Give drivers some help against integer overflows */
2531 if (crtc_w > INT_MAX ||
2532 crtc_x > INT_MAX - (int32_t) crtc_w ||
2533 crtc_h > INT_MAX ||
2534 crtc_y > INT_MAX - (int32_t) crtc_h) {
2535 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2536 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002537 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002538 goto out;
2539 }
2540
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002541 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2542 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002543 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002544
Daniel Vetter3d30a592014-07-27 13:42:42 +02002545 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002546 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002547 crtc_x, crtc_y, crtc_w, crtc_h,
2548 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002549 if (!ret) {
2550 plane->crtc = crtc;
2551 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002552 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002553 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002554 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002555 }
2556
2557out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002558 if (fb)
2559 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002560 if (plane->old_fb)
2561 drm_framebuffer_unreference(plane->old_fb);
2562 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002563
2564 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002565}
Matt Roperb36552b2014-06-10 08:28:09 -07002566
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002567static int setplane_internal(struct drm_plane *plane,
2568 struct drm_crtc *crtc,
2569 struct drm_framebuffer *fb,
2570 int32_t crtc_x, int32_t crtc_y,
2571 uint32_t crtc_w, uint32_t crtc_h,
2572 /* src_{x,y,w,h} values are 16.16 fixed point */
2573 uint32_t src_x, uint32_t src_y,
2574 uint32_t src_w, uint32_t src_h)
2575{
2576 int ret;
2577
2578 drm_modeset_lock_all(plane->dev);
2579 ret = __setplane_internal(plane, crtc, fb,
2580 crtc_x, crtc_y, crtc_w, crtc_h,
2581 src_x, src_y, src_w, src_h);
2582 drm_modeset_unlock_all(plane->dev);
2583
2584 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002585}
2586
2587/**
2588 * drm_mode_setplane - configure a plane's configuration
2589 * @dev: DRM device
2590 * @data: ioctl data*
2591 * @file_priv: DRM file info
2592 *
2593 * Set plane configuration, including placement, fb, scaling, and other factors.
2594 * Or pass a NULL fb to disable (planes may be disabled without providing a
2595 * valid crtc).
2596 *
2597 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002598 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002599 */
2600int drm_mode_setplane(struct drm_device *dev, void *data,
2601 struct drm_file *file_priv)
2602{
2603 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002604 struct drm_plane *plane;
2605 struct drm_crtc *crtc = NULL;
2606 struct drm_framebuffer *fb = NULL;
2607
2608 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2609 return -EINVAL;
2610
Matt Roperb36552b2014-06-10 08:28:09 -07002611 /*
2612 * First, find the plane, crtc, and fb objects. If not available,
2613 * we don't bother to call the driver.
2614 */
Rob Clark933f6222014-11-25 20:33:11 -05002615 plane = drm_plane_find(dev, plane_req->plane_id);
2616 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002617 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2618 plane_req->plane_id);
2619 return -ENOENT;
2620 }
Matt Roperb36552b2014-06-10 08:28:09 -07002621
2622 if (plane_req->fb_id) {
2623 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2624 if (!fb) {
2625 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2626 plane_req->fb_id);
2627 return -ENOENT;
2628 }
2629
Rob Clark933f6222014-11-25 20:33:11 -05002630 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2631 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002632 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2633 plane_req->crtc_id);
2634 return -ENOENT;
2635 }
Matt Roperb36552b2014-06-10 08:28:09 -07002636 }
2637
Matt Roper161d0dc2014-06-10 08:28:10 -07002638 /*
2639 * setplane_internal will take care of deref'ing either the old or new
2640 * framebuffer depending on success.
2641 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002642 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002643 plane_req->crtc_x, plane_req->crtc_y,
2644 plane_req->crtc_w, plane_req->crtc_h,
2645 plane_req->src_x, plane_req->src_y,
2646 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002647}
2648
2649/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002650 * drm_mode_set_config_internal - helper to call ->set_config
2651 * @set: modeset config to set
2652 *
2653 * This is a little helper to wrap internal calls to the ->set_config driver
2654 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002655 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002656 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002657 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002658 */
2659int drm_mode_set_config_internal(struct drm_mode_set *set)
2660{
2661 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002662 struct drm_framebuffer *fb;
2663 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002664 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002665
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002666 /*
2667 * NOTE: ->set_config can also disable other crtcs (if we steal all
2668 * connectors from it), hence we need to refcount the fbs across all
2669 * crtcs. Atomic modeset will have saner semantics ...
2670 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002671 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002672 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002673
Daniel Vetterb0d12322012-12-11 01:07:12 +01002674 fb = set->fb;
2675
2676 ret = crtc->funcs->set_config(set);
2677 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002678 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002679 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002680 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002681
Daniel Vettere4f62542015-07-09 23:44:35 +02002682 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002683 if (tmp->primary->fb)
2684 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002685 if (tmp->primary->old_fb)
2686 drm_framebuffer_unreference(tmp->primary->old_fb);
2687 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002688 }
2689
2690 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002691}
2692EXPORT_SYMBOL(drm_mode_set_config_internal);
2693
Matt Roperaf936292014-04-01 15:22:34 -07002694/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002695 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2696 * @mode: mode to query
2697 * @hdisplay: hdisplay value to fill in
2698 * @vdisplay: vdisplay value to fill in
2699 *
2700 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2701 * the appropriate layout.
2702 */
2703void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2704 int *hdisplay, int *vdisplay)
2705{
2706 struct drm_display_mode adjusted;
2707
2708 drm_mode_copy(&adjusted, mode);
2709 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2710 *hdisplay = adjusted.crtc_hdisplay;
2711 *vdisplay = adjusted.crtc_vdisplay;
2712}
2713EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2714
2715/**
Matt Roperaf936292014-04-01 15:22:34 -07002716 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2717 * CRTC viewport
2718 * @crtc: CRTC that framebuffer will be displayed on
2719 * @x: x panning
2720 * @y: y panning
2721 * @mode: mode that framebuffer will be displayed under
2722 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002723 */
Matt Roperaf936292014-04-01 15:22:34 -07002724int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2725 int x, int y,
2726 const struct drm_display_mode *mode,
2727 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002728
2729{
2730 int hdisplay, vdisplay;
2731
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002732 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002733
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002734 if (crtc->state &&
2735 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2736 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002737 swap(hdisplay, vdisplay);
2738
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002739 return check_src_coords(x << 16, y << 16,
2740 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002741}
Matt Roperaf936292014-04-01 15:22:34 -07002742EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002743
Daniel Vetter2d13b672012-12-11 13:47:23 +01002744/**
Dave Airlief453ba02008-11-07 14:05:41 -08002745 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002746 * @dev: drm device for the ioctl
2747 * @data: data pointer for the ioctl
2748 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002749 *
Dave Airlief453ba02008-11-07 14:05:41 -08002750 * Build a new CRTC configuration based on user request.
2751 *
2752 * Called by the user via ioctl.
2753 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002754 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002755 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002756 */
2757int drm_mode_setcrtc(struct drm_device *dev, void *data,
2758 struct drm_file *file_priv)
2759{
2760 struct drm_mode_config *config = &dev->mode_config;
2761 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002762 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002763 struct drm_connector **connector_set = NULL, *connector;
2764 struct drm_framebuffer *fb = NULL;
2765 struct drm_display_mode *mode = NULL;
2766 struct drm_mode_set set;
2767 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002768 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002769 int i;
2770
Dave Airliefb3b06c2011-02-08 13:55:21 +10002771 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2772 return -EINVAL;
2773
Zhao Junwang01447e92015-07-07 17:08:35 +08002774 /*
2775 * Universal plane src offsets are only 16.16, prevent havoc for
2776 * drivers using universal plane code internally.
2777 */
2778 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002779 return -ERANGE;
2780
Daniel Vetter84849902012-12-02 00:28:11 +01002781 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002782 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2783 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002784 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002785 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002786 goto out;
2787 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002788 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002789
2790 if (crtc_req->mode_valid) {
2791 /* If we have a mode we need a framebuffer. */
2792 /* If we pass -1, set the mode with the currently bound fb */
2793 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002794 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002795 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2796 ret = -EINVAL;
2797 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002798 }
Matt Roperf4510a22014-04-01 15:22:40 -07002799 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002800 /* Make refcounting symmetric with the lookup path. */
2801 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002802 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002803 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2804 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002805 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2806 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002807 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002808 goto out;
2809 }
Dave Airlief453ba02008-11-07 14:05:41 -08002810 }
2811
2812 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002813 if (!mode) {
2814 ret = -ENOMEM;
2815 goto out;
2816 }
2817
Daniel Stone934a8a82015-05-22 13:34:48 +01002818 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002819 if (ret) {
2820 DRM_DEBUG_KMS("Invalid mode\n");
2821 goto out;
2822 }
2823
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002824 /*
2825 * Check whether the primary plane supports the fb pixel format.
2826 * Drivers not implementing the universal planes API use a
2827 * default formats list provided by the DRM core which doesn't
2828 * match real hardware capabilities. Skip the check in that
2829 * case.
2830 */
2831 if (!crtc->primary->format_default) {
2832 ret = drm_plane_check_pixel_format(crtc->primary,
2833 fb->pixel_format);
2834 if (ret) {
2835 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2836 drm_get_format_name(fb->pixel_format));
2837 goto out;
2838 }
2839 }
2840
Damien Lespiauc11e9282013-09-25 16:45:30 +01002841 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2842 mode, fb);
2843 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002844 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002845
Dave Airlief453ba02008-11-07 14:05:41 -08002846 }
2847
2848 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002849 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002850 ret = -EINVAL;
2851 goto out;
2852 }
2853
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002854 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002855 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002856 crtc_req->count_connectors);
2857 ret = -EINVAL;
2858 goto out;
2859 }
2860
2861 if (crtc_req->count_connectors > 0) {
2862 u32 out_id;
2863
2864 /* Avoid unbounded kernel memory allocation */
2865 if (crtc_req->count_connectors > config->num_connector) {
2866 ret = -EINVAL;
2867 goto out;
2868 }
2869
Thierry Reding2f6c5382014-12-10 13:03:36 +01002870 connector_set = kmalloc_array(crtc_req->count_connectors,
2871 sizeof(struct drm_connector *),
2872 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002873 if (!connector_set) {
2874 ret = -ENOMEM;
2875 goto out;
2876 }
2877
2878 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002879 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002880 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002881 if (get_user(out_id, &set_connectors_ptr[i])) {
2882 ret = -EFAULT;
2883 goto out;
2884 }
2885
Dave Airlieb164d312016-04-27 11:10:09 +10002886 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002887 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002888 DRM_DEBUG_KMS("Connector id %d unknown\n",
2889 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002890 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002891 goto out;
2892 }
Jerome Glisse94401062010-07-15 15:43:25 -04002893 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2894 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002895 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002896
2897 connector_set[i] = connector;
2898 }
2899 }
2900
2901 set.crtc = crtc;
2902 set.x = crtc_req->x;
2903 set.y = crtc_req->y;
2904 set.mode = mode;
2905 set.connectors = connector_set;
2906 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002907 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002908 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002909
2910out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002911 if (fb)
2912 drm_framebuffer_unreference(fb);
2913
Dave Airlieb164d312016-04-27 11:10:09 +10002914 if (connector_set) {
2915 for (i = 0; i < crtc_req->count_connectors; i++) {
2916 if (connector_set[i])
2917 drm_connector_unreference(connector_set[i]);
2918 }
2919 }
Dave Airlief453ba02008-11-07 14:05:41 -08002920 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002921 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002922 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002923 return ret;
2924}
2925
Matt Roper161d0dc2014-06-10 08:28:10 -07002926/**
2927 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2928 * universal plane handler call
2929 * @crtc: crtc to update cursor for
2930 * @req: data pointer for the ioctl
2931 * @file_priv: drm file for the ioctl call
2932 *
2933 * Legacy cursor ioctl's work directly with driver buffer handles. To
2934 * translate legacy ioctl calls into universal plane handler calls, we need to
2935 * wrap the native buffer handle in a drm_framebuffer.
2936 *
2937 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2938 * buffer with a pitch of 4*width; the universal plane interface should be used
2939 * directly in cases where the hardware can support other buffer settings and
2940 * userspace wants to make use of these capabilities.
2941 *
2942 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002943 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002944 */
2945static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2946 struct drm_mode_cursor2 *req,
2947 struct drm_file *file_priv)
2948{
2949 struct drm_device *dev = crtc->dev;
2950 struct drm_framebuffer *fb = NULL;
2951 struct drm_mode_fb_cmd2 fbreq = {
2952 .width = req->width,
2953 .height = req->height,
2954 .pixel_format = DRM_FORMAT_ARGB8888,
2955 .pitches = { req->width * 4 },
2956 .handles = { req->handle },
2957 };
2958 int32_t crtc_x, crtc_y;
2959 uint32_t crtc_w = 0, crtc_h = 0;
2960 uint32_t src_w = 0, src_h = 0;
2961 int ret = 0;
2962
2963 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002964 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002965
2966 /*
2967 * Obtain fb we'll be using (either new or existing) and take an extra
2968 * reference to it if fb != null. setplane will take care of dropping
2969 * the reference if the plane update fails.
2970 */
2971 if (req->flags & DRM_MODE_CURSOR_BO) {
2972 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002973 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002974 if (IS_ERR(fb)) {
2975 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2976 return PTR_ERR(fb);
2977 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02002978 fb->hot_x = req->hot_x;
2979 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07002980 } else {
2981 fb = NULL;
2982 }
2983 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002984 fb = crtc->cursor->fb;
2985 if (fb)
2986 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002987 }
2988
2989 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2990 crtc_x = req->x;
2991 crtc_y = req->y;
2992 } else {
2993 crtc_x = crtc->cursor_x;
2994 crtc_y = crtc->cursor_y;
2995 }
2996
2997 if (fb) {
2998 crtc_w = fb->width;
2999 crtc_h = fb->height;
3000 src_w = fb->width << 16;
3001 src_h = fb->height << 16;
3002 }
3003
3004 /*
3005 * setplane_internal will take care of deref'ing either the old or new
3006 * framebuffer depending on success.
3007 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003008 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003009 crtc_x, crtc_y, crtc_w, crtc_h,
3010 0, 0, src_w, src_h);
3011
3012 /* Update successful; save new cursor position, if necessary */
3013 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3014 crtc->cursor_x = req->x;
3015 crtc->cursor_y = req->y;
3016 }
3017
3018 return ret;
3019}
3020
Dave Airlie4c813d42013-06-20 11:48:52 +10003021static int drm_mode_cursor_common(struct drm_device *dev,
3022 struct drm_mode_cursor2 *req,
3023 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003024{
Dave Airlief453ba02008-11-07 14:05:41 -08003025 struct drm_crtc *crtc;
3026 int ret = 0;
3027
Dave Airliefb3b06c2011-02-08 13:55:21 +10003028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3029 return -EINVAL;
3030
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003031 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003032 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003033
Rob Clarka2b34e22013-10-05 16:36:52 -04003034 crtc = drm_crtc_find(dev, req->crtc_id);
3035 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003036 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003037 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003038 }
Dave Airlief453ba02008-11-07 14:05:41 -08003039
Matt Roper161d0dc2014-06-10 08:28:10 -07003040 /*
3041 * If this crtc has a universal cursor plane, call that plane's update
3042 * handler rather than using legacy cursor handlers.
3043 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003044 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003045 if (crtc->cursor) {
3046 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3047 goto out;
3048 }
3049
Dave Airlief453ba02008-11-07 14:05:41 -08003050 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003051 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003052 ret = -ENXIO;
3053 goto out;
3054 }
3055 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003056 if (crtc->funcs->cursor_set2)
3057 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3058 req->width, req->height, req->hot_x, req->hot_y);
3059 else
3060 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3061 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003062 }
3063
3064 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3065 if (crtc->funcs->cursor_move) {
3066 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3067 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003068 ret = -EFAULT;
3069 goto out;
3070 }
3071 }
3072out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003073 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003074
Dave Airlief453ba02008-11-07 14:05:41 -08003075 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003076
3077}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003078
3079
3080/**
3081 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3082 * @dev: drm device for the ioctl
3083 * @data: data pointer for the ioctl
3084 * @file_priv: drm file for the ioctl call
3085 *
3086 * Set the cursor configuration based on user request.
3087 *
3088 * Called by the user via ioctl.
3089 *
3090 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003091 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003092 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003093int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003094 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003095{
3096 struct drm_mode_cursor *req = data;
3097 struct drm_mode_cursor2 new_req;
3098
3099 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3100 new_req.hot_x = new_req.hot_y = 0;
3101
3102 return drm_mode_cursor_common(dev, &new_req, file_priv);
3103}
3104
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003105/**
3106 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3107 * @dev: drm device for the ioctl
3108 * @data: data pointer for the ioctl
3109 * @file_priv: drm file for the ioctl call
3110 *
3111 * Set the cursor configuration based on user request. This implements the 2nd
3112 * version of the cursor ioctl, which allows userspace to additionally specify
3113 * the hotspot of the pointer.
3114 *
3115 * Called by the user via ioctl.
3116 *
3117 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003118 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003119 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003120int drm_mode_cursor2_ioctl(struct drm_device *dev,
3121 void *data, struct drm_file *file_priv)
3122{
3123 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003124
Dave Airlie4c813d42013-06-20 11:48:52 +10003125 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003126}
3127
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003128/**
3129 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3130 * @bpp: bits per pixels
3131 * @depth: bit depth per pixel
3132 *
3133 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3134 * Useful in fbdev emulation code, since that deals in those values.
3135 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003136uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3137{
3138 uint32_t fmt;
3139
3140 switch (bpp) {
3141 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003142 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003143 break;
3144 case 16:
3145 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003146 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003147 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003148 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003149 break;
3150 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003151 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003152 break;
3153 case 32:
3154 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003155 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003156 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003157 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003158 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003159 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003160 break;
3161 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003162 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3163 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003164 break;
3165 }
3166
3167 return fmt;
3168}
3169EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3170
Dave Airlief453ba02008-11-07 14:05:41 -08003171/**
3172 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003173 * @dev: drm device for the ioctl
3174 * @data: data pointer for the ioctl
3175 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003176 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003177 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003178 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003179 *
3180 * Called by the user via ioctl.
3181 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003182 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003183 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003184 */
3185int drm_mode_addfb(struct drm_device *dev,
3186 void *data, struct drm_file *file_priv)
3187{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003188 struct drm_mode_fb_cmd *or = data;
3189 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003190 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003191
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003192 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003193 r.fb_id = or->fb_id;
3194 r.width = or->width;
3195 r.height = or->height;
3196 r.pitches[0] = or->pitch;
3197 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3198 r.handles[0] = or->handle;
3199
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003200 ret = drm_mode_addfb2(dev, &r, file_priv);
3201 if (ret)
3202 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003203
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003204 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003205
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003206 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003207}
3208
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003209static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003210{
3211 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3212
3213 switch (format) {
3214 case DRM_FORMAT_C8:
3215 case DRM_FORMAT_RGB332:
3216 case DRM_FORMAT_BGR233:
3217 case DRM_FORMAT_XRGB4444:
3218 case DRM_FORMAT_XBGR4444:
3219 case DRM_FORMAT_RGBX4444:
3220 case DRM_FORMAT_BGRX4444:
3221 case DRM_FORMAT_ARGB4444:
3222 case DRM_FORMAT_ABGR4444:
3223 case DRM_FORMAT_RGBA4444:
3224 case DRM_FORMAT_BGRA4444:
3225 case DRM_FORMAT_XRGB1555:
3226 case DRM_FORMAT_XBGR1555:
3227 case DRM_FORMAT_RGBX5551:
3228 case DRM_FORMAT_BGRX5551:
3229 case DRM_FORMAT_ARGB1555:
3230 case DRM_FORMAT_ABGR1555:
3231 case DRM_FORMAT_RGBA5551:
3232 case DRM_FORMAT_BGRA5551:
3233 case DRM_FORMAT_RGB565:
3234 case DRM_FORMAT_BGR565:
3235 case DRM_FORMAT_RGB888:
3236 case DRM_FORMAT_BGR888:
3237 case DRM_FORMAT_XRGB8888:
3238 case DRM_FORMAT_XBGR8888:
3239 case DRM_FORMAT_RGBX8888:
3240 case DRM_FORMAT_BGRX8888:
3241 case DRM_FORMAT_ARGB8888:
3242 case DRM_FORMAT_ABGR8888:
3243 case DRM_FORMAT_RGBA8888:
3244 case DRM_FORMAT_BGRA8888:
3245 case DRM_FORMAT_XRGB2101010:
3246 case DRM_FORMAT_XBGR2101010:
3247 case DRM_FORMAT_RGBX1010102:
3248 case DRM_FORMAT_BGRX1010102:
3249 case DRM_FORMAT_ARGB2101010:
3250 case DRM_FORMAT_ABGR2101010:
3251 case DRM_FORMAT_RGBA1010102:
3252 case DRM_FORMAT_BGRA1010102:
3253 case DRM_FORMAT_YUYV:
3254 case DRM_FORMAT_YVYU:
3255 case DRM_FORMAT_UYVY:
3256 case DRM_FORMAT_VYUY:
3257 case DRM_FORMAT_AYUV:
3258 case DRM_FORMAT_NV12:
3259 case DRM_FORMAT_NV21:
3260 case DRM_FORMAT_NV16:
3261 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003262 case DRM_FORMAT_NV24:
3263 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003264 case DRM_FORMAT_YUV410:
3265 case DRM_FORMAT_YVU410:
3266 case DRM_FORMAT_YUV411:
3267 case DRM_FORMAT_YVU411:
3268 case DRM_FORMAT_YUV420:
3269 case DRM_FORMAT_YVU420:
3270 case DRM_FORMAT_YUV422:
3271 case DRM_FORMAT_YVU422:
3272 case DRM_FORMAT_YUV444:
3273 case DRM_FORMAT_YVU444:
3274 return 0;
3275 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003276 DRM_DEBUG_KMS("invalid pixel format %s\n",
3277 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003278 return -EINVAL;
3279 }
3280}
3281
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003282static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003283{
3284 int ret, hsub, vsub, num_planes, i;
3285
3286 ret = format_check(r);
3287 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003288 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3289 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003290 return ret;
3291 }
3292
3293 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3294 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3295 num_planes = drm_format_num_planes(r->pixel_format);
3296
3297 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003298 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003299 return -EINVAL;
3300 }
3301
3302 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003303 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003304 return -EINVAL;
3305 }
3306
3307 for (i = 0; i < num_planes; i++) {
3308 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003309 unsigned int height = r->height / (i != 0 ? vsub : 1);
3310 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003311
3312 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003313 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003314 return -EINVAL;
3315 }
3316
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003317 if ((uint64_t) width * cpp > UINT_MAX)
3318 return -ERANGE;
3319
3320 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3321 return -ERANGE;
3322
3323 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003324 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003325 return -EINVAL;
3326 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003327
3328 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3329 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3330 r->modifier[i], i);
3331 return -EINVAL;
3332 }
Rob Clark570655b2015-01-30 20:18:11 +05303333
3334 /* modifier specific checks: */
3335 switch (r->modifier[i]) {
3336 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3337 /* NOTE: the pitch restriction may be lifted later if it turns
3338 * out that no hw has this restriction:
3339 */
3340 if (r->pixel_format != DRM_FORMAT_NV12 ||
3341 width % 128 || height % 32 ||
3342 r->pitches[i] % 128) {
3343 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3344 return -EINVAL;
3345 }
3346 break;
3347
3348 default:
3349 break;
3350 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003351 }
3352
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003353 for (i = num_planes; i < 4; i++) {
3354 if (r->modifier[i]) {
3355 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3356 return -EINVAL;
3357 }
3358
3359 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3360 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3361 continue;
3362
3363 if (r->handles[i]) {
3364 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3365 return -EINVAL;
3366 }
3367
3368 if (r->pitches[i]) {
3369 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3370 return -EINVAL;
3371 }
3372
3373 if (r->offsets[i]) {
3374 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3375 return -EINVAL;
3376 }
3377 }
3378
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003379 return 0;
3380}
3381
Chris Wilson9a6f5132015-02-25 13:45:26 +00003382static struct drm_framebuffer *
3383internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003384 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003385 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003386{
3387 struct drm_mode_config *config = &dev->mode_config;
3388 struct drm_framebuffer *fb;
3389 int ret;
3390
Rob Clarke3eb3252015-02-05 14:41:52 +00003391 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003392 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3393 return ERR_PTR(-EINVAL);
3394 }
3395
3396 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3397 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3398 r->width, config->min_width, config->max_width);
3399 return ERR_PTR(-EINVAL);
3400 }
3401 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3402 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3403 r->height, config->min_height, config->max_height);
3404 return ERR_PTR(-EINVAL);
3405 }
3406
Rob Clarke3eb3252015-02-05 14:41:52 +00003407 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3408 !dev->mode_config.allow_fb_modifiers) {
3409 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3410 return ERR_PTR(-EINVAL);
3411 }
3412
Matt Roperc394c2b2014-06-10 08:28:08 -07003413 ret = framebuffer_check(r);
3414 if (ret)
3415 return ERR_PTR(ret);
3416
3417 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3418 if (IS_ERR(fb)) {
3419 DRM_DEBUG_KMS("could not create framebuffer\n");
3420 return fb;
3421 }
3422
Matt Roperc394c2b2014-06-10 08:28:08 -07003423 return fb;
3424}
3425
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003426/**
3427 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003428 * @dev: drm device for the ioctl
3429 * @data: data pointer for the ioctl
3430 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003431 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003432 * Add a new FB to the specified CRTC, given a user request with format. This is
3433 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3434 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003435 *
3436 * Called by the user via ioctl.
3437 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003438 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003439 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003440 */
3441int drm_mode_addfb2(struct drm_device *dev,
3442 void *data, struct drm_file *file_priv)
3443{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003444 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003445 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003446
Dave Airliefb3b06c2011-02-08 13:55:21 +10003447 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3448 return -EINVAL;
3449
Chris Wilson9a6f5132015-02-25 13:45:26 +00003450 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003451 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003452 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003453
Chris Wilson9a6f5132015-02-25 13:45:26 +00003454 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003455 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003456
3457 /* Transfer ownership to the filp for reaping on close */
3458 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003459 list_add(&fb->filp_head, &file_priv->fbs);
3460 mutex_unlock(&file_priv->fbs_lock);
3461
Matt Roperc394c2b2014-06-10 08:28:08 -07003462 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003463}
3464
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003465struct drm_mode_rmfb_work {
3466 struct work_struct work;
3467 struct list_head fbs;
3468};
3469
3470static void drm_mode_rmfb_work_fn(struct work_struct *w)
3471{
3472 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3473
3474 while (!list_empty(&arg->fbs)) {
3475 struct drm_framebuffer *fb =
3476 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3477
3478 list_del_init(&fb->filp_head);
3479 drm_framebuffer_remove(fb);
3480 }
3481}
3482
Dave Airlief453ba02008-11-07 14:05:41 -08003483/**
3484 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003485 * @dev: drm device for the ioctl
3486 * @data: data pointer for the ioctl
3487 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003488 *
Dave Airlief453ba02008-11-07 14:05:41 -08003489 * Remove the FB specified by the user.
3490 *
3491 * Called by the user via ioctl.
3492 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003493 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003494 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003495 */
3496int drm_mode_rmfb(struct drm_device *dev,
3497 void *data, struct drm_file *file_priv)
3498{
Dave Airlief453ba02008-11-07 14:05:41 -08003499 struct drm_framebuffer *fb = NULL;
3500 struct drm_framebuffer *fbl = NULL;
3501 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003502 int found = 0;
3503
Dave Airliefb3b06c2011-02-08 13:55:21 +10003504 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3505 return -EINVAL;
3506
Dave Airlie72fe90b2016-04-15 15:10:40 +10003507 fb = drm_framebuffer_lookup(dev, *id);
3508 if (!fb)
3509 return -ENOENT;
3510
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003511 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003512 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3513 if (fb == fbl)
3514 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003515 if (!found) {
3516 mutex_unlock(&file_priv->fbs_lock);
3517 goto fail_unref;
3518 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003519
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003520 list_del_init(&fb->filp_head);
3521 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003522
Dave Airlie72fe90b2016-04-15 15:10:40 +10003523 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003524 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003525
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003526 /*
3527 * we now own the reference that was stored in the fbs list
3528 *
3529 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3530 * so run this in a separate stack as there's no way to correctly
3531 * handle this after the fb is already removed from the lookup table.
3532 */
3533 if (drm_framebuffer_read_refcount(fb) > 1) {
3534 struct drm_mode_rmfb_work arg;
3535
3536 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3537 INIT_LIST_HEAD(&arg.fbs);
3538 list_add_tail(&fb->filp_head, &arg.fbs);
3539
3540 schedule_work(&arg.work);
3541 flush_work(&arg.work);
3542 destroy_work_on_stack(&arg.work);
3543 } else
3544 drm_framebuffer_unreference(fb);
3545
Daniel Vetter2b677e82012-12-10 21:16:05 +01003546 return 0;
3547
Dave Airlie72fe90b2016-04-15 15:10:40 +10003548fail_unref:
3549 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003550 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003551}
3552
3553/**
3554 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003555 * @dev: drm device for the ioctl
3556 * @data: data pointer for the ioctl
3557 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003558 *
Dave Airlief453ba02008-11-07 14:05:41 -08003559 * Lookup the FB given its ID and return info about it.
3560 *
3561 * Called by the user via ioctl.
3562 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003563 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003564 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003565 */
3566int drm_mode_getfb(struct drm_device *dev,
3567 void *data, struct drm_file *file_priv)
3568{
3569 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003570 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003571 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003572
Dave Airliefb3b06c2011-02-08 13:55:21 +10003573 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3574 return -EINVAL;
3575
Daniel Vetter786b99e2012-12-02 21:53:40 +01003576 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003577 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003578 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003579
3580 r->height = fb->height;
3581 r->width = fb->width;
3582 r->depth = fb->depth;
3583 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003584 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003585 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003586 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003587 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003588 ret = fb->funcs->create_handle(fb, file_priv,
3589 &r->handle);
3590 } else {
3591 /* GET_FB() is an unprivileged ioctl so we must not
3592 * return a buffer-handle to non-master processes! For
3593 * backwards-compatibility reasons, we cannot make
3594 * GET_FB() privileged, so just return an invalid handle
3595 * for non-masters. */
3596 r->handle = 0;
3597 ret = 0;
3598 }
3599 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003600 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003601 }
Dave Airlief453ba02008-11-07 14:05:41 -08003602
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003603 drm_framebuffer_unreference(fb);
3604
Dave Airlief453ba02008-11-07 14:05:41 -08003605 return ret;
3606}
3607
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003608/**
3609 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3610 * @dev: drm device for the ioctl
3611 * @data: data pointer for the ioctl
3612 * @file_priv: drm file for the ioctl call
3613 *
3614 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3615 * rectangle list. Generic userspace which does frontbuffer rendering must call
3616 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3617 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3618 *
3619 * Modesetting drivers which always update the frontbuffer do not need to
3620 * implement the corresponding ->dirty framebuffer callback.
3621 *
3622 * Called by the user via ioctl.
3623 *
3624 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003625 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003626 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003627int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3628 void *data, struct drm_file *file_priv)
3629{
3630 struct drm_clip_rect __user *clips_ptr;
3631 struct drm_clip_rect *clips = NULL;
3632 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003633 struct drm_framebuffer *fb;
3634 unsigned flags;
3635 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003636 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003637
Dave Airliefb3b06c2011-02-08 13:55:21 +10003638 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3639 return -EINVAL;
3640
Daniel Vetter786b99e2012-12-02 21:53:40 +01003641 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003642 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003643 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003644
3645 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003646 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003647
3648 if (!num_clips != !clips_ptr) {
3649 ret = -EINVAL;
3650 goto out_err1;
3651 }
3652
3653 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3654
3655 /* If userspace annotates copy, clips must come in pairs */
3656 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3657 ret = -EINVAL;
3658 goto out_err1;
3659 }
3660
3661 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003662 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3663 ret = -EINVAL;
3664 goto out_err1;
3665 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003666 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003667 if (!clips) {
3668 ret = -ENOMEM;
3669 goto out_err1;
3670 }
3671
3672 ret = copy_from_user(clips, clips_ptr,
3673 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003674 if (ret) {
3675 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003676 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003677 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003678 }
3679
3680 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003681 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3682 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003683 } else {
3684 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003685 }
3686
3687out_err2:
3688 kfree(clips);
3689out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003690 drm_framebuffer_unreference(fb);
3691
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003692 return ret;
3693}
3694
Dave Airlief453ba02008-11-07 14:05:41 -08003695/**
3696 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003697 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003698 *
Dave Airlief453ba02008-11-07 14:05:41 -08003699 * Destroy all the FBs associated with @filp.
3700 *
3701 * Called by the user via ioctl.
3702 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003703 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003704 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003705 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003706void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003707{
Dave Airlief453ba02008-11-07 14:05:41 -08003708 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003709 struct drm_mode_rmfb_work arg;
3710
3711 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003712
Daniel Vetter1b116292014-09-24 21:51:06 +02003713 /*
3714 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003715 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003716 * avoid upsetting lockdep since the universal cursor code adds a
3717 * framebuffer while holding mutex locks.
3718 *
3719 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3720 * locks is impossible here since no one else but this function can get
3721 * at it any more.
3722 */
Dave Airlief453ba02008-11-07 14:05:41 -08003723 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003724 if (drm_framebuffer_read_refcount(fb) > 1) {
3725 list_move_tail(&fb->filp_head, &arg.fbs);
3726 } else {
3727 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003728
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003729 /* This drops the fpriv->fbs reference. */
3730 drm_framebuffer_unreference(fb);
3731 }
3732 }
3733
3734 if (!list_empty(&arg.fbs)) {
3735 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3736
3737 schedule_work(&arg.work);
3738 flush_work(&arg.work);
3739 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003740 }
Dave Airlief453ba02008-11-07 14:05:41 -08003741}
3742
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003743/**
3744 * drm_property_create - create a new property type
3745 * @dev: drm device
3746 * @flags: flags specifying the property type
3747 * @name: name of the property
3748 * @num_values: number of pre-defined values
3749 *
3750 * This creates a new generic drm property which can then be attached to a drm
3751 * object with drm_object_attach_property. The returned property object must be
3752 * freed with drm_property_destroy.
3753 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003754 * Note that the DRM core keeps a per-device list of properties and that, if
3755 * drm_mode_config_cleanup() is called, it will destroy all properties created
3756 * by the driver.
3757 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003758 * Returns:
3759 * A pointer to the newly created property on success, NULL on failure.
3760 */
Dave Airlief453ba02008-11-07 14:05:41 -08003761struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3762 const char *name, int num_values)
3763{
3764 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003765 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003766
3767 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3768 if (!property)
3769 return NULL;
3770
Rob Clark98f75de2014-05-30 11:37:03 -04003771 property->dev = dev;
3772
Dave Airlief453ba02008-11-07 14:05:41 -08003773 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003774 property->values = kcalloc(num_values, sizeof(uint64_t),
3775 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003776 if (!property->values)
3777 goto fail;
3778 }
3779
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003780 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3781 if (ret)
3782 goto fail;
3783
Dave Airlief453ba02008-11-07 14:05:41 -08003784 property->flags = flags;
3785 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003786 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003787
Vinson Lee471dd2e2011-11-10 11:55:40 -08003788 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003789 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003790 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3791 }
Dave Airlief453ba02008-11-07 14:05:41 -08003792
3793 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003794
3795 WARN_ON(!drm_property_type_valid(property));
3796
Dave Airlief453ba02008-11-07 14:05:41 -08003797 return property;
3798fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003799 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003800 kfree(property);
3801 return NULL;
3802}
3803EXPORT_SYMBOL(drm_property_create);
3804
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003805/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003806 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003807 * @dev: drm device
3808 * @flags: flags specifying the property type
3809 * @name: name of the property
3810 * @props: enumeration lists with property values
3811 * @num_values: number of pre-defined values
3812 *
3813 * This creates a new generic drm property which can then be attached to a drm
3814 * object with drm_object_attach_property. The returned property object must be
3815 * freed with drm_property_destroy.
3816 *
3817 * Userspace is only allowed to set one of the predefined values for enumeration
3818 * properties.
3819 *
3820 * Returns:
3821 * A pointer to the newly created property on success, NULL on failure.
3822 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003823struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3824 const char *name,
3825 const struct drm_prop_enum_list *props,
3826 int num_values)
3827{
3828 struct drm_property *property;
3829 int i, ret;
3830
3831 flags |= DRM_MODE_PROP_ENUM;
3832
3833 property = drm_property_create(dev, flags, name, num_values);
3834 if (!property)
3835 return NULL;
3836
3837 for (i = 0; i < num_values; i++) {
3838 ret = drm_property_add_enum(property, i,
3839 props[i].type,
3840 props[i].name);
3841 if (ret) {
3842 drm_property_destroy(dev, property);
3843 return NULL;
3844 }
3845 }
3846
3847 return property;
3848}
3849EXPORT_SYMBOL(drm_property_create_enum);
3850
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003851/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003852 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003853 * @dev: drm device
3854 * @flags: flags specifying the property type
3855 * @name: name of the property
3856 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003857 * @num_props: size of the @props array
3858 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003859 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003860 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003861 * object with drm_object_attach_property. The returned property object must be
3862 * freed with drm_property_destroy.
3863 *
3864 * Compared to plain enumeration properties userspace is allowed to set any
3865 * or'ed together combination of the predefined property bitflag values
3866 *
3867 * Returns:
3868 * A pointer to the newly created property on success, NULL on failure.
3869 */
Rob Clark49e27542012-05-17 02:23:26 -06003870struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3871 int flags, const char *name,
3872 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303873 int num_props,
3874 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003875{
3876 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303877 int i, ret, index = 0;
3878 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003879
3880 flags |= DRM_MODE_PROP_BITMASK;
3881
3882 property = drm_property_create(dev, flags, name, num_values);
3883 if (!property)
3884 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303885 for (i = 0; i < num_props; i++) {
3886 if (!(supported_bits & (1ULL << props[i].type)))
3887 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003888
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303889 if (WARN_ON(index >= num_values)) {
3890 drm_property_destroy(dev, property);
3891 return NULL;
3892 }
3893
3894 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003895 props[i].type,
3896 props[i].name);
3897 if (ret) {
3898 drm_property_destroy(dev, property);
3899 return NULL;
3900 }
3901 }
3902
3903 return property;
3904}
3905EXPORT_SYMBOL(drm_property_create_bitmask);
3906
Rob Clarkebc44cf2012-09-12 22:22:31 -05003907static struct drm_property *property_create_range(struct drm_device *dev,
3908 int flags, const char *name,
3909 uint64_t min, uint64_t max)
3910{
3911 struct drm_property *property;
3912
3913 property = drm_property_create(dev, flags, name, 2);
3914 if (!property)
3915 return NULL;
3916
3917 property->values[0] = min;
3918 property->values[1] = max;
3919
3920 return property;
3921}
3922
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003923/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003924 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003925 * @dev: drm device
3926 * @flags: flags specifying the property type
3927 * @name: name of the property
3928 * @min: minimum value of the property
3929 * @max: maximum value of the property
3930 *
3931 * This creates a new generic drm property which can then be attached to a drm
3932 * object with drm_object_attach_property. The returned property object must be
3933 * freed with drm_property_destroy.
3934 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003935 * Userspace is allowed to set any unsigned integer value in the (min, max)
3936 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003937 *
3938 * Returns:
3939 * A pointer to the newly created property on success, NULL on failure.
3940 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003941struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3942 const char *name,
3943 uint64_t min, uint64_t max)
3944{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003945 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3946 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003947}
3948EXPORT_SYMBOL(drm_property_create_range);
3949
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003950/**
3951 * drm_property_create_signed_range - create a new signed ranged property type
3952 * @dev: drm device
3953 * @flags: flags specifying the property type
3954 * @name: name of the property
3955 * @min: minimum value of the property
3956 * @max: maximum value of the property
3957 *
3958 * This creates a new generic drm property which can then be attached to a drm
3959 * object with drm_object_attach_property. The returned property object must be
3960 * freed with drm_property_destroy.
3961 *
3962 * Userspace is allowed to set any signed integer value in the (min, max)
3963 * range inclusive.
3964 *
3965 * Returns:
3966 * A pointer to the newly created property on success, NULL on failure.
3967 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003968struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3969 int flags, const char *name,
3970 int64_t min, int64_t max)
3971{
3972 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3973 name, I642U64(min), I642U64(max));
3974}
3975EXPORT_SYMBOL(drm_property_create_signed_range);
3976
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003977/**
3978 * drm_property_create_object - create a new object property type
3979 * @dev: drm device
3980 * @flags: flags specifying the property type
3981 * @name: name of the property
3982 * @type: object type from DRM_MODE_OBJECT_* defines
3983 *
3984 * This creates a new generic drm property which can then be attached to a drm
3985 * object with drm_object_attach_property. The returned property object must be
3986 * freed with drm_property_destroy.
3987 *
3988 * Userspace is only allowed to set this to any property value of the given
3989 * @type. Only useful for atomic properties, which is enforced.
3990 *
3991 * Returns:
3992 * A pointer to the newly created property on success, NULL on failure.
3993 */
Rob Clark98f75de2014-05-30 11:37:03 -04003994struct drm_property *drm_property_create_object(struct drm_device *dev,
3995 int flags, const char *name, uint32_t type)
3996{
3997 struct drm_property *property;
3998
3999 flags |= DRM_MODE_PROP_OBJECT;
4000
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004001 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
4002 return NULL;
4003
Rob Clark98f75de2014-05-30 11:37:03 -04004004 property = drm_property_create(dev, flags, name, 1);
4005 if (!property)
4006 return NULL;
4007
4008 property->values[0] = type;
4009
4010 return property;
4011}
4012EXPORT_SYMBOL(drm_property_create_object);
4013
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004014/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004015 * drm_property_create_bool - create a new boolean property type
4016 * @dev: drm device
4017 * @flags: flags specifying the property type
4018 * @name: name of the property
4019 *
4020 * This creates a new generic drm property which can then be attached to a drm
4021 * object with drm_object_attach_property. The returned property object must be
4022 * freed with drm_property_destroy.
4023 *
4024 * This is implemented as a ranged property with only {0, 1} as valid values.
4025 *
4026 * Returns:
4027 * A pointer to the newly created property on success, NULL on failure.
4028 */
4029struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
4030 const char *name)
4031{
4032 return drm_property_create_range(dev, flags, name, 0, 1);
4033}
4034EXPORT_SYMBOL(drm_property_create_bool);
4035
4036/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004037 * drm_property_add_enum - add a possible value to an enumeration property
4038 * @property: enumeration property to change
4039 * @index: index of the new enumeration
4040 * @value: value of the new enumeration
4041 * @name: symbolic name of the new enumeration
4042 *
4043 * This functions adds enumerations to a property.
4044 *
4045 * It's use is deprecated, drivers should use one of the more specific helpers
4046 * to directly create the property with all enumerations already attached.
4047 *
4048 * Returns:
4049 * Zero on success, error code on failure.
4050 */
Dave Airlief453ba02008-11-07 14:05:41 -08004051int drm_property_add_enum(struct drm_property *property, int index,
4052 uint64_t value, const char *name)
4053{
4054 struct drm_property_enum *prop_enum;
4055
Rob Clark5ea22f22014-05-30 11:34:01 -04004056 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4057 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004058 return -EINVAL;
4059
4060 /*
4061 * Bitmask enum properties have the additional constraint of values
4062 * from 0 to 63
4063 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004064 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4065 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004066 return -EINVAL;
4067
Daniel Vetter3758b342014-11-19 18:38:10 +01004068 if (!list_empty(&property->enum_list)) {
4069 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004070 if (prop_enum->value == value) {
4071 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4072 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4073 return 0;
4074 }
4075 }
4076 }
4077
4078 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4079 if (!prop_enum)
4080 return -ENOMEM;
4081
4082 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4083 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4084 prop_enum->value = value;
4085
4086 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004087 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004088 return 0;
4089}
4090EXPORT_SYMBOL(drm_property_add_enum);
4091
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004092/**
4093 * drm_property_destroy - destroy a drm property
4094 * @dev: drm device
4095 * @property: property to destry
4096 *
4097 * This function frees a property including any attached resources like
4098 * enumeration values.
4099 */
Dave Airlief453ba02008-11-07 14:05:41 -08004100void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4101{
4102 struct drm_property_enum *prop_enum, *pt;
4103
Daniel Vetter3758b342014-11-19 18:38:10 +01004104 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004105 list_del(&prop_enum->head);
4106 kfree(prop_enum);
4107 }
4108
4109 if (property->num_values)
4110 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004111 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004112 list_del(&property->head);
4113 kfree(property);
4114}
4115EXPORT_SYMBOL(drm_property_destroy);
4116
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004117/**
4118 * drm_object_attach_property - attach a property to a modeset object
4119 * @obj: drm modeset object
4120 * @property: property to attach
4121 * @init_val: initial value of the property
4122 *
4123 * This attaches the given property to the modeset object with the given initial
4124 * value. Currently this function cannot fail since the properties are stored in
4125 * a statically sized array.
4126 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004127void drm_object_attach_property(struct drm_mode_object *obj,
4128 struct drm_property *property,
4129 uint64_t init_val)
4130{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004131 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004132
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004133 if (count == DRM_OBJECT_MAX_PROPERTY) {
4134 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4135 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4136 "you see this message on the same object type.\n",
4137 obj->type);
4138 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004139 }
4140
Rob Clarkb17cd752014-12-16 18:05:30 -05004141 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004142 obj->properties->values[count] = init_val;
4143 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004144 if (property->flags & DRM_MODE_PROP_ATOMIC)
4145 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004146}
4147EXPORT_SYMBOL(drm_object_attach_property);
4148
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004149/**
4150 * drm_object_property_set_value - set the value of a property
4151 * @obj: drm mode object to set property value for
4152 * @property: property to set
4153 * @val: value the property should be set to
4154 *
4155 * This functions sets a given property on a given object. This function only
4156 * changes the software state of the property, it does not call into the
4157 * driver's ->set_property callback.
4158 *
4159 * Returns:
4160 * Zero on success, error code on failure.
4161 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004162int drm_object_property_set_value(struct drm_mode_object *obj,
4163 struct drm_property *property, uint64_t val)
4164{
4165 int i;
4166
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004167 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004168 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004169 obj->properties->values[i] = val;
4170 return 0;
4171 }
4172 }
4173
4174 return -EINVAL;
4175}
4176EXPORT_SYMBOL(drm_object_property_set_value);
4177
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004178/**
4179 * drm_object_property_get_value - retrieve the value of a property
4180 * @obj: drm mode object to get property value from
4181 * @property: property to retrieve
4182 * @val: storage for the property value
4183 *
4184 * This function retrieves the softare state of the given property for the given
4185 * property. Since there is no driver callback to retrieve the current property
4186 * value this might be out of sync with the hardware, depending upon the driver
4187 * and property.
4188 *
4189 * Returns:
4190 * Zero on success, error code on failure.
4191 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004192int drm_object_property_get_value(struct drm_mode_object *obj,
4193 struct drm_property *property, uint64_t *val)
4194{
4195 int i;
4196
Rob Clark88a48e22014-12-18 16:01:50 -05004197 /* read-only properties bypass atomic mechanism and still store
4198 * their value in obj->properties->values[].. mostly to avoid
4199 * having to deal w/ EDID and similar props in atomic paths:
4200 */
4201 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4202 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4203 return drm_atomic_get_property(obj, property, val);
4204
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004205 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004206 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004207 *val = obj->properties->values[i];
4208 return 0;
4209 }
4210 }
4211
4212 return -EINVAL;
4213}
4214EXPORT_SYMBOL(drm_object_property_get_value);
4215
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004216/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004217 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004218 * @dev: DRM device
4219 * @data: ioctl data
4220 * @file_priv: DRM file info
4221 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004222 * This function retrieves the metadata for a given property, like the different
4223 * possible values for an enum property or the limits for a range property.
4224 *
4225 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004226 *
4227 * Called by the user via ioctl.
4228 *
4229 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004230 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004231 */
Dave Airlief453ba02008-11-07 14:05:41 -08004232int drm_mode_getproperty_ioctl(struct drm_device *dev,
4233 void *data, struct drm_file *file_priv)
4234{
Dave Airlief453ba02008-11-07 14:05:41 -08004235 struct drm_mode_get_property *out_resp = data;
4236 struct drm_property *property;
4237 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004238 int value_count = 0;
4239 int ret = 0, i;
4240 int copied;
4241 struct drm_property_enum *prop_enum;
4242 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004243 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004244
Dave Airliefb3b06c2011-02-08 13:55:21 +10004245 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4246 return -EINVAL;
4247
Daniel Vetter84849902012-12-02 00:28:11 +01004248 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004249 property = drm_property_find(dev, out_resp->prop_id);
4250 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004251 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004252 goto done;
4253 }
Dave Airlief453ba02008-11-07 14:05:41 -08004254
Rob Clark5ea22f22014-05-30 11:34:01 -04004255 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4256 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004257 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004258 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004259 }
4260
4261 value_count = property->num_values;
4262
4263 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4264 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4265 out_resp->flags = property->flags;
4266
4267 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004268 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004269 for (i = 0; i < value_count; i++) {
4270 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4271 ret = -EFAULT;
4272 goto done;
4273 }
4274 }
4275 }
4276 out_resp->count_values = value_count;
4277
Rob Clark5ea22f22014-05-30 11:34:01 -04004278 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4279 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004280 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4281 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004282 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004283 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004284
4285 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4286 ret = -EFAULT;
4287 goto done;
4288 }
4289
4290 if (copy_to_user(&enum_ptr[copied].name,
4291 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4292 ret = -EFAULT;
4293 goto done;
4294 }
4295 copied++;
4296 }
4297 }
4298 out_resp->count_enum_blobs = enum_count;
4299 }
4300
Daniel Vetter3758b342014-11-19 18:38:10 +01004301 /*
4302 * NOTE: The idea seems to have been to use this to read all the blob
4303 * property values. But nothing ever added them to the corresponding
4304 * list, userspace always used the special-purpose get_blob ioctl to
4305 * read the value for a blob property. It also doesn't make a lot of
4306 * sense to return values here when everything else is just metadata for
4307 * the property itself.
4308 */
4309 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4310 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004311done:
Daniel Vetter84849902012-12-02 00:28:11 +01004312 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004313 return ret;
4314}
4315
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004316static void drm_property_free_blob(struct kref *kref)
4317{
4318 struct drm_property_blob *blob =
4319 container_of(kref, struct drm_property_blob, base.refcount);
4320
4321 mutex_lock(&blob->dev->mode_config.blob_lock);
4322 list_del(&blob->head_global);
4323 mutex_unlock(&blob->dev->mode_config.blob_lock);
4324
4325 drm_mode_object_unregister(blob->dev, &blob->base);
4326
4327 kfree(blob);
4328}
4329
Daniel Stone99531d92015-05-22 13:34:49 +01004330/**
4331 * drm_property_create_blob - Create new blob property
4332 *
4333 * Creates a new blob property for a specified DRM device, optionally
4334 * copying data.
4335 *
4336 * @dev: DRM device to create property for
4337 * @length: Length to allocate for blob data
4338 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004339 *
4340 * Returns:
4341 * New blob property with a single reference on success, or an ERR_PTR
4342 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004343 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004344struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004345drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004346 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004347{
4348 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004349 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004350
Dan Carpenter9ac09342015-10-29 16:37:54 +03004351 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004352 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004353
4354 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4355 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004356 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004357
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004358 /* This must be explicitly initialised, so we can safely call list_del
4359 * on it in the removal handler, even if it isn't in a file list. */
4360 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004361 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004362 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004363
Daniel Stone99531d92015-05-22 13:34:49 +01004364 if (data)
4365 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004366
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004367 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4368 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004369 if (ret) {
4370 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004371 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004372 }
4373
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004374 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004375 list_add_tail(&blob->head_global,
4376 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004377 mutex_unlock(&dev->mode_config.blob_lock);
4378
Dave Airlief453ba02008-11-07 14:05:41 -08004379 return blob;
4380}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004381EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004382
Daniel Stone6bcacf52015-04-20 19:22:55 +01004383/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004384 * drm_property_unreference_blob - Unreference a blob property
4385 *
4386 * Drop a reference on a blob property. May free the object.
4387 *
Daniel Stonef102c162015-05-22 13:34:44 +01004388 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004389 */
4390void drm_property_unreference_blob(struct drm_property_blob *blob)
4391{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004392 if (!blob)
4393 return;
4394
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004395 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004396}
4397EXPORT_SYMBOL(drm_property_unreference_blob);
4398
4399/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004400 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4401 * @dev: DRM device
4402 * @file_priv: destroy all blobs owned by this file handle
4403 */
4404void drm_property_destroy_user_blobs(struct drm_device *dev,
4405 struct drm_file *file_priv)
4406{
4407 struct drm_property_blob *blob, *bt;
4408
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004409 /*
4410 * When the file gets released that means no one else can access the
4411 * blob list any more, so no need to grab dev->blob_lock.
4412 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004413 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4414 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004415 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004416 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004417}
4418
4419/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004420 * drm_property_reference_blob - Take a reference on an existing property
4421 *
4422 * Take a new reference on an existing blob property.
4423 *
Daniel Stonef102c162015-05-22 13:34:44 +01004424 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004425 */
4426struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4427{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004428 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004429 return blob;
4430}
4431EXPORT_SYMBOL(drm_property_reference_blob);
4432
Daniel Stone6bcacf52015-04-20 19:22:55 +01004433/**
4434 * drm_property_lookup_blob - look up a blob property and take a reference
4435 * @dev: drm device
4436 * @id: id of the blob property
4437 *
4438 * If successful, this takes an additional reference to the blob property.
4439 * callers need to make sure to eventually unreference the returned property
4440 * again, using @drm_property_unreference_blob.
4441 */
4442struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4443 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004444{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004445 struct drm_mode_object *obj;
4446 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004447
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004448 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4449 if (obj)
4450 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004451 return blob;
4452}
4453EXPORT_SYMBOL(drm_property_lookup_blob);
4454
4455/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004456 * drm_property_replace_global_blob - atomically replace existing blob property
4457 * @dev: drm device
4458 * @replace: location of blob property pointer to be replaced
4459 * @length: length of data for new blob, or 0 for no data
4460 * @data: content for new blob, or NULL for no data
4461 * @obj_holds_id: optional object for property holding blob ID
4462 * @prop_holds_id: optional property holding blob ID
4463 * @return 0 on success or error on failure
4464 *
4465 * This function will atomically replace a global property in the blob list,
4466 * optionally updating a property which holds the ID of that property. It is
4467 * guaranteed to be atomic: no caller will be allowed to see intermediate
4468 * results, and either the entire operation will succeed and clean up the
4469 * previous property, or it will fail and the state will be unchanged.
4470 *
4471 * If length is 0 or data is NULL, no new blob will be created, and the holding
4472 * property, if specified, will be set to 0.
4473 *
4474 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4475 * by holding the relevant modesetting object lock for its parent.
4476 *
4477 * For example, a drm_connector has a 'PATH' property, which contains the ID
4478 * of a blob property with the value of the MST path information. Calling this
4479 * function with replace pointing to the connector's path_blob_ptr, length and
4480 * data set for the new path information, obj_holds_id set to the connector's
4481 * base object, and prop_holds_id set to the path property name, will perform
4482 * a completely atomic update. The access to path_blob_ptr is protected by the
4483 * caller holding a lock on the connector.
4484 */
4485static int drm_property_replace_global_blob(struct drm_device *dev,
4486 struct drm_property_blob **replace,
4487 size_t length,
4488 const void *data,
4489 struct drm_mode_object *obj_holds_id,
4490 struct drm_property *prop_holds_id)
4491{
4492 struct drm_property_blob *new_blob = NULL;
4493 struct drm_property_blob *old_blob = NULL;
4494 int ret;
4495
4496 WARN_ON(replace == NULL);
4497
4498 old_blob = *replace;
4499
4500 if (length && data) {
4501 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004502 if (IS_ERR(new_blob))
4503 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004504 }
4505
4506 /* This does not need to be synchronised with blob_lock, as the
4507 * get_properties ioctl locks all modesetting objects, and
4508 * obj_holds_id must be locked before calling here, so we cannot
4509 * have its value out of sync with the list membership modified
4510 * below under blob_lock. */
4511 if (obj_holds_id) {
4512 ret = drm_object_property_set_value(obj_holds_id,
4513 prop_holds_id,
4514 new_blob ?
4515 new_blob->base.id : 0);
4516 if (ret != 0)
4517 goto err_created;
4518 }
4519
Markus Elfringec530822015-07-05 21:55:10 +02004520 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004521 *replace = new_blob;
4522
4523 return 0;
4524
4525err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004526 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004527 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004528}
4529
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004530/**
4531 * drm_mode_getblob_ioctl - get the contents of a blob property value
4532 * @dev: DRM device
4533 * @data: ioctl data
4534 * @file_priv: DRM file info
4535 *
4536 * This function retrieves the contents of a blob property. The value stored in
4537 * an object's blob property is just a normal modeset object id.
4538 *
4539 * Called by the user via ioctl.
4540 *
4541 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004542 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004543 */
Dave Airlief453ba02008-11-07 14:05:41 -08004544int drm_mode_getblob_ioctl(struct drm_device *dev,
4545 void *data, struct drm_file *file_priv)
4546{
Dave Airlief453ba02008-11-07 14:05:41 -08004547 struct drm_mode_get_blob *out_resp = data;
4548 struct drm_property_blob *blob;
4549 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004550 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004551
Dave Airliefb3b06c2011-02-08 13:55:21 +10004552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4553 return -EINVAL;
4554
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004555 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4556 if (!blob)
4557 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004558
4559 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004560 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004561 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004562 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004563 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004564 }
4565 }
4566 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004567unref:
4568 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004569
Dave Airlief453ba02008-11-07 14:05:41 -08004570 return ret;
4571}
4572
Dave Airliecc7096f2014-10-22 12:03:04 +10004573/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004574 * drm_mode_createblob_ioctl - create a new blob property
4575 * @dev: DRM device
4576 * @data: ioctl data
4577 * @file_priv: DRM file info
4578 *
4579 * This function creates a new blob property with user-defined values. In order
4580 * to give us sensible validation and checking when creating, rather than at
4581 * every potential use, we also require a type to be provided upfront.
4582 *
4583 * Called by the user via ioctl.
4584 *
4585 * Returns:
4586 * Zero on success, negative errno on failure.
4587 */
4588int drm_mode_createblob_ioctl(struct drm_device *dev,
4589 void *data, struct drm_file *file_priv)
4590{
4591 struct drm_mode_create_blob *out_resp = data;
4592 struct drm_property_blob *blob;
4593 void __user *blob_ptr;
4594 int ret = 0;
4595
4596 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4597 return -EINVAL;
4598
4599 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4600 if (IS_ERR(blob))
4601 return PTR_ERR(blob);
4602
4603 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4604 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4605 ret = -EFAULT;
4606 goto out_blob;
4607 }
4608
4609 /* Dropping the lock between create_blob and our access here is safe
4610 * as only the same file_priv can remove the blob; at this point, it is
4611 * not associated with any file_priv. */
4612 mutex_lock(&dev->mode_config.blob_lock);
4613 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004614 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004615 mutex_unlock(&dev->mode_config.blob_lock);
4616
4617 return 0;
4618
4619out_blob:
4620 drm_property_unreference_blob(blob);
4621 return ret;
4622}
4623
4624/**
4625 * drm_mode_destroyblob_ioctl - destroy a user blob property
4626 * @dev: DRM device
4627 * @data: ioctl data
4628 * @file_priv: DRM file info
4629 *
4630 * Destroy an existing user-defined blob property.
4631 *
4632 * Called by the user via ioctl.
4633 *
4634 * Returns:
4635 * Zero on success, negative errno on failure.
4636 */
4637int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4638 void *data, struct drm_file *file_priv)
4639{
4640 struct drm_mode_destroy_blob *out_resp = data;
4641 struct drm_property_blob *blob = NULL, *bt;
4642 bool found = false;
4643 int ret = 0;
4644
4645 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4646 return -EINVAL;
4647
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004648 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4649 if (!blob)
4650 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004651
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004652 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004653 /* Ensure the property was actually created by this user. */
4654 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4655 if (bt == blob) {
4656 found = true;
4657 break;
4658 }
4659 }
4660
4661 if (!found) {
4662 ret = -EPERM;
4663 goto err;
4664 }
4665
4666 /* We must drop head_file here, because we may not be the last
4667 * reference on the blob. */
4668 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004669 mutex_unlock(&dev->mode_config.blob_lock);
4670
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004671 /* One reference from lookup, and one from the filp. */
4672 drm_property_unreference_blob(blob);
4673 drm_property_unreference_blob(blob);
4674
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004675 return 0;
4676
4677err:
4678 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004679 drm_property_unreference_blob(blob);
4680
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004681 return ret;
4682}
4683
4684/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004685 * drm_mode_connector_set_path_property - set tile property on connector
4686 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004687 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004688 *
4689 * This creates a property to expose to userspace to specify a
4690 * connector path. This is mainly used for DisplayPort MST where
4691 * connectors have a topology and we want to allow userspace to give
4692 * them more meaningful names.
4693 *
4694 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004695 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004696 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004697int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004698 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004699{
4700 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004701 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004702
Daniel Stoned2ed3432015-04-20 19:22:53 +01004703 ret = drm_property_replace_global_blob(dev,
4704 &connector->path_blob_ptr,
4705 strlen(path) + 1,
4706 path,
4707 &connector->base,
4708 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004709 return ret;
4710}
4711EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4712
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004713/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004714 * drm_mode_connector_set_tile_property - set tile property on connector
4715 * @connector: connector to set property on.
4716 *
4717 * This looks up the tile information for a connector, and creates a
4718 * property for userspace to parse if it exists. The property is of
4719 * the form of 8 integers using ':' as a separator.
4720 *
4721 * Returns:
4722 * Zero on success, errno on failure.
4723 */
4724int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4725{
4726 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004727 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004728 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004729
4730 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004731 ret = drm_property_replace_global_blob(dev,
4732 &connector->tile_blob_ptr,
4733 0,
4734 NULL,
4735 &connector->base,
4736 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004737 return ret;
4738 }
4739
4740 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4741 connector->tile_group->id, connector->tile_is_single_monitor,
4742 connector->num_h_tile, connector->num_v_tile,
4743 connector->tile_h_loc, connector->tile_v_loc,
4744 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004745
Daniel Stoned2ed3432015-04-20 19:22:53 +01004746 ret = drm_property_replace_global_blob(dev,
4747 &connector->tile_blob_ptr,
4748 strlen(tile) + 1,
4749 tile,
4750 &connector->base,
4751 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004752 return ret;
4753}
4754EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4755
4756/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004757 * drm_mode_connector_update_edid_property - update the edid property of a connector
4758 * @connector: drm connector
4759 * @edid: new value of the edid property
4760 *
4761 * This function creates a new blob modeset object and assigns its id to the
4762 * connector's edid property.
4763 *
4764 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004765 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004766 */
Dave Airlief453ba02008-11-07 14:05:41 -08004767int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004768 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004769{
4770 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004771 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004772 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004773
Thomas Wood4cf2b282014-06-18 17:52:33 +01004774 /* ignore requests to set edid when overridden */
4775 if (connector->override_edid)
4776 return 0;
4777
Daniel Stoned2ed3432015-04-20 19:22:53 +01004778 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004779 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004780
Daniel Stoned2ed3432015-04-20 19:22:53 +01004781 ret = drm_property_replace_global_blob(dev,
4782 &connector->edid_blob_ptr,
4783 size,
4784 edid,
4785 &connector->base,
4786 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004787 return ret;
4788}
4789EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4790
Rob Clark3843e712014-12-16 18:05:29 -05004791/* Some properties could refer to dynamic refcnt'd objects, or things that
4792 * need special locking to handle lifetime issues (ie. to ensure the prop
4793 * value doesn't become invalid part way through the property update due to
4794 * race). The value returned by reference via 'obj' should be passed back
4795 * to drm_property_change_valid_put() after the property is set (and the
4796 * object to which the property is attached has a chance to take it's own
4797 * reference).
4798 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004799bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004800 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004801{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004802 int i;
4803
Paulo Zanoni26a34812012-05-15 18:08:59 -03004804 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4805 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004806
Rob Clark3843e712014-12-16 18:05:29 -05004807 *ref = NULL;
4808
Rob Clark5ea22f22014-05-30 11:34:01 -04004809 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004810 if (value < property->values[0] || value > property->values[1])
4811 return false;
4812 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004813 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4814 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004815
Rob Clarkebc44cf2012-09-12 22:22:31 -05004816 if (svalue < U642I64(property->values[0]) ||
4817 svalue > U642I64(property->values[1]))
4818 return false;
4819 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004820 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004821 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004822
Rob Clark49e27542012-05-17 02:23:26 -06004823 for (i = 0; i < property->num_values; i++)
4824 valid_mask |= (1ULL << property->values[i]);
4825 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004826 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004827 struct drm_property_blob *blob;
4828
4829 if (value == 0)
4830 return true;
4831
4832 blob = drm_property_lookup_blob(property->dev, value);
4833 if (blob) {
4834 *ref = &blob->base;
4835 return true;
4836 } else {
4837 return false;
4838 }
Rob Clark98f75de2014-05-30 11:37:03 -04004839 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004840 /* a zero value for an object property translates to null: */
4841 if (value == 0)
4842 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004843
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004844 *ref = _object_find(property->dev, value, property->values[0]);
4845 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004846 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004847
4848 for (i = 0; i < property->num_values; i++)
4849 if (property->values[i] == value)
4850 return true;
4851 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004852}
4853
Rob Clarkd34f20d2014-12-18 16:01:56 -05004854void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004855 struct drm_mode_object *ref)
4856{
4857 if (!ref)
4858 return;
4859
4860 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004861 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004862 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4863 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004864}
4865
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004866/**
4867 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4868 * @dev: DRM device
4869 * @data: ioctl data
4870 * @file_priv: DRM file info
4871 *
4872 * This function sets the current value for a connectors's property. It also
4873 * calls into a driver's ->set_property callback to update the hardware state
4874 *
4875 * Called by the user via ioctl.
4876 *
4877 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004878 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004879 */
Dave Airlief453ba02008-11-07 14:05:41 -08004880int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4881 void *data, struct drm_file *file_priv)
4882{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004883 struct drm_mode_connector_set_property *conn_set_prop = data;
4884 struct drm_mode_obj_set_property obj_set_prop = {
4885 .value = conn_set_prop->value,
4886 .prop_id = conn_set_prop->prop_id,
4887 .obj_id = conn_set_prop->connector_id,
4888 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4889 };
Dave Airlief453ba02008-11-07 14:05:41 -08004890
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004891 /* It does all the locking and checking we need */
4892 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004893}
4894
Paulo Zanonic5431882012-05-15 18:09:02 -03004895static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4896 struct drm_property *property,
4897 uint64_t value)
4898{
4899 int ret = -EINVAL;
4900 struct drm_connector *connector = obj_to_connector(obj);
4901
4902 /* Do DPMS ourselves */
4903 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004904 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004905 } else if (connector->funcs->set_property)
4906 ret = connector->funcs->set_property(connector, property, value);
4907
4908 /* store the property value if successful */
4909 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004910 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004911 return ret;
4912}
4913
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004914static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4915 struct drm_property *property,
4916 uint64_t value)
4917{
4918 int ret = -EINVAL;
4919 struct drm_crtc *crtc = obj_to_crtc(obj);
4920
4921 if (crtc->funcs->set_property)
4922 ret = crtc->funcs->set_property(crtc, property, value);
4923 if (!ret)
4924 drm_object_property_set_value(obj, property, value);
4925
4926 return ret;
4927}
4928
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004929/**
4930 * drm_mode_plane_set_obj_prop - set the value of a property
4931 * @plane: drm plane object to set property value for
4932 * @property: property to set
4933 * @value: value the property should be set to
4934 *
4935 * This functions sets a given property on a given plane object. This function
4936 * calls the driver's ->set_property callback and changes the software state of
4937 * the property if the callback succeeds.
4938 *
4939 * Returns:
4940 * Zero on success, error code on failure.
4941 */
4942int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4943 struct drm_property *property,
4944 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004945{
4946 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004947 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004948
4949 if (plane->funcs->set_property)
4950 ret = plane->funcs->set_property(plane, property, value);
4951 if (!ret)
4952 drm_object_property_set_value(obj, property, value);
4953
4954 return ret;
4955}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004956EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004957
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004958/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004959 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004960 * @dev: DRM device
4961 * @data: ioctl data
4962 * @file_priv: DRM file info
4963 *
4964 * This function retrieves the current value for an object's property. Compared
4965 * to the connector specific ioctl this one is extended to also work on crtc and
4966 * plane objects.
4967 *
4968 * Called by the user via ioctl.
4969 *
4970 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004971 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004972 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004973int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4974 struct drm_file *file_priv)
4975{
4976 struct drm_mode_obj_get_properties *arg = data;
4977 struct drm_mode_object *obj;
4978 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004979
4980 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4981 return -EINVAL;
4982
Daniel Vetter84849902012-12-02 00:28:11 +01004983 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004984
4985 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4986 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004987 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004988 goto out;
4989 }
4990 if (!obj->properties) {
4991 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004992 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004993 }
4994
Rob Clark88a48e22014-12-18 16:01:50 -05004995 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004996 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4997 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4998 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004999
Daniel Vetter1649c332016-04-22 22:10:28 +02005000out_unref:
5001 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005002out:
Daniel Vetter84849902012-12-02 00:28:11 +01005003 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005004 return ret;
5005}
5006
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005007/**
5008 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5009 * @dev: DRM device
5010 * @data: ioctl data
5011 * @file_priv: DRM file info
5012 *
5013 * This function sets the current value for an object's property. It also calls
5014 * into a driver's ->set_property callback to update the hardware state.
5015 * Compared to the connector specific ioctl this one is extended to also work on
5016 * crtc and plane objects.
5017 *
5018 * Called by the user via ioctl.
5019 *
5020 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005021 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005022 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005023int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5024 struct drm_file *file_priv)
5025{
5026 struct drm_mode_obj_set_property *arg = data;
5027 struct drm_mode_object *arg_obj;
5028 struct drm_mode_object *prop_obj;
5029 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005030 int i, ret = -EINVAL;
5031 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005032
5033 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5034 return -EINVAL;
5035
Daniel Vetter84849902012-12-02 00:28:11 +01005036 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005037
5038 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005039 if (!arg_obj) {
5040 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005041 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005042 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005043 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02005044 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005045
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005046 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005047 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005048 break;
5049
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005050 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02005051 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005052
5053 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5054 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005055 if (!prop_obj) {
5056 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02005057 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005058 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005059 property = obj_to_property(prop_obj);
5060
Rob Clark3843e712014-12-16 18:05:29 -05005061 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10005062 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005063
5064 switch (arg_obj->type) {
5065 case DRM_MODE_OBJECT_CONNECTOR:
5066 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5067 arg->value);
5068 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005069 case DRM_MODE_OBJECT_CRTC:
5070 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5071 break;
Rob Clark4d939142012-05-17 02:23:27 -06005072 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005073 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5074 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005075 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005076 }
5077
Rob Clark3843e712014-12-16 18:05:29 -05005078 drm_property_change_valid_put(property, ref);
5079
Daniel Vetter1649c332016-04-22 22:10:28 +02005080out_unref:
5081 drm_mode_object_unreference(arg_obj);
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
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005919 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5920 head) {
5921 plane->funcs->destroy(plane);
5922 }
5923
5924 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5925 crtc->funcs->destroy(crtc);
5926 }
5927
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005928 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005929 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005930 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005931 }
5932
5933 /*
5934 * Single-threaded teardown context, so it's not required to grab the
5935 * fb_lock to protect against concurrent fb_list access. Contrary, it
5936 * would actually deadlock with the drm_framebuffer_cleanup function.
5937 *
5938 * Also, if there are any framebuffers left, that's a driver leak now,
5939 * so politely WARN about this.
5940 */
5941 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5942 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005943 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005944 }
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);