blob: 2c761afbcecd154048b569bb69c40ad2d386ac8a [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" },
Dave Airlief453ba02008-11-07 14:05:41 -0800171};
172
Thierry Reding4dfd9092014-12-10 13:03:34 +0100173static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
174 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800175 { DRM_MODE_ENCODER_DAC, "DAC" },
176 { DRM_MODE_ENCODER_TMDS, "TMDS" },
177 { DRM_MODE_ENCODER_LVDS, "LVDS" },
178 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200179 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300180 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000181 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800182};
183
Thierry Reding4dfd9092014-12-10 13:03:34 +0100184static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800185 { SubPixelUnknown, "Unknown" },
186 { SubPixelHorizontalRGB, "Horizontal RGB" },
187 { SubPixelHorizontalBGR, "Horizontal BGR" },
188 { SubPixelVerticalRGB, "Vertical RGB" },
189 { SubPixelVerticalBGR, "Vertical BGR" },
190 { SubPixelNone, "None" },
191};
192
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400193void drm_connector_ida_init(void)
194{
195 int i;
196
197 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
198 ida_init(&drm_connector_enum_list[i].ida);
199}
200
201void drm_connector_ida_destroy(void)
202{
203 int i;
204
205 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
206 ida_destroy(&drm_connector_enum_list[i].ida);
207}
208
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100209/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100210 * drm_get_connector_status_name - return a string for connector status
211 * @status: connector status to compute name of
212 *
213 * In contrast to the other drm_get_*_name functions this one here returns a
214 * const pointer and hence is threadsafe.
215 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000216const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800217{
218 if (status == connector_status_connected)
219 return "connected";
220 else if (status == connector_status_disconnected)
221 return "disconnected";
222 else
223 return "unknown";
224}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000225EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800226
Jesse Barnesac1bb362014-02-10 15:32:44 -0800227/**
228 * drm_get_subpixel_order_name - return a string for a given subpixel enum
229 * @order: enum of subpixel_order
230 *
231 * Note you could abuse this and return something out of bounds, but that
232 * would be a caller error. No unscrubbed user data should make it here.
233 */
234const char *drm_get_subpixel_order_name(enum subpixel_order order)
235{
236 return drm_subpixel_enum_list[order].name;
237}
238EXPORT_SYMBOL(drm_get_subpixel_order_name);
239
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300240static char printable_char(int c)
241{
242 return isascii(c) && isprint(c) ? c : '?';
243}
244
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100245/**
246 * drm_get_format_name - return a string for drm fourcc format
247 * @format: format to compute name of
248 *
249 * Note that the buffer used by this function is globally shared and owned by
250 * the function itself.
251 *
252 * FIXME: This isn't really multithreading safe.
253 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000254const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300255{
256 static char buf[32];
257
258 snprintf(buf, sizeof(buf),
259 "%c%c%c%c %s-endian (0x%08x)",
260 printable_char(format & 0xff),
261 printable_char((format >> 8) & 0xff),
262 printable_char((format >> 16) & 0xff),
263 printable_char((format >> 24) & 0x7f),
264 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
265 format);
266
267 return buf;
268}
269EXPORT_SYMBOL(drm_get_format_name);
270
Dave Airlie2ee39452014-07-24 11:53:45 +1000271/*
272 * Internal function to assign a slot in the object idr and optionally
273 * register the object into the idr.
274 */
275static int drm_mode_object_get_reg(struct drm_device *dev,
276 struct drm_mode_object *obj,
277 uint32_t obj_type,
Dave Airlied0f37cf2016-04-15 15:10:36 +1000278 bool register_obj,
279 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000280{
281 int ret;
282
283 mutex_lock(&dev->mode_config.idr_mutex);
284 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
285 if (ret >= 0) {
286 /*
287 * Set up the object linking under the protection of the idr
288 * lock so that other users can't see inconsistent state.
289 */
290 obj->id = ret;
291 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +1000292 if (obj_free_cb) {
293 obj->free_cb = obj_free_cb;
294 kref_init(&obj->refcount);
295 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000296 }
297 mutex_unlock(&dev->mode_config.idr_mutex);
298
299 return ret < 0 ? ret : 0;
300}
301
Dave Airlief453ba02008-11-07 14:05:41 -0800302/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100303 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800304 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100305 * @obj: object pointer, used to generate unique ID
306 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800307 *
Dave Airlief453ba02008-11-07 14:05:41 -0800308 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100309 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
310 * modeset identifiers are _not_ reference counted. Hence don't use this for
311 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800312 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100313 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200314 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800315 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100316int drm_mode_object_get(struct drm_device *dev,
317 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800318{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000319 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000320}
Dave Airlief453ba02008-11-07 14:05:41 -0800321
Dave Airlie2ee39452014-07-24 11:53:45 +1000322static void drm_mode_object_register(struct drm_device *dev,
323 struct drm_mode_object *obj)
324{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000325 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000326 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000327 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800328}
329
330/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000331 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100333 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800334 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000335 * Free @id from @dev's unique identifier pool.
336 * This function can be called multiple times, and guards against
337 * multiple removals.
338 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100339 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800340 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000341void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100342 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800343{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000344 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000345 if (object->id) {
346 idr_remove(&dev->mode_config.crtc_idr, object->id);
347 object->id = 0;
348 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000349 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800350}
351
Rob Clark98f75de2014-05-30 11:37:03 -0400352static struct drm_mode_object *_object_find(struct drm_device *dev,
353 uint32_t id, uint32_t type)
354{
355 struct drm_mode_object *obj = NULL;
356
357 mutex_lock(&dev->mode_config.idr_mutex);
358 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200359 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
360 obj = NULL;
361 if (obj && obj->id != id)
362 obj = NULL;
363 /* don't leak out unref'd fb's */
Daniel Stone6bcacf52015-04-20 19:22:55 +0100364 if (obj &&
Dave Airliecee26ac2016-04-15 15:10:37 +1000365 obj->type == DRM_MODE_OBJECT_BLOB)
Rob Clark98f75de2014-05-30 11:37:03 -0400366 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000367
368 if (obj && obj->free_cb) {
369 if (!kref_get_unless_zero(&obj->refcount))
370 obj = NULL;
371 }
Rob Clark98f75de2014-05-30 11:37:03 -0400372 mutex_unlock(&dev->mode_config.idr_mutex);
373
374 return obj;
375}
376
Daniel Vetter786b99e2012-12-02 21:53:40 +0100377/**
378 * drm_mode_object_find - look up a drm object with static lifetime
379 * @dev: drm device
380 * @id: id of the mode object
381 * @type: type of the mode object
382 *
Daniel Vetter059814222016-04-22 22:10:27 +0200383 * This function is used to look up a modeset object. It will acquire a
384 * reference for reference counted objects. This reference must be dropped again
385 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100386 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200387struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
388 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800389{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000390 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800391
Daniel Vetter1649c332016-04-22 22:10:28 +0200392 WARN_ON(type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400393 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800394 return obj;
395}
396EXPORT_SYMBOL(drm_mode_object_find);
397
Daniel Vetter059814222016-04-22 22:10:27 +0200398/**
399 * drm_mode_object_unreference - decr the object refcnt
400 * @obj: mode_object
401 *
402 * This functions decrements the object's refcount if it is a refcounted modeset
403 * object. It is a no-op on any other object. This is used to drop references
404 * acquired with drm_mode_object_reference().
405 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000406void drm_mode_object_unreference(struct drm_mode_object *obj)
407{
408 if (obj->free_cb) {
409 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
410 kref_put(&obj->refcount, obj->free_cb);
411 }
412}
413EXPORT_SYMBOL(drm_mode_object_unreference);
414
415/**
Daniel Vetter059814222016-04-22 22:10:27 +0200416 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000417 * @obj: mode_object
418 *
Daniel Vetter059814222016-04-22 22:10:27 +0200419 * This functions increments the object's refcount if it is a refcounted modeset
420 * object. It is a no-op on any other object. References should be dropped again
421 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000422 */
423void drm_mode_object_reference(struct drm_mode_object *obj)
424{
425 if (obj->free_cb) {
426 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
427 kref_get(&obj->refcount);
428 }
429}
430EXPORT_SYMBOL(drm_mode_object_reference);
431
Dave Airlief55f1f92016-04-15 15:10:33 +1000432static void drm_framebuffer_free(struct kref *kref)
433{
434 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000435 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000436 struct drm_device *dev = fb->dev;
437
438 /*
439 * The lookup idr holds a weak reference, which has not necessarily been
440 * removed at this point. Check for that.
441 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000442 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000443
444 fb->funcs->destroy(fb);
445}
446
Dave Airlief453ba02008-11-07 14:05:41 -0800447/**
Dave Airlief453ba02008-11-07 14:05:41 -0800448 * drm_framebuffer_init - initialize a framebuffer
449 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100450 * @fb: framebuffer to be initialized
451 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800452 *
Dave Airlief453ba02008-11-07 14:05:41 -0800453 * Allocates an ID for the framebuffer's parent mode object, sets its mode
454 * functions & device file and adds it to the master fd list.
455 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100456 * IMPORTANT:
457 * This functions publishes the fb and makes it available for concurrent access
458 * by other users. Which means by this point the fb _must_ be fully set up -
459 * since all the fb attributes are invariant over its lifetime, no further
460 * locking but only correct reference counting is required.
461 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100462 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200463 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800464 */
465int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
466 const struct drm_framebuffer_funcs *funcs)
467{
468 int ret;
469
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100470 INIT_LIST_HEAD(&fb->filp_head);
471 fb->dev = dev;
472 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000473
Dave Airlied0f37cf2016-04-15 15:10:36 +1000474 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000475 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200476 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100477 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800478
Dave Airlie9cd47422016-04-15 15:10:38 +1000479 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800480 dev->mode_config.num_fb++;
481 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000482 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800483
Dave Airlie9cd47422016-04-15 15:10:38 +1000484 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000485out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200486 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800487}
488EXPORT_SYMBOL(drm_framebuffer_init);
489
Rob Clarkf7eff602012-09-05 21:48:38 +0000490/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100491 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
492 * @dev: drm device
493 * @id: id of the fb object
494 *
495 * If successful, this grabs an additional reference to the framebuffer -
496 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100497 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100498 */
499struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
500 uint32_t id)
501{
Dave Airliecee26ac2016-04-15 15:10:37 +1000502 struct drm_mode_object *obj;
503 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100504
Dave Airliecee26ac2016-04-15 15:10:37 +1000505 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000506 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000507 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100508 return fb;
509}
510EXPORT_SYMBOL(drm_framebuffer_lookup);
511
512/**
Daniel Vetter36206362012-12-10 20:42:17 +0100513 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
514 * @fb: fb to unregister
515 *
516 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
517 * those used for fbdev. Note that the caller must hold a reference of it's own,
518 * i.e. the object may not be destroyed through this call (since it'll lead to a
519 * locking inversion).
520 */
521void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
522{
Daniel Vettera39a3572015-08-25 15:45:11 +0200523 struct drm_device *dev;
524
525 if (!fb)
526 return;
527
528 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100529
Daniel Vetter2b677e82012-12-10 21:16:05 +0100530 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000531 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100532}
533EXPORT_SYMBOL(drm_framebuffer_unregister_private);
534
535/**
Dave Airlief453ba02008-11-07 14:05:41 -0800536 * drm_framebuffer_cleanup - remove a framebuffer object
537 * @fb: framebuffer to remove
538 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100539 * Cleanup framebuffer. This function is intended to be used from the drivers
540 * ->destroy callback. It can also be used to clean up driver private
541 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100542 *
543 * Note that this function does not remove the fb from active usuage - if it is
544 * still used anywhere, hilarity can ensue since userspace could call getfb on
545 * the id and get back -EINVAL. Obviously no concern at driver unload time.
546 *
547 * Also, the framebuffer will not be removed from the lookup idr - for
548 * user-created framebuffers this will happen in in the rmfb ioctl. For
549 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
550 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800551 */
552void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
553{
554 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100555
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100556 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000557 list_del(&fb->head);
558 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100559 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000560}
561EXPORT_SYMBOL(drm_framebuffer_cleanup);
562
563/**
564 * drm_framebuffer_remove - remove and unreference a framebuffer object
565 * @fb: framebuffer to remove
566 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000567 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100568 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100569 * passed-in framebuffer. Might take the modeset locks.
570 *
571 * Note that this function optimizes the cleanup away if the caller holds the
572 * last reference to the framebuffer. It is also guaranteed to not take the
573 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000574 */
575void drm_framebuffer_remove(struct drm_framebuffer *fb)
576{
Daniel Vettera39a3572015-08-25 15:45:11 +0200577 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800578 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800579 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000580 struct drm_mode_set set;
581 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800582
Daniel Vettera39a3572015-08-25 15:45:11 +0200583 if (!fb)
584 return;
585
586 dev = fb->dev;
587
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100588 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100589
Daniel Vetterb62584e2012-12-11 16:51:35 +0100590 /*
591 * drm ABI mandates that we remove any deleted framebuffers from active
592 * useage. But since most sane clients only remove framebuffers they no
593 * longer need, try to optimize this away.
594 *
595 * Since we're holding a reference ourselves, observing a refcount of 1
596 * means that we're the last holder and can skip it. Also, the refcount
597 * can never increase from 1 again, so we don't need any barriers or
598 * locks.
599 *
600 * Note that userspace could try to race with use and instate a new
601 * usage _after_ we've cleared all current ones. End result will be an
602 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
603 * in this manner.
604 */
Dave Airlie747a5982016-04-15 15:10:35 +1000605 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100606 drm_modeset_lock_all(dev);
607 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200608 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700609 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100610 /* should turn off the crtc */
611 memset(&set, 0, sizeof(struct drm_mode_set));
612 set.crtc = crtc;
613 set.fb = NULL;
614 ret = drm_mode_set_config_internal(&set);
615 if (ret)
616 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
617 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000618 }
Dave Airlief453ba02008-11-07 14:05:41 -0800619
Daniel Vetter6295d602015-07-09 23:44:25 +0200620 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300621 if (plane->fb == fb)
622 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800623 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100624 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800625 }
626
Rob Clarkf7eff602012-09-05 21:48:38 +0000627 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800628}
Rob Clarkf7eff602012-09-05 21:48:38 +0000629EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800630
Rob Clark51fd3712013-11-19 12:10:12 -0500631DEFINE_WW_CLASS(crtc_ww_class);
632
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200633static unsigned int drm_num_crtcs(struct drm_device *dev)
634{
635 unsigned int num = 0;
636 struct drm_crtc *tmp;
637
638 drm_for_each_crtc(tmp, dev) {
639 num++;
640 }
641
642 return num;
643}
644
Dave Airlief453ba02008-11-07 14:05:41 -0800645/**
Matt Ropere13161a2014-04-01 15:22:38 -0700646 * drm_crtc_init_with_planes - Initialise a new CRTC object with
647 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800648 * @dev: DRM device
649 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700650 * @primary: Primary plane for CRTC
651 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800652 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200653 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800654 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000655 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200656 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100657 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200658 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800659 */
Matt Ropere13161a2014-04-01 15:22:38 -0700660int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
661 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700662 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200663 const struct drm_crtc_funcs *funcs,
664 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800665{
Rob Clark51fd3712013-11-19 12:10:12 -0500666 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200667 int ret;
668
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100669 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
670 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
671
Dave Airlief453ba02008-11-07 14:05:41 -0800672 crtc->dev = dev;
673 crtc->funcs = funcs;
674
Rob Clark51fd3712013-11-19 12:10:12 -0500675 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200676 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
677 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100678 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800679
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200680 if (name) {
681 va_list ap;
682
683 va_start(ap, name);
684 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
685 va_end(ap);
686 } else {
687 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
688 drm_num_crtcs(dev));
689 }
690 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000691 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200692 return -ENOMEM;
693 }
694
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300695 crtc->base.properties = &crtc->properties;
696
Rob Clark51fd3712013-11-19 12:10:12 -0500697 list_add_tail(&crtc->head, &config->crtc_list);
698 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200699
Matt Ropere13161a2014-04-01 15:22:38 -0700700 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700701 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700702 if (primary)
703 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700704 if (cursor)
705 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700706
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100707 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
708 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100709 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100710 }
711
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100712 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800713}
Matt Ropere13161a2014-04-01 15:22:38 -0700714EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800715
716/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000717 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800718 * @crtc: CRTC to cleanup
719 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000720 * This function cleans up @crtc and removes it from the DRM mode setting
721 * core. Note that the function does *not* free the crtc structure itself,
722 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800723 */
724void drm_crtc_cleanup(struct drm_crtc *crtc)
725{
726 struct drm_device *dev = crtc->dev;
727
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000728 kfree(crtc->gamma_store);
729 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800730
Rob Clark51fd3712013-11-19 12:10:12 -0500731 drm_modeset_lock_fini(&crtc->mutex);
732
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000733 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800734 list_del(&crtc->head);
735 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100736
737 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
738 if (crtc->state && crtc->funcs->atomic_destroy_state)
739 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100740
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200741 kfree(crtc->name);
742
Thierry Redinga18c0af2014-12-10 11:38:49 +0100743 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800744}
745EXPORT_SYMBOL(drm_crtc_cleanup);
746
747/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000748 * drm_crtc_index - find the index of a registered CRTC
749 * @crtc: CRTC to find index for
750 *
751 * Given a registered CRTC, return the index of that CRTC within a DRM
752 * device's list of CRTCs.
753 */
754unsigned int drm_crtc_index(struct drm_crtc *crtc)
755{
756 unsigned int index = 0;
757 struct drm_crtc *tmp;
758
Daniel Vettere4f62542015-07-09 23:44:35 +0200759 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000760 if (tmp == crtc)
761 return index;
762
763 index++;
764 }
765
766 BUG();
767}
768EXPORT_SYMBOL(drm_crtc_index);
769
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100770/*
Dave Airlief453ba02008-11-07 14:05:41 -0800771 * drm_mode_remove - remove and free a mode
772 * @connector: connector list to modify
773 * @mode: mode to remove
774 *
Dave Airlief453ba02008-11-07 14:05:41 -0800775 * Remove @mode from @connector's mode list, then free it.
776 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100777static void drm_mode_remove(struct drm_connector *connector,
778 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800779{
780 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100781 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800782}
Dave Airlief453ba02008-11-07 14:05:41 -0800783
784/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200785 * drm_display_info_set_bus_formats - set the supported bus formats
786 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100787 * @formats: array containing the supported bus formats
788 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200789 *
790 * Store the supported bus formats in display info structure.
791 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
792 * a full list of available formats.
793 */
794int drm_display_info_set_bus_formats(struct drm_display_info *info,
795 const u32 *formats,
796 unsigned int num_formats)
797{
798 u32 *fmts = NULL;
799
800 if (!formats && num_formats)
801 return -EINVAL;
802
803 if (formats && num_formats) {
804 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
805 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300806 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200807 return -ENOMEM;
808 }
809
810 kfree(info->bus_formats);
811 info->bus_formats = fmts;
812 info->num_bus_formats = num_formats;
813
814 return 0;
815}
816EXPORT_SYMBOL(drm_display_info_set_bus_formats);
817
818/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200819 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
820 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200821 *
822 * The kernel supports per-connector configration of its consoles through
823 * use of the video= parameter. This function parses that option and
824 * extracts the user's specified mode (or enable/disable status) for a
825 * particular connector. This is typically only used during the early fbdev
826 * setup.
827 */
828static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
829{
830 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
831 char *option = NULL;
832
833 if (fb_get_options(connector->name, &option))
834 return;
835
836 if (!drm_mode_parse_command_line_for_connector(option,
837 connector,
838 mode))
839 return;
840
841 if (mode->force) {
842 const char *s;
843
844 switch (mode->force) {
845 case DRM_FORCE_OFF:
846 s = "OFF";
847 break;
848 case DRM_FORCE_ON_DIGITAL:
849 s = "ON - dig";
850 break;
851 default:
852 case DRM_FORCE_ON:
853 s = "ON";
854 break;
855 }
856
857 DRM_INFO("forcing %s connector %s\n", connector->name, s);
858 connector->force = mode->force;
859 }
860
861 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
862 connector->name,
863 mode->xres, mode->yres,
864 mode->refresh_specified ? mode->refresh : 60,
865 mode->rb ? " reduced blanking" : "",
866 mode->margins ? " with margins" : "",
867 mode->interlace ? " interlaced" : "");
868}
869
870/**
Dave Airlief453ba02008-11-07 14:05:41 -0800871 * drm_connector_init - Init a preallocated connector
872 * @dev: DRM device
873 * @connector: the connector to init
874 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100875 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800876 *
Dave Airlief453ba02008-11-07 14:05:41 -0800877 * Initialises a preallocated connector. Connectors should be
878 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200879 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100880 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200881 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800882 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200883int drm_connector_init(struct drm_device *dev,
884 struct drm_connector *connector,
885 const struct drm_connector_funcs *funcs,
886 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800887{
Rob Clarkae16c592014-12-18 16:01:54 -0500888 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200889 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400890 struct ida *connector_ida =
891 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200892
Daniel Vetter84849902012-12-02 00:28:11 +0100893 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800894
Dave Airlied0f37cf2016-04-15 15:10:36 +1000895 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300897 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200898
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300899 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800900 connector->dev = dev;
901 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100902
903 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
904 if (connector->connector_id < 0) {
905 ret = connector->connector_id;
906 goto out_put;
907 }
908
Dave Airlief453ba02008-11-07 14:05:41 -0800909 connector->connector_type = connector_type;
910 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400911 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
912 if (connector->connector_type_id < 0) {
913 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100914 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400915 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300916 connector->name =
917 kasprintf(GFP_KERNEL, "%s-%d",
918 drm_connector_enum_list[connector_type].name,
919 connector->connector_type_id);
920 if (!connector->name) {
921 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100922 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300923 }
924
Dave Airlief453ba02008-11-07 14:05:41 -0800925 INIT_LIST_HEAD(&connector->probed_modes);
926 INIT_LIST_HEAD(&connector->modes);
927 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000928 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800929
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200930 drm_connector_get_cmdline_mode(connector);
931
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100932 /* We should add connectors at the end to avoid upsetting the connector
933 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500934 list_add_tail(&connector->head, &config->connector_list);
935 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800936
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200937 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500938 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500939 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200940 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800941
Rob Clark58495562012-10-11 20:50:56 -0500942 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500943 config->dpms_property, 0);
944
945 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
946 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
947 }
Dave Airlief453ba02008-11-07 14:05:41 -0800948
Thomas Wood30f65702014-06-18 17:52:32 +0100949 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100950out_put_type_id:
951 if (ret)
952 ida_remove(connector_ida, connector->connector_type_id);
953out_put_id:
954 if (ret)
955 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300956out_put:
957 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000958 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300959
960out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100961 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200962
963 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800964}
965EXPORT_SYMBOL(drm_connector_init);
966
967/**
968 * drm_connector_cleanup - cleans up an initialised connector
969 * @connector: connector to cleanup
970 *
Dave Airlief453ba02008-11-07 14:05:41 -0800971 * Cleans up the connector but doesn't free the object.
972 */
973void drm_connector_cleanup(struct drm_connector *connector)
974{
975 struct drm_device *dev = connector->dev;
976 struct drm_display_mode *mode, *t;
977
Dave Airlie40d9b042014-10-20 16:29:33 +1000978 if (connector->tile_group) {
979 drm_mode_put_tile_group(dev, connector->tile_group);
980 connector->tile_group = NULL;
981 }
982
Dave Airlief453ba02008-11-07 14:05:41 -0800983 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
984 drm_mode_remove(connector, mode);
985
986 list_for_each_entry_safe(mode, t, &connector->modes, head)
987 drm_mode_remove(connector, mode);
988
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400989 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
990 connector->connector_type_id);
991
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100992 ida_remove(&dev->mode_config.connector_ida,
993 connector->connector_id);
994
Boris Brezillonb5571e92014-07-22 12:09:10 +0200995 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000996 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300997 kfree(connector->name);
998 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800999 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001000 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001001
1002 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1003 if (connector->state && connector->funcs->atomic_destroy_state)
1004 connector->funcs->atomic_destroy_state(connector,
1005 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001006
1007 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001008}
1009EXPORT_SYMBOL(drm_connector_cleanup);
1010
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001011/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001012 * drm_connector_register - register a connector
1013 * @connector: the connector to register
1014 *
1015 * Register userspace interfaces for a connector
1016 *
1017 * Returns:
1018 * Zero on success, error code on failure.
1019 */
1020int drm_connector_register(struct drm_connector *connector)
1021{
Thomas Wood30f65702014-06-18 17:52:32 +01001022 int ret;
1023
Dave Airlie2ee39452014-07-24 11:53:45 +10001024 drm_mode_object_register(connector->dev, &connector->base);
1025
Thomas Wood30f65702014-06-18 17:52:32 +01001026 ret = drm_sysfs_connector_add(connector);
1027 if (ret)
1028 return ret;
1029
1030 ret = drm_debugfs_connector_add(connector);
1031 if (ret) {
1032 drm_sysfs_connector_remove(connector);
1033 return ret;
1034 }
1035
1036 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001037}
1038EXPORT_SYMBOL(drm_connector_register);
1039
1040/**
1041 * drm_connector_unregister - unregister a connector
1042 * @connector: the connector to unregister
1043 *
1044 * Unregister userspace interfaces for a connector
1045 */
1046void drm_connector_unregister(struct drm_connector *connector)
1047{
1048 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001049 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001050}
1051EXPORT_SYMBOL(drm_connector_unregister);
1052
Thomas Wood34ea3d32014-05-29 16:57:41 +01001053/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001054 * drm_connector_register_all - register all connectors
1055 * @dev: drm device
1056 *
1057 * This function registers all connectors in sysfs and other places so that
1058 * userspace can start to access them. Drivers can call it after calling
1059 * drm_dev_register() to complete the device registration, if they don't call
1060 * drm_connector_register() on each connector individually.
1061 *
1062 * When a device is unplugged and should be removed from userspace access,
1063 * call drm_connector_unregister_all(), which is the inverse of this
1064 * function.
1065 *
1066 * Returns:
1067 * Zero on success, error code on failure.
1068 */
1069int drm_connector_register_all(struct drm_device *dev)
1070{
1071 struct drm_connector *connector;
1072 int ret;
1073
1074 mutex_lock(&dev->mode_config.mutex);
1075
1076 drm_for_each_connector(connector, dev) {
1077 ret = drm_connector_register(connector);
1078 if (ret)
1079 goto err;
1080 }
1081
1082 mutex_unlock(&dev->mode_config.mutex);
1083
1084 return 0;
1085
1086err:
1087 mutex_unlock(&dev->mode_config.mutex);
1088 drm_connector_unregister_all(dev);
1089 return ret;
1090}
1091EXPORT_SYMBOL(drm_connector_register_all);
1092
1093/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001094 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001095 * @dev: drm device
1096 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001097 * This functions unregisters all connectors from sysfs and other places so
1098 * that userspace can no longer access them. Drivers should call this as the
1099 * first step tearing down the device instace, or when the underlying
1100 * physical device disappeared (e.g. USB unplug), right before calling
1101 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001102 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001103void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001104{
1105 struct drm_connector *connector;
1106
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001107 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001108 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001109 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001110}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001111EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001112
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001113/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001114 * drm_encoder_init - Init a preallocated encoder
1115 * @dev: drm device
1116 * @encoder: the encoder to init
1117 * @funcs: callbacks for this encoder
1118 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001119 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001120 *
1121 * Initialises a preallocated encoder. Encoder should be
1122 * subclassed as part of driver encoder objects.
1123 *
1124 * Returns:
1125 * Zero on success, error code on failure.
1126 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001127int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001128 struct drm_encoder *encoder,
1129 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001130 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001131{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001132 int ret;
1133
Daniel Vetter84849902012-12-02 00:28:11 +01001134 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001135
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001136 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1137 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001138 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001139
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001140 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001141 encoder->encoder_type = encoder_type;
1142 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001143 if (name) {
1144 va_list ap;
1145
1146 va_start(ap, name);
1147 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1148 va_end(ap);
1149 } else {
1150 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1151 drm_encoder_enum_list[encoder_type].name,
1152 encoder->base.id);
1153 }
Jani Nikulae5748942014-05-14 16:58:20 +03001154 if (!encoder->name) {
1155 ret = -ENOMEM;
1156 goto out_put;
1157 }
Dave Airlief453ba02008-11-07 14:05:41 -08001158
1159 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1160 dev->mode_config.num_encoder++;
1161
Jani Nikulae5748942014-05-14 16:58:20 +03001162out_put:
1163 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001164 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001165
1166out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001167 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001168
1169 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001170}
1171EXPORT_SYMBOL(drm_encoder_init);
1172
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001173/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001174 * drm_encoder_index - find the index of a registered encoder
1175 * @encoder: encoder to find index for
1176 *
1177 * Given a registered encoder, return the index of that encoder within a DRM
1178 * device's list of encoders.
1179 */
1180unsigned int drm_encoder_index(struct drm_encoder *encoder)
1181{
1182 unsigned int index = 0;
1183 struct drm_encoder *tmp;
1184
1185 drm_for_each_encoder(tmp, encoder->dev) {
1186 if (tmp == encoder)
1187 return index;
1188
1189 index++;
1190 }
1191
1192 BUG();
1193}
1194EXPORT_SYMBOL(drm_encoder_index);
1195
1196/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001197 * drm_encoder_cleanup - cleans up an initialised encoder
1198 * @encoder: encoder to cleanup
1199 *
1200 * Cleans up the encoder but doesn't free the object.
1201 */
Dave Airlief453ba02008-11-07 14:05:41 -08001202void drm_encoder_cleanup(struct drm_encoder *encoder)
1203{
1204 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001205
Daniel Vetter84849902012-12-02 00:28:11 +01001206 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001207 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001208 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001209 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001210 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001211 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001212
1213 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001214}
1215EXPORT_SYMBOL(drm_encoder_cleanup);
1216
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001217static unsigned int drm_num_planes(struct drm_device *dev)
1218{
1219 unsigned int num = 0;
1220 struct drm_plane *tmp;
1221
1222 drm_for_each_plane(tmp, dev) {
1223 num++;
1224 }
1225
1226 return num;
1227}
1228
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001229/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001230 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001231 * @dev: DRM device
1232 * @plane: plane object to init
1233 * @possible_crtcs: bitmask of possible CRTCs
1234 * @funcs: callbacks for the new plane
1235 * @formats: array of supported formats (%DRM_FORMAT_*)
1236 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001237 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001238 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001239 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001240 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001241 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001242 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001243 * Zero on success, error code on failure.
1244 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001245int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1246 unsigned long possible_crtcs,
1247 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001248 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001249 enum drm_plane_type type,
1250 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001251{
Rob Clark6b4959f2014-12-18 16:01:53 -05001252 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001253 int ret;
1254
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001255 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1256 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001257 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001258
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001259 drm_modeset_lock_init(&plane->mutex);
1260
Rob Clark4d939142012-05-17 02:23:27 -06001261 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001262 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001263 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001264 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1265 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001266 if (!plane->format_types) {
1267 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001268 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001269 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001270 }
1271
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001272 if (name) {
1273 va_list ap;
1274
1275 va_start(ap, name);
1276 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1277 va_end(ap);
1278 } else {
1279 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1280 drm_num_planes(dev));
1281 }
1282 if (!plane->name) {
1283 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001284 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001285 return -ENOMEM;
1286 }
1287
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001288 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001289 plane->format_count = format_count;
1290 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001291 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001292
Rob Clark6b4959f2014-12-18 16:01:53 -05001293 list_add_tail(&plane->head, &config->plane_list);
1294 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001295 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001296 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001297
Rob Clark9922ab52014-04-01 20:16:57 -04001298 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001299 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001300 plane->type);
1301
Rob Clark6b4959f2014-12-18 16:01:53 -05001302 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1303 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1304 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1305 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1306 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1307 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1308 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1309 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1310 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1311 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1312 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1313 }
1314
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001315 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001316}
Matt Roperdc415ff2014-04-01 15:22:36 -07001317EXPORT_SYMBOL(drm_universal_plane_init);
1318
1319/**
1320 * drm_plane_init - Initialize a legacy plane
1321 * @dev: DRM device
1322 * @plane: plane object to init
1323 * @possible_crtcs: bitmask of possible CRTCs
1324 * @funcs: callbacks for the new plane
1325 * @formats: array of supported formats (%DRM_FORMAT_*)
1326 * @format_count: number of elements in @formats
1327 * @is_primary: plane type (primary vs overlay)
1328 *
1329 * Legacy API to initialize a DRM plane.
1330 *
1331 * New drivers should call drm_universal_plane_init() instead.
1332 *
1333 * Returns:
1334 * Zero on success, error code on failure.
1335 */
1336int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1337 unsigned long possible_crtcs,
1338 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001339 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001340 bool is_primary)
1341{
1342 enum drm_plane_type type;
1343
1344 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1345 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001346 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001347}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001348EXPORT_SYMBOL(drm_plane_init);
1349
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001350/**
1351 * drm_plane_cleanup - Clean up the core plane usage
1352 * @plane: plane to cleanup
1353 *
1354 * This function cleans up @plane and removes it from the DRM mode setting
1355 * core. Note that the function does *not* free the plane structure itself,
1356 * this is the responsibility of the caller.
1357 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001358void drm_plane_cleanup(struct drm_plane *plane)
1359{
1360 struct drm_device *dev = plane->dev;
1361
Daniel Vetter84849902012-12-02 00:28:11 +01001362 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001363 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001364 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001365
1366 BUG_ON(list_empty(&plane->head));
1367
1368 list_del(&plane->head);
1369 dev->mode_config.num_total_plane--;
1370 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1371 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001372 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001373
1374 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1375 if (plane->state && plane->funcs->atomic_destroy_state)
1376 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001377
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001378 kfree(plane->name);
1379
Thierry Redinga18c0af2014-12-10 11:38:49 +01001380 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001381}
1382EXPORT_SYMBOL(drm_plane_cleanup);
1383
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001384/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001385 * drm_plane_index - find the index of a registered plane
1386 * @plane: plane to find index for
1387 *
1388 * Given a registered plane, return the index of that CRTC within a DRM
1389 * device's list of planes.
1390 */
1391unsigned int drm_plane_index(struct drm_plane *plane)
1392{
1393 unsigned int index = 0;
1394 struct drm_plane *tmp;
1395
Daniel Vettere4f62542015-07-09 23:44:35 +02001396 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001397 if (tmp == plane)
1398 return index;
1399
1400 index++;
1401 }
1402
1403 BUG();
1404}
1405EXPORT_SYMBOL(drm_plane_index);
1406
1407/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001408 * drm_plane_from_index - find the registered plane at an index
1409 * @dev: DRM device
1410 * @idx: index of registered plane to find for
1411 *
1412 * Given a plane index, return the registered plane from DRM device's
1413 * list of planes with matching index.
1414 */
1415struct drm_plane *
1416drm_plane_from_index(struct drm_device *dev, int idx)
1417{
1418 struct drm_plane *plane;
1419 unsigned int i = 0;
1420
Daniel Vetter6295d602015-07-09 23:44:25 +02001421 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001422 if (i == idx)
1423 return plane;
1424 i++;
1425 }
1426 return NULL;
1427}
1428EXPORT_SYMBOL(drm_plane_from_index);
1429
1430/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001431 * drm_plane_force_disable - Forcibly disable a plane
1432 * @plane: plane to disable
1433 *
1434 * Forces the plane to be disabled.
1435 *
1436 * Used when the plane's current framebuffer is destroyed,
1437 * and when restoring fbdev mode.
1438 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001439void drm_plane_force_disable(struct drm_plane *plane)
1440{
1441 int ret;
1442
Daniel Vetter3d30a592014-07-27 13:42:42 +02001443 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001444 return;
1445
Daniel Vetter3d30a592014-07-27 13:42:42 +02001446 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001447 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001448 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001449 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001450 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001451 return;
1452 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001453 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001454 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001455 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001456 plane->fb = NULL;
1457 plane->crtc = NULL;
1458}
1459EXPORT_SYMBOL(drm_plane_force_disable);
1460
Rob Clark6b4959f2014-12-18 16:01:53 -05001461static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001462{
Rob Clark356af0e2014-12-18 16:01:52 -05001463 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001464
1465 /*
1466 * Standard properties (apply to all connectors)
1467 */
Rob Clark356af0e2014-12-18 16:01:52 -05001468 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001469 DRM_MODE_PROP_IMMUTABLE,
1470 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001471 if (!prop)
1472 return -ENOMEM;
1473 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001474
Rob Clark356af0e2014-12-18 16:01:52 -05001475 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001476 "DPMS", drm_dpms_enum_list,
1477 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001478 if (!prop)
1479 return -ENOMEM;
1480 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001481
Rob Clark356af0e2014-12-18 16:01:52 -05001482 prop = drm_property_create(dev,
1483 DRM_MODE_PROP_BLOB |
1484 DRM_MODE_PROP_IMMUTABLE,
1485 "PATH", 0);
1486 if (!prop)
1487 return -ENOMEM;
1488 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001489
Rob Clark356af0e2014-12-18 16:01:52 -05001490 prop = drm_property_create(dev,
1491 DRM_MODE_PROP_BLOB |
1492 DRM_MODE_PROP_IMMUTABLE,
1493 "TILE", 0);
1494 if (!prop)
1495 return -ENOMEM;
1496 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001497
Rob Clark6b4959f2014-12-18 16:01:53 -05001498 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001499 "type", drm_plane_type_enum_list,
1500 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001501 if (!prop)
1502 return -ENOMEM;
1503 dev->mode_config.plane_type_property = prop;
1504
1505 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1506 "SRC_X", 0, UINT_MAX);
1507 if (!prop)
1508 return -ENOMEM;
1509 dev->mode_config.prop_src_x = prop;
1510
1511 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1512 "SRC_Y", 0, UINT_MAX);
1513 if (!prop)
1514 return -ENOMEM;
1515 dev->mode_config.prop_src_y = prop;
1516
1517 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1518 "SRC_W", 0, UINT_MAX);
1519 if (!prop)
1520 return -ENOMEM;
1521 dev->mode_config.prop_src_w = prop;
1522
1523 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1524 "SRC_H", 0, UINT_MAX);
1525 if (!prop)
1526 return -ENOMEM;
1527 dev->mode_config.prop_src_h = prop;
1528
1529 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1530 "CRTC_X", INT_MIN, INT_MAX);
1531 if (!prop)
1532 return -ENOMEM;
1533 dev->mode_config.prop_crtc_x = prop;
1534
1535 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1536 "CRTC_Y", INT_MIN, INT_MAX);
1537 if (!prop)
1538 return -ENOMEM;
1539 dev->mode_config.prop_crtc_y = prop;
1540
1541 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1542 "CRTC_W", 0, INT_MAX);
1543 if (!prop)
1544 return -ENOMEM;
1545 dev->mode_config.prop_crtc_w = prop;
1546
1547 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1548 "CRTC_H", 0, INT_MAX);
1549 if (!prop)
1550 return -ENOMEM;
1551 dev->mode_config.prop_crtc_h = prop;
1552
1553 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1554 "FB_ID", DRM_MODE_OBJECT_FB);
1555 if (!prop)
1556 return -ENOMEM;
1557 dev->mode_config.prop_fb_id = prop;
1558
1559 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1560 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1561 if (!prop)
1562 return -ENOMEM;
1563 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001564
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001565 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1566 "ACTIVE");
1567 if (!prop)
1568 return -ENOMEM;
1569 dev->mode_config.prop_active = prop;
1570
Daniel Stone955f3c32015-05-25 19:11:52 +01001571 prop = drm_property_create(dev,
1572 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1573 "MODE_ID", 0);
1574 if (!prop)
1575 return -ENOMEM;
1576 dev->mode_config.prop_mode_id = prop;
1577
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001578 prop = drm_property_create(dev,
1579 DRM_MODE_PROP_BLOB,
1580 "DEGAMMA_LUT", 0);
1581 if (!prop)
1582 return -ENOMEM;
1583 dev->mode_config.degamma_lut_property = prop;
1584
1585 prop = drm_property_create_range(dev,
1586 DRM_MODE_PROP_IMMUTABLE,
1587 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1588 if (!prop)
1589 return -ENOMEM;
1590 dev->mode_config.degamma_lut_size_property = prop;
1591
1592 prop = drm_property_create(dev,
1593 DRM_MODE_PROP_BLOB,
1594 "CTM", 0);
1595 if (!prop)
1596 return -ENOMEM;
1597 dev->mode_config.ctm_property = prop;
1598
1599 prop = drm_property_create(dev,
1600 DRM_MODE_PROP_BLOB,
1601 "GAMMA_LUT", 0);
1602 if (!prop)
1603 return -ENOMEM;
1604 dev->mode_config.gamma_lut_property = prop;
1605
1606 prop = drm_property_create_range(dev,
1607 DRM_MODE_PROP_IMMUTABLE,
1608 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1609 if (!prop)
1610 return -ENOMEM;
1611 dev->mode_config.gamma_lut_size_property = prop;
1612
Rob Clark9922ab52014-04-01 20:16:57 -04001613 return 0;
1614}
1615
Dave Airlief453ba02008-11-07 14:05:41 -08001616/**
1617 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1618 * @dev: DRM device
1619 *
1620 * Called by a driver the first time a DVI-I connector is made.
1621 */
1622int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1623{
1624 struct drm_property *dvi_i_selector;
1625 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001626
1627 if (dev->mode_config.dvi_i_select_subconnector_property)
1628 return 0;
1629
1630 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001631 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001632 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001633 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001634 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001635 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1636
Sascha Hauer4a67d392012-02-06 10:58:17 +01001637 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001638 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001639 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001640 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001641 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1642
1643 return 0;
1644}
1645EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1646
1647/**
1648 * drm_create_tv_properties - create TV specific connector properties
1649 * @dev: DRM device
1650 * @num_modes: number of different TV formats (modes) supported
1651 * @modes: array of pointers to strings containing name of each format
1652 *
1653 * Called by a driver's TV initialization routine, this function creates
1654 * the TV specific connector properties for a given device. Caller is
1655 * responsible for allocating a list of format names and passing them to
1656 * this routine.
1657 */
Thierry Reding2f763312014-10-13 12:45:57 +02001658int drm_mode_create_tv_properties(struct drm_device *dev,
1659 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001660 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001661{
1662 struct drm_property *tv_selector;
1663 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001664 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001665
1666 if (dev->mode_config.tv_select_subconnector_property)
1667 return 0;
1668
1669 /*
1670 * Basic connector properties
1671 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001672 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001673 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001674 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001675 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001676 if (!tv_selector)
1677 goto nomem;
1678
Dave Airlief453ba02008-11-07 14:05:41 -08001679 dev->mode_config.tv_select_subconnector_property = tv_selector;
1680
1681 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001682 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1683 "subconnector",
1684 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001685 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001686 if (!tv_subconnector)
1687 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001688 dev->mode_config.tv_subconnector_property = tv_subconnector;
1689
1690 /*
1691 * Other, TV specific properties: margins & TV modes.
1692 */
1693 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001694 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001695 if (!dev->mode_config.tv_left_margin_property)
1696 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001697
1698 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001699 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001700 if (!dev->mode_config.tv_right_margin_property)
1701 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001702
1703 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001704 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001705 if (!dev->mode_config.tv_top_margin_property)
1706 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001707
1708 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001709 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001710 if (!dev->mode_config.tv_bottom_margin_property)
1711 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001712
1713 dev->mode_config.tv_mode_property =
1714 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1715 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001716 if (!dev->mode_config.tv_mode_property)
1717 goto nomem;
1718
Dave Airlief453ba02008-11-07 14:05:41 -08001719 for (i = 0; i < num_modes; i++)
1720 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1721 i, modes[i]);
1722
Francisco Jerezb6b79022009-08-02 04:19:20 +02001723 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001724 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001725 if (!dev->mode_config.tv_brightness_property)
1726 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001727
1728 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001729 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001730 if (!dev->mode_config.tv_contrast_property)
1731 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001732
1733 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001734 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001735 if (!dev->mode_config.tv_flicker_reduction_property)
1736 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001737
Francisco Jereza75f0232009-08-12 02:30:10 +02001738 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001739 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001740 if (!dev->mode_config.tv_overscan_property)
1741 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001742
1743 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001744 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001745 if (!dev->mode_config.tv_saturation_property)
1746 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001747
1748 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001749 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001750 if (!dev->mode_config.tv_hue_property)
1751 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001752
Dave Airlief453ba02008-11-07 14:05:41 -08001753 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001754nomem:
1755 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001756}
1757EXPORT_SYMBOL(drm_mode_create_tv_properties);
1758
1759/**
1760 * drm_mode_create_scaling_mode_property - create scaling mode property
1761 * @dev: DRM device
1762 *
1763 * Called by a driver the first time it's needed, must be attached to desired
1764 * connectors.
1765 */
1766int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1767{
1768 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001769
1770 if (dev->mode_config.scaling_mode_property)
1771 return 0;
1772
1773 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001774 drm_property_create_enum(dev, 0, "scaling mode",
1775 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001776 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001777
1778 dev->mode_config.scaling_mode_property = scaling_mode;
1779
1780 return 0;
1781}
1782EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1783
1784/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301785 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1786 * @dev: DRM device
1787 *
1788 * Called by a driver the first time it's needed, must be attached to desired
1789 * connectors.
1790 *
1791 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001792 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301793 */
1794int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1795{
1796 if (dev->mode_config.aspect_ratio_property)
1797 return 0;
1798
1799 dev->mode_config.aspect_ratio_property =
1800 drm_property_create_enum(dev, 0, "aspect ratio",
1801 drm_aspect_ratio_enum_list,
1802 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1803
1804 if (dev->mode_config.aspect_ratio_property == NULL)
1805 return -ENOMEM;
1806
1807 return 0;
1808}
1809EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1810
1811/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001812 * drm_mode_create_dirty_property - create dirty property
1813 * @dev: DRM device
1814 *
1815 * Called by a driver the first time it's needed, must be attached to desired
1816 * connectors.
1817 */
1818int drm_mode_create_dirty_info_property(struct drm_device *dev)
1819{
1820 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001821
1822 if (dev->mode_config.dirty_info_property)
1823 return 0;
1824
1825 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001826 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001827 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001828 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001829 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001830 dev->mode_config.dirty_info_property = dirty_info;
1831
1832 return 0;
1833}
1834EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1835
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001836/**
1837 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1838 * @dev: DRM device
1839 *
1840 * Create the the suggested x/y offset property for connectors.
1841 */
1842int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1843{
1844 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1845 return 0;
1846
1847 dev->mode_config.suggested_x_property =
1848 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1849
1850 dev->mode_config.suggested_y_property =
1851 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1852
1853 if (dev->mode_config.suggested_x_property == NULL ||
1854 dev->mode_config.suggested_y_property == NULL)
1855 return -ENOMEM;
1856 return 0;
1857}
1858EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1859
Dave Airlief453ba02008-11-07 14:05:41 -08001860/**
Dave Airlief453ba02008-11-07 14:05:41 -08001861 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001862 * @dev: drm device for the ioctl
1863 * @data: data pointer for the ioctl
1864 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001865 *
Dave Airlief453ba02008-11-07 14:05:41 -08001866 * Construct a set of configuration description structures and return
1867 * them to the user, including CRTC, connector and framebuffer configuration.
1868 *
1869 * Called by the user via ioctl.
1870 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001871 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001872 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001873 */
1874int drm_mode_getresources(struct drm_device *dev, void *data,
1875 struct drm_file *file_priv)
1876{
1877 struct drm_mode_card_res *card_res = data;
1878 struct list_head *lh;
1879 struct drm_framebuffer *fb;
1880 struct drm_connector *connector;
1881 struct drm_crtc *crtc;
1882 struct drm_encoder *encoder;
1883 int ret = 0;
1884 int connector_count = 0;
1885 int crtc_count = 0;
1886 int fb_count = 0;
1887 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001888 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001889 uint32_t __user *fb_id;
1890 uint32_t __user *crtc_id;
1891 uint32_t __user *connector_id;
1892 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001893
Dave Airliefb3b06c2011-02-08 13:55:21 +10001894 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1895 return -EINVAL;
1896
Dave Airlief453ba02008-11-07 14:05:41 -08001897
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001898 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001899 /*
1900 * For the non-control nodes we need to limit the list of resources
1901 * by IDs in the group list for this node
1902 */
1903 list_for_each(lh, &file_priv->fbs)
1904 fb_count++;
1905
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001906 /* handle this in 4 parts */
1907 /* FBs */
1908 if (card_res->count_fbs >= fb_count) {
1909 copied = 0;
1910 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1911 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1912 if (put_user(fb->base.id, fb_id + copied)) {
1913 mutex_unlock(&file_priv->fbs_lock);
1914 return -EFAULT;
1915 }
1916 copied++;
1917 }
1918 }
1919 card_res->count_fbs = fb_count;
1920 mutex_unlock(&file_priv->fbs_lock);
1921
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001922 /* mode_config.mutex protects the connector list against e.g. DP MST
1923 * connector hot-adding. CRTC/Plane lists are invariant. */
1924 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001925 drm_for_each_crtc(crtc, dev)
1926 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001927
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001928 drm_for_each_connector(connector, dev)
1929 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001930
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001931 drm_for_each_encoder(encoder, dev)
1932 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001933
1934 card_res->max_height = dev->mode_config.max_height;
1935 card_res->min_height = dev->mode_config.min_height;
1936 card_res->max_width = dev->mode_config.max_width;
1937 card_res->min_width = dev->mode_config.min_width;
1938
Dave Airlief453ba02008-11-07 14:05:41 -08001939 /* CRTCs */
1940 if (card_res->count_crtcs >= crtc_count) {
1941 copied = 0;
1942 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001943 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001944 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1945 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001946 if (put_user(crtc->base.id, crtc_id + copied)) {
1947 ret = -EFAULT;
1948 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001949 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001950 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001951 }
1952 }
1953 card_res->count_crtcs = crtc_count;
1954
1955 /* Encoders */
1956 if (card_res->count_encoders >= encoder_count) {
1957 copied = 0;
1958 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001959 drm_for_each_encoder(encoder, dev) {
1960 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1961 encoder->name);
1962 if (put_user(encoder->base.id, encoder_id +
1963 copied)) {
1964 ret = -EFAULT;
1965 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001967 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001968 }
1969 }
1970 card_res->count_encoders = encoder_count;
1971
1972 /* Connectors */
1973 if (card_res->count_connectors >= connector_count) {
1974 copied = 0;
1975 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001976 drm_for_each_connector(connector, dev) {
1977 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1978 connector->base.id,
1979 connector->name);
1980 if (put_user(connector->base.id,
1981 connector_id + copied)) {
1982 ret = -EFAULT;
1983 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001984 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001985 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001986 }
1987 }
1988 card_res->count_connectors = connector_count;
1989
Jerome Glisse94401062010-07-15 15:43:25 -04001990 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001991 card_res->count_connectors, card_res->count_encoders);
1992
1993out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001994 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001995 return ret;
1996}
1997
1998/**
1999 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002000 * @dev: drm device for the ioctl
2001 * @data: data pointer for the ioctl
2002 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002003 *
Dave Airlief453ba02008-11-07 14:05:41 -08002004 * Construct a CRTC configuration structure to return to the user.
2005 *
2006 * Called by the user via ioctl.
2007 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002008 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002009 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002010 */
2011int drm_mode_getcrtc(struct drm_device *dev,
2012 void *data, struct drm_file *file_priv)
2013{
2014 struct drm_mode_crtc *crtc_resp = data;
2015 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002016
Dave Airliefb3b06c2011-02-08 13:55:21 +10002017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2018 return -EINVAL;
2019
Rob Clarka2b34e22013-10-05 16:36:52 -04002020 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002021 if (!crtc)
2022 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002023
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002024 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002025 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002026 if (crtc->primary->fb)
2027 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002028 else
2029 crtc_resp->fb_id = 0;
2030
Daniel Vetter31c946e2015-02-22 12:24:17 +01002031 if (crtc->state) {
2032 crtc_resp->x = crtc->primary->state->src_x >> 16;
2033 crtc_resp->y = crtc->primary->state->src_y >> 16;
2034 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002035 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002036 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002037
Daniel Vetter31c946e2015-02-22 12:24:17 +01002038 } else {
2039 crtc_resp->mode_valid = 0;
2040 }
Dave Airlief453ba02008-11-07 14:05:41 -08002041 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002042 crtc_resp->x = crtc->x;
2043 crtc_resp->y = crtc->y;
2044 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002045 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002046 crtc_resp->mode_valid = 1;
2047
2048 } else {
2049 crtc_resp->mode_valid = 0;
2050 }
Dave Airlief453ba02008-11-07 14:05:41 -08002051 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002052 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002053
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002054 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002055}
2056
Damien Lespiau61d8e322013-09-25 16:45:22 +01002057static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2058 const struct drm_file *file_priv)
2059{
2060 /*
2061 * If user-space hasn't configured the driver to expose the stereo 3D
2062 * modes, don't expose them.
2063 */
2064 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2065 return false;
2066
2067 return true;
2068}
2069
Daniel Vetterabd69c52014-11-25 23:50:05 +01002070static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2071{
2072 /* For atomic drivers only state objects are synchronously updated and
2073 * protected by modeset locks, so check those first. */
2074 if (connector->state)
2075 return connector->state->best_encoder;
2076 return connector->encoder;
2077}
2078
Rob Clark95cbf112014-12-18 16:01:49 -05002079/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002080static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002081 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2082 uint32_t *arg_count_props)
2083{
Rob Clark88a48e22014-12-18 16:01:50 -05002084 int props_count;
2085 int i, ret, copied;
2086
2087 props_count = obj->properties->count;
2088 if (!atomic)
2089 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002090
2091 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002092 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002093 struct drm_property *prop = obj->properties->properties[i];
2094 uint64_t val;
2095
Rob Clark88a48e22014-12-18 16:01:50 -05002096 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2097 continue;
2098
Rob Clark95cbf112014-12-18 16:01:49 -05002099 ret = drm_object_property_get_value(obj, prop, &val);
2100 if (ret)
2101 return ret;
2102
2103 if (put_user(prop->base.id, prop_ptr + copied))
2104 return -EFAULT;
2105
2106 if (put_user(val, prop_values + copied))
2107 return -EFAULT;
2108
2109 copied++;
2110 }
2111 }
2112 *arg_count_props = props_count;
2113
2114 return 0;
2115}
2116
Dave Airlief453ba02008-11-07 14:05:41 -08002117/**
2118 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002119 * @dev: drm device for the ioctl
2120 * @data: data pointer for the ioctl
2121 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002122 *
Dave Airlief453ba02008-11-07 14:05:41 -08002123 * Construct a connector configuration structure to return to the user.
2124 *
2125 * Called by the user via ioctl.
2126 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002127 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002128 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002129 */
2130int drm_mode_getconnector(struct drm_device *dev, void *data,
2131 struct drm_file *file_priv)
2132{
2133 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002134 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002135 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002136 struct drm_display_mode *mode;
2137 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002138 int encoders_count = 0;
2139 int ret = 0;
2140 int copied = 0;
2141 int i;
2142 struct drm_mode_modeinfo u_mode;
2143 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002144 uint32_t __user *encoder_ptr;
2145
Dave Airliefb3b06c2011-02-08 13:55:21 +10002146 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2147 return -EINVAL;
2148
Dave Airlief453ba02008-11-07 14:05:41 -08002149 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2150
Jerome Glisse94401062010-07-15 15:43:25 -04002151 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002152
Daniel Vetter7b240562012-12-12 00:35:33 +01002153 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002154
Rob Clarka2b34e22013-10-05 16:36:52 -04002155 connector = drm_connector_find(dev, out_resp->connector_id);
2156 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002157 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002158 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002159 }
Dave Airlief453ba02008-11-07 14:05:41 -08002160
Thierry Reding01073b02014-12-10 13:03:38 +01002161 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2162 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002163 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002164
2165 if (out_resp->count_modes == 0) {
2166 connector->funcs->fill_modes(connector,
2167 dev->mode_config.max_width,
2168 dev->mode_config.max_height);
2169 }
2170
2171 /* delayed so we get modes regardless of pre-fill_modes state */
2172 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002173 if (drm_mode_expose_to_userspace(mode, file_priv))
2174 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002175
2176 out_resp->connector_id = connector->base.id;
2177 out_resp->connector_type = connector->connector_type;
2178 out_resp->connector_type_id = connector->connector_type_id;
2179 out_resp->mm_width = connector->display_info.width_mm;
2180 out_resp->mm_height = connector->display_info.height_mm;
2181 out_resp->subpixel = connector->display_info.subpixel_order;
2182 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002183
2184 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002185 encoder = drm_connector_get_encoder(connector);
2186 if (encoder)
2187 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002188 else
2189 out_resp->encoder_id = 0;
2190
2191 /*
2192 * This ioctl is called twice, once to determine how much space is
2193 * needed, and the 2nd time to fill it.
2194 */
2195 if ((out_resp->count_modes >= mode_count) && mode_count) {
2196 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002197 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002198 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002199 if (!drm_mode_expose_to_userspace(mode, file_priv))
2200 continue;
2201
Daniel Stone934a8a82015-05-22 13:34:48 +01002202 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002203 if (copy_to_user(mode_ptr + copied,
2204 &u_mode, sizeof(u_mode))) {
2205 ret = -EFAULT;
2206 goto out;
2207 }
2208 copied++;
2209 }
2210 }
2211 out_resp->count_modes = mode_count;
2212
Rob Clark88a48e22014-12-18 16:01:50 -05002213 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002214 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2215 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2216 &out_resp->count_props);
2217 if (ret)
2218 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002219
2220 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2221 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002222 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002223 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2224 if (connector->encoder_ids[i] != 0) {
2225 if (put_user(connector->encoder_ids[i],
2226 encoder_ptr + copied)) {
2227 ret = -EFAULT;
2228 goto out;
2229 }
2230 copied++;
2231 }
2232 }
2233 }
2234 out_resp->count_encoders = encoders_count;
2235
2236out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002237 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002238
2239out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002240 mutex_unlock(&dev->mode_config.mutex);
2241
Dave Airlief453ba02008-11-07 14:05:41 -08002242 return ret;
2243}
2244
Daniel Vetterabd69c52014-11-25 23:50:05 +01002245static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2246{
2247 struct drm_connector *connector;
2248 struct drm_device *dev = encoder->dev;
2249 bool uses_atomic = false;
2250
2251 /* For atomic drivers only state objects are synchronously updated and
2252 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002253 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002254 if (!connector->state)
2255 continue;
2256
2257 uses_atomic = true;
2258
2259 if (connector->state->best_encoder != encoder)
2260 continue;
2261
2262 return connector->state->crtc;
2263 }
2264
2265 /* Don't return stale data (e.g. pending async disable). */
2266 if (uses_atomic)
2267 return NULL;
2268
2269 return encoder->crtc;
2270}
2271
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002272/**
2273 * drm_mode_getencoder - get encoder configuration
2274 * @dev: drm device for the ioctl
2275 * @data: data pointer for the ioctl
2276 * @file_priv: drm file for the ioctl call
2277 *
2278 * Construct a encoder configuration structure to return to the user.
2279 *
2280 * Called by the user via ioctl.
2281 *
2282 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002283 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002284 */
Dave Airlief453ba02008-11-07 14:05:41 -08002285int drm_mode_getencoder(struct drm_device *dev, void *data,
2286 struct drm_file *file_priv)
2287{
2288 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002289 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002290 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002291
Dave Airliefb3b06c2011-02-08 13:55:21 +10002292 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2293 return -EINVAL;
2294
Rob Clarka2b34e22013-10-05 16:36:52 -04002295 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002296 if (!encoder)
2297 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002298
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002299 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002300 crtc = drm_encoder_get_crtc(encoder);
2301 if (crtc)
2302 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002303 else
2304 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002305 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2306
Dave Airlief453ba02008-11-07 14:05:41 -08002307 enc_resp->encoder_type = encoder->encoder_type;
2308 enc_resp->encoder_id = encoder->base.id;
2309 enc_resp->possible_crtcs = encoder->possible_crtcs;
2310 enc_resp->possible_clones = encoder->possible_clones;
2311
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002312 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002313}
2314
2315/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002316 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002317 * @dev: DRM device
2318 * @data: ioctl data
2319 * @file_priv: DRM file info
2320 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002321 * Construct a list of plane ids to return to the user.
2322 *
2323 * Called by the user via ioctl.
2324 *
2325 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002326 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002327 */
2328int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002329 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002330{
2331 struct drm_mode_get_plane_res *plane_resp = data;
2332 struct drm_mode_config *config;
2333 struct drm_plane *plane;
2334 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002335 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002336 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002337
2338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2339 return -EINVAL;
2340
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341 config = &dev->mode_config;
2342
Matt Roper681e7ec2014-04-01 15:22:42 -07002343 if (file_priv->universal_planes)
2344 num_planes = config->num_total_plane;
2345 else
2346 num_planes = config->num_overlay_plane;
2347
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002348 /*
2349 * This ioctl is called twice, once to determine how much space is
2350 * needed, and the 2nd time to fill it.
2351 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002352 if (num_planes &&
2353 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002354 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002355
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002356 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002357 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002358 /*
2359 * Unless userspace set the 'universal planes'
2360 * capability bit, only advertise overlays.
2361 */
2362 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2363 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002364 continue;
2365
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002366 if (put_user(plane->base.id, plane_ptr + copied))
2367 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002368 copied++;
2369 }
2370 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002371 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002373 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002374}
2375
2376/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002377 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002378 * @dev: DRM device
2379 * @data: ioctl data
2380 * @file_priv: DRM file info
2381 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002382 * Construct a plane configuration structure to return to the user.
2383 *
2384 * Called by the user via ioctl.
2385 *
2386 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002387 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002388 */
2389int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002390 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002391{
2392 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002393 struct drm_plane *plane;
2394 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002395
2396 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2397 return -EINVAL;
2398
Rob Clarka2b34e22013-10-05 16:36:52 -04002399 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002400 if (!plane)
2401 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002402
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002403 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002404 if (plane->crtc)
2405 plane_resp->crtc_id = plane->crtc->base.id;
2406 else
2407 plane_resp->crtc_id = 0;
2408
2409 if (plane->fb)
2410 plane_resp->fb_id = plane->fb->base.id;
2411 else
2412 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002413 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414
2415 plane_resp->plane_id = plane->base.id;
2416 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002417 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002418
2419 /*
2420 * This ioctl is called twice, once to determine how much space is
2421 * needed, and the 2nd time to fill it.
2422 */
2423 if (plane->format_count &&
2424 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002425 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002426 if (copy_to_user(format_ptr,
2427 plane->format_types,
2428 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002429 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002430 }
2431 }
2432 plane_resp->count_format_types = plane->format_count;
2433
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002434 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002435}
2436
Laurent Pinchartead86102015-03-05 02:25:43 +02002437/**
2438 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2439 * @plane: plane to check for format support
2440 * @format: the pixel format
2441 *
2442 * Returns:
2443 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2444 * otherwise.
2445 */
2446int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2447{
2448 unsigned int i;
2449
2450 for (i = 0; i < plane->format_count; i++) {
2451 if (format == plane->format_types[i])
2452 return 0;
2453 }
2454
2455 return -EINVAL;
2456}
2457
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002458static int check_src_coords(uint32_t src_x, uint32_t src_y,
2459 uint32_t src_w, uint32_t src_h,
2460 const struct drm_framebuffer *fb)
2461{
2462 unsigned int fb_width, fb_height;
2463
2464 fb_width = fb->width << 16;
2465 fb_height = fb->height << 16;
2466
2467 /* Make sure source coordinates are inside the fb. */
2468 if (src_w > fb_width ||
2469 src_x > fb_width - src_w ||
2470 src_h > fb_height ||
2471 src_y > fb_height - src_h) {
2472 DRM_DEBUG_KMS("Invalid source coordinates "
2473 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2474 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2475 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2476 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2477 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2478 return -ENOSPC;
2479 }
2480
2481 return 0;
2482}
2483
Matt Roperb36552b2014-06-10 08:28:09 -07002484/*
2485 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002486 *
Matt Roperb36552b2014-06-10 08:28:09 -07002487 * Note that we assume an extra reference has already been taken on fb. If the
2488 * update fails, this reference will be dropped before return; if it succeeds,
2489 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002490 *
Matt Roperb36552b2014-06-10 08:28:09 -07002491 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002492 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002493static int __setplane_internal(struct drm_plane *plane,
2494 struct drm_crtc *crtc,
2495 struct drm_framebuffer *fb,
2496 int32_t crtc_x, int32_t crtc_y,
2497 uint32_t crtc_w, uint32_t crtc_h,
2498 /* src_{x,y,w,h} values are 16.16 fixed point */
2499 uint32_t src_x, uint32_t src_y,
2500 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002501{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 int ret = 0;
2503
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002504 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002505 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002506 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002507 ret = plane->funcs->disable_plane(plane);
2508 if (!ret) {
2509 plane->crtc = NULL;
2510 plane->fb = NULL;
2511 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002512 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002513 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002514 goto out;
2515 }
2516
Matt Roper7f994f32014-05-29 08:06:51 -07002517 /* Check whether this plane is usable on this CRTC */
2518 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2519 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2520 ret = -EINVAL;
2521 goto out;
2522 }
2523
Ville Syrjälä62443be2011-12-20 00:06:47 +02002524 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002525 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2526 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002527 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2528 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002529 goto out;
2530 }
2531
Matt Roper3968be92015-04-13 11:06:13 -07002532 /* Give drivers some help against integer overflows */
2533 if (crtc_w > INT_MAX ||
2534 crtc_x > INT_MAX - (int32_t) crtc_w ||
2535 crtc_h > INT_MAX ||
2536 crtc_y > INT_MAX - (int32_t) crtc_h) {
2537 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2538 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002539 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002540 goto out;
2541 }
2542
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002543 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2544 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002545 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002546
Daniel Vetter3d30a592014-07-27 13:42:42 +02002547 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002548 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002549 crtc_x, crtc_y, crtc_w, crtc_h,
2550 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002551 if (!ret) {
2552 plane->crtc = crtc;
2553 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002554 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002555 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002556 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002557 }
2558
2559out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002560 if (fb)
2561 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002562 if (plane->old_fb)
2563 drm_framebuffer_unreference(plane->old_fb);
2564 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002565
2566 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002567}
Matt Roperb36552b2014-06-10 08:28:09 -07002568
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002569static int setplane_internal(struct drm_plane *plane,
2570 struct drm_crtc *crtc,
2571 struct drm_framebuffer *fb,
2572 int32_t crtc_x, int32_t crtc_y,
2573 uint32_t crtc_w, uint32_t crtc_h,
2574 /* src_{x,y,w,h} values are 16.16 fixed point */
2575 uint32_t src_x, uint32_t src_y,
2576 uint32_t src_w, uint32_t src_h)
2577{
2578 int ret;
2579
2580 drm_modeset_lock_all(plane->dev);
2581 ret = __setplane_internal(plane, crtc, fb,
2582 crtc_x, crtc_y, crtc_w, crtc_h,
2583 src_x, src_y, src_w, src_h);
2584 drm_modeset_unlock_all(plane->dev);
2585
2586 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002587}
2588
2589/**
2590 * drm_mode_setplane - configure a plane's configuration
2591 * @dev: DRM device
2592 * @data: ioctl data*
2593 * @file_priv: DRM file info
2594 *
2595 * Set plane configuration, including placement, fb, scaling, and other factors.
2596 * Or pass a NULL fb to disable (planes may be disabled without providing a
2597 * valid crtc).
2598 *
2599 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002600 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002601 */
2602int drm_mode_setplane(struct drm_device *dev, void *data,
2603 struct drm_file *file_priv)
2604{
2605 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002606 struct drm_plane *plane;
2607 struct drm_crtc *crtc = NULL;
2608 struct drm_framebuffer *fb = NULL;
2609
2610 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2611 return -EINVAL;
2612
Matt Roperb36552b2014-06-10 08:28:09 -07002613 /*
2614 * First, find the plane, crtc, and fb objects. If not available,
2615 * we don't bother to call the driver.
2616 */
Rob Clark933f6222014-11-25 20:33:11 -05002617 plane = drm_plane_find(dev, plane_req->plane_id);
2618 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002619 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2620 plane_req->plane_id);
2621 return -ENOENT;
2622 }
Matt Roperb36552b2014-06-10 08:28:09 -07002623
2624 if (plane_req->fb_id) {
2625 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2626 if (!fb) {
2627 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2628 plane_req->fb_id);
2629 return -ENOENT;
2630 }
2631
Rob Clark933f6222014-11-25 20:33:11 -05002632 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2633 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002634 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2635 plane_req->crtc_id);
2636 return -ENOENT;
2637 }
Matt Roperb36552b2014-06-10 08:28:09 -07002638 }
2639
Matt Roper161d0dc2014-06-10 08:28:10 -07002640 /*
2641 * setplane_internal will take care of deref'ing either the old or new
2642 * framebuffer depending on success.
2643 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002644 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002645 plane_req->crtc_x, plane_req->crtc_y,
2646 plane_req->crtc_w, plane_req->crtc_h,
2647 plane_req->src_x, plane_req->src_y,
2648 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002649}
2650
2651/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002652 * drm_mode_set_config_internal - helper to call ->set_config
2653 * @set: modeset config to set
2654 *
2655 * This is a little helper to wrap internal calls to the ->set_config driver
2656 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002657 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002658 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002659 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002660 */
2661int drm_mode_set_config_internal(struct drm_mode_set *set)
2662{
2663 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002664 struct drm_framebuffer *fb;
2665 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002666 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002667
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002668 /*
2669 * NOTE: ->set_config can also disable other crtcs (if we steal all
2670 * connectors from it), hence we need to refcount the fbs across all
2671 * crtcs. Atomic modeset will have saner semantics ...
2672 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002673 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002674 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002675
Daniel Vetterb0d12322012-12-11 01:07:12 +01002676 fb = set->fb;
2677
2678 ret = crtc->funcs->set_config(set);
2679 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002680 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002681 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002682 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002683
Daniel Vettere4f62542015-07-09 23:44:35 +02002684 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002685 if (tmp->primary->fb)
2686 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002687 if (tmp->primary->old_fb)
2688 drm_framebuffer_unreference(tmp->primary->old_fb);
2689 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002690 }
2691
2692 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002693}
2694EXPORT_SYMBOL(drm_mode_set_config_internal);
2695
Matt Roperaf936292014-04-01 15:22:34 -07002696/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002697 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2698 * @mode: mode to query
2699 * @hdisplay: hdisplay value to fill in
2700 * @vdisplay: vdisplay value to fill in
2701 *
2702 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2703 * the appropriate layout.
2704 */
2705void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2706 int *hdisplay, int *vdisplay)
2707{
2708 struct drm_display_mode adjusted;
2709
2710 drm_mode_copy(&adjusted, mode);
2711 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2712 *hdisplay = adjusted.crtc_hdisplay;
2713 *vdisplay = adjusted.crtc_vdisplay;
2714}
2715EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2716
2717/**
Matt Roperaf936292014-04-01 15:22:34 -07002718 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2719 * CRTC viewport
2720 * @crtc: CRTC that framebuffer will be displayed on
2721 * @x: x panning
2722 * @y: y panning
2723 * @mode: mode that framebuffer will be displayed under
2724 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002725 */
Matt Roperaf936292014-04-01 15:22:34 -07002726int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2727 int x, int y,
2728 const struct drm_display_mode *mode,
2729 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002730
2731{
2732 int hdisplay, vdisplay;
2733
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002734 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002735
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002736 if (crtc->state &&
2737 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2738 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002739 swap(hdisplay, vdisplay);
2740
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002741 return check_src_coords(x << 16, y << 16,
2742 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002743}
Matt Roperaf936292014-04-01 15:22:34 -07002744EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002745
Daniel Vetter2d13b672012-12-11 13:47:23 +01002746/**
Dave Airlief453ba02008-11-07 14:05:41 -08002747 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002748 * @dev: drm device for the ioctl
2749 * @data: data pointer for the ioctl
2750 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002751 *
Dave Airlief453ba02008-11-07 14:05:41 -08002752 * Build a new CRTC configuration based on user request.
2753 *
2754 * Called by the user via ioctl.
2755 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002756 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002757 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002758 */
2759int drm_mode_setcrtc(struct drm_device *dev, void *data,
2760 struct drm_file *file_priv)
2761{
2762 struct drm_mode_config *config = &dev->mode_config;
2763 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002764 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002765 struct drm_connector **connector_set = NULL, *connector;
2766 struct drm_framebuffer *fb = NULL;
2767 struct drm_display_mode *mode = NULL;
2768 struct drm_mode_set set;
2769 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002770 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002771 int i;
2772
Dave Airliefb3b06c2011-02-08 13:55:21 +10002773 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2774 return -EINVAL;
2775
Zhao Junwang01447e92015-07-07 17:08:35 +08002776 /*
2777 * Universal plane src offsets are only 16.16, prevent havoc for
2778 * drivers using universal plane code internally.
2779 */
2780 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002781 return -ERANGE;
2782
Daniel Vetter84849902012-12-02 00:28:11 +01002783 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002784 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2785 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002786 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002787 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002788 goto out;
2789 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002790 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002791
2792 if (crtc_req->mode_valid) {
2793 /* If we have a mode we need a framebuffer. */
2794 /* If we pass -1, set the mode with the currently bound fb */
2795 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002796 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002797 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2798 ret = -EINVAL;
2799 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002800 }
Matt Roperf4510a22014-04-01 15:22:40 -07002801 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002802 /* Make refcounting symmetric with the lookup path. */
2803 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002804 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002805 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2806 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002807 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2808 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002809 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002810 goto out;
2811 }
Dave Airlief453ba02008-11-07 14:05:41 -08002812 }
2813
2814 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002815 if (!mode) {
2816 ret = -ENOMEM;
2817 goto out;
2818 }
2819
Daniel Stone934a8a82015-05-22 13:34:48 +01002820 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002821 if (ret) {
2822 DRM_DEBUG_KMS("Invalid mode\n");
2823 goto out;
2824 }
2825
Dave Airlief453ba02008-11-07 14:05:41 -08002826 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002827
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002828 /*
2829 * Check whether the primary plane supports the fb pixel format.
2830 * Drivers not implementing the universal planes API use a
2831 * default formats list provided by the DRM core which doesn't
2832 * match real hardware capabilities. Skip the check in that
2833 * case.
2834 */
2835 if (!crtc->primary->format_default) {
2836 ret = drm_plane_check_pixel_format(crtc->primary,
2837 fb->pixel_format);
2838 if (ret) {
2839 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2840 drm_get_format_name(fb->pixel_format));
2841 goto out;
2842 }
2843 }
2844
Damien Lespiauc11e9282013-09-25 16:45:30 +01002845 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2846 mode, fb);
2847 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002848 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002849
Dave Airlief453ba02008-11-07 14:05:41 -08002850 }
2851
2852 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002853 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002854 ret = -EINVAL;
2855 goto out;
2856 }
2857
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002858 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002859 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002860 crtc_req->count_connectors);
2861 ret = -EINVAL;
2862 goto out;
2863 }
2864
2865 if (crtc_req->count_connectors > 0) {
2866 u32 out_id;
2867
2868 /* Avoid unbounded kernel memory allocation */
2869 if (crtc_req->count_connectors > config->num_connector) {
2870 ret = -EINVAL;
2871 goto out;
2872 }
2873
Thierry Reding2f6c5382014-12-10 13:03:36 +01002874 connector_set = kmalloc_array(crtc_req->count_connectors,
2875 sizeof(struct drm_connector *),
2876 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002877 if (!connector_set) {
2878 ret = -ENOMEM;
2879 goto out;
2880 }
2881
2882 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002883 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002884 if (get_user(out_id, &set_connectors_ptr[i])) {
2885 ret = -EFAULT;
2886 goto out;
2887 }
2888
Rob Clarka2b34e22013-10-05 16:36:52 -04002889 connector = drm_connector_find(dev, out_id);
2890 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002891 DRM_DEBUG_KMS("Connector id %d unknown\n",
2892 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002893 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002894 goto out;
2895 }
Jerome Glisse94401062010-07-15 15:43:25 -04002896 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2897 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002898 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002899
2900 connector_set[i] = connector;
2901 }
2902 }
2903
2904 set.crtc = crtc;
2905 set.x = crtc_req->x;
2906 set.y = crtc_req->y;
2907 set.mode = mode;
2908 set.connectors = connector_set;
2909 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002910 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002911 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002912
2913out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002914 if (fb)
2915 drm_framebuffer_unreference(fb);
2916
Dave Airlief453ba02008-11-07 14:05:41 -08002917 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002918 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002919 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002920 return ret;
2921}
2922
Matt Roper161d0dc2014-06-10 08:28:10 -07002923/**
2924 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2925 * universal plane handler call
2926 * @crtc: crtc to update cursor for
2927 * @req: data pointer for the ioctl
2928 * @file_priv: drm file for the ioctl call
2929 *
2930 * Legacy cursor ioctl's work directly with driver buffer handles. To
2931 * translate legacy ioctl calls into universal plane handler calls, we need to
2932 * wrap the native buffer handle in a drm_framebuffer.
2933 *
2934 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2935 * buffer with a pitch of 4*width; the universal plane interface should be used
2936 * directly in cases where the hardware can support other buffer settings and
2937 * userspace wants to make use of these capabilities.
2938 *
2939 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002940 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002941 */
2942static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2943 struct drm_mode_cursor2 *req,
2944 struct drm_file *file_priv)
2945{
2946 struct drm_device *dev = crtc->dev;
2947 struct drm_framebuffer *fb = NULL;
2948 struct drm_mode_fb_cmd2 fbreq = {
2949 .width = req->width,
2950 .height = req->height,
2951 .pixel_format = DRM_FORMAT_ARGB8888,
2952 .pitches = { req->width * 4 },
2953 .handles = { req->handle },
2954 };
2955 int32_t crtc_x, crtc_y;
2956 uint32_t crtc_w = 0, crtc_h = 0;
2957 uint32_t src_w = 0, src_h = 0;
2958 int ret = 0;
2959
2960 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002961 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002962
2963 /*
2964 * Obtain fb we'll be using (either new or existing) and take an extra
2965 * reference to it if fb != null. setplane will take care of dropping
2966 * the reference if the plane update fails.
2967 */
2968 if (req->flags & DRM_MODE_CURSOR_BO) {
2969 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002970 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002971 if (IS_ERR(fb)) {
2972 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2973 return PTR_ERR(fb);
2974 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002975 } else {
2976 fb = NULL;
2977 }
2978 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002979 fb = crtc->cursor->fb;
2980 if (fb)
2981 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002982 }
2983
2984 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2985 crtc_x = req->x;
2986 crtc_y = req->y;
2987 } else {
2988 crtc_x = crtc->cursor_x;
2989 crtc_y = crtc->cursor_y;
2990 }
2991
2992 if (fb) {
2993 crtc_w = fb->width;
2994 crtc_h = fb->height;
2995 src_w = fb->width << 16;
2996 src_h = fb->height << 16;
2997 }
2998
2999 /*
3000 * setplane_internal will take care of deref'ing either the old or new
3001 * framebuffer depending on success.
3002 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003003 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003004 crtc_x, crtc_y, crtc_w, crtc_h,
3005 0, 0, src_w, src_h);
3006
3007 /* Update successful; save new cursor position, if necessary */
3008 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3009 crtc->cursor_x = req->x;
3010 crtc->cursor_y = req->y;
3011 }
3012
3013 return ret;
3014}
3015
Dave Airlie4c813d42013-06-20 11:48:52 +10003016static int drm_mode_cursor_common(struct drm_device *dev,
3017 struct drm_mode_cursor2 *req,
3018 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003019{
Dave Airlief453ba02008-11-07 14:05:41 -08003020 struct drm_crtc *crtc;
3021 int ret = 0;
3022
Dave Airliefb3b06c2011-02-08 13:55:21 +10003023 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3024 return -EINVAL;
3025
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003026 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003027 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003028
Rob Clarka2b34e22013-10-05 16:36:52 -04003029 crtc = drm_crtc_find(dev, req->crtc_id);
3030 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003031 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003032 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003033 }
Dave Airlief453ba02008-11-07 14:05:41 -08003034
Matt Roper161d0dc2014-06-10 08:28:10 -07003035 /*
3036 * If this crtc has a universal cursor plane, call that plane's update
3037 * handler rather than using legacy cursor handlers.
3038 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003039 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003040 if (crtc->cursor) {
3041 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3042 goto out;
3043 }
3044
Dave Airlief453ba02008-11-07 14:05:41 -08003045 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003046 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003047 ret = -ENXIO;
3048 goto out;
3049 }
3050 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003051 if (crtc->funcs->cursor_set2)
3052 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3053 req->width, req->height, req->hot_x, req->hot_y);
3054 else
3055 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3056 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003057 }
3058
3059 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3060 if (crtc->funcs->cursor_move) {
3061 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3062 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003063 ret = -EFAULT;
3064 goto out;
3065 }
3066 }
3067out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003068 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003069
Dave Airlief453ba02008-11-07 14:05:41 -08003070 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003071
3072}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003073
3074
3075/**
3076 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3077 * @dev: drm device for the ioctl
3078 * @data: data pointer for the ioctl
3079 * @file_priv: drm file for the ioctl call
3080 *
3081 * Set the cursor configuration based on user request.
3082 *
3083 * Called by the user via ioctl.
3084 *
3085 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003086 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003087 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003088int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003089 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003090{
3091 struct drm_mode_cursor *req = data;
3092 struct drm_mode_cursor2 new_req;
3093
3094 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3095 new_req.hot_x = new_req.hot_y = 0;
3096
3097 return drm_mode_cursor_common(dev, &new_req, file_priv);
3098}
3099
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003100/**
3101 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3102 * @dev: drm device for the ioctl
3103 * @data: data pointer for the ioctl
3104 * @file_priv: drm file for the ioctl call
3105 *
3106 * Set the cursor configuration based on user request. This implements the 2nd
3107 * version of the cursor ioctl, which allows userspace to additionally specify
3108 * the hotspot of the pointer.
3109 *
3110 * Called by the user via ioctl.
3111 *
3112 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003113 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003114 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003115int drm_mode_cursor2_ioctl(struct drm_device *dev,
3116 void *data, struct drm_file *file_priv)
3117{
3118 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003119
Dave Airlie4c813d42013-06-20 11:48:52 +10003120 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003121}
3122
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003123/**
3124 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3125 * @bpp: bits per pixels
3126 * @depth: bit depth per pixel
3127 *
3128 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3129 * Useful in fbdev emulation code, since that deals in those values.
3130 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003131uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3132{
3133 uint32_t fmt;
3134
3135 switch (bpp) {
3136 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003137 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003138 break;
3139 case 16:
3140 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003141 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003142 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003143 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003144 break;
3145 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003146 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003147 break;
3148 case 32:
3149 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003150 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003151 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003152 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003153 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003154 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003155 break;
3156 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003157 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3158 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003159 break;
3160 }
3161
3162 return fmt;
3163}
3164EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3165
Dave Airlief453ba02008-11-07 14:05:41 -08003166/**
3167 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003168 * @dev: drm device for the ioctl
3169 * @data: data pointer for the ioctl
3170 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003171 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003172 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003173 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003174 *
3175 * Called by the user via ioctl.
3176 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003177 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003178 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003179 */
3180int drm_mode_addfb(struct drm_device *dev,
3181 void *data, struct drm_file *file_priv)
3182{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003183 struct drm_mode_fb_cmd *or = data;
3184 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003185 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003186
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003187 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003188 r.fb_id = or->fb_id;
3189 r.width = or->width;
3190 r.height = or->height;
3191 r.pitches[0] = or->pitch;
3192 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3193 r.handles[0] = or->handle;
3194
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003195 ret = drm_mode_addfb2(dev, &r, file_priv);
3196 if (ret)
3197 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003198
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003199 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003200
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003201 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003202}
3203
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003204static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003205{
3206 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3207
3208 switch (format) {
3209 case DRM_FORMAT_C8:
3210 case DRM_FORMAT_RGB332:
3211 case DRM_FORMAT_BGR233:
3212 case DRM_FORMAT_XRGB4444:
3213 case DRM_FORMAT_XBGR4444:
3214 case DRM_FORMAT_RGBX4444:
3215 case DRM_FORMAT_BGRX4444:
3216 case DRM_FORMAT_ARGB4444:
3217 case DRM_FORMAT_ABGR4444:
3218 case DRM_FORMAT_RGBA4444:
3219 case DRM_FORMAT_BGRA4444:
3220 case DRM_FORMAT_XRGB1555:
3221 case DRM_FORMAT_XBGR1555:
3222 case DRM_FORMAT_RGBX5551:
3223 case DRM_FORMAT_BGRX5551:
3224 case DRM_FORMAT_ARGB1555:
3225 case DRM_FORMAT_ABGR1555:
3226 case DRM_FORMAT_RGBA5551:
3227 case DRM_FORMAT_BGRA5551:
3228 case DRM_FORMAT_RGB565:
3229 case DRM_FORMAT_BGR565:
3230 case DRM_FORMAT_RGB888:
3231 case DRM_FORMAT_BGR888:
3232 case DRM_FORMAT_XRGB8888:
3233 case DRM_FORMAT_XBGR8888:
3234 case DRM_FORMAT_RGBX8888:
3235 case DRM_FORMAT_BGRX8888:
3236 case DRM_FORMAT_ARGB8888:
3237 case DRM_FORMAT_ABGR8888:
3238 case DRM_FORMAT_RGBA8888:
3239 case DRM_FORMAT_BGRA8888:
3240 case DRM_FORMAT_XRGB2101010:
3241 case DRM_FORMAT_XBGR2101010:
3242 case DRM_FORMAT_RGBX1010102:
3243 case DRM_FORMAT_BGRX1010102:
3244 case DRM_FORMAT_ARGB2101010:
3245 case DRM_FORMAT_ABGR2101010:
3246 case DRM_FORMAT_RGBA1010102:
3247 case DRM_FORMAT_BGRA1010102:
3248 case DRM_FORMAT_YUYV:
3249 case DRM_FORMAT_YVYU:
3250 case DRM_FORMAT_UYVY:
3251 case DRM_FORMAT_VYUY:
3252 case DRM_FORMAT_AYUV:
3253 case DRM_FORMAT_NV12:
3254 case DRM_FORMAT_NV21:
3255 case DRM_FORMAT_NV16:
3256 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003257 case DRM_FORMAT_NV24:
3258 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003259 case DRM_FORMAT_YUV410:
3260 case DRM_FORMAT_YVU410:
3261 case DRM_FORMAT_YUV411:
3262 case DRM_FORMAT_YVU411:
3263 case DRM_FORMAT_YUV420:
3264 case DRM_FORMAT_YVU420:
3265 case DRM_FORMAT_YUV422:
3266 case DRM_FORMAT_YVU422:
3267 case DRM_FORMAT_YUV444:
3268 case DRM_FORMAT_YVU444:
3269 return 0;
3270 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003271 DRM_DEBUG_KMS("invalid pixel format %s\n",
3272 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003273 return -EINVAL;
3274 }
3275}
3276
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003277static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003278{
3279 int ret, hsub, vsub, num_planes, i;
3280
3281 ret = format_check(r);
3282 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003283 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3284 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003285 return ret;
3286 }
3287
3288 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3289 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3290 num_planes = drm_format_num_planes(r->pixel_format);
3291
3292 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003293 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003294 return -EINVAL;
3295 }
3296
3297 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003298 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003299 return -EINVAL;
3300 }
3301
3302 for (i = 0; i < num_planes; i++) {
3303 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003304 unsigned int height = r->height / (i != 0 ? vsub : 1);
3305 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003306
3307 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003308 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003309 return -EINVAL;
3310 }
3311
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003312 if ((uint64_t) width * cpp > UINT_MAX)
3313 return -ERANGE;
3314
3315 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3316 return -ERANGE;
3317
3318 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003319 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003320 return -EINVAL;
3321 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003322
3323 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3324 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3325 r->modifier[i], i);
3326 return -EINVAL;
3327 }
Rob Clark570655b2015-01-30 20:18:11 +05303328
3329 /* modifier specific checks: */
3330 switch (r->modifier[i]) {
3331 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3332 /* NOTE: the pitch restriction may be lifted later if it turns
3333 * out that no hw has this restriction:
3334 */
3335 if (r->pixel_format != DRM_FORMAT_NV12 ||
3336 width % 128 || height % 32 ||
3337 r->pitches[i] % 128) {
3338 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3339 return -EINVAL;
3340 }
3341 break;
3342
3343 default:
3344 break;
3345 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003346 }
3347
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003348 for (i = num_planes; i < 4; i++) {
3349 if (r->modifier[i]) {
3350 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3351 return -EINVAL;
3352 }
3353
3354 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3355 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3356 continue;
3357
3358 if (r->handles[i]) {
3359 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3360 return -EINVAL;
3361 }
3362
3363 if (r->pitches[i]) {
3364 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3365 return -EINVAL;
3366 }
3367
3368 if (r->offsets[i]) {
3369 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3370 return -EINVAL;
3371 }
3372 }
3373
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003374 return 0;
3375}
3376
Chris Wilson9a6f5132015-02-25 13:45:26 +00003377static struct drm_framebuffer *
3378internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003379 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003380 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003381{
3382 struct drm_mode_config *config = &dev->mode_config;
3383 struct drm_framebuffer *fb;
3384 int ret;
3385
Rob Clarke3eb3252015-02-05 14:41:52 +00003386 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003387 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3388 return ERR_PTR(-EINVAL);
3389 }
3390
3391 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3392 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3393 r->width, config->min_width, config->max_width);
3394 return ERR_PTR(-EINVAL);
3395 }
3396 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3397 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3398 r->height, config->min_height, config->max_height);
3399 return ERR_PTR(-EINVAL);
3400 }
3401
Rob Clarke3eb3252015-02-05 14:41:52 +00003402 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3403 !dev->mode_config.allow_fb_modifiers) {
3404 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3405 return ERR_PTR(-EINVAL);
3406 }
3407
Matt Roperc394c2b2014-06-10 08:28:08 -07003408 ret = framebuffer_check(r);
3409 if (ret)
3410 return ERR_PTR(ret);
3411
3412 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3413 if (IS_ERR(fb)) {
3414 DRM_DEBUG_KMS("could not create framebuffer\n");
3415 return fb;
3416 }
3417
Matt Roperc394c2b2014-06-10 08:28:08 -07003418 return fb;
3419}
3420
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003421/**
3422 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003423 * @dev: drm device for the ioctl
3424 * @data: data pointer for the ioctl
3425 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003426 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003427 * Add a new FB to the specified CRTC, given a user request with format. This is
3428 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3429 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003430 *
3431 * Called by the user via ioctl.
3432 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003433 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003434 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003435 */
3436int drm_mode_addfb2(struct drm_device *dev,
3437 void *data, struct drm_file *file_priv)
3438{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003439 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003440 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003441
Dave Airliefb3b06c2011-02-08 13:55:21 +10003442 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3443 return -EINVAL;
3444
Chris Wilson9a6f5132015-02-25 13:45:26 +00003445 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003446 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003447 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003448
Chris Wilson9a6f5132015-02-25 13:45:26 +00003449 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003450 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003451
3452 /* Transfer ownership to the filp for reaping on close */
3453 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003454 list_add(&fb->filp_head, &file_priv->fbs);
3455 mutex_unlock(&file_priv->fbs_lock);
3456
Matt Roperc394c2b2014-06-10 08:28:08 -07003457 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003458}
3459
3460/**
3461 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003462 * @dev: drm device for the ioctl
3463 * @data: data pointer for the ioctl
3464 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003465 *
Dave Airlief453ba02008-11-07 14:05:41 -08003466 * Remove the FB specified by the user.
3467 *
3468 * Called by the user via ioctl.
3469 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003470 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003471 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003472 */
3473int drm_mode_rmfb(struct drm_device *dev,
3474 void *data, struct drm_file *file_priv)
3475{
Dave Airlief453ba02008-11-07 14:05:41 -08003476 struct drm_framebuffer *fb = NULL;
3477 struct drm_framebuffer *fbl = NULL;
3478 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003479 int found = 0;
3480
Dave Airliefb3b06c2011-02-08 13:55:21 +10003481 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3482 return -EINVAL;
3483
Dave Airlie72fe90b2016-04-15 15:10:40 +10003484 fb = drm_framebuffer_lookup(dev, *id);
3485 if (!fb)
3486 return -ENOENT;
3487
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003488 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003489 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3490 if (fb == fbl)
3491 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003492 if (!found) {
3493 mutex_unlock(&file_priv->fbs_lock);
3494 goto fail_unref;
3495 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003496
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003497 list_del_init(&fb->filp_head);
3498 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003499
Dave Airlie72fe90b2016-04-15 15:10:40 +10003500 /* we now own the reference that was stored in the fbs list */
3501 drm_framebuffer_unreference(fb);
3502
3503 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003504 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003505
Daniel Vetter2b677e82012-12-10 21:16:05 +01003506 return 0;
3507
Dave Airlie72fe90b2016-04-15 15:10:40 +10003508fail_unref:
3509 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003510 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003511}
3512
3513/**
3514 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003515 * @dev: drm device for the ioctl
3516 * @data: data pointer for the ioctl
3517 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003518 *
Dave Airlief453ba02008-11-07 14:05:41 -08003519 * Lookup the FB given its ID and return info about it.
3520 *
3521 * Called by the user via ioctl.
3522 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003523 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003524 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003525 */
3526int drm_mode_getfb(struct drm_device *dev,
3527 void *data, struct drm_file *file_priv)
3528{
3529 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003530 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003531 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003532
Dave Airliefb3b06c2011-02-08 13:55:21 +10003533 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3534 return -EINVAL;
3535
Daniel Vetter786b99e2012-12-02 21:53:40 +01003536 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003537 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003538 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003539
3540 r->height = fb->height;
3541 r->width = fb->width;
3542 r->depth = fb->depth;
3543 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003544 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003545 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003546 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003547 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003548 ret = fb->funcs->create_handle(fb, file_priv,
3549 &r->handle);
3550 } else {
3551 /* GET_FB() is an unprivileged ioctl so we must not
3552 * return a buffer-handle to non-master processes! For
3553 * backwards-compatibility reasons, we cannot make
3554 * GET_FB() privileged, so just return an invalid handle
3555 * for non-masters. */
3556 r->handle = 0;
3557 ret = 0;
3558 }
3559 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003560 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003561 }
Dave Airlief453ba02008-11-07 14:05:41 -08003562
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003563 drm_framebuffer_unreference(fb);
3564
Dave Airlief453ba02008-11-07 14:05:41 -08003565 return ret;
3566}
3567
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003568/**
3569 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3570 * @dev: drm device for the ioctl
3571 * @data: data pointer for the ioctl
3572 * @file_priv: drm file for the ioctl call
3573 *
3574 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3575 * rectangle list. Generic userspace which does frontbuffer rendering must call
3576 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3577 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3578 *
3579 * Modesetting drivers which always update the frontbuffer do not need to
3580 * implement the corresponding ->dirty framebuffer callback.
3581 *
3582 * Called by the user via ioctl.
3583 *
3584 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003585 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003586 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003587int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3588 void *data, struct drm_file *file_priv)
3589{
3590 struct drm_clip_rect __user *clips_ptr;
3591 struct drm_clip_rect *clips = NULL;
3592 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003593 struct drm_framebuffer *fb;
3594 unsigned flags;
3595 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003596 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003597
Dave Airliefb3b06c2011-02-08 13:55:21 +10003598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3599 return -EINVAL;
3600
Daniel Vetter786b99e2012-12-02 21:53:40 +01003601 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003602 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003603 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003604
3605 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003606 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003607
3608 if (!num_clips != !clips_ptr) {
3609 ret = -EINVAL;
3610 goto out_err1;
3611 }
3612
3613 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3614
3615 /* If userspace annotates copy, clips must come in pairs */
3616 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3617 ret = -EINVAL;
3618 goto out_err1;
3619 }
3620
3621 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003622 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3623 ret = -EINVAL;
3624 goto out_err1;
3625 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003626 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003627 if (!clips) {
3628 ret = -ENOMEM;
3629 goto out_err1;
3630 }
3631
3632 ret = copy_from_user(clips, clips_ptr,
3633 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003634 if (ret) {
3635 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003636 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003637 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003638 }
3639
3640 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003641 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3642 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003643 } else {
3644 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003645 }
3646
3647out_err2:
3648 kfree(clips);
3649out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003650 drm_framebuffer_unreference(fb);
3651
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003652 return ret;
3653}
3654
3655
Dave Airlief453ba02008-11-07 14:05:41 -08003656/**
3657 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003658 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003659 *
Dave Airlief453ba02008-11-07 14:05:41 -08003660 * Destroy all the FBs associated with @filp.
3661 *
3662 * Called by the user via ioctl.
3663 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003664 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003665 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003666 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003667void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003668{
Dave Airlief453ba02008-11-07 14:05:41 -08003669 struct drm_framebuffer *fb, *tfb;
3670
Daniel Vetter1b116292014-09-24 21:51:06 +02003671 /*
3672 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003673 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003674 * avoid upsetting lockdep since the universal cursor code adds a
3675 * framebuffer while holding mutex locks.
3676 *
3677 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3678 * locks is impossible here since no one else but this function can get
3679 * at it any more.
3680 */
Dave Airlief453ba02008-11-07 14:05:41 -08003681 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003682 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003683
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003684 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003685 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003686 }
Dave Airlief453ba02008-11-07 14:05:41 -08003687}
3688
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003689/**
3690 * drm_property_create - create a new property type
3691 * @dev: drm device
3692 * @flags: flags specifying the property type
3693 * @name: name of the property
3694 * @num_values: number of pre-defined values
3695 *
3696 * This creates a new generic drm property which can then be attached to a drm
3697 * object with drm_object_attach_property. The returned property object must be
3698 * freed with drm_property_destroy.
3699 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003700 * Note that the DRM core keeps a per-device list of properties and that, if
3701 * drm_mode_config_cleanup() is called, it will destroy all properties created
3702 * by the driver.
3703 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003704 * Returns:
3705 * A pointer to the newly created property on success, NULL on failure.
3706 */
Dave Airlief453ba02008-11-07 14:05:41 -08003707struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3708 const char *name, int num_values)
3709{
3710 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003711 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003712
3713 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3714 if (!property)
3715 return NULL;
3716
Rob Clark98f75de2014-05-30 11:37:03 -04003717 property->dev = dev;
3718
Dave Airlief453ba02008-11-07 14:05:41 -08003719 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003720 property->values = kcalloc(num_values, sizeof(uint64_t),
3721 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003722 if (!property->values)
3723 goto fail;
3724 }
3725
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003726 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3727 if (ret)
3728 goto fail;
3729
Dave Airlief453ba02008-11-07 14:05:41 -08003730 property->flags = flags;
3731 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003732 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003733
Vinson Lee471dd2e2011-11-10 11:55:40 -08003734 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003735 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003736 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3737 }
Dave Airlief453ba02008-11-07 14:05:41 -08003738
3739 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003740
3741 WARN_ON(!drm_property_type_valid(property));
3742
Dave Airlief453ba02008-11-07 14:05:41 -08003743 return property;
3744fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003745 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003746 kfree(property);
3747 return NULL;
3748}
3749EXPORT_SYMBOL(drm_property_create);
3750
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003751/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003752 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003753 * @dev: drm device
3754 * @flags: flags specifying the property type
3755 * @name: name of the property
3756 * @props: enumeration lists with property values
3757 * @num_values: number of pre-defined values
3758 *
3759 * This creates a new generic drm property which can then be attached to a drm
3760 * object with drm_object_attach_property. The returned property object must be
3761 * freed with drm_property_destroy.
3762 *
3763 * Userspace is only allowed to set one of the predefined values for enumeration
3764 * properties.
3765 *
3766 * Returns:
3767 * A pointer to the newly created property on success, NULL on failure.
3768 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003769struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3770 const char *name,
3771 const struct drm_prop_enum_list *props,
3772 int num_values)
3773{
3774 struct drm_property *property;
3775 int i, ret;
3776
3777 flags |= DRM_MODE_PROP_ENUM;
3778
3779 property = drm_property_create(dev, flags, name, num_values);
3780 if (!property)
3781 return NULL;
3782
3783 for (i = 0; i < num_values; i++) {
3784 ret = drm_property_add_enum(property, i,
3785 props[i].type,
3786 props[i].name);
3787 if (ret) {
3788 drm_property_destroy(dev, property);
3789 return NULL;
3790 }
3791 }
3792
3793 return property;
3794}
3795EXPORT_SYMBOL(drm_property_create_enum);
3796
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003797/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003798 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003799 * @dev: drm device
3800 * @flags: flags specifying the property type
3801 * @name: name of the property
3802 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003803 * @num_props: size of the @props array
3804 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003805 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003806 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003807 * object with drm_object_attach_property. The returned property object must be
3808 * freed with drm_property_destroy.
3809 *
3810 * Compared to plain enumeration properties userspace is allowed to set any
3811 * or'ed together combination of the predefined property bitflag values
3812 *
3813 * Returns:
3814 * A pointer to the newly created property on success, NULL on failure.
3815 */
Rob Clark49e27542012-05-17 02:23:26 -06003816struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3817 int flags, const char *name,
3818 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303819 int num_props,
3820 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003821{
3822 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303823 int i, ret, index = 0;
3824 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003825
3826 flags |= DRM_MODE_PROP_BITMASK;
3827
3828 property = drm_property_create(dev, flags, name, num_values);
3829 if (!property)
3830 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303831 for (i = 0; i < num_props; i++) {
3832 if (!(supported_bits & (1ULL << props[i].type)))
3833 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003834
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303835 if (WARN_ON(index >= num_values)) {
3836 drm_property_destroy(dev, property);
3837 return NULL;
3838 }
3839
3840 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003841 props[i].type,
3842 props[i].name);
3843 if (ret) {
3844 drm_property_destroy(dev, property);
3845 return NULL;
3846 }
3847 }
3848
3849 return property;
3850}
3851EXPORT_SYMBOL(drm_property_create_bitmask);
3852
Rob Clarkebc44cf2012-09-12 22:22:31 -05003853static struct drm_property *property_create_range(struct drm_device *dev,
3854 int flags, const char *name,
3855 uint64_t min, uint64_t max)
3856{
3857 struct drm_property *property;
3858
3859 property = drm_property_create(dev, flags, name, 2);
3860 if (!property)
3861 return NULL;
3862
3863 property->values[0] = min;
3864 property->values[1] = max;
3865
3866 return property;
3867}
3868
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003869/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003870 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003871 * @dev: drm device
3872 * @flags: flags specifying the property type
3873 * @name: name of the property
3874 * @min: minimum value of the property
3875 * @max: maximum value of the property
3876 *
3877 * This creates a new generic drm property which can then be attached to a drm
3878 * object with drm_object_attach_property. The returned property object must be
3879 * freed with drm_property_destroy.
3880 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003881 * Userspace is allowed to set any unsigned integer value in the (min, max)
3882 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003883 *
3884 * Returns:
3885 * A pointer to the newly created property on success, NULL on failure.
3886 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003887struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3888 const char *name,
3889 uint64_t min, uint64_t max)
3890{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003891 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3892 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003893}
3894EXPORT_SYMBOL(drm_property_create_range);
3895
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003896/**
3897 * drm_property_create_signed_range - create a new signed ranged property type
3898 * @dev: drm device
3899 * @flags: flags specifying the property type
3900 * @name: name of the property
3901 * @min: minimum value of the property
3902 * @max: maximum value of the property
3903 *
3904 * This creates a new generic drm property which can then be attached to a drm
3905 * object with drm_object_attach_property. The returned property object must be
3906 * freed with drm_property_destroy.
3907 *
3908 * Userspace is allowed to set any signed integer value in the (min, max)
3909 * range inclusive.
3910 *
3911 * Returns:
3912 * A pointer to the newly created property on success, NULL on failure.
3913 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003914struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3915 int flags, const char *name,
3916 int64_t min, int64_t max)
3917{
3918 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3919 name, I642U64(min), I642U64(max));
3920}
3921EXPORT_SYMBOL(drm_property_create_signed_range);
3922
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003923/**
3924 * drm_property_create_object - create a new object property type
3925 * @dev: drm device
3926 * @flags: flags specifying the property type
3927 * @name: name of the property
3928 * @type: object type from DRM_MODE_OBJECT_* defines
3929 *
3930 * This creates a new generic drm property which can then be attached to a drm
3931 * object with drm_object_attach_property. The returned property object must be
3932 * freed with drm_property_destroy.
3933 *
3934 * Userspace is only allowed to set this to any property value of the given
3935 * @type. Only useful for atomic properties, which is enforced.
3936 *
3937 * Returns:
3938 * A pointer to the newly created property on success, NULL on failure.
3939 */
Rob Clark98f75de2014-05-30 11:37:03 -04003940struct drm_property *drm_property_create_object(struct drm_device *dev,
3941 int flags, const char *name, uint32_t type)
3942{
3943 struct drm_property *property;
3944
3945 flags |= DRM_MODE_PROP_OBJECT;
3946
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003947 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3948 return NULL;
3949
Rob Clark98f75de2014-05-30 11:37:03 -04003950 property = drm_property_create(dev, flags, name, 1);
3951 if (!property)
3952 return NULL;
3953
3954 property->values[0] = type;
3955
3956 return property;
3957}
3958EXPORT_SYMBOL(drm_property_create_object);
3959
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003960/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003961 * drm_property_create_bool - create a new boolean property type
3962 * @dev: drm device
3963 * @flags: flags specifying the property type
3964 * @name: name of the property
3965 *
3966 * This creates a new generic drm property which can then be attached to a drm
3967 * object with drm_object_attach_property. The returned property object must be
3968 * freed with drm_property_destroy.
3969 *
3970 * This is implemented as a ranged property with only {0, 1} as valid values.
3971 *
3972 * Returns:
3973 * A pointer to the newly created property on success, NULL on failure.
3974 */
3975struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3976 const char *name)
3977{
3978 return drm_property_create_range(dev, flags, name, 0, 1);
3979}
3980EXPORT_SYMBOL(drm_property_create_bool);
3981
3982/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003983 * drm_property_add_enum - add a possible value to an enumeration property
3984 * @property: enumeration property to change
3985 * @index: index of the new enumeration
3986 * @value: value of the new enumeration
3987 * @name: symbolic name of the new enumeration
3988 *
3989 * This functions adds enumerations to a property.
3990 *
3991 * It's use is deprecated, drivers should use one of the more specific helpers
3992 * to directly create the property with all enumerations already attached.
3993 *
3994 * Returns:
3995 * Zero on success, error code on failure.
3996 */
Dave Airlief453ba02008-11-07 14:05:41 -08003997int drm_property_add_enum(struct drm_property *property, int index,
3998 uint64_t value, const char *name)
3999{
4000 struct drm_property_enum *prop_enum;
4001
Rob Clark5ea22f22014-05-30 11:34:01 -04004002 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4003 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004004 return -EINVAL;
4005
4006 /*
4007 * Bitmask enum properties have the additional constraint of values
4008 * from 0 to 63
4009 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004010 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4011 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004012 return -EINVAL;
4013
Daniel Vetter3758b342014-11-19 18:38:10 +01004014 if (!list_empty(&property->enum_list)) {
4015 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004016 if (prop_enum->value == value) {
4017 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4018 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4019 return 0;
4020 }
4021 }
4022 }
4023
4024 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4025 if (!prop_enum)
4026 return -ENOMEM;
4027
4028 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4029 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4030 prop_enum->value = value;
4031
4032 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004033 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004034 return 0;
4035}
4036EXPORT_SYMBOL(drm_property_add_enum);
4037
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004038/**
4039 * drm_property_destroy - destroy a drm property
4040 * @dev: drm device
4041 * @property: property to destry
4042 *
4043 * This function frees a property including any attached resources like
4044 * enumeration values.
4045 */
Dave Airlief453ba02008-11-07 14:05:41 -08004046void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4047{
4048 struct drm_property_enum *prop_enum, *pt;
4049
Daniel Vetter3758b342014-11-19 18:38:10 +01004050 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004051 list_del(&prop_enum->head);
4052 kfree(prop_enum);
4053 }
4054
4055 if (property->num_values)
4056 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004057 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004058 list_del(&property->head);
4059 kfree(property);
4060}
4061EXPORT_SYMBOL(drm_property_destroy);
4062
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004063/**
4064 * drm_object_attach_property - attach a property to a modeset object
4065 * @obj: drm modeset object
4066 * @property: property to attach
4067 * @init_val: initial value of the property
4068 *
4069 * This attaches the given property to the modeset object with the given initial
4070 * value. Currently this function cannot fail since the properties are stored in
4071 * a statically sized array.
4072 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004073void drm_object_attach_property(struct drm_mode_object *obj,
4074 struct drm_property *property,
4075 uint64_t init_val)
4076{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004077 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004078
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004079 if (count == DRM_OBJECT_MAX_PROPERTY) {
4080 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4081 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4082 "you see this message on the same object type.\n",
4083 obj->type);
4084 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004085 }
4086
Rob Clarkb17cd752014-12-16 18:05:30 -05004087 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004088 obj->properties->values[count] = init_val;
4089 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004090 if (property->flags & DRM_MODE_PROP_ATOMIC)
4091 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004092}
4093EXPORT_SYMBOL(drm_object_attach_property);
4094
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004095/**
4096 * drm_object_property_set_value - set the value of a property
4097 * @obj: drm mode object to set property value for
4098 * @property: property to set
4099 * @val: value the property should be set to
4100 *
4101 * This functions sets a given property on a given object. This function only
4102 * changes the software state of the property, it does not call into the
4103 * driver's ->set_property callback.
4104 *
4105 * Returns:
4106 * Zero on success, error code on failure.
4107 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004108int drm_object_property_set_value(struct drm_mode_object *obj,
4109 struct drm_property *property, uint64_t val)
4110{
4111 int i;
4112
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004113 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004114 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004115 obj->properties->values[i] = val;
4116 return 0;
4117 }
4118 }
4119
4120 return -EINVAL;
4121}
4122EXPORT_SYMBOL(drm_object_property_set_value);
4123
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004124/**
4125 * drm_object_property_get_value - retrieve the value of a property
4126 * @obj: drm mode object to get property value from
4127 * @property: property to retrieve
4128 * @val: storage for the property value
4129 *
4130 * This function retrieves the softare state of the given property for the given
4131 * property. Since there is no driver callback to retrieve the current property
4132 * value this might be out of sync with the hardware, depending upon the driver
4133 * and property.
4134 *
4135 * Returns:
4136 * Zero on success, error code on failure.
4137 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004138int drm_object_property_get_value(struct drm_mode_object *obj,
4139 struct drm_property *property, uint64_t *val)
4140{
4141 int i;
4142
Rob Clark88a48e22014-12-18 16:01:50 -05004143 /* read-only properties bypass atomic mechanism and still store
4144 * their value in obj->properties->values[].. mostly to avoid
4145 * having to deal w/ EDID and similar props in atomic paths:
4146 */
4147 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4148 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4149 return drm_atomic_get_property(obj, property, val);
4150
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004151 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004152 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004153 *val = obj->properties->values[i];
4154 return 0;
4155 }
4156 }
4157
4158 return -EINVAL;
4159}
4160EXPORT_SYMBOL(drm_object_property_get_value);
4161
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004162/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004163 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004164 * @dev: DRM device
4165 * @data: ioctl data
4166 * @file_priv: DRM file info
4167 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004168 * This function retrieves the metadata for a given property, like the different
4169 * possible values for an enum property or the limits for a range property.
4170 *
4171 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004172 *
4173 * Called by the user via ioctl.
4174 *
4175 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004176 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004177 */
Dave Airlief453ba02008-11-07 14:05:41 -08004178int drm_mode_getproperty_ioctl(struct drm_device *dev,
4179 void *data, struct drm_file *file_priv)
4180{
Dave Airlief453ba02008-11-07 14:05:41 -08004181 struct drm_mode_get_property *out_resp = data;
4182 struct drm_property *property;
4183 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004184 int value_count = 0;
4185 int ret = 0, i;
4186 int copied;
4187 struct drm_property_enum *prop_enum;
4188 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004189 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004190
Dave Airliefb3b06c2011-02-08 13:55:21 +10004191 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4192 return -EINVAL;
4193
Daniel Vetter84849902012-12-02 00:28:11 +01004194 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004195 property = drm_property_find(dev, out_resp->prop_id);
4196 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004197 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004198 goto done;
4199 }
Dave Airlief453ba02008-11-07 14:05:41 -08004200
Rob Clark5ea22f22014-05-30 11:34:01 -04004201 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4202 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004203 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004204 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004205 }
4206
4207 value_count = property->num_values;
4208
4209 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4210 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4211 out_resp->flags = property->flags;
4212
4213 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004214 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004215 for (i = 0; i < value_count; i++) {
4216 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4217 ret = -EFAULT;
4218 goto done;
4219 }
4220 }
4221 }
4222 out_resp->count_values = value_count;
4223
Rob Clark5ea22f22014-05-30 11:34:01 -04004224 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4225 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004226 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4227 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004228 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004229 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004230
4231 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4232 ret = -EFAULT;
4233 goto done;
4234 }
4235
4236 if (copy_to_user(&enum_ptr[copied].name,
4237 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4238 ret = -EFAULT;
4239 goto done;
4240 }
4241 copied++;
4242 }
4243 }
4244 out_resp->count_enum_blobs = enum_count;
4245 }
4246
Daniel Vetter3758b342014-11-19 18:38:10 +01004247 /*
4248 * NOTE: The idea seems to have been to use this to read all the blob
4249 * property values. But nothing ever added them to the corresponding
4250 * list, userspace always used the special-purpose get_blob ioctl to
4251 * read the value for a blob property. It also doesn't make a lot of
4252 * sense to return values here when everything else is just metadata for
4253 * the property itself.
4254 */
4255 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4256 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004257done:
Daniel Vetter84849902012-12-02 00:28:11 +01004258 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004259 return ret;
4260}
4261
Daniel Stone99531d92015-05-22 13:34:49 +01004262/**
4263 * drm_property_create_blob - Create new blob property
4264 *
4265 * Creates a new blob property for a specified DRM device, optionally
4266 * copying data.
4267 *
4268 * @dev: DRM device to create property for
4269 * @length: Length to allocate for blob data
4270 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004271 *
4272 * Returns:
4273 * New blob property with a single reference on success, or an ERR_PTR
4274 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004275 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004276struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004277drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004278 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004279{
4280 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004281 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004282
Dan Carpenter9ac09342015-10-29 16:37:54 +03004283 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004284 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004285
4286 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4287 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004288 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004289
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004290 /* This must be explicitly initialised, so we can safely call list_del
4291 * on it in the removal handler, even if it isn't in a file list. */
4292 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004293 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004294 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004295
Daniel Stone99531d92015-05-22 13:34:49 +01004296 if (data)
4297 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004298
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004299 mutex_lock(&dev->mode_config.blob_lock);
4300
4301 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4302 if (ret) {
4303 kfree(blob);
4304 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004305 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004306 }
4307
Daniel Stone6bcacf52015-04-20 19:22:55 +01004308 kref_init(&blob->refcount);
4309
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004310 list_add_tail(&blob->head_global,
4311 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004312
4313 mutex_unlock(&dev->mode_config.blob_lock);
4314
Dave Airlief453ba02008-11-07 14:05:41 -08004315 return blob;
4316}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004317EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004318
Daniel Stone6bcacf52015-04-20 19:22:55 +01004319/**
4320 * drm_property_free_blob - Blob property destructor
4321 *
4322 * Internal free function for blob properties; must not be used directly.
4323 *
Daniel Stonef102c162015-05-22 13:34:44 +01004324 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004325 */
4326static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004327{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004328 struct drm_property_blob *blob =
4329 container_of(kref, struct drm_property_blob, refcount);
4330
4331 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4332
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004333 list_del(&blob->head_global);
4334 list_del(&blob->head_file);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004335 drm_mode_object_unregister(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004336
Dave Airlief453ba02008-11-07 14:05:41 -08004337 kfree(blob);
4338}
4339
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004340/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004341 * drm_property_unreference_blob - Unreference a blob property
4342 *
4343 * Drop a reference on a blob property. May free the object.
4344 *
Daniel Stonef102c162015-05-22 13:34:44 +01004345 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004346 */
4347void drm_property_unreference_blob(struct drm_property_blob *blob)
4348{
4349 struct drm_device *dev;
4350
4351 if (!blob)
4352 return;
4353
4354 dev = blob->dev;
4355
4356 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4357
4358 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4359 &dev->mode_config.blob_lock))
4360 mutex_unlock(&dev->mode_config.blob_lock);
4361 else
4362 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004363}
4364EXPORT_SYMBOL(drm_property_unreference_blob);
4365
4366/**
4367 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4368 *
4369 * Drop a reference on a blob property. May free the object. This must be
4370 * called with blob_lock held.
4371 *
Daniel Stonef102c162015-05-22 13:34:44 +01004372 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004373 */
4374static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4375{
4376 if (!blob)
4377 return;
4378
4379 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4380
4381 kref_put(&blob->refcount, drm_property_free_blob);
4382}
4383
4384/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004385 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4386 * @dev: DRM device
4387 * @file_priv: destroy all blobs owned by this file handle
4388 */
4389void drm_property_destroy_user_blobs(struct drm_device *dev,
4390 struct drm_file *file_priv)
4391{
4392 struct drm_property_blob *blob, *bt;
4393
4394 mutex_lock(&dev->mode_config.blob_lock);
4395
4396 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4397 list_del_init(&blob->head_file);
4398 drm_property_unreference_blob_locked(blob);
4399 }
4400
4401 mutex_unlock(&dev->mode_config.blob_lock);
4402}
4403
4404/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004405 * drm_property_reference_blob - Take a reference on an existing property
4406 *
4407 * Take a new reference on an existing blob property.
4408 *
Daniel Stonef102c162015-05-22 13:34:44 +01004409 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004410 */
4411struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4412{
4413 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4414 kref_get(&blob->refcount);
4415 return blob;
4416}
4417EXPORT_SYMBOL(drm_property_reference_blob);
4418
4419/*
4420 * Like drm_property_lookup_blob, but does not return an additional reference.
4421 * Must be called with blob_lock held.
4422 */
4423static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4424 uint32_t id)
4425{
4426 struct drm_mode_object *obj = NULL;
4427 struct drm_property_blob *blob;
4428
4429 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4430
4431 mutex_lock(&dev->mode_config.idr_mutex);
4432 obj = idr_find(&dev->mode_config.crtc_idr, id);
4433 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4434 blob = NULL;
4435 else
4436 blob = obj_to_blob(obj);
4437 mutex_unlock(&dev->mode_config.idr_mutex);
4438
Dave Airlief453ba02008-11-07 14:05:41 -08004439 return blob;
4440}
4441
Daniel Stone6bcacf52015-04-20 19:22:55 +01004442/**
4443 * drm_property_lookup_blob - look up a blob property and take a reference
4444 * @dev: drm device
4445 * @id: id of the blob property
4446 *
4447 * If successful, this takes an additional reference to the blob property.
4448 * callers need to make sure to eventually unreference the returned property
4449 * again, using @drm_property_unreference_blob.
4450 */
4451struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4452 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004453{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004454 struct drm_property_blob *blob;
4455
4456 mutex_lock(&dev->mode_config.blob_lock);
4457 blob = __drm_property_lookup_blob(dev, id);
4458 if (blob) {
4459 if (!kref_get_unless_zero(&blob->refcount))
4460 blob = NULL;
4461 }
4462 mutex_unlock(&dev->mode_config.blob_lock);
4463
4464 return blob;
4465}
4466EXPORT_SYMBOL(drm_property_lookup_blob);
4467
4468/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004469 * drm_property_replace_global_blob - atomically replace existing blob property
4470 * @dev: drm device
4471 * @replace: location of blob property pointer to be replaced
4472 * @length: length of data for new blob, or 0 for no data
4473 * @data: content for new blob, or NULL for no data
4474 * @obj_holds_id: optional object for property holding blob ID
4475 * @prop_holds_id: optional property holding blob ID
4476 * @return 0 on success or error on failure
4477 *
4478 * This function will atomically replace a global property in the blob list,
4479 * optionally updating a property which holds the ID of that property. It is
4480 * guaranteed to be atomic: no caller will be allowed to see intermediate
4481 * results, and either the entire operation will succeed and clean up the
4482 * previous property, or it will fail and the state will be unchanged.
4483 *
4484 * If length is 0 or data is NULL, no new blob will be created, and the holding
4485 * property, if specified, will be set to 0.
4486 *
4487 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4488 * by holding the relevant modesetting object lock for its parent.
4489 *
4490 * For example, a drm_connector has a 'PATH' property, which contains the ID
4491 * of a blob property with the value of the MST path information. Calling this
4492 * function with replace pointing to the connector's path_blob_ptr, length and
4493 * data set for the new path information, obj_holds_id set to the connector's
4494 * base object, and prop_holds_id set to the path property name, will perform
4495 * a completely atomic update. The access to path_blob_ptr is protected by the
4496 * caller holding a lock on the connector.
4497 */
4498static int drm_property_replace_global_blob(struct drm_device *dev,
4499 struct drm_property_blob **replace,
4500 size_t length,
4501 const void *data,
4502 struct drm_mode_object *obj_holds_id,
4503 struct drm_property *prop_holds_id)
4504{
4505 struct drm_property_blob *new_blob = NULL;
4506 struct drm_property_blob *old_blob = NULL;
4507 int ret;
4508
4509 WARN_ON(replace == NULL);
4510
4511 old_blob = *replace;
4512
4513 if (length && data) {
4514 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004515 if (IS_ERR(new_blob))
4516 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004517 }
4518
4519 /* This does not need to be synchronised with blob_lock, as the
4520 * get_properties ioctl locks all modesetting objects, and
4521 * obj_holds_id must be locked before calling here, so we cannot
4522 * have its value out of sync with the list membership modified
4523 * below under blob_lock. */
4524 if (obj_holds_id) {
4525 ret = drm_object_property_set_value(obj_holds_id,
4526 prop_holds_id,
4527 new_blob ?
4528 new_blob->base.id : 0);
4529 if (ret != 0)
4530 goto err_created;
4531 }
4532
Markus Elfringec530822015-07-05 21:55:10 +02004533 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004534 *replace = new_blob;
4535
4536 return 0;
4537
4538err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004539 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004540 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004541}
4542
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004543/**
4544 * drm_mode_getblob_ioctl - get the contents of a blob property value
4545 * @dev: DRM device
4546 * @data: ioctl data
4547 * @file_priv: DRM file info
4548 *
4549 * This function retrieves the contents of a blob property. The value stored in
4550 * an object's blob property is just a normal modeset object id.
4551 *
4552 * Called by the user via ioctl.
4553 *
4554 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004555 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004556 */
Dave Airlief453ba02008-11-07 14:05:41 -08004557int drm_mode_getblob_ioctl(struct drm_device *dev,
4558 void *data, struct drm_file *file_priv)
4559{
Dave Airlief453ba02008-11-07 14:05:41 -08004560 struct drm_mode_get_blob *out_resp = data;
4561 struct drm_property_blob *blob;
4562 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004563 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004564
Dave Airliefb3b06c2011-02-08 13:55:21 +10004565 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4566 return -EINVAL;
4567
Daniel Vetter84849902012-12-02 00:28:11 +01004568 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004569 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004570 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004571 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004572 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004573 goto done;
4574 }
Dave Airlief453ba02008-11-07 14:05:41 -08004575
4576 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004577 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004578 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004579 ret = -EFAULT;
4580 goto done;
4581 }
4582 }
4583 out_resp->length = blob->length;
4584
4585done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004586 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004587 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004588 return ret;
4589}
4590
Dave Airliecc7096f2014-10-22 12:03:04 +10004591/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004592 * drm_mode_createblob_ioctl - create a new blob property
4593 * @dev: DRM device
4594 * @data: ioctl data
4595 * @file_priv: DRM file info
4596 *
4597 * This function creates a new blob property with user-defined values. In order
4598 * to give us sensible validation and checking when creating, rather than at
4599 * every potential use, we also require a type to be provided upfront.
4600 *
4601 * Called by the user via ioctl.
4602 *
4603 * Returns:
4604 * Zero on success, negative errno on failure.
4605 */
4606int drm_mode_createblob_ioctl(struct drm_device *dev,
4607 void *data, struct drm_file *file_priv)
4608{
4609 struct drm_mode_create_blob *out_resp = data;
4610 struct drm_property_blob *blob;
4611 void __user *blob_ptr;
4612 int ret = 0;
4613
4614 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4615 return -EINVAL;
4616
4617 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4618 if (IS_ERR(blob))
4619 return PTR_ERR(blob);
4620
4621 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4622 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4623 ret = -EFAULT;
4624 goto out_blob;
4625 }
4626
4627 /* Dropping the lock between create_blob and our access here is safe
4628 * as only the same file_priv can remove the blob; at this point, it is
4629 * not associated with any file_priv. */
4630 mutex_lock(&dev->mode_config.blob_lock);
4631 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004632 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004633 mutex_unlock(&dev->mode_config.blob_lock);
4634
4635 return 0;
4636
4637out_blob:
4638 drm_property_unreference_blob(blob);
4639 return ret;
4640}
4641
4642/**
4643 * drm_mode_destroyblob_ioctl - destroy a user blob property
4644 * @dev: DRM device
4645 * @data: ioctl data
4646 * @file_priv: DRM file info
4647 *
4648 * Destroy an existing user-defined blob property.
4649 *
4650 * Called by the user via ioctl.
4651 *
4652 * Returns:
4653 * Zero on success, negative errno on failure.
4654 */
4655int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4656 void *data, struct drm_file *file_priv)
4657{
4658 struct drm_mode_destroy_blob *out_resp = data;
4659 struct drm_property_blob *blob = NULL, *bt;
4660 bool found = false;
4661 int ret = 0;
4662
4663 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4664 return -EINVAL;
4665
4666 mutex_lock(&dev->mode_config.blob_lock);
4667 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4668 if (!blob) {
4669 ret = -ENOENT;
4670 goto err;
4671 }
4672
4673 /* Ensure the property was actually created by this user. */
4674 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4675 if (bt == blob) {
4676 found = true;
4677 break;
4678 }
4679 }
4680
4681 if (!found) {
4682 ret = -EPERM;
4683 goto err;
4684 }
4685
4686 /* We must drop head_file here, because we may not be the last
4687 * reference on the blob. */
4688 list_del_init(&blob->head_file);
4689 drm_property_unreference_blob_locked(blob);
4690 mutex_unlock(&dev->mode_config.blob_lock);
4691
4692 return 0;
4693
4694err:
4695 mutex_unlock(&dev->mode_config.blob_lock);
4696 return ret;
4697}
4698
4699/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004700 * drm_mode_connector_set_path_property - set tile property on connector
4701 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004702 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004703 *
4704 * This creates a property to expose to userspace to specify a
4705 * connector path. This is mainly used for DisplayPort MST where
4706 * connectors have a topology and we want to allow userspace to give
4707 * them more meaningful names.
4708 *
4709 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004710 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004711 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004712int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004713 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004714{
4715 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004716 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004717
Daniel Stoned2ed3432015-04-20 19:22:53 +01004718 ret = drm_property_replace_global_blob(dev,
4719 &connector->path_blob_ptr,
4720 strlen(path) + 1,
4721 path,
4722 &connector->base,
4723 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004724 return ret;
4725}
4726EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4727
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004728/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004729 * drm_mode_connector_set_tile_property - set tile property on connector
4730 * @connector: connector to set property on.
4731 *
4732 * This looks up the tile information for a connector, and creates a
4733 * property for userspace to parse if it exists. The property is of
4734 * the form of 8 integers using ':' as a separator.
4735 *
4736 * Returns:
4737 * Zero on success, errno on failure.
4738 */
4739int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4740{
4741 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004742 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004743 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004744
4745 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004746 ret = drm_property_replace_global_blob(dev,
4747 &connector->tile_blob_ptr,
4748 0,
4749 NULL,
4750 &connector->base,
4751 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004752 return ret;
4753 }
4754
4755 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4756 connector->tile_group->id, connector->tile_is_single_monitor,
4757 connector->num_h_tile, connector->num_v_tile,
4758 connector->tile_h_loc, connector->tile_v_loc,
4759 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004760
Daniel Stoned2ed3432015-04-20 19:22:53 +01004761 ret = drm_property_replace_global_blob(dev,
4762 &connector->tile_blob_ptr,
4763 strlen(tile) + 1,
4764 tile,
4765 &connector->base,
4766 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004767 return ret;
4768}
4769EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4770
4771/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004772 * drm_mode_connector_update_edid_property - update the edid property of a connector
4773 * @connector: drm connector
4774 * @edid: new value of the edid property
4775 *
4776 * This function creates a new blob modeset object and assigns its id to the
4777 * connector's edid property.
4778 *
4779 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004780 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004781 */
Dave Airlief453ba02008-11-07 14:05:41 -08004782int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004783 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004784{
4785 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004786 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004787 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004788
Thomas Wood4cf2b282014-06-18 17:52:33 +01004789 /* ignore requests to set edid when overridden */
4790 if (connector->override_edid)
4791 return 0;
4792
Daniel Stoned2ed3432015-04-20 19:22:53 +01004793 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004794 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004795
Daniel Stoned2ed3432015-04-20 19:22:53 +01004796 ret = drm_property_replace_global_blob(dev,
4797 &connector->edid_blob_ptr,
4798 size,
4799 edid,
4800 &connector->base,
4801 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004802 return ret;
4803}
4804EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4805
Rob Clark3843e712014-12-16 18:05:29 -05004806/* Some properties could refer to dynamic refcnt'd objects, or things that
4807 * need special locking to handle lifetime issues (ie. to ensure the prop
4808 * value doesn't become invalid part way through the property update due to
4809 * race). The value returned by reference via 'obj' should be passed back
4810 * to drm_property_change_valid_put() after the property is set (and the
4811 * object to which the property is attached has a chance to take it's own
4812 * reference).
4813 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004814bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004815 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004816{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004817 int i;
4818
Paulo Zanoni26a34812012-05-15 18:08:59 -03004819 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4820 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004821
Rob Clark3843e712014-12-16 18:05:29 -05004822 *ref = NULL;
4823
Rob Clark5ea22f22014-05-30 11:34:01 -04004824 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004825 if (value < property->values[0] || value > property->values[1])
4826 return false;
4827 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004828 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4829 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004830
Rob Clarkebc44cf2012-09-12 22:22:31 -05004831 if (svalue < U642I64(property->values[0]) ||
4832 svalue > U642I64(property->values[1]))
4833 return false;
4834 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004835 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004836 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004837
Rob Clark49e27542012-05-17 02:23:26 -06004838 for (i = 0; i < property->num_values; i++)
4839 valid_mask |= (1ULL << property->values[i]);
4840 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004841 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004842 struct drm_property_blob *blob;
4843
4844 if (value == 0)
4845 return true;
4846
4847 blob = drm_property_lookup_blob(property->dev, value);
4848 if (blob) {
4849 *ref = &blob->base;
4850 return true;
4851 } else {
4852 return false;
4853 }
Rob Clark98f75de2014-05-30 11:37:03 -04004854 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004855 /* a zero value for an object property translates to null: */
4856 if (value == 0)
4857 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004858
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004859 return _object_find(property->dev, value, property->values[0]) != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004860 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004861
4862 for (i = 0; i < property->num_values; i++)
4863 if (property->values[i] == value)
4864 return true;
4865 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004866}
4867
Rob Clarkd34f20d2014-12-18 16:01:56 -05004868void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004869 struct drm_mode_object *ref)
4870{
4871 if (!ref)
4872 return;
4873
4874 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004875 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004876 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4877 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004878}
4879
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004880/**
4881 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4882 * @dev: DRM device
4883 * @data: ioctl data
4884 * @file_priv: DRM file info
4885 *
4886 * This function sets the current value for a connectors's property. It also
4887 * calls into a driver's ->set_property callback to update the hardware state
4888 *
4889 * Called by the user via ioctl.
4890 *
4891 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004892 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004893 */
Dave Airlief453ba02008-11-07 14:05:41 -08004894int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4895 void *data, struct drm_file *file_priv)
4896{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004897 struct drm_mode_connector_set_property *conn_set_prop = data;
4898 struct drm_mode_obj_set_property obj_set_prop = {
4899 .value = conn_set_prop->value,
4900 .prop_id = conn_set_prop->prop_id,
4901 .obj_id = conn_set_prop->connector_id,
4902 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4903 };
Dave Airlief453ba02008-11-07 14:05:41 -08004904
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004905 /* It does all the locking and checking we need */
4906 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004907}
4908
Paulo Zanonic5431882012-05-15 18:09:02 -03004909static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4910 struct drm_property *property,
4911 uint64_t value)
4912{
4913 int ret = -EINVAL;
4914 struct drm_connector *connector = obj_to_connector(obj);
4915
4916 /* Do DPMS ourselves */
4917 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004918 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004919 } else if (connector->funcs->set_property)
4920 ret = connector->funcs->set_property(connector, property, value);
4921
4922 /* store the property value if successful */
4923 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004924 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004925 return ret;
4926}
4927
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004928static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4929 struct drm_property *property,
4930 uint64_t value)
4931{
4932 int ret = -EINVAL;
4933 struct drm_crtc *crtc = obj_to_crtc(obj);
4934
4935 if (crtc->funcs->set_property)
4936 ret = crtc->funcs->set_property(crtc, property, value);
4937 if (!ret)
4938 drm_object_property_set_value(obj, property, value);
4939
4940 return ret;
4941}
4942
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004943/**
4944 * drm_mode_plane_set_obj_prop - set the value of a property
4945 * @plane: drm plane object to set property value for
4946 * @property: property to set
4947 * @value: value the property should be set to
4948 *
4949 * This functions sets a given property on a given plane object. This function
4950 * calls the driver's ->set_property callback and changes the software state of
4951 * the property if the callback succeeds.
4952 *
4953 * Returns:
4954 * Zero on success, error code on failure.
4955 */
4956int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4957 struct drm_property *property,
4958 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004959{
4960 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004961 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004962
4963 if (plane->funcs->set_property)
4964 ret = plane->funcs->set_property(plane, property, value);
4965 if (!ret)
4966 drm_object_property_set_value(obj, property, value);
4967
4968 return ret;
4969}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004970EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004971
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004972/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004973 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004974 * @dev: DRM device
4975 * @data: ioctl data
4976 * @file_priv: DRM file info
4977 *
4978 * This function retrieves the current value for an object's property. Compared
4979 * to the connector specific ioctl this one is extended to also work on crtc and
4980 * plane objects.
4981 *
4982 * Called by the user via ioctl.
4983 *
4984 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004985 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004986 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004987int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4988 struct drm_file *file_priv)
4989{
4990 struct drm_mode_obj_get_properties *arg = data;
4991 struct drm_mode_object *obj;
4992 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004993
4994 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4995 return -EINVAL;
4996
Daniel Vetter84849902012-12-02 00:28:11 +01004997 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004998
4999 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5000 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005001 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005002 goto out;
5003 }
5004 if (!obj->properties) {
5005 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02005006 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005007 }
5008
Rob Clark88a48e22014-12-18 16:01:50 -05005009 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005010 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5011 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5012 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005013
Daniel Vetter1649c332016-04-22 22:10:28 +02005014out_unref:
5015 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005016out:
Daniel Vetter84849902012-12-02 00:28:11 +01005017 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005018 return ret;
5019}
5020
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005021/**
5022 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5023 * @dev: DRM device
5024 * @data: ioctl data
5025 * @file_priv: DRM file info
5026 *
5027 * This function sets the current value for an object's property. It also calls
5028 * into a driver's ->set_property callback to update the hardware state.
5029 * Compared to the connector specific ioctl this one is extended to also work on
5030 * crtc and plane objects.
5031 *
5032 * Called by the user via ioctl.
5033 *
5034 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005035 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005036 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005037int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5038 struct drm_file *file_priv)
5039{
5040 struct drm_mode_obj_set_property *arg = data;
5041 struct drm_mode_object *arg_obj;
5042 struct drm_mode_object *prop_obj;
5043 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005044 int i, ret = -EINVAL;
5045 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005046
5047 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5048 return -EINVAL;
5049
Daniel Vetter84849902012-12-02 00:28:11 +01005050 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005051
5052 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005053 if (!arg_obj) {
5054 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005055 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005056 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005057 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02005058 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005059
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005060 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005061 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005062 break;
5063
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005064 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02005065 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005066
5067 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5068 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005069 if (!prop_obj) {
5070 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02005071 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005072 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005073 property = obj_to_property(prop_obj);
5074
Rob Clark3843e712014-12-16 18:05:29 -05005075 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005076 goto out;
5077
5078 switch (arg_obj->type) {
5079 case DRM_MODE_OBJECT_CONNECTOR:
5080 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5081 arg->value);
5082 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005083 case DRM_MODE_OBJECT_CRTC:
5084 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5085 break;
Rob Clark4d939142012-05-17 02:23:27 -06005086 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005087 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5088 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005089 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005090 }
5091
Rob Clark3843e712014-12-16 18:05:29 -05005092 drm_property_change_valid_put(property, ref);
5093
Daniel Vetter1649c332016-04-22 22:10:28 +02005094out_unref:
5095 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005096out:
Daniel Vetter84849902012-12-02 00:28:11 +01005097 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005098 return ret;
5099}
5100
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005101/**
5102 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5103 * @connector: connector to attach
5104 * @encoder: encoder to attach @connector to
5105 *
5106 * This function links up a connector to an encoder. Note that the routing
5107 * restrictions between encoders and crtcs are exposed to userspace through the
5108 * possible_clones and possible_crtcs bitmasks.
5109 *
5110 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005111 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005112 */
Dave Airlief453ba02008-11-07 14:05:41 -08005113int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5114 struct drm_encoder *encoder)
5115{
5116 int i;
5117
Thierry Redingeb47fe82015-11-16 18:19:53 +01005118 /*
5119 * In the past, drivers have attempted to model the static association
5120 * of connector to encoder in simple connector/encoder devices using a
5121 * direct assignment of connector->encoder = encoder. This connection
5122 * is a logical one and the responsibility of the core, so drivers are
5123 * expected not to mess with this.
5124 *
5125 * Note that the error return should've been enough here, but a large
5126 * majority of drivers ignores the return value, so add in a big WARN
5127 * to get people's attention.
5128 */
5129 if (WARN_ON(connector->encoder))
5130 return -EINVAL;
5131
Dave Airlief453ba02008-11-07 14:05:41 -08005132 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5133 if (connector->encoder_ids[i] == 0) {
5134 connector->encoder_ids[i] = encoder->base.id;
5135 return 0;
5136 }
5137 }
5138 return -ENOMEM;
5139}
5140EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5141
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005142/**
5143 * drm_mode_crtc_set_gamma_size - set the gamma table size
5144 * @crtc: CRTC to set the gamma table size for
5145 * @gamma_size: size of the gamma table
5146 *
5147 * Drivers which support gamma tables should set this to the supported gamma
5148 * table size when initializing the CRTC. Currently the drm core only supports a
5149 * fixed gamma table size.
5150 *
5151 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005152 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005153 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005154int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005155 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005156{
5157 crtc->gamma_size = gamma_size;
5158
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005159 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5160 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005161 if (!crtc->gamma_store) {
5162 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005163 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005164 }
5165
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005166 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005167}
5168EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5169
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005170/**
5171 * drm_mode_gamma_set_ioctl - set the gamma table
5172 * @dev: DRM device
5173 * @data: ioctl data
5174 * @file_priv: DRM file info
5175 *
5176 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5177 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5178 *
5179 * Called by the user via ioctl.
5180 *
5181 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005182 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005183 */
Dave Airlief453ba02008-11-07 14:05:41 -08005184int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5185 void *data, struct drm_file *file_priv)
5186{
5187 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005188 struct drm_crtc *crtc;
5189 void *r_base, *g_base, *b_base;
5190 int size;
5191 int ret = 0;
5192
Dave Airliefb3b06c2011-02-08 13:55:21 +10005193 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5194 return -EINVAL;
5195
Daniel Vetter84849902012-12-02 00:28:11 +01005196 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005197 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5198 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005199 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005200 goto out;
5201 }
Dave Airlief453ba02008-11-07 14:05:41 -08005202
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005203 if (crtc->funcs->gamma_set == NULL) {
5204 ret = -ENOSYS;
5205 goto out;
5206 }
5207
Dave Airlief453ba02008-11-07 14:05:41 -08005208 /* memcpy into gamma store */
5209 if (crtc_lut->gamma_size != crtc->gamma_size) {
5210 ret = -EINVAL;
5211 goto out;
5212 }
5213
5214 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5215 r_base = crtc->gamma_store;
5216 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5217 ret = -EFAULT;
5218 goto out;
5219 }
5220
5221 g_base = r_base + size;
5222 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5223 ret = -EFAULT;
5224 goto out;
5225 }
5226
5227 b_base = g_base + size;
5228 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5229 ret = -EFAULT;
5230 goto out;
5231 }
5232
James Simmons72034252010-08-03 01:33:19 +01005233 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005234
5235out:
Daniel Vetter84849902012-12-02 00:28:11 +01005236 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005237 return ret;
5238
5239}
5240
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005241/**
5242 * drm_mode_gamma_get_ioctl - get the gamma table
5243 * @dev: DRM device
5244 * @data: ioctl data
5245 * @file_priv: DRM file info
5246 *
5247 * Copy the current gamma table into the storage provided. This also provides
5248 * the gamma table size the driver expects, which can be used to size the
5249 * allocated storage.
5250 *
5251 * Called by the user via ioctl.
5252 *
5253 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005254 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005255 */
Dave Airlief453ba02008-11-07 14:05:41 -08005256int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5257 void *data, struct drm_file *file_priv)
5258{
5259 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005260 struct drm_crtc *crtc;
5261 void *r_base, *g_base, *b_base;
5262 int size;
5263 int ret = 0;
5264
Dave Airliefb3b06c2011-02-08 13:55:21 +10005265 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5266 return -EINVAL;
5267
Daniel Vetter84849902012-12-02 00:28:11 +01005268 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005269 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5270 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005271 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005272 goto out;
5273 }
Dave Airlief453ba02008-11-07 14:05:41 -08005274
5275 /* memcpy into gamma store */
5276 if (crtc_lut->gamma_size != crtc->gamma_size) {
5277 ret = -EINVAL;
5278 goto out;
5279 }
5280
5281 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5282 r_base = crtc->gamma_store;
5283 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5284 ret = -EFAULT;
5285 goto out;
5286 }
5287
5288 g_base = r_base + size;
5289 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5290 ret = -EFAULT;
5291 goto out;
5292 }
5293
5294 b_base = g_base + size;
5295 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5296 ret = -EFAULT;
5297 goto out;
5298 }
5299out:
Daniel Vetter84849902012-12-02 00:28:11 +01005300 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005301 return ret;
5302}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005303
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005304/**
5305 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5306 * @dev: DRM device
5307 * @data: ioctl data
5308 * @file_priv: DRM file info
5309 *
5310 * This schedules an asynchronous update on a given CRTC, called page flip.
5311 * Optionally a drm event is generated to signal the completion of the event.
5312 * Generic drivers cannot assume that a pageflip with changed framebuffer
5313 * properties (including driver specific metadata like tiling layout) will work,
5314 * but some drivers support e.g. pixel format changes through the pageflip
5315 * ioctl.
5316 *
5317 * Called by the user via ioctl.
5318 *
5319 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005320 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005321 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005322int drm_mode_page_flip_ioctl(struct drm_device *dev,
5323 void *data, struct drm_file *file_priv)
5324{
5325 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005326 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005327 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005328 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005329 int ret = -EINVAL;
5330
5331 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5332 page_flip->reserved != 0)
5333 return -EINVAL;
5334
Keith Packard62f21042013-07-22 18:50:00 -07005335 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5336 return -EINVAL;
5337
Rob Clarka2b34e22013-10-05 16:36:52 -04005338 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5339 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005340 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005341
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005342 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005343 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005344 /* The framebuffer is currently unbound, presumably
5345 * due to a hotplug event, that userspace has not
5346 * yet discovered.
5347 */
5348 ret = -EBUSY;
5349 goto out;
5350 }
5351
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005352 if (crtc->funcs->page_flip == NULL)
5353 goto out;
5354
Daniel Vetter786b99e2012-12-02 21:53:40 +01005355 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005356 if (!fb) {
5357 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005358 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005359 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005360
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005361 if (crtc->state) {
5362 const struct drm_plane_state *state = crtc->primary->state;
5363
5364 ret = check_src_coords(state->src_x, state->src_y,
5365 state->src_w, state->src_h, fb);
5366 } else {
5367 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5368 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005369 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005370 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005371
Matt Roperf4510a22014-04-01 15:22:40 -07005372 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005373 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5374 ret = -EINVAL;
5375 goto out;
5376 }
5377
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005378 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005379 e = kzalloc(sizeof *e, GFP_KERNEL);
5380 if (!e) {
5381 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005382 goto out;
5383 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005384 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005385 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005386 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005387 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5388 if (ret) {
5389 kfree(e);
5390 goto out;
5391 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005392 }
5393
Daniel Vetter3d30a592014-07-27 13:42:42 +02005394 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005395 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005396 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005397 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5398 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005399 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005400 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005401 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005402 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005403 /* Unref only the old framebuffer. */
5404 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005405 }
5406
5407out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005408 if (fb)
5409 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005410 if (crtc->primary->old_fb)
5411 drm_framebuffer_unreference(crtc->primary->old_fb);
5412 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005413 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005414
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005415 return ret;
5416}
Chris Wilsoneb033552011-01-24 15:11:08 +00005417
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005418/**
5419 * drm_mode_config_reset - call ->reset callbacks
5420 * @dev: drm device
5421 *
5422 * This functions calls all the crtc's, encoder's and connector's ->reset
5423 * callback. Drivers can use this in e.g. their driver load or resume code to
5424 * reset hardware and software state.
5425 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005426void drm_mode_config_reset(struct drm_device *dev)
5427{
5428 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005429 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005430 struct drm_encoder *encoder;
5431 struct drm_connector *connector;
5432
Daniel Vettere4f62542015-07-09 23:44:35 +02005433 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005434 if (plane->funcs->reset)
5435 plane->funcs->reset(plane);
5436
Daniel Vettere4f62542015-07-09 23:44:35 +02005437 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005438 if (crtc->funcs->reset)
5439 crtc->funcs->reset(crtc);
5440
Daniel Vettere4f62542015-07-09 23:44:35 +02005441 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005442 if (encoder->funcs->reset)
5443 encoder->funcs->reset(encoder);
5444
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005445 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005446 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005447 if (connector->funcs->reset)
5448 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005449 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005450}
5451EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005452
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005453/**
5454 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5455 * @dev: DRM device
5456 * @data: ioctl data
5457 * @file_priv: DRM file info
5458 *
5459 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5460 * TTM or something else entirely) and returns the resulting buffer handle. This
5461 * handle can then be wrapped up into a framebuffer modeset object.
5462 *
5463 * Note that userspace is not allowed to use such objects for render
5464 * acceleration - drivers must create their own private ioctls for such a use
5465 * case.
5466 *
5467 * Called by the user via ioctl.
5468 *
5469 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005470 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005471 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005472int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5473 void *data, struct drm_file *file_priv)
5474{
5475 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005476 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005477
5478 if (!dev->driver->dumb_create)
5479 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005480 if (!args->width || !args->height || !args->bpp)
5481 return -EINVAL;
5482
5483 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005484 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005485 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005486 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005487 return -EINVAL;
5488 stride = cpp * args->width;
5489 if (args->height > 0xffffffffU / stride)
5490 return -EINVAL;
5491
5492 /* test for wrap-around */
5493 size = args->height * stride;
5494 if (PAGE_ALIGN(size) == 0)
5495 return -EINVAL;
5496
Thierry Redingf6085952014-11-03 11:14:14 +01005497 /*
5498 * handle, pitch and size are output parameters. Zero them out to
5499 * prevent drivers from accidentally using uninitialized data. Since
5500 * not all existing userspace is clearing these fields properly we
5501 * cannot reject IOCTL with garbage in them.
5502 */
5503 args->handle = 0;
5504 args->pitch = 0;
5505 args->size = 0;
5506
Dave Airlieff72145b2011-02-07 12:16:14 +10005507 return dev->driver->dumb_create(file_priv, dev, args);
5508}
5509
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005510/**
5511 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5512 * @dev: DRM device
5513 * @data: ioctl data
5514 * @file_priv: DRM file info
5515 *
5516 * Allocate an offset in the drm device node's address space to be able to
5517 * memory map a dumb buffer.
5518 *
5519 * Called by the user via ioctl.
5520 *
5521 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005522 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005523 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005524int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5525 void *data, struct drm_file *file_priv)
5526{
5527 struct drm_mode_map_dumb *args = data;
5528
5529 /* call driver ioctl to get mmap offset */
5530 if (!dev->driver->dumb_map_offset)
5531 return -ENOSYS;
5532
5533 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5534}
5535
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005536/**
5537 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5538 * @dev: DRM device
5539 * @data: ioctl data
5540 * @file_priv: DRM file info
5541 *
5542 * This destroys the userspace handle for the given dumb backing storage buffer.
5543 * Since buffer objects must be reference counted in the kernel a buffer object
5544 * won't be immediately freed if a framebuffer modeset object still uses it.
5545 *
5546 * Called by the user via ioctl.
5547 *
5548 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005549 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005550 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005551int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5552 void *data, struct drm_file *file_priv)
5553{
5554 struct drm_mode_destroy_dumb *args = data;
5555
5556 if (!dev->driver->dumb_destroy)
5557 return -ENOSYS;
5558
5559 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5560}
Dave Airlie248dbc22011-11-29 20:02:54 +00005561
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005562/**
5563 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5564 * @format: pixel format (DRM_FORMAT_*)
5565 * @depth: storage for the depth value
5566 * @bpp: storage for the bpp value
5567 *
5568 * This only supports RGB formats here for compat with code that doesn't use
5569 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005570 */
5571void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5572 int *bpp)
5573{
5574 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005575 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005576 case DRM_FORMAT_RGB332:
5577 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005578 *depth = 8;
5579 *bpp = 8;
5580 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005581 case DRM_FORMAT_XRGB1555:
5582 case DRM_FORMAT_XBGR1555:
5583 case DRM_FORMAT_RGBX5551:
5584 case DRM_FORMAT_BGRX5551:
5585 case DRM_FORMAT_ARGB1555:
5586 case DRM_FORMAT_ABGR1555:
5587 case DRM_FORMAT_RGBA5551:
5588 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005589 *depth = 15;
5590 *bpp = 16;
5591 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005592 case DRM_FORMAT_RGB565:
5593 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005594 *depth = 16;
5595 *bpp = 16;
5596 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005597 case DRM_FORMAT_RGB888:
5598 case DRM_FORMAT_BGR888:
5599 *depth = 24;
5600 *bpp = 24;
5601 break;
5602 case DRM_FORMAT_XRGB8888:
5603 case DRM_FORMAT_XBGR8888:
5604 case DRM_FORMAT_RGBX8888:
5605 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005606 *depth = 24;
5607 *bpp = 32;
5608 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005609 case DRM_FORMAT_XRGB2101010:
5610 case DRM_FORMAT_XBGR2101010:
5611 case DRM_FORMAT_RGBX1010102:
5612 case DRM_FORMAT_BGRX1010102:
5613 case DRM_FORMAT_ARGB2101010:
5614 case DRM_FORMAT_ABGR2101010:
5615 case DRM_FORMAT_RGBA1010102:
5616 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005617 *depth = 30;
5618 *bpp = 32;
5619 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005620 case DRM_FORMAT_ARGB8888:
5621 case DRM_FORMAT_ABGR8888:
5622 case DRM_FORMAT_RGBA8888:
5623 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005624 *depth = 32;
5625 *bpp = 32;
5626 break;
5627 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005628 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5629 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005630 *depth = 0;
5631 *bpp = 0;
5632 break;
5633 }
5634}
5635EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005636
5637/**
5638 * drm_format_num_planes - get the number of planes for format
5639 * @format: pixel format (DRM_FORMAT_*)
5640 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005641 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005642 * The number of planes used by the specified pixel format.
5643 */
5644int drm_format_num_planes(uint32_t format)
5645{
5646 switch (format) {
5647 case DRM_FORMAT_YUV410:
5648 case DRM_FORMAT_YVU410:
5649 case DRM_FORMAT_YUV411:
5650 case DRM_FORMAT_YVU411:
5651 case DRM_FORMAT_YUV420:
5652 case DRM_FORMAT_YVU420:
5653 case DRM_FORMAT_YUV422:
5654 case DRM_FORMAT_YVU422:
5655 case DRM_FORMAT_YUV444:
5656 case DRM_FORMAT_YVU444:
5657 return 3;
5658 case DRM_FORMAT_NV12:
5659 case DRM_FORMAT_NV21:
5660 case DRM_FORMAT_NV16:
5661 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005662 case DRM_FORMAT_NV24:
5663 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005664 return 2;
5665 default:
5666 return 1;
5667 }
5668}
5669EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005670
5671/**
5672 * drm_format_plane_cpp - determine the bytes per pixel value
5673 * @format: pixel format (DRM_FORMAT_*)
5674 * @plane: plane index
5675 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005676 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005677 * The bytes per pixel value for the specified plane.
5678 */
5679int drm_format_plane_cpp(uint32_t format, int plane)
5680{
5681 unsigned int depth;
5682 int bpp;
5683
5684 if (plane >= drm_format_num_planes(format))
5685 return 0;
5686
5687 switch (format) {
5688 case DRM_FORMAT_YUYV:
5689 case DRM_FORMAT_YVYU:
5690 case DRM_FORMAT_UYVY:
5691 case DRM_FORMAT_VYUY:
5692 return 2;
5693 case DRM_FORMAT_NV12:
5694 case DRM_FORMAT_NV21:
5695 case DRM_FORMAT_NV16:
5696 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005697 case DRM_FORMAT_NV24:
5698 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005699 return plane ? 2 : 1;
5700 case DRM_FORMAT_YUV410:
5701 case DRM_FORMAT_YVU410:
5702 case DRM_FORMAT_YUV411:
5703 case DRM_FORMAT_YVU411:
5704 case DRM_FORMAT_YUV420:
5705 case DRM_FORMAT_YVU420:
5706 case DRM_FORMAT_YUV422:
5707 case DRM_FORMAT_YVU422:
5708 case DRM_FORMAT_YUV444:
5709 case DRM_FORMAT_YVU444:
5710 return 1;
5711 default:
5712 drm_fb_get_bpp_depth(format, &depth, &bpp);
5713 return bpp >> 3;
5714 }
5715}
5716EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005717
5718/**
5719 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5720 * @format: pixel format (DRM_FORMAT_*)
5721 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005722 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005723 * The horizontal chroma subsampling factor for the
5724 * specified pixel format.
5725 */
5726int drm_format_horz_chroma_subsampling(uint32_t format)
5727{
5728 switch (format) {
5729 case DRM_FORMAT_YUV411:
5730 case DRM_FORMAT_YVU411:
5731 case DRM_FORMAT_YUV410:
5732 case DRM_FORMAT_YVU410:
5733 return 4;
5734 case DRM_FORMAT_YUYV:
5735 case DRM_FORMAT_YVYU:
5736 case DRM_FORMAT_UYVY:
5737 case DRM_FORMAT_VYUY:
5738 case DRM_FORMAT_NV12:
5739 case DRM_FORMAT_NV21:
5740 case DRM_FORMAT_NV16:
5741 case DRM_FORMAT_NV61:
5742 case DRM_FORMAT_YUV422:
5743 case DRM_FORMAT_YVU422:
5744 case DRM_FORMAT_YUV420:
5745 case DRM_FORMAT_YVU420:
5746 return 2;
5747 default:
5748 return 1;
5749 }
5750}
5751EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5752
5753/**
5754 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5755 * @format: pixel format (DRM_FORMAT_*)
5756 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005757 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005758 * The vertical chroma subsampling factor for the
5759 * specified pixel format.
5760 */
5761int drm_format_vert_chroma_subsampling(uint32_t format)
5762{
5763 switch (format) {
5764 case DRM_FORMAT_YUV410:
5765 case DRM_FORMAT_YVU410:
5766 return 4;
5767 case DRM_FORMAT_YUV420:
5768 case DRM_FORMAT_YVU420:
5769 case DRM_FORMAT_NV12:
5770 case DRM_FORMAT_NV21:
5771 return 2;
5772 default:
5773 return 1;
5774 }
5775}
5776EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005777
5778/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005779 * drm_format_plane_width - width of the plane given the first plane
5780 * @width: width of the first plane
5781 * @format: pixel format
5782 * @plane: plane index
5783 *
5784 * Returns:
5785 * The width of @plane, given that the width of the first plane is @width.
5786 */
5787int drm_format_plane_width(int width, uint32_t format, int plane)
5788{
5789 if (plane >= drm_format_num_planes(format))
5790 return 0;
5791
5792 if (plane == 0)
5793 return width;
5794
5795 return width / drm_format_horz_chroma_subsampling(format);
5796}
5797EXPORT_SYMBOL(drm_format_plane_width);
5798
5799/**
5800 * drm_format_plane_height - height of the plane given the first plane
5801 * @height: height of the first plane
5802 * @format: pixel format
5803 * @plane: plane index
5804 *
5805 * Returns:
5806 * The height of @plane, given that the height of the first plane is @height.
5807 */
5808int drm_format_plane_height(int height, uint32_t format, int plane)
5809{
5810 if (plane >= drm_format_num_planes(format))
5811 return 0;
5812
5813 if (plane == 0)
5814 return height;
5815
5816 return height / drm_format_vert_chroma_subsampling(format);
5817}
5818EXPORT_SYMBOL(drm_format_plane_height);
5819
5820/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305821 * drm_rotation_simplify() - Try to simplify the rotation
5822 * @rotation: Rotation to be simplified
5823 * @supported_rotations: Supported rotations
5824 *
5825 * Attempt to simplify the rotation to a form that is supported.
5826 * Eg. if the hardware supports everything except DRM_REFLECT_X
5827 * one could call this function like this:
5828 *
5829 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5830 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5831 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5832 *
5833 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5834 * transforms the hardware supports, this function may not
5835 * be able to produce a supported transform, so the caller should
5836 * check the result afterwards.
5837 */
5838unsigned int drm_rotation_simplify(unsigned int rotation,
5839 unsigned int supported_rotations)
5840{
5841 if (rotation & ~supported_rotations) {
5842 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005843 rotation = (rotation & DRM_REFLECT_MASK) |
5844 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305845 }
5846
5847 return rotation;
5848}
5849EXPORT_SYMBOL(drm_rotation_simplify);
5850
5851/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005852 * drm_mode_config_init - initialize DRM mode_configuration structure
5853 * @dev: DRM device
5854 *
5855 * Initialize @dev's mode_config structure, used for tracking the graphics
5856 * configuration of @dev.
5857 *
5858 * Since this initializes the modeset locks, no locking is possible. Which is no
5859 * problem, since this should happen single threaded at init time. It is the
5860 * driver's problem to ensure this guarantee.
5861 *
5862 */
5863void drm_mode_config_init(struct drm_device *dev)
5864{
5865 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005866 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005867 mutex_init(&dev->mode_config.idr_mutex);
5868 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005869 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005870 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5871 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5872 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5873 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5874 INIT_LIST_HEAD(&dev->mode_config.property_list);
5875 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5876 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5877 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005878 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005879 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005880
5881 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005882 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005883 drm_modeset_unlock_all(dev);
5884
5885 /* Just to be sure */
5886 dev->mode_config.num_fb = 0;
5887 dev->mode_config.num_connector = 0;
5888 dev->mode_config.num_crtc = 0;
5889 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005890 dev->mode_config.num_overlay_plane = 0;
5891 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005892}
5893EXPORT_SYMBOL(drm_mode_config_init);
5894
5895/**
5896 * drm_mode_config_cleanup - free up DRM mode_config info
5897 * @dev: DRM device
5898 *
5899 * Free up all the connectors and CRTCs associated with this DRM device, then
5900 * free up the framebuffers and associated buffer objects.
5901 *
5902 * Note that since this /should/ happen single-threaded at driver/device
5903 * teardown time, no locking is required. It's the driver's job to ensure that
5904 * this guarantee actually holds true.
5905 *
5906 * FIXME: cleanup any dangling user buffer objects too
5907 */
5908void drm_mode_config_cleanup(struct drm_device *dev)
5909{
5910 struct drm_connector *connector, *ot;
5911 struct drm_crtc *crtc, *ct;
5912 struct drm_encoder *encoder, *enct;
5913 struct drm_framebuffer *fb, *fbt;
5914 struct drm_property *property, *pt;
5915 struct drm_property_blob *blob, *bt;
5916 struct drm_plane *plane, *plt;
5917
5918 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5919 head) {
5920 encoder->funcs->destroy(encoder);
5921 }
5922
5923 list_for_each_entry_safe(connector, ot,
5924 &dev->mode_config.connector_list, head) {
5925 connector->funcs->destroy(connector);
5926 }
5927
5928 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5929 head) {
5930 drm_property_destroy(dev, property);
5931 }
5932
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005933 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5934 head) {
5935 plane->funcs->destroy(plane);
5936 }
5937
5938 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5939 crtc->funcs->destroy(crtc);
5940 }
5941
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005942 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005943 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005944 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005945 }
5946
5947 /*
5948 * Single-threaded teardown context, so it's not required to grab the
5949 * fb_lock to protect against concurrent fb_list access. Contrary, it
5950 * would actually deadlock with the drm_framebuffer_cleanup function.
5951 *
5952 * Also, if there are any framebuffers left, that's a driver leak now,
5953 * so politely WARN about this.
5954 */
5955 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5956 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005957 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005958 }
5959
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005960 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005961 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005962 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005963 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005964}
5965EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305966
5967struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5968 unsigned int supported_rotations)
5969{
5970 static const struct drm_prop_enum_list props[] = {
5971 { DRM_ROTATE_0, "rotate-0" },
5972 { DRM_ROTATE_90, "rotate-90" },
5973 { DRM_ROTATE_180, "rotate-180" },
5974 { DRM_ROTATE_270, "rotate-270" },
5975 { DRM_REFLECT_X, "reflect-x" },
5976 { DRM_REFLECT_Y, "reflect-y" },
5977 };
5978
5979 return drm_property_create_bitmask(dev, 0, "rotation",
5980 props, ARRAY_SIZE(props),
5981 supported_rotations);
5982}
5983EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005984
5985/**
5986 * DOC: Tile group
5987 *
5988 * Tile groups are used to represent tiled monitors with a unique
5989 * integer identifier. Tiled monitors using DisplayID v1.3 have
5990 * a unique 8-byte handle, we store this in a tile group, so we
5991 * have a common identifier for all tiles in a monitor group.
5992 */
5993static void drm_tile_group_free(struct kref *kref)
5994{
5995 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5996 struct drm_device *dev = tg->dev;
5997 mutex_lock(&dev->mode_config.idr_mutex);
5998 idr_remove(&dev->mode_config.tile_idr, tg->id);
5999 mutex_unlock(&dev->mode_config.idr_mutex);
6000 kfree(tg);
6001}
6002
6003/**
6004 * drm_mode_put_tile_group - drop a reference to a tile group.
6005 * @dev: DRM device
6006 * @tg: tile group to drop reference to.
6007 *
6008 * drop reference to tile group and free if 0.
6009 */
6010void drm_mode_put_tile_group(struct drm_device *dev,
6011 struct drm_tile_group *tg)
6012{
6013 kref_put(&tg->refcount, drm_tile_group_free);
6014}
6015
6016/**
6017 * drm_mode_get_tile_group - get a reference to an existing tile group
6018 * @dev: DRM device
6019 * @topology: 8-bytes unique per monitor.
6020 *
6021 * Use the unique bytes to get a reference to an existing tile group.
6022 *
6023 * RETURNS:
6024 * tile group or NULL if not found.
6025 */
6026struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6027 char topology[8])
6028{
6029 struct drm_tile_group *tg;
6030 int id;
6031 mutex_lock(&dev->mode_config.idr_mutex);
6032 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6033 if (!memcmp(tg->group_data, topology, 8)) {
6034 if (!kref_get_unless_zero(&tg->refcount))
6035 tg = NULL;
6036 mutex_unlock(&dev->mode_config.idr_mutex);
6037 return tg;
6038 }
6039 }
6040 mutex_unlock(&dev->mode_config.idr_mutex);
6041 return NULL;
6042}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006043EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006044
6045/**
6046 * drm_mode_create_tile_group - create a tile group from a displayid description
6047 * @dev: DRM device
6048 * @topology: 8-bytes unique per monitor.
6049 *
6050 * Create a tile group for the unique monitor, and get a unique
6051 * identifier for the tile group.
6052 *
6053 * RETURNS:
6054 * new tile group or error.
6055 */
6056struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6057 char topology[8])
6058{
6059 struct drm_tile_group *tg;
6060 int ret;
6061
6062 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6063 if (!tg)
6064 return ERR_PTR(-ENOMEM);
6065
6066 kref_init(&tg->refcount);
6067 memcpy(tg->group_data, topology, 8);
6068 tg->dev = dev;
6069
6070 mutex_lock(&dev->mode_config.idr_mutex);
6071 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6072 if (ret >= 0) {
6073 tg->id = ret;
6074 } else {
6075 kfree(tg);
6076 tg = ERR_PTR(ret);
6077 }
6078
6079 mutex_unlock(&dev->mode_config.idr_mutex);
6080 return tg;
6081}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006082EXPORT_SYMBOL(drm_mode_create_tile_group);