blob: 0e4e25509c3d5c5bb9f821ba66d6c58c642b69de [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;
367 mutex_unlock(&dev->mode_config.idr_mutex);
368
369 return obj;
370}
371
Daniel Vetter786b99e2012-12-02 21:53:40 +0100372/**
373 * drm_mode_object_find - look up a drm object with static lifetime
374 * @dev: drm device
375 * @id: id of the mode object
376 * @type: type of the mode object
377 *
378 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400379 * are reference counted, they need special treatment. Even with
380 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
381 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100382 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200383struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
384 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800385{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000386 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800387
Daniel Vetter786b99e2012-12-02 21:53:40 +0100388 /* Framebuffers are reference counted and need their own lookup
389 * function.*/
Daniel Stone6bcacf52015-04-20 19:22:55 +0100390 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400391 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800392 return obj;
393}
394EXPORT_SYMBOL(drm_mode_object_find);
395
Dave Airlied0f37cf2016-04-15 15:10:36 +1000396void drm_mode_object_unreference(struct drm_mode_object *obj)
397{
398 if (obj->free_cb) {
399 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
400 kref_put(&obj->refcount, obj->free_cb);
401 }
402}
403EXPORT_SYMBOL(drm_mode_object_unreference);
404
405/**
406 * drm_mode_object_reference - incr the fb refcnt
407 * @obj: mode_object
408 *
409 * This function operates only on refcounted objects.
410 * This functions increments the object's refcount.
411 */
412void drm_mode_object_reference(struct drm_mode_object *obj)
413{
414 if (obj->free_cb) {
415 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
416 kref_get(&obj->refcount);
417 }
418}
419EXPORT_SYMBOL(drm_mode_object_reference);
420
Dave Airlief55f1f92016-04-15 15:10:33 +1000421static void drm_framebuffer_free(struct kref *kref)
422{
423 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000424 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000425 struct drm_device *dev = fb->dev;
426
427 /*
428 * The lookup idr holds a weak reference, which has not necessarily been
429 * removed at this point. Check for that.
430 */
431 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlie19ab3f82016-04-15 15:10:34 +1000432 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000433 mutex_unlock(&dev->mode_config.fb_lock);
434
435 fb->funcs->destroy(fb);
436}
437
Dave Airlief453ba02008-11-07 14:05:41 -0800438/**
Dave Airlief453ba02008-11-07 14:05:41 -0800439 * drm_framebuffer_init - initialize a framebuffer
440 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100441 * @fb: framebuffer to be initialized
442 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800443 *
Dave Airlief453ba02008-11-07 14:05:41 -0800444 * Allocates an ID for the framebuffer's parent mode object, sets its mode
445 * functions & device file and adds it to the master fd list.
446 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100447 * IMPORTANT:
448 * This functions publishes the fb and makes it available for concurrent access
449 * by other users. Which means by this point the fb _must_ be fully set up -
450 * since all the fb attributes are invariant over its lifetime, no further
451 * locking but only correct reference counting is required.
452 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100453 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200454 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800455 */
456int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
457 const struct drm_framebuffer_funcs *funcs)
458{
459 int ret;
460
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100461 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100462 INIT_LIST_HEAD(&fb->filp_head);
463 fb->dev = dev;
464 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000465
Dave Airlied0f37cf2016-04-15 15:10:36 +1000466 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
467 true, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200468 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100469 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800470
Dave Airlief453ba02008-11-07 14:05:41 -0800471 dev->mode_config.num_fb++;
472 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100473out:
474 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800475
Lukas Wunner3c67d832015-10-15 11:56:56 +0200476 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800477}
478EXPORT_SYMBOL(drm_framebuffer_init);
479
Rob Clarkf7eff602012-09-05 21:48:38 +0000480/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100481 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
482 * @dev: drm device
483 * @id: id of the fb object
484 *
485 * If successful, this grabs an additional reference to the framebuffer -
486 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100487 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100488 */
489struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
490 uint32_t id)
491{
Dave Airliecee26ac2016-04-15 15:10:37 +1000492 struct drm_mode_object *obj;
493 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100494
495 mutex_lock(&dev->mode_config.fb_lock);
Dave Airliecee26ac2016-04-15 15:10:37 +1000496 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
497 if (obj) {
498 fb = obj_to_fb(obj);
Dave Airlied0f37cf2016-04-15 15:10:36 +1000499 if (!kref_get_unless_zero(&fb->base.refcount))
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200500 fb = NULL;
501 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100502 mutex_unlock(&dev->mode_config.fb_lock);
503
504 return fb;
505}
506EXPORT_SYMBOL(drm_framebuffer_lookup);
507
508/**
Daniel Vetter36206362012-12-10 20:42:17 +0100509 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
510 * @fb: fb to unregister
511 *
512 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
513 * those used for fbdev. Note that the caller must hold a reference of it's own,
514 * i.e. the object may not be destroyed through this call (since it'll lead to a
515 * locking inversion).
516 */
517void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
518{
Daniel Vettera39a3572015-08-25 15:45:11 +0200519 struct drm_device *dev;
520
521 if (!fb)
522 return;
523
524 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100525
526 mutex_lock(&dev->mode_config.fb_lock);
527 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000528 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100529 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100530}
531EXPORT_SYMBOL(drm_framebuffer_unregister_private);
532
533/**
Dave Airlief453ba02008-11-07 14:05:41 -0800534 * drm_framebuffer_cleanup - remove a framebuffer object
535 * @fb: framebuffer to remove
536 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100537 * Cleanup framebuffer. This function is intended to be used from the drivers
538 * ->destroy callback. It can also be used to clean up driver private
539 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100540 *
541 * Note that this function does not remove the fb from active usuage - if it is
542 * still used anywhere, hilarity can ensue since userspace could call getfb on
543 * the id and get back -EINVAL. Obviously no concern at driver unload time.
544 *
545 * Also, the framebuffer will not be removed from the lookup idr - for
546 * user-created framebuffers this will happen in in the rmfb ioctl. For
547 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
548 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800549 */
550void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
551{
552 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100553
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100554 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000555 list_del(&fb->head);
556 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100557 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000558}
559EXPORT_SYMBOL(drm_framebuffer_cleanup);
560
561/**
562 * drm_framebuffer_remove - remove and unreference a framebuffer object
563 * @fb: framebuffer to remove
564 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000565 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100566 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100567 * passed-in framebuffer. Might take the modeset locks.
568 *
569 * Note that this function optimizes the cleanup away if the caller holds the
570 * last reference to the framebuffer. It is also guaranteed to not take the
571 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000572 */
573void drm_framebuffer_remove(struct drm_framebuffer *fb)
574{
Daniel Vettera39a3572015-08-25 15:45:11 +0200575 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800576 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800577 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000578 struct drm_mode_set set;
579 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800580
Daniel Vettera39a3572015-08-25 15:45:11 +0200581 if (!fb)
582 return;
583
584 dev = fb->dev;
585
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100586 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100587
Daniel Vetterb62584e2012-12-11 16:51:35 +0100588 /*
589 * drm ABI mandates that we remove any deleted framebuffers from active
590 * useage. But since most sane clients only remove framebuffers they no
591 * longer need, try to optimize this away.
592 *
593 * Since we're holding a reference ourselves, observing a refcount of 1
594 * means that we're the last holder and can skip it. Also, the refcount
595 * can never increase from 1 again, so we don't need any barriers or
596 * locks.
597 *
598 * Note that userspace could try to race with use and instate a new
599 * usage _after_ we've cleared all current ones. End result will be an
600 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
601 * in this manner.
602 */
Dave Airlie747a5982016-04-15 15:10:35 +1000603 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100604 drm_modeset_lock_all(dev);
605 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200606 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700607 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100608 /* should turn off the crtc */
609 memset(&set, 0, sizeof(struct drm_mode_set));
610 set.crtc = crtc;
611 set.fb = NULL;
612 ret = drm_mode_set_config_internal(&set);
613 if (ret)
614 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
615 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000616 }
Dave Airlief453ba02008-11-07 14:05:41 -0800617
Daniel Vetter6295d602015-07-09 23:44:25 +0200618 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300619 if (plane->fb == fb)
620 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800621 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100622 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800623 }
624
Rob Clarkf7eff602012-09-05 21:48:38 +0000625 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800626}
Rob Clarkf7eff602012-09-05 21:48:38 +0000627EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800628
Rob Clark51fd3712013-11-19 12:10:12 -0500629DEFINE_WW_CLASS(crtc_ww_class);
630
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200631static unsigned int drm_num_crtcs(struct drm_device *dev)
632{
633 unsigned int num = 0;
634 struct drm_crtc *tmp;
635
636 drm_for_each_crtc(tmp, dev) {
637 num++;
638 }
639
640 return num;
641}
642
Dave Airlief453ba02008-11-07 14:05:41 -0800643/**
Matt Ropere13161a2014-04-01 15:22:38 -0700644 * drm_crtc_init_with_planes - Initialise a new CRTC object with
645 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800646 * @dev: DRM device
647 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700648 * @primary: Primary plane for CRTC
649 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800650 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200651 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800652 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000653 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200654 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100655 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200656 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800657 */
Matt Ropere13161a2014-04-01 15:22:38 -0700658int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
659 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700660 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200661 const struct drm_crtc_funcs *funcs,
662 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800663{
Rob Clark51fd3712013-11-19 12:10:12 -0500664 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200665 int ret;
666
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100667 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
668 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
669
Dave Airlief453ba02008-11-07 14:05:41 -0800670 crtc->dev = dev;
671 crtc->funcs = funcs;
672
Rob Clark51fd3712013-11-19 12:10:12 -0500673 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200674 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
675 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100676 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800677
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200678 if (name) {
679 va_list ap;
680
681 va_start(ap, name);
682 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
683 va_end(ap);
684 } else {
685 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
686 drm_num_crtcs(dev));
687 }
688 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000689 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200690 return -ENOMEM;
691 }
692
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300693 crtc->base.properties = &crtc->properties;
694
Rob Clark51fd3712013-11-19 12:10:12 -0500695 list_add_tail(&crtc->head, &config->crtc_list);
696 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200697
Matt Ropere13161a2014-04-01 15:22:38 -0700698 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700699 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700700 if (primary)
701 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700702 if (cursor)
703 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700704
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100705 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
706 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100707 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100708 }
709
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100710 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800711}
Matt Ropere13161a2014-04-01 15:22:38 -0700712EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800713
714/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000715 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800716 * @crtc: CRTC to cleanup
717 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000718 * This function cleans up @crtc and removes it from the DRM mode setting
719 * core. Note that the function does *not* free the crtc structure itself,
720 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800721 */
722void drm_crtc_cleanup(struct drm_crtc *crtc)
723{
724 struct drm_device *dev = crtc->dev;
725
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000726 kfree(crtc->gamma_store);
727 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800728
Rob Clark51fd3712013-11-19 12:10:12 -0500729 drm_modeset_lock_fini(&crtc->mutex);
730
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000731 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800732 list_del(&crtc->head);
733 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100734
735 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
736 if (crtc->state && crtc->funcs->atomic_destroy_state)
737 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100738
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200739 kfree(crtc->name);
740
Thierry Redinga18c0af2014-12-10 11:38:49 +0100741 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800742}
743EXPORT_SYMBOL(drm_crtc_cleanup);
744
745/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000746 * drm_crtc_index - find the index of a registered CRTC
747 * @crtc: CRTC to find index for
748 *
749 * Given a registered CRTC, return the index of that CRTC within a DRM
750 * device's list of CRTCs.
751 */
752unsigned int drm_crtc_index(struct drm_crtc *crtc)
753{
754 unsigned int index = 0;
755 struct drm_crtc *tmp;
756
Daniel Vettere4f62542015-07-09 23:44:35 +0200757 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000758 if (tmp == crtc)
759 return index;
760
761 index++;
762 }
763
764 BUG();
765}
766EXPORT_SYMBOL(drm_crtc_index);
767
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100768/*
Dave Airlief453ba02008-11-07 14:05:41 -0800769 * drm_mode_remove - remove and free a mode
770 * @connector: connector list to modify
771 * @mode: mode to remove
772 *
Dave Airlief453ba02008-11-07 14:05:41 -0800773 * Remove @mode from @connector's mode list, then free it.
774 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100775static void drm_mode_remove(struct drm_connector *connector,
776 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800777{
778 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100779 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800780}
Dave Airlief453ba02008-11-07 14:05:41 -0800781
782/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200783 * drm_display_info_set_bus_formats - set the supported bus formats
784 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100785 * @formats: array containing the supported bus formats
786 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200787 *
788 * Store the supported bus formats in display info structure.
789 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
790 * a full list of available formats.
791 */
792int drm_display_info_set_bus_formats(struct drm_display_info *info,
793 const u32 *formats,
794 unsigned int num_formats)
795{
796 u32 *fmts = NULL;
797
798 if (!formats && num_formats)
799 return -EINVAL;
800
801 if (formats && num_formats) {
802 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
803 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300804 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200805 return -ENOMEM;
806 }
807
808 kfree(info->bus_formats);
809 info->bus_formats = fmts;
810 info->num_bus_formats = num_formats;
811
812 return 0;
813}
814EXPORT_SYMBOL(drm_display_info_set_bus_formats);
815
816/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200817 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
818 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200819 *
820 * The kernel supports per-connector configration of its consoles through
821 * use of the video= parameter. This function parses that option and
822 * extracts the user's specified mode (or enable/disable status) for a
823 * particular connector. This is typically only used during the early fbdev
824 * setup.
825 */
826static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
827{
828 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
829 char *option = NULL;
830
831 if (fb_get_options(connector->name, &option))
832 return;
833
834 if (!drm_mode_parse_command_line_for_connector(option,
835 connector,
836 mode))
837 return;
838
839 if (mode->force) {
840 const char *s;
841
842 switch (mode->force) {
843 case DRM_FORCE_OFF:
844 s = "OFF";
845 break;
846 case DRM_FORCE_ON_DIGITAL:
847 s = "ON - dig";
848 break;
849 default:
850 case DRM_FORCE_ON:
851 s = "ON";
852 break;
853 }
854
855 DRM_INFO("forcing %s connector %s\n", connector->name, s);
856 connector->force = mode->force;
857 }
858
859 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
860 connector->name,
861 mode->xres, mode->yres,
862 mode->refresh_specified ? mode->refresh : 60,
863 mode->rb ? " reduced blanking" : "",
864 mode->margins ? " with margins" : "",
865 mode->interlace ? " interlaced" : "");
866}
867
868/**
Dave Airlief453ba02008-11-07 14:05:41 -0800869 * drm_connector_init - Init a preallocated connector
870 * @dev: DRM device
871 * @connector: the connector to init
872 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100873 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800874 *
Dave Airlief453ba02008-11-07 14:05:41 -0800875 * Initialises a preallocated connector. Connectors should be
876 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200877 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100878 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200879 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800880 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200881int drm_connector_init(struct drm_device *dev,
882 struct drm_connector *connector,
883 const struct drm_connector_funcs *funcs,
884 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800885{
Rob Clarkae16c592014-12-18 16:01:54 -0500886 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200887 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400888 struct ida *connector_ida =
889 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890
Daniel Vetter84849902012-12-02 00:28:11 +0100891 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800892
Dave Airlied0f37cf2016-04-15 15:10:36 +1000893 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200894 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300895 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300897 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800898 connector->dev = dev;
899 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100900
901 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
902 if (connector->connector_id < 0) {
903 ret = connector->connector_id;
904 goto out_put;
905 }
906
Dave Airlief453ba02008-11-07 14:05:41 -0800907 connector->connector_type = connector_type;
908 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400909 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
910 if (connector->connector_type_id < 0) {
911 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100912 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400913 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300914 connector->name =
915 kasprintf(GFP_KERNEL, "%s-%d",
916 drm_connector_enum_list[connector_type].name,
917 connector->connector_type_id);
918 if (!connector->name) {
919 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100920 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300921 }
922
Dave Airlief453ba02008-11-07 14:05:41 -0800923 INIT_LIST_HEAD(&connector->probed_modes);
924 INIT_LIST_HEAD(&connector->modes);
925 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000926 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800927
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200928 drm_connector_get_cmdline_mode(connector);
929
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100930 /* We should add connectors at the end to avoid upsetting the connector
931 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500932 list_add_tail(&connector->head, &config->connector_list);
933 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800934
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200935 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500936 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500937 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200938 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800939
Rob Clark58495562012-10-11 20:50:56 -0500940 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500941 config->dpms_property, 0);
942
943 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
944 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
945 }
Dave Airlief453ba02008-11-07 14:05:41 -0800946
Thomas Wood30f65702014-06-18 17:52:32 +0100947 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100948out_put_type_id:
949 if (ret)
950 ida_remove(connector_ida, connector->connector_type_id);
951out_put_id:
952 if (ret)
953 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300954out_put:
955 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000956 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300957
958out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100959 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200960
961 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800962}
963EXPORT_SYMBOL(drm_connector_init);
964
965/**
966 * drm_connector_cleanup - cleans up an initialised connector
967 * @connector: connector to cleanup
968 *
Dave Airlief453ba02008-11-07 14:05:41 -0800969 * Cleans up the connector but doesn't free the object.
970 */
971void drm_connector_cleanup(struct drm_connector *connector)
972{
973 struct drm_device *dev = connector->dev;
974 struct drm_display_mode *mode, *t;
975
Dave Airlie40d9b042014-10-20 16:29:33 +1000976 if (connector->tile_group) {
977 drm_mode_put_tile_group(dev, connector->tile_group);
978 connector->tile_group = NULL;
979 }
980
Dave Airlief453ba02008-11-07 14:05:41 -0800981 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
982 drm_mode_remove(connector, mode);
983
984 list_for_each_entry_safe(mode, t, &connector->modes, head)
985 drm_mode_remove(connector, mode);
986
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400987 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
988 connector->connector_type_id);
989
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100990 ida_remove(&dev->mode_config.connector_ida,
991 connector->connector_id);
992
Boris Brezillonb5571e92014-07-22 12:09:10 +0200993 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000994 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300995 kfree(connector->name);
996 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800997 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000998 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100999
1000 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1001 if (connector->state && connector->funcs->atomic_destroy_state)
1002 connector->funcs->atomic_destroy_state(connector,
1003 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001004
1005 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001006}
1007EXPORT_SYMBOL(drm_connector_cleanup);
1008
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001009/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001010 * drm_connector_register - register a connector
1011 * @connector: the connector to register
1012 *
1013 * Register userspace interfaces for a connector
1014 *
1015 * Returns:
1016 * Zero on success, error code on failure.
1017 */
1018int drm_connector_register(struct drm_connector *connector)
1019{
Thomas Wood30f65702014-06-18 17:52:32 +01001020 int ret;
1021
Dave Airlie2ee39452014-07-24 11:53:45 +10001022 drm_mode_object_register(connector->dev, &connector->base);
1023
Thomas Wood30f65702014-06-18 17:52:32 +01001024 ret = drm_sysfs_connector_add(connector);
1025 if (ret)
1026 return ret;
1027
1028 ret = drm_debugfs_connector_add(connector);
1029 if (ret) {
1030 drm_sysfs_connector_remove(connector);
1031 return ret;
1032 }
1033
1034 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001035}
1036EXPORT_SYMBOL(drm_connector_register);
1037
1038/**
1039 * drm_connector_unregister - unregister a connector
1040 * @connector: the connector to unregister
1041 *
1042 * Unregister userspace interfaces for a connector
1043 */
1044void drm_connector_unregister(struct drm_connector *connector)
1045{
1046 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001047 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001048}
1049EXPORT_SYMBOL(drm_connector_unregister);
1050
Thomas Wood34ea3d32014-05-29 16:57:41 +01001051/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001052 * drm_connector_register_all - register all connectors
1053 * @dev: drm device
1054 *
1055 * This function registers all connectors in sysfs and other places so that
1056 * userspace can start to access them. Drivers can call it after calling
1057 * drm_dev_register() to complete the device registration, if they don't call
1058 * drm_connector_register() on each connector individually.
1059 *
1060 * When a device is unplugged and should be removed from userspace access,
1061 * call drm_connector_unregister_all(), which is the inverse of this
1062 * function.
1063 *
1064 * Returns:
1065 * Zero on success, error code on failure.
1066 */
1067int drm_connector_register_all(struct drm_device *dev)
1068{
1069 struct drm_connector *connector;
1070 int ret;
1071
1072 mutex_lock(&dev->mode_config.mutex);
1073
1074 drm_for_each_connector(connector, dev) {
1075 ret = drm_connector_register(connector);
1076 if (ret)
1077 goto err;
1078 }
1079
1080 mutex_unlock(&dev->mode_config.mutex);
1081
1082 return 0;
1083
1084err:
1085 mutex_unlock(&dev->mode_config.mutex);
1086 drm_connector_unregister_all(dev);
1087 return ret;
1088}
1089EXPORT_SYMBOL(drm_connector_register_all);
1090
1091/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001092 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001093 * @dev: drm device
1094 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001095 * This functions unregisters all connectors from sysfs and other places so
1096 * that userspace can no longer access them. Drivers should call this as the
1097 * first step tearing down the device instace, or when the underlying
1098 * physical device disappeared (e.g. USB unplug), right before calling
1099 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001100 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001101void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001102{
1103 struct drm_connector *connector;
1104
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001105 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001106 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001107 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001108}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001109EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001110
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001111/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001112 * drm_encoder_init - Init a preallocated encoder
1113 * @dev: drm device
1114 * @encoder: the encoder to init
1115 * @funcs: callbacks for this encoder
1116 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001117 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001118 *
1119 * Initialises a preallocated encoder. Encoder should be
1120 * subclassed as part of driver encoder objects.
1121 *
1122 * Returns:
1123 * Zero on success, error code on failure.
1124 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001125int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001126 struct drm_encoder *encoder,
1127 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001128 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001129{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001130 int ret;
1131
Daniel Vetter84849902012-12-02 00:28:11 +01001132 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001133
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001134 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1135 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001136 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001137
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001138 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001139 encoder->encoder_type = encoder_type;
1140 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001141 if (name) {
1142 va_list ap;
1143
1144 va_start(ap, name);
1145 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1146 va_end(ap);
1147 } else {
1148 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1149 drm_encoder_enum_list[encoder_type].name,
1150 encoder->base.id);
1151 }
Jani Nikulae5748942014-05-14 16:58:20 +03001152 if (!encoder->name) {
1153 ret = -ENOMEM;
1154 goto out_put;
1155 }
Dave Airlief453ba02008-11-07 14:05:41 -08001156
1157 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1158 dev->mode_config.num_encoder++;
1159
Jani Nikulae5748942014-05-14 16:58:20 +03001160out_put:
1161 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001162 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001163
1164out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001165 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001166
1167 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001168}
1169EXPORT_SYMBOL(drm_encoder_init);
1170
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001171/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001172 * drm_encoder_index - find the index of a registered encoder
1173 * @encoder: encoder to find index for
1174 *
1175 * Given a registered encoder, return the index of that encoder within a DRM
1176 * device's list of encoders.
1177 */
1178unsigned int drm_encoder_index(struct drm_encoder *encoder)
1179{
1180 unsigned int index = 0;
1181 struct drm_encoder *tmp;
1182
1183 drm_for_each_encoder(tmp, encoder->dev) {
1184 if (tmp == encoder)
1185 return index;
1186
1187 index++;
1188 }
1189
1190 BUG();
1191}
1192EXPORT_SYMBOL(drm_encoder_index);
1193
1194/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001195 * drm_encoder_cleanup - cleans up an initialised encoder
1196 * @encoder: encoder to cleanup
1197 *
1198 * Cleans up the encoder but doesn't free the object.
1199 */
Dave Airlief453ba02008-11-07 14:05:41 -08001200void drm_encoder_cleanup(struct drm_encoder *encoder)
1201{
1202 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001203
Daniel Vetter84849902012-12-02 00:28:11 +01001204 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001205 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001206 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001207 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001208 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001209 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001210
1211 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001212}
1213EXPORT_SYMBOL(drm_encoder_cleanup);
1214
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001215static unsigned int drm_num_planes(struct drm_device *dev)
1216{
1217 unsigned int num = 0;
1218 struct drm_plane *tmp;
1219
1220 drm_for_each_plane(tmp, dev) {
1221 num++;
1222 }
1223
1224 return num;
1225}
1226
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001227/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001228 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001229 * @dev: DRM device
1230 * @plane: plane object to init
1231 * @possible_crtcs: bitmask of possible CRTCs
1232 * @funcs: callbacks for the new plane
1233 * @formats: array of supported formats (%DRM_FORMAT_*)
1234 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001235 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001236 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001237 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001238 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001239 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001240 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001241 * Zero on success, error code on failure.
1242 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001243int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1244 unsigned long possible_crtcs,
1245 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001246 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001247 enum drm_plane_type type,
1248 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001249{
Rob Clark6b4959f2014-12-18 16:01:53 -05001250 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001251 int ret;
1252
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001253 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1254 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001255 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001256
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001257 drm_modeset_lock_init(&plane->mutex);
1258
Rob Clark4d939142012-05-17 02:23:27 -06001259 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001260 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001261 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001262 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1263 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001264 if (!plane->format_types) {
1265 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001266 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001267 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001268 }
1269
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001270 if (name) {
1271 va_list ap;
1272
1273 va_start(ap, name);
1274 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1275 va_end(ap);
1276 } else {
1277 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1278 drm_num_planes(dev));
1279 }
1280 if (!plane->name) {
1281 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001282 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001283 return -ENOMEM;
1284 }
1285
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001286 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001287 plane->format_count = format_count;
1288 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001289 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001290
Rob Clark6b4959f2014-12-18 16:01:53 -05001291 list_add_tail(&plane->head, &config->plane_list);
1292 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001293 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001294 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001295
Rob Clark9922ab52014-04-01 20:16:57 -04001296 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001297 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001298 plane->type);
1299
Rob Clark6b4959f2014-12-18 16:01:53 -05001300 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1301 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1302 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1303 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1304 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1305 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1306 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1307 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1308 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1309 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1310 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1311 }
1312
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001313 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001314}
Matt Roperdc415ff2014-04-01 15:22:36 -07001315EXPORT_SYMBOL(drm_universal_plane_init);
1316
1317/**
1318 * drm_plane_init - Initialize a legacy plane
1319 * @dev: DRM device
1320 * @plane: plane object to init
1321 * @possible_crtcs: bitmask of possible CRTCs
1322 * @funcs: callbacks for the new plane
1323 * @formats: array of supported formats (%DRM_FORMAT_*)
1324 * @format_count: number of elements in @formats
1325 * @is_primary: plane type (primary vs overlay)
1326 *
1327 * Legacy API to initialize a DRM plane.
1328 *
1329 * New drivers should call drm_universal_plane_init() instead.
1330 *
1331 * Returns:
1332 * Zero on success, error code on failure.
1333 */
1334int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1335 unsigned long possible_crtcs,
1336 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001337 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001338 bool is_primary)
1339{
1340 enum drm_plane_type type;
1341
1342 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1343 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001344 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001345}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001346EXPORT_SYMBOL(drm_plane_init);
1347
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001348/**
1349 * drm_plane_cleanup - Clean up the core plane usage
1350 * @plane: plane to cleanup
1351 *
1352 * This function cleans up @plane and removes it from the DRM mode setting
1353 * core. Note that the function does *not* free the plane structure itself,
1354 * this is the responsibility of the caller.
1355 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001356void drm_plane_cleanup(struct drm_plane *plane)
1357{
1358 struct drm_device *dev = plane->dev;
1359
Daniel Vetter84849902012-12-02 00:28:11 +01001360 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001361 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001362 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001363
1364 BUG_ON(list_empty(&plane->head));
1365
1366 list_del(&plane->head);
1367 dev->mode_config.num_total_plane--;
1368 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1369 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001370 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001371
1372 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1373 if (plane->state && plane->funcs->atomic_destroy_state)
1374 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001375
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001376 kfree(plane->name);
1377
Thierry Redinga18c0af2014-12-10 11:38:49 +01001378 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001379}
1380EXPORT_SYMBOL(drm_plane_cleanup);
1381
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001382/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001383 * drm_plane_index - find the index of a registered plane
1384 * @plane: plane to find index for
1385 *
1386 * Given a registered plane, return the index of that CRTC within a DRM
1387 * device's list of planes.
1388 */
1389unsigned int drm_plane_index(struct drm_plane *plane)
1390{
1391 unsigned int index = 0;
1392 struct drm_plane *tmp;
1393
Daniel Vettere4f62542015-07-09 23:44:35 +02001394 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001395 if (tmp == plane)
1396 return index;
1397
1398 index++;
1399 }
1400
1401 BUG();
1402}
1403EXPORT_SYMBOL(drm_plane_index);
1404
1405/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001406 * drm_plane_from_index - find the registered plane at an index
1407 * @dev: DRM device
1408 * @idx: index of registered plane to find for
1409 *
1410 * Given a plane index, return the registered plane from DRM device's
1411 * list of planes with matching index.
1412 */
1413struct drm_plane *
1414drm_plane_from_index(struct drm_device *dev, int idx)
1415{
1416 struct drm_plane *plane;
1417 unsigned int i = 0;
1418
Daniel Vetter6295d602015-07-09 23:44:25 +02001419 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001420 if (i == idx)
1421 return plane;
1422 i++;
1423 }
1424 return NULL;
1425}
1426EXPORT_SYMBOL(drm_plane_from_index);
1427
1428/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001429 * drm_plane_force_disable - Forcibly disable a plane
1430 * @plane: plane to disable
1431 *
1432 * Forces the plane to be disabled.
1433 *
1434 * Used when the plane's current framebuffer is destroyed,
1435 * and when restoring fbdev mode.
1436 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001437void drm_plane_force_disable(struct drm_plane *plane)
1438{
1439 int ret;
1440
Daniel Vetter3d30a592014-07-27 13:42:42 +02001441 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001442 return;
1443
Daniel Vetter3d30a592014-07-27 13:42:42 +02001444 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001445 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001446 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001447 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001448 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001449 return;
1450 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001451 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001452 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001453 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001454 plane->fb = NULL;
1455 plane->crtc = NULL;
1456}
1457EXPORT_SYMBOL(drm_plane_force_disable);
1458
Rob Clark6b4959f2014-12-18 16:01:53 -05001459static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001460{
Rob Clark356af0e2014-12-18 16:01:52 -05001461 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001462
1463 /*
1464 * Standard properties (apply to all connectors)
1465 */
Rob Clark356af0e2014-12-18 16:01:52 -05001466 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001467 DRM_MODE_PROP_IMMUTABLE,
1468 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001469 if (!prop)
1470 return -ENOMEM;
1471 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001472
Rob Clark356af0e2014-12-18 16:01:52 -05001473 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001474 "DPMS", drm_dpms_enum_list,
1475 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001476 if (!prop)
1477 return -ENOMEM;
1478 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001479
Rob Clark356af0e2014-12-18 16:01:52 -05001480 prop = drm_property_create(dev,
1481 DRM_MODE_PROP_BLOB |
1482 DRM_MODE_PROP_IMMUTABLE,
1483 "PATH", 0);
1484 if (!prop)
1485 return -ENOMEM;
1486 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001487
Rob Clark356af0e2014-12-18 16:01:52 -05001488 prop = drm_property_create(dev,
1489 DRM_MODE_PROP_BLOB |
1490 DRM_MODE_PROP_IMMUTABLE,
1491 "TILE", 0);
1492 if (!prop)
1493 return -ENOMEM;
1494 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001495
Rob Clark6b4959f2014-12-18 16:01:53 -05001496 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001497 "type", drm_plane_type_enum_list,
1498 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001499 if (!prop)
1500 return -ENOMEM;
1501 dev->mode_config.plane_type_property = prop;
1502
1503 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1504 "SRC_X", 0, UINT_MAX);
1505 if (!prop)
1506 return -ENOMEM;
1507 dev->mode_config.prop_src_x = prop;
1508
1509 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1510 "SRC_Y", 0, UINT_MAX);
1511 if (!prop)
1512 return -ENOMEM;
1513 dev->mode_config.prop_src_y = prop;
1514
1515 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1516 "SRC_W", 0, UINT_MAX);
1517 if (!prop)
1518 return -ENOMEM;
1519 dev->mode_config.prop_src_w = prop;
1520
1521 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1522 "SRC_H", 0, UINT_MAX);
1523 if (!prop)
1524 return -ENOMEM;
1525 dev->mode_config.prop_src_h = prop;
1526
1527 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1528 "CRTC_X", INT_MIN, INT_MAX);
1529 if (!prop)
1530 return -ENOMEM;
1531 dev->mode_config.prop_crtc_x = prop;
1532
1533 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1534 "CRTC_Y", INT_MIN, INT_MAX);
1535 if (!prop)
1536 return -ENOMEM;
1537 dev->mode_config.prop_crtc_y = prop;
1538
1539 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1540 "CRTC_W", 0, INT_MAX);
1541 if (!prop)
1542 return -ENOMEM;
1543 dev->mode_config.prop_crtc_w = prop;
1544
1545 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1546 "CRTC_H", 0, INT_MAX);
1547 if (!prop)
1548 return -ENOMEM;
1549 dev->mode_config.prop_crtc_h = prop;
1550
1551 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1552 "FB_ID", DRM_MODE_OBJECT_FB);
1553 if (!prop)
1554 return -ENOMEM;
1555 dev->mode_config.prop_fb_id = prop;
1556
1557 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1558 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1559 if (!prop)
1560 return -ENOMEM;
1561 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001562
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001563 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1564 "ACTIVE");
1565 if (!prop)
1566 return -ENOMEM;
1567 dev->mode_config.prop_active = prop;
1568
Daniel Stone955f3c32015-05-25 19:11:52 +01001569 prop = drm_property_create(dev,
1570 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1571 "MODE_ID", 0);
1572 if (!prop)
1573 return -ENOMEM;
1574 dev->mode_config.prop_mode_id = prop;
1575
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001576 prop = drm_property_create(dev,
1577 DRM_MODE_PROP_BLOB,
1578 "DEGAMMA_LUT", 0);
1579 if (!prop)
1580 return -ENOMEM;
1581 dev->mode_config.degamma_lut_property = prop;
1582
1583 prop = drm_property_create_range(dev,
1584 DRM_MODE_PROP_IMMUTABLE,
1585 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1586 if (!prop)
1587 return -ENOMEM;
1588 dev->mode_config.degamma_lut_size_property = prop;
1589
1590 prop = drm_property_create(dev,
1591 DRM_MODE_PROP_BLOB,
1592 "CTM", 0);
1593 if (!prop)
1594 return -ENOMEM;
1595 dev->mode_config.ctm_property = prop;
1596
1597 prop = drm_property_create(dev,
1598 DRM_MODE_PROP_BLOB,
1599 "GAMMA_LUT", 0);
1600 if (!prop)
1601 return -ENOMEM;
1602 dev->mode_config.gamma_lut_property = prop;
1603
1604 prop = drm_property_create_range(dev,
1605 DRM_MODE_PROP_IMMUTABLE,
1606 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1607 if (!prop)
1608 return -ENOMEM;
1609 dev->mode_config.gamma_lut_size_property = prop;
1610
Rob Clark9922ab52014-04-01 20:16:57 -04001611 return 0;
1612}
1613
Dave Airlief453ba02008-11-07 14:05:41 -08001614/**
1615 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1616 * @dev: DRM device
1617 *
1618 * Called by a driver the first time a DVI-I connector is made.
1619 */
1620int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1621{
1622 struct drm_property *dvi_i_selector;
1623 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001624
1625 if (dev->mode_config.dvi_i_select_subconnector_property)
1626 return 0;
1627
1628 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001629 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001630 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001631 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001632 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001633 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1634
Sascha Hauer4a67d392012-02-06 10:58:17 +01001635 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001636 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001637 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001638 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001639 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1640
1641 return 0;
1642}
1643EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1644
1645/**
1646 * drm_create_tv_properties - create TV specific connector properties
1647 * @dev: DRM device
1648 * @num_modes: number of different TV formats (modes) supported
1649 * @modes: array of pointers to strings containing name of each format
1650 *
1651 * Called by a driver's TV initialization routine, this function creates
1652 * the TV specific connector properties for a given device. Caller is
1653 * responsible for allocating a list of format names and passing them to
1654 * this routine.
1655 */
Thierry Reding2f763312014-10-13 12:45:57 +02001656int drm_mode_create_tv_properties(struct drm_device *dev,
1657 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001658 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001659{
1660 struct drm_property *tv_selector;
1661 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001662 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001663
1664 if (dev->mode_config.tv_select_subconnector_property)
1665 return 0;
1666
1667 /*
1668 * Basic connector properties
1669 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001670 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001671 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001672 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001673 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001674 if (!tv_selector)
1675 goto nomem;
1676
Dave Airlief453ba02008-11-07 14:05:41 -08001677 dev->mode_config.tv_select_subconnector_property = tv_selector;
1678
1679 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001680 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1681 "subconnector",
1682 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001683 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001684 if (!tv_subconnector)
1685 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001686 dev->mode_config.tv_subconnector_property = tv_subconnector;
1687
1688 /*
1689 * Other, TV specific properties: margins & TV modes.
1690 */
1691 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001692 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001693 if (!dev->mode_config.tv_left_margin_property)
1694 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001695
1696 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001697 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001698 if (!dev->mode_config.tv_right_margin_property)
1699 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001700
1701 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001702 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001703 if (!dev->mode_config.tv_top_margin_property)
1704 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001705
1706 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001707 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001708 if (!dev->mode_config.tv_bottom_margin_property)
1709 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001710
1711 dev->mode_config.tv_mode_property =
1712 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1713 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001714 if (!dev->mode_config.tv_mode_property)
1715 goto nomem;
1716
Dave Airlief453ba02008-11-07 14:05:41 -08001717 for (i = 0; i < num_modes; i++)
1718 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1719 i, modes[i]);
1720
Francisco Jerezb6b79022009-08-02 04:19:20 +02001721 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001722 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001723 if (!dev->mode_config.tv_brightness_property)
1724 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001725
1726 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001727 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001728 if (!dev->mode_config.tv_contrast_property)
1729 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001730
1731 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001732 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001733 if (!dev->mode_config.tv_flicker_reduction_property)
1734 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001735
Francisco Jereza75f0232009-08-12 02:30:10 +02001736 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001737 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001738 if (!dev->mode_config.tv_overscan_property)
1739 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001740
1741 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001742 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001743 if (!dev->mode_config.tv_saturation_property)
1744 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001745
1746 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001747 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001748 if (!dev->mode_config.tv_hue_property)
1749 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001750
Dave Airlief453ba02008-11-07 14:05:41 -08001751 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001752nomem:
1753 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001754}
1755EXPORT_SYMBOL(drm_mode_create_tv_properties);
1756
1757/**
1758 * drm_mode_create_scaling_mode_property - create scaling mode property
1759 * @dev: DRM device
1760 *
1761 * Called by a driver the first time it's needed, must be attached to desired
1762 * connectors.
1763 */
1764int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1765{
1766 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001767
1768 if (dev->mode_config.scaling_mode_property)
1769 return 0;
1770
1771 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001772 drm_property_create_enum(dev, 0, "scaling mode",
1773 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001774 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001775
1776 dev->mode_config.scaling_mode_property = scaling_mode;
1777
1778 return 0;
1779}
1780EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1781
1782/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301783 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1784 * @dev: DRM device
1785 *
1786 * Called by a driver the first time it's needed, must be attached to desired
1787 * connectors.
1788 *
1789 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001790 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301791 */
1792int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1793{
1794 if (dev->mode_config.aspect_ratio_property)
1795 return 0;
1796
1797 dev->mode_config.aspect_ratio_property =
1798 drm_property_create_enum(dev, 0, "aspect ratio",
1799 drm_aspect_ratio_enum_list,
1800 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1801
1802 if (dev->mode_config.aspect_ratio_property == NULL)
1803 return -ENOMEM;
1804
1805 return 0;
1806}
1807EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1808
1809/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001810 * drm_mode_create_dirty_property - create dirty property
1811 * @dev: DRM device
1812 *
1813 * Called by a driver the first time it's needed, must be attached to desired
1814 * connectors.
1815 */
1816int drm_mode_create_dirty_info_property(struct drm_device *dev)
1817{
1818 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001819
1820 if (dev->mode_config.dirty_info_property)
1821 return 0;
1822
1823 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001824 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001825 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001826 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001827 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001828 dev->mode_config.dirty_info_property = dirty_info;
1829
1830 return 0;
1831}
1832EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1833
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001834/**
1835 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1836 * @dev: DRM device
1837 *
1838 * Create the the suggested x/y offset property for connectors.
1839 */
1840int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1841{
1842 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1843 return 0;
1844
1845 dev->mode_config.suggested_x_property =
1846 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1847
1848 dev->mode_config.suggested_y_property =
1849 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1850
1851 if (dev->mode_config.suggested_x_property == NULL ||
1852 dev->mode_config.suggested_y_property == NULL)
1853 return -ENOMEM;
1854 return 0;
1855}
1856EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1857
Dave Airlief453ba02008-11-07 14:05:41 -08001858/**
Dave Airlief453ba02008-11-07 14:05:41 -08001859 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001860 * @dev: drm device for the ioctl
1861 * @data: data pointer for the ioctl
1862 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001863 *
Dave Airlief453ba02008-11-07 14:05:41 -08001864 * Construct a set of configuration description structures and return
1865 * them to the user, including CRTC, connector and framebuffer configuration.
1866 *
1867 * Called by the user via ioctl.
1868 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001869 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001870 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001871 */
1872int drm_mode_getresources(struct drm_device *dev, void *data,
1873 struct drm_file *file_priv)
1874{
1875 struct drm_mode_card_res *card_res = data;
1876 struct list_head *lh;
1877 struct drm_framebuffer *fb;
1878 struct drm_connector *connector;
1879 struct drm_crtc *crtc;
1880 struct drm_encoder *encoder;
1881 int ret = 0;
1882 int connector_count = 0;
1883 int crtc_count = 0;
1884 int fb_count = 0;
1885 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001886 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001887 uint32_t __user *fb_id;
1888 uint32_t __user *crtc_id;
1889 uint32_t __user *connector_id;
1890 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001891
Dave Airliefb3b06c2011-02-08 13:55:21 +10001892 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1893 return -EINVAL;
1894
Dave Airlief453ba02008-11-07 14:05:41 -08001895
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001896 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001897 /*
1898 * For the non-control nodes we need to limit the list of resources
1899 * by IDs in the group list for this node
1900 */
1901 list_for_each(lh, &file_priv->fbs)
1902 fb_count++;
1903
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001904 /* handle this in 4 parts */
1905 /* FBs */
1906 if (card_res->count_fbs >= fb_count) {
1907 copied = 0;
1908 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1909 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1910 if (put_user(fb->base.id, fb_id + copied)) {
1911 mutex_unlock(&file_priv->fbs_lock);
1912 return -EFAULT;
1913 }
1914 copied++;
1915 }
1916 }
1917 card_res->count_fbs = fb_count;
1918 mutex_unlock(&file_priv->fbs_lock);
1919
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001920 /* mode_config.mutex protects the connector list against e.g. DP MST
1921 * connector hot-adding. CRTC/Plane lists are invariant. */
1922 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001923 drm_for_each_crtc(crtc, dev)
1924 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001925
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001926 drm_for_each_connector(connector, dev)
1927 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001928
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001929 drm_for_each_encoder(encoder, dev)
1930 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001931
1932 card_res->max_height = dev->mode_config.max_height;
1933 card_res->min_height = dev->mode_config.min_height;
1934 card_res->max_width = dev->mode_config.max_width;
1935 card_res->min_width = dev->mode_config.min_width;
1936
Dave Airlief453ba02008-11-07 14:05:41 -08001937 /* CRTCs */
1938 if (card_res->count_crtcs >= crtc_count) {
1939 copied = 0;
1940 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001941 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001942 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1943 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001944 if (put_user(crtc->base.id, crtc_id + copied)) {
1945 ret = -EFAULT;
1946 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001947 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001948 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001949 }
1950 }
1951 card_res->count_crtcs = crtc_count;
1952
1953 /* Encoders */
1954 if (card_res->count_encoders >= encoder_count) {
1955 copied = 0;
1956 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001957 drm_for_each_encoder(encoder, dev) {
1958 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1959 encoder->name);
1960 if (put_user(encoder->base.id, encoder_id +
1961 copied)) {
1962 ret = -EFAULT;
1963 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001964 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001965 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 }
1967 }
1968 card_res->count_encoders = encoder_count;
1969
1970 /* Connectors */
1971 if (card_res->count_connectors >= connector_count) {
1972 copied = 0;
1973 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001974 drm_for_each_connector(connector, dev) {
1975 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1976 connector->base.id,
1977 connector->name);
1978 if (put_user(connector->base.id,
1979 connector_id + copied)) {
1980 ret = -EFAULT;
1981 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001982 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001983 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001984 }
1985 }
1986 card_res->count_connectors = connector_count;
1987
Jerome Glisse94401062010-07-15 15:43:25 -04001988 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001989 card_res->count_connectors, card_res->count_encoders);
1990
1991out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001992 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001993 return ret;
1994}
1995
1996/**
1997 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001998 * @dev: drm device for the ioctl
1999 * @data: data pointer for the ioctl
2000 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002001 *
Dave Airlief453ba02008-11-07 14:05:41 -08002002 * Construct a CRTC configuration structure to return to the user.
2003 *
2004 * Called by the user via ioctl.
2005 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002006 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002007 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002008 */
2009int drm_mode_getcrtc(struct drm_device *dev,
2010 void *data, struct drm_file *file_priv)
2011{
2012 struct drm_mode_crtc *crtc_resp = data;
2013 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002014
Dave Airliefb3b06c2011-02-08 13:55:21 +10002015 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2016 return -EINVAL;
2017
Rob Clarka2b34e22013-10-05 16:36:52 -04002018 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002019 if (!crtc)
2020 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002021
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002022 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002023 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002024 if (crtc->primary->fb)
2025 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002026 else
2027 crtc_resp->fb_id = 0;
2028
Daniel Vetter31c946e2015-02-22 12:24:17 +01002029 if (crtc->state) {
2030 crtc_resp->x = crtc->primary->state->src_x >> 16;
2031 crtc_resp->y = crtc->primary->state->src_y >> 16;
2032 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002033 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002034 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002035
Daniel Vetter31c946e2015-02-22 12:24:17 +01002036 } else {
2037 crtc_resp->mode_valid = 0;
2038 }
Dave Airlief453ba02008-11-07 14:05:41 -08002039 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002040 crtc_resp->x = crtc->x;
2041 crtc_resp->y = crtc->y;
2042 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002043 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002044 crtc_resp->mode_valid = 1;
2045
2046 } else {
2047 crtc_resp->mode_valid = 0;
2048 }
Dave Airlief453ba02008-11-07 14:05:41 -08002049 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002050 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002051
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002052 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002053}
2054
Damien Lespiau61d8e322013-09-25 16:45:22 +01002055static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2056 const struct drm_file *file_priv)
2057{
2058 /*
2059 * If user-space hasn't configured the driver to expose the stereo 3D
2060 * modes, don't expose them.
2061 */
2062 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2063 return false;
2064
2065 return true;
2066}
2067
Daniel Vetterabd69c52014-11-25 23:50:05 +01002068static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2069{
2070 /* For atomic drivers only state objects are synchronously updated and
2071 * protected by modeset locks, so check those first. */
2072 if (connector->state)
2073 return connector->state->best_encoder;
2074 return connector->encoder;
2075}
2076
Rob Clark95cbf112014-12-18 16:01:49 -05002077/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002078static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002079 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2080 uint32_t *arg_count_props)
2081{
Rob Clark88a48e22014-12-18 16:01:50 -05002082 int props_count;
2083 int i, ret, copied;
2084
2085 props_count = obj->properties->count;
2086 if (!atomic)
2087 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002088
2089 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002090 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002091 struct drm_property *prop = obj->properties->properties[i];
2092 uint64_t val;
2093
Rob Clark88a48e22014-12-18 16:01:50 -05002094 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2095 continue;
2096
Rob Clark95cbf112014-12-18 16:01:49 -05002097 ret = drm_object_property_get_value(obj, prop, &val);
2098 if (ret)
2099 return ret;
2100
2101 if (put_user(prop->base.id, prop_ptr + copied))
2102 return -EFAULT;
2103
2104 if (put_user(val, prop_values + copied))
2105 return -EFAULT;
2106
2107 copied++;
2108 }
2109 }
2110 *arg_count_props = props_count;
2111
2112 return 0;
2113}
2114
Dave Airlief453ba02008-11-07 14:05:41 -08002115/**
2116 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002117 * @dev: drm device for the ioctl
2118 * @data: data pointer for the ioctl
2119 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002120 *
Dave Airlief453ba02008-11-07 14:05:41 -08002121 * Construct a connector configuration structure to return to the user.
2122 *
2123 * Called by the user via ioctl.
2124 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002125 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002126 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002127 */
2128int drm_mode_getconnector(struct drm_device *dev, void *data,
2129 struct drm_file *file_priv)
2130{
2131 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002132 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002133 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002134 struct drm_display_mode *mode;
2135 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002136 int encoders_count = 0;
2137 int ret = 0;
2138 int copied = 0;
2139 int i;
2140 struct drm_mode_modeinfo u_mode;
2141 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002142 uint32_t __user *encoder_ptr;
2143
Dave Airliefb3b06c2011-02-08 13:55:21 +10002144 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2145 return -EINVAL;
2146
Dave Airlief453ba02008-11-07 14:05:41 -08002147 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2148
Jerome Glisse94401062010-07-15 15:43:25 -04002149 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002150
Daniel Vetter7b240562012-12-12 00:35:33 +01002151 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002152
Rob Clarka2b34e22013-10-05 16:36:52 -04002153 connector = drm_connector_find(dev, out_resp->connector_id);
2154 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002155 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002156 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002157 }
Dave Airlief453ba02008-11-07 14:05:41 -08002158
Thierry Reding01073b02014-12-10 13:03:38 +01002159 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2160 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002161 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002162
2163 if (out_resp->count_modes == 0) {
2164 connector->funcs->fill_modes(connector,
2165 dev->mode_config.max_width,
2166 dev->mode_config.max_height);
2167 }
2168
2169 /* delayed so we get modes regardless of pre-fill_modes state */
2170 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002171 if (drm_mode_expose_to_userspace(mode, file_priv))
2172 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002173
2174 out_resp->connector_id = connector->base.id;
2175 out_resp->connector_type = connector->connector_type;
2176 out_resp->connector_type_id = connector->connector_type_id;
2177 out_resp->mm_width = connector->display_info.width_mm;
2178 out_resp->mm_height = connector->display_info.height_mm;
2179 out_resp->subpixel = connector->display_info.subpixel_order;
2180 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002181
2182 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002183 encoder = drm_connector_get_encoder(connector);
2184 if (encoder)
2185 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002186 else
2187 out_resp->encoder_id = 0;
2188
2189 /*
2190 * This ioctl is called twice, once to determine how much space is
2191 * needed, and the 2nd time to fill it.
2192 */
2193 if ((out_resp->count_modes >= mode_count) && mode_count) {
2194 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002195 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002196 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002197 if (!drm_mode_expose_to_userspace(mode, file_priv))
2198 continue;
2199
Daniel Stone934a8a82015-05-22 13:34:48 +01002200 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002201 if (copy_to_user(mode_ptr + copied,
2202 &u_mode, sizeof(u_mode))) {
2203 ret = -EFAULT;
2204 goto out;
2205 }
2206 copied++;
2207 }
2208 }
2209 out_resp->count_modes = mode_count;
2210
Rob Clark88a48e22014-12-18 16:01:50 -05002211 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002212 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2213 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2214 &out_resp->count_props);
2215 if (ret)
2216 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002217
2218 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2219 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002220 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002221 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2222 if (connector->encoder_ids[i] != 0) {
2223 if (put_user(connector->encoder_ids[i],
2224 encoder_ptr + copied)) {
2225 ret = -EFAULT;
2226 goto out;
2227 }
2228 copied++;
2229 }
2230 }
2231 }
2232 out_resp->count_encoders = encoders_count;
2233
2234out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002236
2237out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002238 mutex_unlock(&dev->mode_config.mutex);
2239
Dave Airlief453ba02008-11-07 14:05:41 -08002240 return ret;
2241}
2242
Daniel Vetterabd69c52014-11-25 23:50:05 +01002243static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2244{
2245 struct drm_connector *connector;
2246 struct drm_device *dev = encoder->dev;
2247 bool uses_atomic = false;
2248
2249 /* For atomic drivers only state objects are synchronously updated and
2250 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002251 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002252 if (!connector->state)
2253 continue;
2254
2255 uses_atomic = true;
2256
2257 if (connector->state->best_encoder != encoder)
2258 continue;
2259
2260 return connector->state->crtc;
2261 }
2262
2263 /* Don't return stale data (e.g. pending async disable). */
2264 if (uses_atomic)
2265 return NULL;
2266
2267 return encoder->crtc;
2268}
2269
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002270/**
2271 * drm_mode_getencoder - get encoder configuration
2272 * @dev: drm device for the ioctl
2273 * @data: data pointer for the ioctl
2274 * @file_priv: drm file for the ioctl call
2275 *
2276 * Construct a encoder configuration structure to return to the user.
2277 *
2278 * Called by the user via ioctl.
2279 *
2280 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002281 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002282 */
Dave Airlief453ba02008-11-07 14:05:41 -08002283int drm_mode_getencoder(struct drm_device *dev, void *data,
2284 struct drm_file *file_priv)
2285{
2286 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002287 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002288 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002289
Dave Airliefb3b06c2011-02-08 13:55:21 +10002290 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2291 return -EINVAL;
2292
Rob Clarka2b34e22013-10-05 16:36:52 -04002293 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002294 if (!encoder)
2295 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002296
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002297 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002298 crtc = drm_encoder_get_crtc(encoder);
2299 if (crtc)
2300 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002301 else
2302 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002303 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2304
Dave Airlief453ba02008-11-07 14:05:41 -08002305 enc_resp->encoder_type = encoder->encoder_type;
2306 enc_resp->encoder_id = encoder->base.id;
2307 enc_resp->possible_crtcs = encoder->possible_crtcs;
2308 enc_resp->possible_clones = encoder->possible_clones;
2309
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002310 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002311}
2312
2313/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002314 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002315 * @dev: DRM device
2316 * @data: ioctl data
2317 * @file_priv: DRM file info
2318 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002319 * Construct a list of plane ids to return to the user.
2320 *
2321 * Called by the user via ioctl.
2322 *
2323 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002324 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325 */
2326int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002327 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328{
2329 struct drm_mode_get_plane_res *plane_resp = data;
2330 struct drm_mode_config *config;
2331 struct drm_plane *plane;
2332 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002333 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002334 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002335
2336 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2337 return -EINVAL;
2338
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002339 config = &dev->mode_config;
2340
Matt Roper681e7ec2014-04-01 15:22:42 -07002341 if (file_priv->universal_planes)
2342 num_planes = config->num_total_plane;
2343 else
2344 num_planes = config->num_overlay_plane;
2345
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002346 /*
2347 * This ioctl is called twice, once to determine how much space is
2348 * needed, and the 2nd time to fill it.
2349 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002350 if (num_planes &&
2351 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002352 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002353
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002354 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002355 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002356 /*
2357 * Unless userspace set the 'universal planes'
2358 * capability bit, only advertise overlays.
2359 */
2360 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2361 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002362 continue;
2363
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002364 if (put_user(plane->base.id, plane_ptr + copied))
2365 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002366 copied++;
2367 }
2368 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002369 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002370
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002371 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372}
2373
2374/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002375 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002376 * @dev: DRM device
2377 * @data: ioctl data
2378 * @file_priv: DRM file info
2379 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002380 * Construct a plane configuration structure to return to the user.
2381 *
2382 * Called by the user via ioctl.
2383 *
2384 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002385 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002386 */
2387int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002388 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389{
2390 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002391 struct drm_plane *plane;
2392 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002393
2394 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2395 return -EINVAL;
2396
Rob Clarka2b34e22013-10-05 16:36:52 -04002397 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002398 if (!plane)
2399 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002400
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002401 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002402 if (plane->crtc)
2403 plane_resp->crtc_id = plane->crtc->base.id;
2404 else
2405 plane_resp->crtc_id = 0;
2406
2407 if (plane->fb)
2408 plane_resp->fb_id = plane->fb->base.id;
2409 else
2410 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002411 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412
2413 plane_resp->plane_id = plane->base.id;
2414 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002415 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002416
2417 /*
2418 * This ioctl is called twice, once to determine how much space is
2419 * needed, and the 2nd time to fill it.
2420 */
2421 if (plane->format_count &&
2422 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002423 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002424 if (copy_to_user(format_ptr,
2425 plane->format_types,
2426 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002427 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002428 }
2429 }
2430 plane_resp->count_format_types = plane->format_count;
2431
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002432 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002433}
2434
Laurent Pinchartead86102015-03-05 02:25:43 +02002435/**
2436 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2437 * @plane: plane to check for format support
2438 * @format: the pixel format
2439 *
2440 * Returns:
2441 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2442 * otherwise.
2443 */
2444int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2445{
2446 unsigned int i;
2447
2448 for (i = 0; i < plane->format_count; i++) {
2449 if (format == plane->format_types[i])
2450 return 0;
2451 }
2452
2453 return -EINVAL;
2454}
2455
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002456static int check_src_coords(uint32_t src_x, uint32_t src_y,
2457 uint32_t src_w, uint32_t src_h,
2458 const struct drm_framebuffer *fb)
2459{
2460 unsigned int fb_width, fb_height;
2461
2462 fb_width = fb->width << 16;
2463 fb_height = fb->height << 16;
2464
2465 /* Make sure source coordinates are inside the fb. */
2466 if (src_w > fb_width ||
2467 src_x > fb_width - src_w ||
2468 src_h > fb_height ||
2469 src_y > fb_height - src_h) {
2470 DRM_DEBUG_KMS("Invalid source coordinates "
2471 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2472 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2473 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2474 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2475 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2476 return -ENOSPC;
2477 }
2478
2479 return 0;
2480}
2481
Matt Roperb36552b2014-06-10 08:28:09 -07002482/*
2483 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002484 *
Matt Roperb36552b2014-06-10 08:28:09 -07002485 * Note that we assume an extra reference has already been taken on fb. If the
2486 * update fails, this reference will be dropped before return; if it succeeds,
2487 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002488 *
Matt Roperb36552b2014-06-10 08:28:09 -07002489 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002490 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002491static int __setplane_internal(struct drm_plane *plane,
2492 struct drm_crtc *crtc,
2493 struct drm_framebuffer *fb,
2494 int32_t crtc_x, int32_t crtc_y,
2495 uint32_t crtc_w, uint32_t crtc_h,
2496 /* src_{x,y,w,h} values are 16.16 fixed point */
2497 uint32_t src_x, uint32_t src_y,
2498 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002499{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002500 int ret = 0;
2501
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002503 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002504 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002505 ret = plane->funcs->disable_plane(plane);
2506 if (!ret) {
2507 plane->crtc = NULL;
2508 plane->fb = NULL;
2509 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002510 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002511 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002512 goto out;
2513 }
2514
Matt Roper7f994f32014-05-29 08:06:51 -07002515 /* Check whether this plane is usable on this CRTC */
2516 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2517 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521
Ville Syrjälä62443be2011-12-20 00:06:47 +02002522 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002523 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2524 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002525 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2526 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002527 goto out;
2528 }
2529
Matt Roper3968be92015-04-13 11:06:13 -07002530 /* Give drivers some help against integer overflows */
2531 if (crtc_w > INT_MAX ||
2532 crtc_x > INT_MAX - (int32_t) crtc_w ||
2533 crtc_h > INT_MAX ||
2534 crtc_y > INT_MAX - (int32_t) crtc_h) {
2535 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2536 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002537 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002538 goto out;
2539 }
2540
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002541 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2542 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002543 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002544
Daniel Vetter3d30a592014-07-27 13:42:42 +02002545 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002546 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002547 crtc_x, crtc_y, crtc_w, crtc_h,
2548 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002549 if (!ret) {
2550 plane->crtc = crtc;
2551 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002552 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002553 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002554 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002555 }
2556
2557out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002558 if (fb)
2559 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002560 if (plane->old_fb)
2561 drm_framebuffer_unreference(plane->old_fb);
2562 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002563
2564 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002565}
Matt Roperb36552b2014-06-10 08:28:09 -07002566
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002567static int setplane_internal(struct drm_plane *plane,
2568 struct drm_crtc *crtc,
2569 struct drm_framebuffer *fb,
2570 int32_t crtc_x, int32_t crtc_y,
2571 uint32_t crtc_w, uint32_t crtc_h,
2572 /* src_{x,y,w,h} values are 16.16 fixed point */
2573 uint32_t src_x, uint32_t src_y,
2574 uint32_t src_w, uint32_t src_h)
2575{
2576 int ret;
2577
2578 drm_modeset_lock_all(plane->dev);
2579 ret = __setplane_internal(plane, crtc, fb,
2580 crtc_x, crtc_y, crtc_w, crtc_h,
2581 src_x, src_y, src_w, src_h);
2582 drm_modeset_unlock_all(plane->dev);
2583
2584 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002585}
2586
2587/**
2588 * drm_mode_setplane - configure a plane's configuration
2589 * @dev: DRM device
2590 * @data: ioctl data*
2591 * @file_priv: DRM file info
2592 *
2593 * Set plane configuration, including placement, fb, scaling, and other factors.
2594 * Or pass a NULL fb to disable (planes may be disabled without providing a
2595 * valid crtc).
2596 *
2597 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002598 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002599 */
2600int drm_mode_setplane(struct drm_device *dev, void *data,
2601 struct drm_file *file_priv)
2602{
2603 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002604 struct drm_plane *plane;
2605 struct drm_crtc *crtc = NULL;
2606 struct drm_framebuffer *fb = NULL;
2607
2608 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2609 return -EINVAL;
2610
Matt Roperb36552b2014-06-10 08:28:09 -07002611 /*
2612 * First, find the plane, crtc, and fb objects. If not available,
2613 * we don't bother to call the driver.
2614 */
Rob Clark933f6222014-11-25 20:33:11 -05002615 plane = drm_plane_find(dev, plane_req->plane_id);
2616 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002617 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2618 plane_req->plane_id);
2619 return -ENOENT;
2620 }
Matt Roperb36552b2014-06-10 08:28:09 -07002621
2622 if (plane_req->fb_id) {
2623 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2624 if (!fb) {
2625 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2626 plane_req->fb_id);
2627 return -ENOENT;
2628 }
2629
Rob Clark933f6222014-11-25 20:33:11 -05002630 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2631 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002632 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2633 plane_req->crtc_id);
2634 return -ENOENT;
2635 }
Matt Roperb36552b2014-06-10 08:28:09 -07002636 }
2637
Matt Roper161d0dc2014-06-10 08:28:10 -07002638 /*
2639 * setplane_internal will take care of deref'ing either the old or new
2640 * framebuffer depending on success.
2641 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002642 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002643 plane_req->crtc_x, plane_req->crtc_y,
2644 plane_req->crtc_w, plane_req->crtc_h,
2645 plane_req->src_x, plane_req->src_y,
2646 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002647}
2648
2649/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002650 * drm_mode_set_config_internal - helper to call ->set_config
2651 * @set: modeset config to set
2652 *
2653 * This is a little helper to wrap internal calls to the ->set_config driver
2654 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002655 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002656 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002657 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002658 */
2659int drm_mode_set_config_internal(struct drm_mode_set *set)
2660{
2661 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002662 struct drm_framebuffer *fb;
2663 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002664 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002665
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002666 /*
2667 * NOTE: ->set_config can also disable other crtcs (if we steal all
2668 * connectors from it), hence we need to refcount the fbs across all
2669 * crtcs. Atomic modeset will have saner semantics ...
2670 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002671 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002672 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002673
Daniel Vetterb0d12322012-12-11 01:07:12 +01002674 fb = set->fb;
2675
2676 ret = crtc->funcs->set_config(set);
2677 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002678 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002679 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002680 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002681
Daniel Vettere4f62542015-07-09 23:44:35 +02002682 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002683 if (tmp->primary->fb)
2684 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002685 if (tmp->primary->old_fb)
2686 drm_framebuffer_unreference(tmp->primary->old_fb);
2687 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002688 }
2689
2690 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002691}
2692EXPORT_SYMBOL(drm_mode_set_config_internal);
2693
Matt Roperaf936292014-04-01 15:22:34 -07002694/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002695 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2696 * @mode: mode to query
2697 * @hdisplay: hdisplay value to fill in
2698 * @vdisplay: vdisplay value to fill in
2699 *
2700 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2701 * the appropriate layout.
2702 */
2703void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2704 int *hdisplay, int *vdisplay)
2705{
2706 struct drm_display_mode adjusted;
2707
2708 drm_mode_copy(&adjusted, mode);
2709 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2710 *hdisplay = adjusted.crtc_hdisplay;
2711 *vdisplay = adjusted.crtc_vdisplay;
2712}
2713EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2714
2715/**
Matt Roperaf936292014-04-01 15:22:34 -07002716 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2717 * CRTC viewport
2718 * @crtc: CRTC that framebuffer will be displayed on
2719 * @x: x panning
2720 * @y: y panning
2721 * @mode: mode that framebuffer will be displayed under
2722 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002723 */
Matt Roperaf936292014-04-01 15:22:34 -07002724int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2725 int x, int y,
2726 const struct drm_display_mode *mode,
2727 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002728
2729{
2730 int hdisplay, vdisplay;
2731
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002732 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002733
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002734 if (crtc->state &&
2735 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2736 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002737 swap(hdisplay, vdisplay);
2738
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002739 return check_src_coords(x << 16, y << 16,
2740 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002741}
Matt Roperaf936292014-04-01 15:22:34 -07002742EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002743
Daniel Vetter2d13b672012-12-11 13:47:23 +01002744/**
Dave Airlief453ba02008-11-07 14:05:41 -08002745 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002746 * @dev: drm device for the ioctl
2747 * @data: data pointer for the ioctl
2748 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002749 *
Dave Airlief453ba02008-11-07 14:05:41 -08002750 * Build a new CRTC configuration based on user request.
2751 *
2752 * Called by the user via ioctl.
2753 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002754 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002755 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002756 */
2757int drm_mode_setcrtc(struct drm_device *dev, void *data,
2758 struct drm_file *file_priv)
2759{
2760 struct drm_mode_config *config = &dev->mode_config;
2761 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002762 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002763 struct drm_connector **connector_set = NULL, *connector;
2764 struct drm_framebuffer *fb = NULL;
2765 struct drm_display_mode *mode = NULL;
2766 struct drm_mode_set set;
2767 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002768 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002769 int i;
2770
Dave Airliefb3b06c2011-02-08 13:55:21 +10002771 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2772 return -EINVAL;
2773
Zhao Junwang01447e92015-07-07 17:08:35 +08002774 /*
2775 * Universal plane src offsets are only 16.16, prevent havoc for
2776 * drivers using universal plane code internally.
2777 */
2778 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002779 return -ERANGE;
2780
Daniel Vetter84849902012-12-02 00:28:11 +01002781 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002782 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2783 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002784 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002785 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002786 goto out;
2787 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002788 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002789
2790 if (crtc_req->mode_valid) {
2791 /* If we have a mode we need a framebuffer. */
2792 /* If we pass -1, set the mode with the currently bound fb */
2793 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002794 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002795 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2796 ret = -EINVAL;
2797 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002798 }
Matt Roperf4510a22014-04-01 15:22:40 -07002799 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002800 /* Make refcounting symmetric with the lookup path. */
2801 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002802 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002803 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2804 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002805 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2806 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002807 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002808 goto out;
2809 }
Dave Airlief453ba02008-11-07 14:05:41 -08002810 }
2811
2812 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002813 if (!mode) {
2814 ret = -ENOMEM;
2815 goto out;
2816 }
2817
Daniel Stone934a8a82015-05-22 13:34:48 +01002818 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002819 if (ret) {
2820 DRM_DEBUG_KMS("Invalid mode\n");
2821 goto out;
2822 }
2823
Dave Airlief453ba02008-11-07 14:05:41 -08002824 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002825
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002826 /*
2827 * Check whether the primary plane supports the fb pixel format.
2828 * Drivers not implementing the universal planes API use a
2829 * default formats list provided by the DRM core which doesn't
2830 * match real hardware capabilities. Skip the check in that
2831 * case.
2832 */
2833 if (!crtc->primary->format_default) {
2834 ret = drm_plane_check_pixel_format(crtc->primary,
2835 fb->pixel_format);
2836 if (ret) {
2837 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2838 drm_get_format_name(fb->pixel_format));
2839 goto out;
2840 }
2841 }
2842
Damien Lespiauc11e9282013-09-25 16:45:30 +01002843 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2844 mode, fb);
2845 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002846 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002847
Dave Airlief453ba02008-11-07 14:05:41 -08002848 }
2849
2850 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002851 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002852 ret = -EINVAL;
2853 goto out;
2854 }
2855
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002856 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002857 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002858 crtc_req->count_connectors);
2859 ret = -EINVAL;
2860 goto out;
2861 }
2862
2863 if (crtc_req->count_connectors > 0) {
2864 u32 out_id;
2865
2866 /* Avoid unbounded kernel memory allocation */
2867 if (crtc_req->count_connectors > config->num_connector) {
2868 ret = -EINVAL;
2869 goto out;
2870 }
2871
Thierry Reding2f6c5382014-12-10 13:03:36 +01002872 connector_set = kmalloc_array(crtc_req->count_connectors,
2873 sizeof(struct drm_connector *),
2874 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002875 if (!connector_set) {
2876 ret = -ENOMEM;
2877 goto out;
2878 }
2879
2880 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002881 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002882 if (get_user(out_id, &set_connectors_ptr[i])) {
2883 ret = -EFAULT;
2884 goto out;
2885 }
2886
Rob Clarka2b34e22013-10-05 16:36:52 -04002887 connector = drm_connector_find(dev, out_id);
2888 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002889 DRM_DEBUG_KMS("Connector id %d unknown\n",
2890 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002891 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002892 goto out;
2893 }
Jerome Glisse94401062010-07-15 15:43:25 -04002894 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2895 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002896 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002897
2898 connector_set[i] = connector;
2899 }
2900 }
2901
2902 set.crtc = crtc;
2903 set.x = crtc_req->x;
2904 set.y = crtc_req->y;
2905 set.mode = mode;
2906 set.connectors = connector_set;
2907 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002908 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002909 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002910
2911out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002912 if (fb)
2913 drm_framebuffer_unreference(fb);
2914
Dave Airlief453ba02008-11-07 14:05:41 -08002915 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002916 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002917 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002918 return ret;
2919}
2920
Matt Roper161d0dc2014-06-10 08:28:10 -07002921/**
2922 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2923 * universal plane handler call
2924 * @crtc: crtc to update cursor for
2925 * @req: data pointer for the ioctl
2926 * @file_priv: drm file for the ioctl call
2927 *
2928 * Legacy cursor ioctl's work directly with driver buffer handles. To
2929 * translate legacy ioctl calls into universal plane handler calls, we need to
2930 * wrap the native buffer handle in a drm_framebuffer.
2931 *
2932 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2933 * buffer with a pitch of 4*width; the universal plane interface should be used
2934 * directly in cases where the hardware can support other buffer settings and
2935 * userspace wants to make use of these capabilities.
2936 *
2937 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002938 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002939 */
2940static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2941 struct drm_mode_cursor2 *req,
2942 struct drm_file *file_priv)
2943{
2944 struct drm_device *dev = crtc->dev;
2945 struct drm_framebuffer *fb = NULL;
2946 struct drm_mode_fb_cmd2 fbreq = {
2947 .width = req->width,
2948 .height = req->height,
2949 .pixel_format = DRM_FORMAT_ARGB8888,
2950 .pitches = { req->width * 4 },
2951 .handles = { req->handle },
2952 };
2953 int32_t crtc_x, crtc_y;
2954 uint32_t crtc_w = 0, crtc_h = 0;
2955 uint32_t src_w = 0, src_h = 0;
2956 int ret = 0;
2957
2958 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002959 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002960
2961 /*
2962 * Obtain fb we'll be using (either new or existing) and take an extra
2963 * reference to it if fb != null. setplane will take care of dropping
2964 * the reference if the plane update fails.
2965 */
2966 if (req->flags & DRM_MODE_CURSOR_BO) {
2967 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002968 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002969 if (IS_ERR(fb)) {
2970 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2971 return PTR_ERR(fb);
2972 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002973 } else {
2974 fb = NULL;
2975 }
2976 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002977 fb = crtc->cursor->fb;
2978 if (fb)
2979 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002980 }
2981
2982 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2983 crtc_x = req->x;
2984 crtc_y = req->y;
2985 } else {
2986 crtc_x = crtc->cursor_x;
2987 crtc_y = crtc->cursor_y;
2988 }
2989
2990 if (fb) {
2991 crtc_w = fb->width;
2992 crtc_h = fb->height;
2993 src_w = fb->width << 16;
2994 src_h = fb->height << 16;
2995 }
2996
2997 /*
2998 * setplane_internal will take care of deref'ing either the old or new
2999 * framebuffer depending on success.
3000 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003001 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003002 crtc_x, crtc_y, crtc_w, crtc_h,
3003 0, 0, src_w, src_h);
3004
3005 /* Update successful; save new cursor position, if necessary */
3006 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3007 crtc->cursor_x = req->x;
3008 crtc->cursor_y = req->y;
3009 }
3010
3011 return ret;
3012}
3013
Dave Airlie4c813d42013-06-20 11:48:52 +10003014static int drm_mode_cursor_common(struct drm_device *dev,
3015 struct drm_mode_cursor2 *req,
3016 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003017{
Dave Airlief453ba02008-11-07 14:05:41 -08003018 struct drm_crtc *crtc;
3019 int ret = 0;
3020
Dave Airliefb3b06c2011-02-08 13:55:21 +10003021 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3022 return -EINVAL;
3023
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003024 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003025 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003026
Rob Clarka2b34e22013-10-05 16:36:52 -04003027 crtc = drm_crtc_find(dev, req->crtc_id);
3028 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003029 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003030 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003031 }
Dave Airlief453ba02008-11-07 14:05:41 -08003032
Matt Roper161d0dc2014-06-10 08:28:10 -07003033 /*
3034 * If this crtc has a universal cursor plane, call that plane's update
3035 * handler rather than using legacy cursor handlers.
3036 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003037 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003038 if (crtc->cursor) {
3039 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3040 goto out;
3041 }
3042
Dave Airlief453ba02008-11-07 14:05:41 -08003043 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003044 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003045 ret = -ENXIO;
3046 goto out;
3047 }
3048 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003049 if (crtc->funcs->cursor_set2)
3050 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3051 req->width, req->height, req->hot_x, req->hot_y);
3052 else
3053 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3054 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003055 }
3056
3057 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3058 if (crtc->funcs->cursor_move) {
3059 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3060 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003061 ret = -EFAULT;
3062 goto out;
3063 }
3064 }
3065out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003066 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003067
Dave Airlief453ba02008-11-07 14:05:41 -08003068 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003069
3070}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003071
3072
3073/**
3074 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3075 * @dev: drm device for the ioctl
3076 * @data: data pointer for the ioctl
3077 * @file_priv: drm file for the ioctl call
3078 *
3079 * Set the cursor configuration based on user request.
3080 *
3081 * Called by the user via ioctl.
3082 *
3083 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003084 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003085 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003086int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003087 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003088{
3089 struct drm_mode_cursor *req = data;
3090 struct drm_mode_cursor2 new_req;
3091
3092 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3093 new_req.hot_x = new_req.hot_y = 0;
3094
3095 return drm_mode_cursor_common(dev, &new_req, file_priv);
3096}
3097
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003098/**
3099 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3100 * @dev: drm device for the ioctl
3101 * @data: data pointer for the ioctl
3102 * @file_priv: drm file for the ioctl call
3103 *
3104 * Set the cursor configuration based on user request. This implements the 2nd
3105 * version of the cursor ioctl, which allows userspace to additionally specify
3106 * the hotspot of the pointer.
3107 *
3108 * Called by the user via ioctl.
3109 *
3110 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003111 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003112 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003113int drm_mode_cursor2_ioctl(struct drm_device *dev,
3114 void *data, struct drm_file *file_priv)
3115{
3116 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003117
Dave Airlie4c813d42013-06-20 11:48:52 +10003118 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003119}
3120
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003121/**
3122 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3123 * @bpp: bits per pixels
3124 * @depth: bit depth per pixel
3125 *
3126 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3127 * Useful in fbdev emulation code, since that deals in those values.
3128 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003129uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3130{
3131 uint32_t fmt;
3132
3133 switch (bpp) {
3134 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003135 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003136 break;
3137 case 16:
3138 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003139 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003140 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003141 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003142 break;
3143 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003144 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003145 break;
3146 case 32:
3147 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003148 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003149 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003150 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003151 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003152 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003153 break;
3154 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003155 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3156 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003157 break;
3158 }
3159
3160 return fmt;
3161}
3162EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3163
Dave Airlief453ba02008-11-07 14:05:41 -08003164/**
3165 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003166 * @dev: drm device for the ioctl
3167 * @data: data pointer for the ioctl
3168 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003169 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003170 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003171 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003172 *
3173 * Called by the user via ioctl.
3174 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003175 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003176 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003177 */
3178int drm_mode_addfb(struct drm_device *dev,
3179 void *data, struct drm_file *file_priv)
3180{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003181 struct drm_mode_fb_cmd *or = data;
3182 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003183 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003184
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003185 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003186 r.fb_id = or->fb_id;
3187 r.width = or->width;
3188 r.height = or->height;
3189 r.pitches[0] = or->pitch;
3190 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3191 r.handles[0] = or->handle;
3192
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003193 ret = drm_mode_addfb2(dev, &r, file_priv);
3194 if (ret)
3195 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003196
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003197 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003198
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003199 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003200}
3201
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003202static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003203{
3204 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3205
3206 switch (format) {
3207 case DRM_FORMAT_C8:
3208 case DRM_FORMAT_RGB332:
3209 case DRM_FORMAT_BGR233:
3210 case DRM_FORMAT_XRGB4444:
3211 case DRM_FORMAT_XBGR4444:
3212 case DRM_FORMAT_RGBX4444:
3213 case DRM_FORMAT_BGRX4444:
3214 case DRM_FORMAT_ARGB4444:
3215 case DRM_FORMAT_ABGR4444:
3216 case DRM_FORMAT_RGBA4444:
3217 case DRM_FORMAT_BGRA4444:
3218 case DRM_FORMAT_XRGB1555:
3219 case DRM_FORMAT_XBGR1555:
3220 case DRM_FORMAT_RGBX5551:
3221 case DRM_FORMAT_BGRX5551:
3222 case DRM_FORMAT_ARGB1555:
3223 case DRM_FORMAT_ABGR1555:
3224 case DRM_FORMAT_RGBA5551:
3225 case DRM_FORMAT_BGRA5551:
3226 case DRM_FORMAT_RGB565:
3227 case DRM_FORMAT_BGR565:
3228 case DRM_FORMAT_RGB888:
3229 case DRM_FORMAT_BGR888:
3230 case DRM_FORMAT_XRGB8888:
3231 case DRM_FORMAT_XBGR8888:
3232 case DRM_FORMAT_RGBX8888:
3233 case DRM_FORMAT_BGRX8888:
3234 case DRM_FORMAT_ARGB8888:
3235 case DRM_FORMAT_ABGR8888:
3236 case DRM_FORMAT_RGBA8888:
3237 case DRM_FORMAT_BGRA8888:
3238 case DRM_FORMAT_XRGB2101010:
3239 case DRM_FORMAT_XBGR2101010:
3240 case DRM_FORMAT_RGBX1010102:
3241 case DRM_FORMAT_BGRX1010102:
3242 case DRM_FORMAT_ARGB2101010:
3243 case DRM_FORMAT_ABGR2101010:
3244 case DRM_FORMAT_RGBA1010102:
3245 case DRM_FORMAT_BGRA1010102:
3246 case DRM_FORMAT_YUYV:
3247 case DRM_FORMAT_YVYU:
3248 case DRM_FORMAT_UYVY:
3249 case DRM_FORMAT_VYUY:
3250 case DRM_FORMAT_AYUV:
3251 case DRM_FORMAT_NV12:
3252 case DRM_FORMAT_NV21:
3253 case DRM_FORMAT_NV16:
3254 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003255 case DRM_FORMAT_NV24:
3256 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003257 case DRM_FORMAT_YUV410:
3258 case DRM_FORMAT_YVU410:
3259 case DRM_FORMAT_YUV411:
3260 case DRM_FORMAT_YVU411:
3261 case DRM_FORMAT_YUV420:
3262 case DRM_FORMAT_YVU420:
3263 case DRM_FORMAT_YUV422:
3264 case DRM_FORMAT_YVU422:
3265 case DRM_FORMAT_YUV444:
3266 case DRM_FORMAT_YVU444:
3267 return 0;
3268 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003269 DRM_DEBUG_KMS("invalid pixel format %s\n",
3270 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003271 return -EINVAL;
3272 }
3273}
3274
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003275static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003276{
3277 int ret, hsub, vsub, num_planes, i;
3278
3279 ret = format_check(r);
3280 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003281 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3282 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003283 return ret;
3284 }
3285
3286 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3287 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3288 num_planes = drm_format_num_planes(r->pixel_format);
3289
3290 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003291 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003292 return -EINVAL;
3293 }
3294
3295 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003296 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003297 return -EINVAL;
3298 }
3299
3300 for (i = 0; i < num_planes; i++) {
3301 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003302 unsigned int height = r->height / (i != 0 ? vsub : 1);
3303 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003304
3305 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003306 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003307 return -EINVAL;
3308 }
3309
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003310 if ((uint64_t) width * cpp > UINT_MAX)
3311 return -ERANGE;
3312
3313 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3314 return -ERANGE;
3315
3316 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003317 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003318 return -EINVAL;
3319 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003320
3321 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3322 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3323 r->modifier[i], i);
3324 return -EINVAL;
3325 }
Rob Clark570655b2015-01-30 20:18:11 +05303326
3327 /* modifier specific checks: */
3328 switch (r->modifier[i]) {
3329 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3330 /* NOTE: the pitch restriction may be lifted later if it turns
3331 * out that no hw has this restriction:
3332 */
3333 if (r->pixel_format != DRM_FORMAT_NV12 ||
3334 width % 128 || height % 32 ||
3335 r->pitches[i] % 128) {
3336 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3337 return -EINVAL;
3338 }
3339 break;
3340
3341 default:
3342 break;
3343 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003344 }
3345
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003346 for (i = num_planes; i < 4; i++) {
3347 if (r->modifier[i]) {
3348 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3349 return -EINVAL;
3350 }
3351
3352 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3353 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3354 continue;
3355
3356 if (r->handles[i]) {
3357 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3358 return -EINVAL;
3359 }
3360
3361 if (r->pitches[i]) {
3362 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3363 return -EINVAL;
3364 }
3365
3366 if (r->offsets[i]) {
3367 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3368 return -EINVAL;
3369 }
3370 }
3371
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003372 return 0;
3373}
3374
Chris Wilson9a6f5132015-02-25 13:45:26 +00003375static struct drm_framebuffer *
3376internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003377 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003378 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003379{
3380 struct drm_mode_config *config = &dev->mode_config;
3381 struct drm_framebuffer *fb;
3382 int ret;
3383
Rob Clarke3eb3252015-02-05 14:41:52 +00003384 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003385 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3386 return ERR_PTR(-EINVAL);
3387 }
3388
3389 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3390 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3391 r->width, config->min_width, config->max_width);
3392 return ERR_PTR(-EINVAL);
3393 }
3394 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3395 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3396 r->height, config->min_height, config->max_height);
3397 return ERR_PTR(-EINVAL);
3398 }
3399
Rob Clarke3eb3252015-02-05 14:41:52 +00003400 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3401 !dev->mode_config.allow_fb_modifiers) {
3402 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3403 return ERR_PTR(-EINVAL);
3404 }
3405
Matt Roperc394c2b2014-06-10 08:28:08 -07003406 ret = framebuffer_check(r);
3407 if (ret)
3408 return ERR_PTR(ret);
3409
3410 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3411 if (IS_ERR(fb)) {
3412 DRM_DEBUG_KMS("could not create framebuffer\n");
3413 return fb;
3414 }
3415
Matt Roperc394c2b2014-06-10 08:28:08 -07003416 return fb;
3417}
3418
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003419/**
3420 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003421 * @dev: drm device for the ioctl
3422 * @data: data pointer for the ioctl
3423 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003424 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003425 * Add a new FB to the specified CRTC, given a user request with format. This is
3426 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3427 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003428 *
3429 * Called by the user via ioctl.
3430 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003431 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003432 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003433 */
3434int drm_mode_addfb2(struct drm_device *dev,
3435 void *data, struct drm_file *file_priv)
3436{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003437 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003438 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003439
Dave Airliefb3b06c2011-02-08 13:55:21 +10003440 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3441 return -EINVAL;
3442
Chris Wilson9a6f5132015-02-25 13:45:26 +00003443 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003444 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003445 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003446
Chris Wilson9a6f5132015-02-25 13:45:26 +00003447 /* Transfer ownership to the filp for reaping on close */
3448
3449 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3450 mutex_lock(&file_priv->fbs_lock);
3451 r->fb_id = fb->base.id;
3452 list_add(&fb->filp_head, &file_priv->fbs);
3453 mutex_unlock(&file_priv->fbs_lock);
3454
Matt Roperc394c2b2014-06-10 08:28:08 -07003455 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003456}
3457
3458/**
3459 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003460 * @dev: drm device for the ioctl
3461 * @data: data pointer for the ioctl
3462 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003463 *
Dave Airlief453ba02008-11-07 14:05:41 -08003464 * Remove the FB specified by the user.
3465 *
3466 * Called by the user via ioctl.
3467 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003468 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003469 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003470 */
3471int drm_mode_rmfb(struct drm_device *dev,
3472 void *data, struct drm_file *file_priv)
3473{
Dave Airlief453ba02008-11-07 14:05:41 -08003474 struct drm_framebuffer *fb = NULL;
3475 struct drm_framebuffer *fbl = NULL;
Dave Airliecee26ac2016-04-15 15:10:37 +10003476 struct drm_mode_object *obj;
Dave Airlief453ba02008-11-07 14:05:41 -08003477 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003478 int found = 0;
3479
Dave Airliefb3b06c2011-02-08 13:55:21 +10003480 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3481 return -EINVAL;
3482
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003483 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003484 mutex_lock(&dev->mode_config.fb_lock);
Dave Airliecee26ac2016-04-15 15:10:37 +10003485 obj = _object_find(dev, *id, DRM_MODE_OBJECT_FB);
3486 if (!obj)
Daniel Vetter2b677e82012-12-10 21:16:05 +01003487 goto fail_lookup;
Dave Airliecee26ac2016-04-15 15:10:37 +10003488 fb = obj_to_fb(obj);
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;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003492 if (!found)
3493 goto fail_lookup;
3494
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003495 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003496 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003497 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003498
Maarten Lankhorst13803132015-09-09 16:40:56 +02003499 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003500
Daniel Vetter2b677e82012-12-10 21:16:05 +01003501 return 0;
3502
3503fail_lookup:
3504 mutex_unlock(&dev->mode_config.fb_lock);
3505 mutex_unlock(&file_priv->fbs_lock);
3506
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003507 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003508}
3509
3510/**
3511 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003512 * @dev: drm device for the ioctl
3513 * @data: data pointer for the ioctl
3514 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003515 *
Dave Airlief453ba02008-11-07 14:05:41 -08003516 * Lookup the FB given its ID and return info about it.
3517 *
3518 * Called by the user via ioctl.
3519 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003520 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003521 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003522 */
3523int drm_mode_getfb(struct drm_device *dev,
3524 void *data, struct drm_file *file_priv)
3525{
3526 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003527 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003528 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003529
Dave Airliefb3b06c2011-02-08 13:55:21 +10003530 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3531 return -EINVAL;
3532
Daniel Vetter786b99e2012-12-02 21:53:40 +01003533 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003534 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003535 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003536
3537 r->height = fb->height;
3538 r->width = fb->width;
3539 r->depth = fb->depth;
3540 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003541 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003542 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003543 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003544 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003545 ret = fb->funcs->create_handle(fb, file_priv,
3546 &r->handle);
3547 } else {
3548 /* GET_FB() is an unprivileged ioctl so we must not
3549 * return a buffer-handle to non-master processes! For
3550 * backwards-compatibility reasons, we cannot make
3551 * GET_FB() privileged, so just return an invalid handle
3552 * for non-masters. */
3553 r->handle = 0;
3554 ret = 0;
3555 }
3556 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003557 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003558 }
Dave Airlief453ba02008-11-07 14:05:41 -08003559
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003560 drm_framebuffer_unreference(fb);
3561
Dave Airlief453ba02008-11-07 14:05:41 -08003562 return ret;
3563}
3564
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003565/**
3566 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3567 * @dev: drm device for the ioctl
3568 * @data: data pointer for the ioctl
3569 * @file_priv: drm file for the ioctl call
3570 *
3571 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3572 * rectangle list. Generic userspace which does frontbuffer rendering must call
3573 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3574 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3575 *
3576 * Modesetting drivers which always update the frontbuffer do not need to
3577 * implement the corresponding ->dirty framebuffer callback.
3578 *
3579 * Called by the user via ioctl.
3580 *
3581 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003582 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003583 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003584int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3585 void *data, struct drm_file *file_priv)
3586{
3587 struct drm_clip_rect __user *clips_ptr;
3588 struct drm_clip_rect *clips = NULL;
3589 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003590 struct drm_framebuffer *fb;
3591 unsigned flags;
3592 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003593 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003594
Dave Airliefb3b06c2011-02-08 13:55:21 +10003595 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3596 return -EINVAL;
3597
Daniel Vetter786b99e2012-12-02 21:53:40 +01003598 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003599 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003600 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003601
3602 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003603 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003604
3605 if (!num_clips != !clips_ptr) {
3606 ret = -EINVAL;
3607 goto out_err1;
3608 }
3609
3610 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3611
3612 /* If userspace annotates copy, clips must come in pairs */
3613 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3614 ret = -EINVAL;
3615 goto out_err1;
3616 }
3617
3618 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003619 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3620 ret = -EINVAL;
3621 goto out_err1;
3622 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003623 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003624 if (!clips) {
3625 ret = -ENOMEM;
3626 goto out_err1;
3627 }
3628
3629 ret = copy_from_user(clips, clips_ptr,
3630 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003631 if (ret) {
3632 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003633 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003634 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003635 }
3636
3637 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003638 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3639 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003640 } else {
3641 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003642 }
3643
3644out_err2:
3645 kfree(clips);
3646out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003647 drm_framebuffer_unreference(fb);
3648
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003649 return ret;
3650}
3651
3652
Dave Airlief453ba02008-11-07 14:05:41 -08003653/**
3654 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003655 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003656 *
Dave Airlief453ba02008-11-07 14:05:41 -08003657 * Destroy all the FBs associated with @filp.
3658 *
3659 * Called by the user via ioctl.
3660 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003661 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003662 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003663 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003664void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003665{
Dave Airlief453ba02008-11-07 14:05:41 -08003666 struct drm_framebuffer *fb, *tfb;
3667
Daniel Vetter1b116292014-09-24 21:51:06 +02003668 /*
3669 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003670 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003671 * avoid upsetting lockdep since the universal cursor code adds a
3672 * framebuffer while holding mutex locks.
3673 *
3674 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3675 * locks is impossible here since no one else but this function can get
3676 * at it any more.
3677 */
Dave Airlief453ba02008-11-07 14:05:41 -08003678 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003679 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003680
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003681 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003682 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003683 }
Dave Airlief453ba02008-11-07 14:05:41 -08003684}
3685
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003686/**
3687 * drm_property_create - create a new property type
3688 * @dev: drm device
3689 * @flags: flags specifying the property type
3690 * @name: name of the property
3691 * @num_values: number of pre-defined values
3692 *
3693 * This creates a new generic drm property which can then be attached to a drm
3694 * object with drm_object_attach_property. The returned property object must be
3695 * freed with drm_property_destroy.
3696 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003697 * Note that the DRM core keeps a per-device list of properties and that, if
3698 * drm_mode_config_cleanup() is called, it will destroy all properties created
3699 * by the driver.
3700 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003701 * Returns:
3702 * A pointer to the newly created property on success, NULL on failure.
3703 */
Dave Airlief453ba02008-11-07 14:05:41 -08003704struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3705 const char *name, int num_values)
3706{
3707 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003708 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003709
3710 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3711 if (!property)
3712 return NULL;
3713
Rob Clark98f75de2014-05-30 11:37:03 -04003714 property->dev = dev;
3715
Dave Airlief453ba02008-11-07 14:05:41 -08003716 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003717 property->values = kcalloc(num_values, sizeof(uint64_t),
3718 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003719 if (!property->values)
3720 goto fail;
3721 }
3722
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003723 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3724 if (ret)
3725 goto fail;
3726
Dave Airlief453ba02008-11-07 14:05:41 -08003727 property->flags = flags;
3728 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003729 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003730
Vinson Lee471dd2e2011-11-10 11:55:40 -08003731 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003732 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003733 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3734 }
Dave Airlief453ba02008-11-07 14:05:41 -08003735
3736 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003737
3738 WARN_ON(!drm_property_type_valid(property));
3739
Dave Airlief453ba02008-11-07 14:05:41 -08003740 return property;
3741fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003742 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003743 kfree(property);
3744 return NULL;
3745}
3746EXPORT_SYMBOL(drm_property_create);
3747
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003748/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003749 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003750 * @dev: drm device
3751 * @flags: flags specifying the property type
3752 * @name: name of the property
3753 * @props: enumeration lists with property values
3754 * @num_values: number of pre-defined values
3755 *
3756 * This creates a new generic drm property which can then be attached to a drm
3757 * object with drm_object_attach_property. The returned property object must be
3758 * freed with drm_property_destroy.
3759 *
3760 * Userspace is only allowed to set one of the predefined values for enumeration
3761 * properties.
3762 *
3763 * Returns:
3764 * A pointer to the newly created property on success, NULL on failure.
3765 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003766struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3767 const char *name,
3768 const struct drm_prop_enum_list *props,
3769 int num_values)
3770{
3771 struct drm_property *property;
3772 int i, ret;
3773
3774 flags |= DRM_MODE_PROP_ENUM;
3775
3776 property = drm_property_create(dev, flags, name, num_values);
3777 if (!property)
3778 return NULL;
3779
3780 for (i = 0; i < num_values; i++) {
3781 ret = drm_property_add_enum(property, i,
3782 props[i].type,
3783 props[i].name);
3784 if (ret) {
3785 drm_property_destroy(dev, property);
3786 return NULL;
3787 }
3788 }
3789
3790 return property;
3791}
3792EXPORT_SYMBOL(drm_property_create_enum);
3793
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003794/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003795 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003796 * @dev: drm device
3797 * @flags: flags specifying the property type
3798 * @name: name of the property
3799 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003800 * @num_props: size of the @props array
3801 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003802 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003803 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003804 * object with drm_object_attach_property. The returned property object must be
3805 * freed with drm_property_destroy.
3806 *
3807 * Compared to plain enumeration properties userspace is allowed to set any
3808 * or'ed together combination of the predefined property bitflag values
3809 *
3810 * Returns:
3811 * A pointer to the newly created property on success, NULL on failure.
3812 */
Rob Clark49e27542012-05-17 02:23:26 -06003813struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3814 int flags, const char *name,
3815 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303816 int num_props,
3817 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003818{
3819 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303820 int i, ret, index = 0;
3821 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003822
3823 flags |= DRM_MODE_PROP_BITMASK;
3824
3825 property = drm_property_create(dev, flags, name, num_values);
3826 if (!property)
3827 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303828 for (i = 0; i < num_props; i++) {
3829 if (!(supported_bits & (1ULL << props[i].type)))
3830 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003831
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303832 if (WARN_ON(index >= num_values)) {
3833 drm_property_destroy(dev, property);
3834 return NULL;
3835 }
3836
3837 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003838 props[i].type,
3839 props[i].name);
3840 if (ret) {
3841 drm_property_destroy(dev, property);
3842 return NULL;
3843 }
3844 }
3845
3846 return property;
3847}
3848EXPORT_SYMBOL(drm_property_create_bitmask);
3849
Rob Clarkebc44cf2012-09-12 22:22:31 -05003850static struct drm_property *property_create_range(struct drm_device *dev,
3851 int flags, const char *name,
3852 uint64_t min, uint64_t max)
3853{
3854 struct drm_property *property;
3855
3856 property = drm_property_create(dev, flags, name, 2);
3857 if (!property)
3858 return NULL;
3859
3860 property->values[0] = min;
3861 property->values[1] = max;
3862
3863 return property;
3864}
3865
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003866/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003867 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003868 * @dev: drm device
3869 * @flags: flags specifying the property type
3870 * @name: name of the property
3871 * @min: minimum value of the property
3872 * @max: maximum value of the property
3873 *
3874 * This creates a new generic drm property which can then be attached to a drm
3875 * object with drm_object_attach_property. The returned property object must be
3876 * freed with drm_property_destroy.
3877 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003878 * Userspace is allowed to set any unsigned integer value in the (min, max)
3879 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003880 *
3881 * Returns:
3882 * A pointer to the newly created property on success, NULL on failure.
3883 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003884struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3885 const char *name,
3886 uint64_t min, uint64_t max)
3887{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003888 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3889 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003890}
3891EXPORT_SYMBOL(drm_property_create_range);
3892
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003893/**
3894 * drm_property_create_signed_range - create a new signed ranged property type
3895 * @dev: drm device
3896 * @flags: flags specifying the property type
3897 * @name: name of the property
3898 * @min: minimum value of the property
3899 * @max: maximum value of the property
3900 *
3901 * This creates a new generic drm property which can then be attached to a drm
3902 * object with drm_object_attach_property. The returned property object must be
3903 * freed with drm_property_destroy.
3904 *
3905 * Userspace is allowed to set any signed integer value in the (min, max)
3906 * range inclusive.
3907 *
3908 * Returns:
3909 * A pointer to the newly created property on success, NULL on failure.
3910 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003911struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3912 int flags, const char *name,
3913 int64_t min, int64_t max)
3914{
3915 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3916 name, I642U64(min), I642U64(max));
3917}
3918EXPORT_SYMBOL(drm_property_create_signed_range);
3919
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003920/**
3921 * drm_property_create_object - create a new object property type
3922 * @dev: drm device
3923 * @flags: flags specifying the property type
3924 * @name: name of the property
3925 * @type: object type from DRM_MODE_OBJECT_* defines
3926 *
3927 * This creates a new generic drm property which can then be attached to a drm
3928 * object with drm_object_attach_property. The returned property object must be
3929 * freed with drm_property_destroy.
3930 *
3931 * Userspace is only allowed to set this to any property value of the given
3932 * @type. Only useful for atomic properties, which is enforced.
3933 *
3934 * Returns:
3935 * A pointer to the newly created property on success, NULL on failure.
3936 */
Rob Clark98f75de2014-05-30 11:37:03 -04003937struct drm_property *drm_property_create_object(struct drm_device *dev,
3938 int flags, const char *name, uint32_t type)
3939{
3940 struct drm_property *property;
3941
3942 flags |= DRM_MODE_PROP_OBJECT;
3943
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003944 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3945 return NULL;
3946
Rob Clark98f75de2014-05-30 11:37:03 -04003947 property = drm_property_create(dev, flags, name, 1);
3948 if (!property)
3949 return NULL;
3950
3951 property->values[0] = type;
3952
3953 return property;
3954}
3955EXPORT_SYMBOL(drm_property_create_object);
3956
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003957/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003958 * drm_property_create_bool - create a new boolean property type
3959 * @dev: drm device
3960 * @flags: flags specifying the property type
3961 * @name: name of the property
3962 *
3963 * This creates a new generic drm property which can then be attached to a drm
3964 * object with drm_object_attach_property. The returned property object must be
3965 * freed with drm_property_destroy.
3966 *
3967 * This is implemented as a ranged property with only {0, 1} as valid values.
3968 *
3969 * Returns:
3970 * A pointer to the newly created property on success, NULL on failure.
3971 */
3972struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3973 const char *name)
3974{
3975 return drm_property_create_range(dev, flags, name, 0, 1);
3976}
3977EXPORT_SYMBOL(drm_property_create_bool);
3978
3979/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003980 * drm_property_add_enum - add a possible value to an enumeration property
3981 * @property: enumeration property to change
3982 * @index: index of the new enumeration
3983 * @value: value of the new enumeration
3984 * @name: symbolic name of the new enumeration
3985 *
3986 * This functions adds enumerations to a property.
3987 *
3988 * It's use is deprecated, drivers should use one of the more specific helpers
3989 * to directly create the property with all enumerations already attached.
3990 *
3991 * Returns:
3992 * Zero on success, error code on failure.
3993 */
Dave Airlief453ba02008-11-07 14:05:41 -08003994int drm_property_add_enum(struct drm_property *property, int index,
3995 uint64_t value, const char *name)
3996{
3997 struct drm_property_enum *prop_enum;
3998
Rob Clark5ea22f22014-05-30 11:34:01 -04003999 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4000 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004001 return -EINVAL;
4002
4003 /*
4004 * Bitmask enum properties have the additional constraint of values
4005 * from 0 to 63
4006 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004007 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4008 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004009 return -EINVAL;
4010
Daniel Vetter3758b342014-11-19 18:38:10 +01004011 if (!list_empty(&property->enum_list)) {
4012 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004013 if (prop_enum->value == value) {
4014 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4015 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4016 return 0;
4017 }
4018 }
4019 }
4020
4021 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4022 if (!prop_enum)
4023 return -ENOMEM;
4024
4025 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4026 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4027 prop_enum->value = value;
4028
4029 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004030 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004031 return 0;
4032}
4033EXPORT_SYMBOL(drm_property_add_enum);
4034
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004035/**
4036 * drm_property_destroy - destroy a drm property
4037 * @dev: drm device
4038 * @property: property to destry
4039 *
4040 * This function frees a property including any attached resources like
4041 * enumeration values.
4042 */
Dave Airlief453ba02008-11-07 14:05:41 -08004043void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4044{
4045 struct drm_property_enum *prop_enum, *pt;
4046
Daniel Vetter3758b342014-11-19 18:38:10 +01004047 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004048 list_del(&prop_enum->head);
4049 kfree(prop_enum);
4050 }
4051
4052 if (property->num_values)
4053 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004054 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004055 list_del(&property->head);
4056 kfree(property);
4057}
4058EXPORT_SYMBOL(drm_property_destroy);
4059
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004060/**
4061 * drm_object_attach_property - attach a property to a modeset object
4062 * @obj: drm modeset object
4063 * @property: property to attach
4064 * @init_val: initial value of the property
4065 *
4066 * This attaches the given property to the modeset object with the given initial
4067 * value. Currently this function cannot fail since the properties are stored in
4068 * a statically sized array.
4069 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004070void drm_object_attach_property(struct drm_mode_object *obj,
4071 struct drm_property *property,
4072 uint64_t init_val)
4073{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004074 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004075
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004076 if (count == DRM_OBJECT_MAX_PROPERTY) {
4077 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4078 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4079 "you see this message on the same object type.\n",
4080 obj->type);
4081 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004082 }
4083
Rob Clarkb17cd752014-12-16 18:05:30 -05004084 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004085 obj->properties->values[count] = init_val;
4086 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004087 if (property->flags & DRM_MODE_PROP_ATOMIC)
4088 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004089}
4090EXPORT_SYMBOL(drm_object_attach_property);
4091
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004092/**
4093 * drm_object_property_set_value - set the value of a property
4094 * @obj: drm mode object to set property value for
4095 * @property: property to set
4096 * @val: value the property should be set to
4097 *
4098 * This functions sets a given property on a given object. This function only
4099 * changes the software state of the property, it does not call into the
4100 * driver's ->set_property callback.
4101 *
4102 * Returns:
4103 * Zero on success, error code on failure.
4104 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004105int drm_object_property_set_value(struct drm_mode_object *obj,
4106 struct drm_property *property, uint64_t val)
4107{
4108 int i;
4109
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004110 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004111 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004112 obj->properties->values[i] = val;
4113 return 0;
4114 }
4115 }
4116
4117 return -EINVAL;
4118}
4119EXPORT_SYMBOL(drm_object_property_set_value);
4120
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004121/**
4122 * drm_object_property_get_value - retrieve the value of a property
4123 * @obj: drm mode object to get property value from
4124 * @property: property to retrieve
4125 * @val: storage for the property value
4126 *
4127 * This function retrieves the softare state of the given property for the given
4128 * property. Since there is no driver callback to retrieve the current property
4129 * value this might be out of sync with the hardware, depending upon the driver
4130 * and property.
4131 *
4132 * Returns:
4133 * Zero on success, error code on failure.
4134 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004135int drm_object_property_get_value(struct drm_mode_object *obj,
4136 struct drm_property *property, uint64_t *val)
4137{
4138 int i;
4139
Rob Clark88a48e22014-12-18 16:01:50 -05004140 /* read-only properties bypass atomic mechanism and still store
4141 * their value in obj->properties->values[].. mostly to avoid
4142 * having to deal w/ EDID and similar props in atomic paths:
4143 */
4144 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4145 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4146 return drm_atomic_get_property(obj, property, val);
4147
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004148 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004149 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004150 *val = obj->properties->values[i];
4151 return 0;
4152 }
4153 }
4154
4155 return -EINVAL;
4156}
4157EXPORT_SYMBOL(drm_object_property_get_value);
4158
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004159/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004160 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004161 * @dev: DRM device
4162 * @data: ioctl data
4163 * @file_priv: DRM file info
4164 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004165 * This function retrieves the metadata for a given property, like the different
4166 * possible values for an enum property or the limits for a range property.
4167 *
4168 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004169 *
4170 * Called by the user via ioctl.
4171 *
4172 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004173 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004174 */
Dave Airlief453ba02008-11-07 14:05:41 -08004175int drm_mode_getproperty_ioctl(struct drm_device *dev,
4176 void *data, struct drm_file *file_priv)
4177{
Dave Airlief453ba02008-11-07 14:05:41 -08004178 struct drm_mode_get_property *out_resp = data;
4179 struct drm_property *property;
4180 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004181 int value_count = 0;
4182 int ret = 0, i;
4183 int copied;
4184 struct drm_property_enum *prop_enum;
4185 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004186 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004187
Dave Airliefb3b06c2011-02-08 13:55:21 +10004188 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4189 return -EINVAL;
4190
Daniel Vetter84849902012-12-02 00:28:11 +01004191 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004192 property = drm_property_find(dev, out_resp->prop_id);
4193 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004194 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004195 goto done;
4196 }
Dave Airlief453ba02008-11-07 14:05:41 -08004197
Rob Clark5ea22f22014-05-30 11:34:01 -04004198 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4199 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004200 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004201 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004202 }
4203
4204 value_count = property->num_values;
4205
4206 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4207 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4208 out_resp->flags = property->flags;
4209
4210 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004211 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004212 for (i = 0; i < value_count; i++) {
4213 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4214 ret = -EFAULT;
4215 goto done;
4216 }
4217 }
4218 }
4219 out_resp->count_values = value_count;
4220
Rob Clark5ea22f22014-05-30 11:34:01 -04004221 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4222 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004223 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4224 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004225 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004226 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004227
4228 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4229 ret = -EFAULT;
4230 goto done;
4231 }
4232
4233 if (copy_to_user(&enum_ptr[copied].name,
4234 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4235 ret = -EFAULT;
4236 goto done;
4237 }
4238 copied++;
4239 }
4240 }
4241 out_resp->count_enum_blobs = enum_count;
4242 }
4243
Daniel Vetter3758b342014-11-19 18:38:10 +01004244 /*
4245 * NOTE: The idea seems to have been to use this to read all the blob
4246 * property values. But nothing ever added them to the corresponding
4247 * list, userspace always used the special-purpose get_blob ioctl to
4248 * read the value for a blob property. It also doesn't make a lot of
4249 * sense to return values here when everything else is just metadata for
4250 * the property itself.
4251 */
4252 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4253 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004254done:
Daniel Vetter84849902012-12-02 00:28:11 +01004255 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004256 return ret;
4257}
4258
Daniel Stone99531d92015-05-22 13:34:49 +01004259/**
4260 * drm_property_create_blob - Create new blob property
4261 *
4262 * Creates a new blob property for a specified DRM device, optionally
4263 * copying data.
4264 *
4265 * @dev: DRM device to create property for
4266 * @length: Length to allocate for blob data
4267 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004268 *
4269 * Returns:
4270 * New blob property with a single reference on success, or an ERR_PTR
4271 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004272 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004273struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004274drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004275 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004276{
4277 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004278 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004279
Dan Carpenter9ac09342015-10-29 16:37:54 +03004280 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004281 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004282
4283 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4284 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004285 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004286
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004287 /* This must be explicitly initialised, so we can safely call list_del
4288 * on it in the removal handler, even if it isn't in a file list. */
4289 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004290 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004291 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004292
Daniel Stone99531d92015-05-22 13:34:49 +01004293 if (data)
4294 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004295
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004296 mutex_lock(&dev->mode_config.blob_lock);
4297
4298 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4299 if (ret) {
4300 kfree(blob);
4301 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004302 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004303 }
4304
Daniel Stone6bcacf52015-04-20 19:22:55 +01004305 kref_init(&blob->refcount);
4306
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004307 list_add_tail(&blob->head_global,
4308 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004309
4310 mutex_unlock(&dev->mode_config.blob_lock);
4311
Dave Airlief453ba02008-11-07 14:05:41 -08004312 return blob;
4313}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004314EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004315
Daniel Stone6bcacf52015-04-20 19:22:55 +01004316/**
4317 * drm_property_free_blob - Blob property destructor
4318 *
4319 * Internal free function for blob properties; must not be used directly.
4320 *
Daniel Stonef102c162015-05-22 13:34:44 +01004321 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004322 */
4323static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004324{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004325 struct drm_property_blob *blob =
4326 container_of(kref, struct drm_property_blob, refcount);
4327
4328 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4329
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004330 list_del(&blob->head_global);
4331 list_del(&blob->head_file);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004332 drm_mode_object_unregister(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004333
Dave Airlief453ba02008-11-07 14:05:41 -08004334 kfree(blob);
4335}
4336
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004337/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004338 * drm_property_unreference_blob - Unreference a blob property
4339 *
4340 * Drop a reference on a blob property. May free the object.
4341 *
Daniel Stonef102c162015-05-22 13:34:44 +01004342 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004343 */
4344void drm_property_unreference_blob(struct drm_property_blob *blob)
4345{
4346 struct drm_device *dev;
4347
4348 if (!blob)
4349 return;
4350
4351 dev = blob->dev;
4352
4353 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4354
4355 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4356 &dev->mode_config.blob_lock))
4357 mutex_unlock(&dev->mode_config.blob_lock);
4358 else
4359 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004360}
4361EXPORT_SYMBOL(drm_property_unreference_blob);
4362
4363/**
4364 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4365 *
4366 * Drop a reference on a blob property. May free the object. This must be
4367 * called with blob_lock held.
4368 *
Daniel Stonef102c162015-05-22 13:34:44 +01004369 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004370 */
4371static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4372{
4373 if (!blob)
4374 return;
4375
4376 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4377
4378 kref_put(&blob->refcount, drm_property_free_blob);
4379}
4380
4381/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004382 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4383 * @dev: DRM device
4384 * @file_priv: destroy all blobs owned by this file handle
4385 */
4386void drm_property_destroy_user_blobs(struct drm_device *dev,
4387 struct drm_file *file_priv)
4388{
4389 struct drm_property_blob *blob, *bt;
4390
4391 mutex_lock(&dev->mode_config.blob_lock);
4392
4393 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4394 list_del_init(&blob->head_file);
4395 drm_property_unreference_blob_locked(blob);
4396 }
4397
4398 mutex_unlock(&dev->mode_config.blob_lock);
4399}
4400
4401/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004402 * drm_property_reference_blob - Take a reference on an existing property
4403 *
4404 * Take a new reference on an existing blob property.
4405 *
Daniel Stonef102c162015-05-22 13:34:44 +01004406 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004407 */
4408struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4409{
4410 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4411 kref_get(&blob->refcount);
4412 return blob;
4413}
4414EXPORT_SYMBOL(drm_property_reference_blob);
4415
4416/*
4417 * Like drm_property_lookup_blob, but does not return an additional reference.
4418 * Must be called with blob_lock held.
4419 */
4420static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4421 uint32_t id)
4422{
4423 struct drm_mode_object *obj = NULL;
4424 struct drm_property_blob *blob;
4425
4426 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4427
4428 mutex_lock(&dev->mode_config.idr_mutex);
4429 obj = idr_find(&dev->mode_config.crtc_idr, id);
4430 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4431 blob = NULL;
4432 else
4433 blob = obj_to_blob(obj);
4434 mutex_unlock(&dev->mode_config.idr_mutex);
4435
Dave Airlief453ba02008-11-07 14:05:41 -08004436 return blob;
4437}
4438
Daniel Stone6bcacf52015-04-20 19:22:55 +01004439/**
4440 * drm_property_lookup_blob - look up a blob property and take a reference
4441 * @dev: drm device
4442 * @id: id of the blob property
4443 *
4444 * If successful, this takes an additional reference to the blob property.
4445 * callers need to make sure to eventually unreference the returned property
4446 * again, using @drm_property_unreference_blob.
4447 */
4448struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4449 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004450{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004451 struct drm_property_blob *blob;
4452
4453 mutex_lock(&dev->mode_config.blob_lock);
4454 blob = __drm_property_lookup_blob(dev, id);
4455 if (blob) {
4456 if (!kref_get_unless_zero(&blob->refcount))
4457 blob = NULL;
4458 }
4459 mutex_unlock(&dev->mode_config.blob_lock);
4460
4461 return blob;
4462}
4463EXPORT_SYMBOL(drm_property_lookup_blob);
4464
4465/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004466 * drm_property_replace_global_blob - atomically replace existing blob property
4467 * @dev: drm device
4468 * @replace: location of blob property pointer to be replaced
4469 * @length: length of data for new blob, or 0 for no data
4470 * @data: content for new blob, or NULL for no data
4471 * @obj_holds_id: optional object for property holding blob ID
4472 * @prop_holds_id: optional property holding blob ID
4473 * @return 0 on success or error on failure
4474 *
4475 * This function will atomically replace a global property in the blob list,
4476 * optionally updating a property which holds the ID of that property. It is
4477 * guaranteed to be atomic: no caller will be allowed to see intermediate
4478 * results, and either the entire operation will succeed and clean up the
4479 * previous property, or it will fail and the state will be unchanged.
4480 *
4481 * If length is 0 or data is NULL, no new blob will be created, and the holding
4482 * property, if specified, will be set to 0.
4483 *
4484 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4485 * by holding the relevant modesetting object lock for its parent.
4486 *
4487 * For example, a drm_connector has a 'PATH' property, which contains the ID
4488 * of a blob property with the value of the MST path information. Calling this
4489 * function with replace pointing to the connector's path_blob_ptr, length and
4490 * data set for the new path information, obj_holds_id set to the connector's
4491 * base object, and prop_holds_id set to the path property name, will perform
4492 * a completely atomic update. The access to path_blob_ptr is protected by the
4493 * caller holding a lock on the connector.
4494 */
4495static int drm_property_replace_global_blob(struct drm_device *dev,
4496 struct drm_property_blob **replace,
4497 size_t length,
4498 const void *data,
4499 struct drm_mode_object *obj_holds_id,
4500 struct drm_property *prop_holds_id)
4501{
4502 struct drm_property_blob *new_blob = NULL;
4503 struct drm_property_blob *old_blob = NULL;
4504 int ret;
4505
4506 WARN_ON(replace == NULL);
4507
4508 old_blob = *replace;
4509
4510 if (length && data) {
4511 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004512 if (IS_ERR(new_blob))
4513 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004514 }
4515
4516 /* This does not need to be synchronised with blob_lock, as the
4517 * get_properties ioctl locks all modesetting objects, and
4518 * obj_holds_id must be locked before calling here, so we cannot
4519 * have its value out of sync with the list membership modified
4520 * below under blob_lock. */
4521 if (obj_holds_id) {
4522 ret = drm_object_property_set_value(obj_holds_id,
4523 prop_holds_id,
4524 new_blob ?
4525 new_blob->base.id : 0);
4526 if (ret != 0)
4527 goto err_created;
4528 }
4529
Markus Elfringec530822015-07-05 21:55:10 +02004530 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004531 *replace = new_blob;
4532
4533 return 0;
4534
4535err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004536 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004537 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004538}
4539
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004540/**
4541 * drm_mode_getblob_ioctl - get the contents of a blob property value
4542 * @dev: DRM device
4543 * @data: ioctl data
4544 * @file_priv: DRM file info
4545 *
4546 * This function retrieves the contents of a blob property. The value stored in
4547 * an object's blob property is just a normal modeset object id.
4548 *
4549 * Called by the user via ioctl.
4550 *
4551 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004552 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004553 */
Dave Airlief453ba02008-11-07 14:05:41 -08004554int drm_mode_getblob_ioctl(struct drm_device *dev,
4555 void *data, struct drm_file *file_priv)
4556{
Dave Airlief453ba02008-11-07 14:05:41 -08004557 struct drm_mode_get_blob *out_resp = data;
4558 struct drm_property_blob *blob;
4559 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004560 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004561
Dave Airliefb3b06c2011-02-08 13:55:21 +10004562 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4563 return -EINVAL;
4564
Daniel Vetter84849902012-12-02 00:28:11 +01004565 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004566 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004567 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004568 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004569 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004570 goto done;
4571 }
Dave Airlief453ba02008-11-07 14:05:41 -08004572
4573 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004574 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004575 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004576 ret = -EFAULT;
4577 goto done;
4578 }
4579 }
4580 out_resp->length = blob->length;
4581
4582done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004583 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004584 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004585 return ret;
4586}
4587
Dave Airliecc7096f2014-10-22 12:03:04 +10004588/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004589 * drm_mode_createblob_ioctl - create a new blob property
4590 * @dev: DRM device
4591 * @data: ioctl data
4592 * @file_priv: DRM file info
4593 *
4594 * This function creates a new blob property with user-defined values. In order
4595 * to give us sensible validation and checking when creating, rather than at
4596 * every potential use, we also require a type to be provided upfront.
4597 *
4598 * Called by the user via ioctl.
4599 *
4600 * Returns:
4601 * Zero on success, negative errno on failure.
4602 */
4603int drm_mode_createblob_ioctl(struct drm_device *dev,
4604 void *data, struct drm_file *file_priv)
4605{
4606 struct drm_mode_create_blob *out_resp = data;
4607 struct drm_property_blob *blob;
4608 void __user *blob_ptr;
4609 int ret = 0;
4610
4611 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4612 return -EINVAL;
4613
4614 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4615 if (IS_ERR(blob))
4616 return PTR_ERR(blob);
4617
4618 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4619 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4620 ret = -EFAULT;
4621 goto out_blob;
4622 }
4623
4624 /* Dropping the lock between create_blob and our access here is safe
4625 * as only the same file_priv can remove the blob; at this point, it is
4626 * not associated with any file_priv. */
4627 mutex_lock(&dev->mode_config.blob_lock);
4628 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004629 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004630 mutex_unlock(&dev->mode_config.blob_lock);
4631
4632 return 0;
4633
4634out_blob:
4635 drm_property_unreference_blob(blob);
4636 return ret;
4637}
4638
4639/**
4640 * drm_mode_destroyblob_ioctl - destroy a user blob property
4641 * @dev: DRM device
4642 * @data: ioctl data
4643 * @file_priv: DRM file info
4644 *
4645 * Destroy an existing user-defined blob property.
4646 *
4647 * Called by the user via ioctl.
4648 *
4649 * Returns:
4650 * Zero on success, negative errno on failure.
4651 */
4652int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4653 void *data, struct drm_file *file_priv)
4654{
4655 struct drm_mode_destroy_blob *out_resp = data;
4656 struct drm_property_blob *blob = NULL, *bt;
4657 bool found = false;
4658 int ret = 0;
4659
4660 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4661 return -EINVAL;
4662
4663 mutex_lock(&dev->mode_config.blob_lock);
4664 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4665 if (!blob) {
4666 ret = -ENOENT;
4667 goto err;
4668 }
4669
4670 /* Ensure the property was actually created by this user. */
4671 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4672 if (bt == blob) {
4673 found = true;
4674 break;
4675 }
4676 }
4677
4678 if (!found) {
4679 ret = -EPERM;
4680 goto err;
4681 }
4682
4683 /* We must drop head_file here, because we may not be the last
4684 * reference on the blob. */
4685 list_del_init(&blob->head_file);
4686 drm_property_unreference_blob_locked(blob);
4687 mutex_unlock(&dev->mode_config.blob_lock);
4688
4689 return 0;
4690
4691err:
4692 mutex_unlock(&dev->mode_config.blob_lock);
4693 return ret;
4694}
4695
4696/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004697 * drm_mode_connector_set_path_property - set tile property on connector
4698 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004699 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004700 *
4701 * This creates a property to expose to userspace to specify a
4702 * connector path. This is mainly used for DisplayPort MST where
4703 * connectors have a topology and we want to allow userspace to give
4704 * them more meaningful names.
4705 *
4706 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004707 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004708 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004709int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004710 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004711{
4712 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004713 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004714
Daniel Stoned2ed3432015-04-20 19:22:53 +01004715 ret = drm_property_replace_global_blob(dev,
4716 &connector->path_blob_ptr,
4717 strlen(path) + 1,
4718 path,
4719 &connector->base,
4720 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004721 return ret;
4722}
4723EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4724
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004725/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004726 * drm_mode_connector_set_tile_property - set tile property on connector
4727 * @connector: connector to set property on.
4728 *
4729 * This looks up the tile information for a connector, and creates a
4730 * property for userspace to parse if it exists. The property is of
4731 * the form of 8 integers using ':' as a separator.
4732 *
4733 * Returns:
4734 * Zero on success, errno on failure.
4735 */
4736int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4737{
4738 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004739 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004740 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004741
4742 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004743 ret = drm_property_replace_global_blob(dev,
4744 &connector->tile_blob_ptr,
4745 0,
4746 NULL,
4747 &connector->base,
4748 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004749 return ret;
4750 }
4751
4752 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4753 connector->tile_group->id, connector->tile_is_single_monitor,
4754 connector->num_h_tile, connector->num_v_tile,
4755 connector->tile_h_loc, connector->tile_v_loc,
4756 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004757
Daniel Stoned2ed3432015-04-20 19:22:53 +01004758 ret = drm_property_replace_global_blob(dev,
4759 &connector->tile_blob_ptr,
4760 strlen(tile) + 1,
4761 tile,
4762 &connector->base,
4763 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004764 return ret;
4765}
4766EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4767
4768/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004769 * drm_mode_connector_update_edid_property - update the edid property of a connector
4770 * @connector: drm connector
4771 * @edid: new value of the edid property
4772 *
4773 * This function creates a new blob modeset object and assigns its id to the
4774 * connector's edid property.
4775 *
4776 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004777 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004778 */
Dave Airlief453ba02008-11-07 14:05:41 -08004779int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004780 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004781{
4782 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004783 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004784 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004785
Thomas Wood4cf2b282014-06-18 17:52:33 +01004786 /* ignore requests to set edid when overridden */
4787 if (connector->override_edid)
4788 return 0;
4789
Daniel Stoned2ed3432015-04-20 19:22:53 +01004790 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004791 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004792
Daniel Stoned2ed3432015-04-20 19:22:53 +01004793 ret = drm_property_replace_global_blob(dev,
4794 &connector->edid_blob_ptr,
4795 size,
4796 edid,
4797 &connector->base,
4798 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004799 return ret;
4800}
4801EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4802
Rob Clark3843e712014-12-16 18:05:29 -05004803/* Some properties could refer to dynamic refcnt'd objects, or things that
4804 * need special locking to handle lifetime issues (ie. to ensure the prop
4805 * value doesn't become invalid part way through the property update due to
4806 * race). The value returned by reference via 'obj' should be passed back
4807 * to drm_property_change_valid_put() after the property is set (and the
4808 * object to which the property is attached has a chance to take it's own
4809 * reference).
4810 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004811bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004812 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004813{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004814 int i;
4815
Paulo Zanoni26a34812012-05-15 18:08:59 -03004816 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4817 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004818
Rob Clark3843e712014-12-16 18:05:29 -05004819 *ref = NULL;
4820
Rob Clark5ea22f22014-05-30 11:34:01 -04004821 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004822 if (value < property->values[0] || value > property->values[1])
4823 return false;
4824 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004825 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4826 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004827
Rob Clarkebc44cf2012-09-12 22:22:31 -05004828 if (svalue < U642I64(property->values[0]) ||
4829 svalue > U642I64(property->values[1]))
4830 return false;
4831 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004832 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004833 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004834
Rob Clark49e27542012-05-17 02:23:26 -06004835 for (i = 0; i < property->num_values; i++)
4836 valid_mask |= (1ULL << property->values[i]);
4837 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004838 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004839 struct drm_property_blob *blob;
4840
4841 if (value == 0)
4842 return true;
4843
4844 blob = drm_property_lookup_blob(property->dev, value);
4845 if (blob) {
4846 *ref = &blob->base;
4847 return true;
4848 } else {
4849 return false;
4850 }
Rob Clark98f75de2014-05-30 11:37:03 -04004851 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004852 /* a zero value for an object property translates to null: */
4853 if (value == 0)
4854 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004855
4856 /* handle refcnt'd objects specially: */
4857 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4858 struct drm_framebuffer *fb;
4859 fb = drm_framebuffer_lookup(property->dev, value);
4860 if (fb) {
4861 *ref = &fb->base;
4862 return true;
4863 } else {
4864 return false;
4865 }
4866 } else {
4867 return _object_find(property->dev, value, property->values[0]) != NULL;
4868 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004869 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004870
4871 for (i = 0; i < property->num_values; i++)
4872 if (property->values[i] == value)
4873 return true;
4874 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004875}
4876
Rob Clarkd34f20d2014-12-18 16:01:56 -05004877void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004878 struct drm_mode_object *ref)
4879{
4880 if (!ref)
4881 return;
4882
4883 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4884 if (property->values[0] == DRM_MODE_OBJECT_FB)
4885 drm_framebuffer_unreference(obj_to_fb(ref));
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004886 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4887 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004888}
4889
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004890/**
4891 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4892 * @dev: DRM device
4893 * @data: ioctl data
4894 * @file_priv: DRM file info
4895 *
4896 * This function sets the current value for a connectors's property. It also
4897 * calls into a driver's ->set_property callback to update the hardware state
4898 *
4899 * Called by the user via ioctl.
4900 *
4901 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004902 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004903 */
Dave Airlief453ba02008-11-07 14:05:41 -08004904int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4905 void *data, struct drm_file *file_priv)
4906{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004907 struct drm_mode_connector_set_property *conn_set_prop = data;
4908 struct drm_mode_obj_set_property obj_set_prop = {
4909 .value = conn_set_prop->value,
4910 .prop_id = conn_set_prop->prop_id,
4911 .obj_id = conn_set_prop->connector_id,
4912 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4913 };
Dave Airlief453ba02008-11-07 14:05:41 -08004914
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004915 /* It does all the locking and checking we need */
4916 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004917}
4918
Paulo Zanonic5431882012-05-15 18:09:02 -03004919static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4920 struct drm_property *property,
4921 uint64_t value)
4922{
4923 int ret = -EINVAL;
4924 struct drm_connector *connector = obj_to_connector(obj);
4925
4926 /* Do DPMS ourselves */
4927 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004928 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004929 } else if (connector->funcs->set_property)
4930 ret = connector->funcs->set_property(connector, property, value);
4931
4932 /* store the property value if successful */
4933 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004934 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004935 return ret;
4936}
4937
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004938static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4939 struct drm_property *property,
4940 uint64_t value)
4941{
4942 int ret = -EINVAL;
4943 struct drm_crtc *crtc = obj_to_crtc(obj);
4944
4945 if (crtc->funcs->set_property)
4946 ret = crtc->funcs->set_property(crtc, property, value);
4947 if (!ret)
4948 drm_object_property_set_value(obj, property, value);
4949
4950 return ret;
4951}
4952
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004953/**
4954 * drm_mode_plane_set_obj_prop - set the value of a property
4955 * @plane: drm plane object to set property value for
4956 * @property: property to set
4957 * @value: value the property should be set to
4958 *
4959 * This functions sets a given property on a given plane object. This function
4960 * calls the driver's ->set_property callback and changes the software state of
4961 * the property if the callback succeeds.
4962 *
4963 * Returns:
4964 * Zero on success, error code on failure.
4965 */
4966int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4967 struct drm_property *property,
4968 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004969{
4970 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004971 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004972
4973 if (plane->funcs->set_property)
4974 ret = plane->funcs->set_property(plane, property, value);
4975 if (!ret)
4976 drm_object_property_set_value(obj, property, value);
4977
4978 return ret;
4979}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004980EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004981
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004982/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004983 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004984 * @dev: DRM device
4985 * @data: ioctl data
4986 * @file_priv: DRM file info
4987 *
4988 * This function retrieves the current value for an object's property. Compared
4989 * to the connector specific ioctl this one is extended to also work on crtc and
4990 * plane objects.
4991 *
4992 * Called by the user via ioctl.
4993 *
4994 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004995 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004996 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004997int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4998 struct drm_file *file_priv)
4999{
5000 struct drm_mode_obj_get_properties *arg = data;
5001 struct drm_mode_object *obj;
5002 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03005003
5004 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5005 return -EINVAL;
5006
Daniel Vetter84849902012-12-02 00:28:11 +01005007 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005008
5009 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5010 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005011 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005012 goto out;
5013 }
5014 if (!obj->properties) {
5015 ret = -EINVAL;
5016 goto out;
5017 }
5018
Rob Clark88a48e22014-12-18 16:01:50 -05005019 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005020 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5021 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5022 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005023
Paulo Zanonic5431882012-05-15 18:09:02 -03005024out:
Daniel Vetter84849902012-12-02 00:28:11 +01005025 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005026 return ret;
5027}
5028
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005029/**
5030 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5031 * @dev: DRM device
5032 * @data: ioctl data
5033 * @file_priv: DRM file info
5034 *
5035 * This function sets the current value for an object's property. It also calls
5036 * into a driver's ->set_property callback to update the hardware state.
5037 * Compared to the connector specific ioctl this one is extended to also work on
5038 * crtc and plane objects.
5039 *
5040 * Called by the user via ioctl.
5041 *
5042 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005043 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005044 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005045int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5046 struct drm_file *file_priv)
5047{
5048 struct drm_mode_obj_set_property *arg = data;
5049 struct drm_mode_object *arg_obj;
5050 struct drm_mode_object *prop_obj;
5051 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005052 int i, ret = -EINVAL;
5053 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005054
5055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5056 return -EINVAL;
5057
Daniel Vetter84849902012-12-02 00:28:11 +01005058 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005059
5060 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005061 if (!arg_obj) {
5062 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005063 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005064 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005065 if (!arg_obj->properties)
5066 goto out;
5067
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005068 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005069 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005070 break;
5071
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005072 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005073 goto out;
5074
5075 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5076 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005077 if (!prop_obj) {
5078 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005079 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005080 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005081 property = obj_to_property(prop_obj);
5082
Rob Clark3843e712014-12-16 18:05:29 -05005083 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005084 goto out;
5085
5086 switch (arg_obj->type) {
5087 case DRM_MODE_OBJECT_CONNECTOR:
5088 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5089 arg->value);
5090 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005091 case DRM_MODE_OBJECT_CRTC:
5092 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5093 break;
Rob Clark4d939142012-05-17 02:23:27 -06005094 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005095 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5096 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005097 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005098 }
5099
Rob Clark3843e712014-12-16 18:05:29 -05005100 drm_property_change_valid_put(property, ref);
5101
Paulo Zanonic5431882012-05-15 18:09:02 -03005102out:
Daniel Vetter84849902012-12-02 00:28:11 +01005103 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005104 return ret;
5105}
5106
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005107/**
5108 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5109 * @connector: connector to attach
5110 * @encoder: encoder to attach @connector to
5111 *
5112 * This function links up a connector to an encoder. Note that the routing
5113 * restrictions between encoders and crtcs are exposed to userspace through the
5114 * possible_clones and possible_crtcs bitmasks.
5115 *
5116 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005117 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005118 */
Dave Airlief453ba02008-11-07 14:05:41 -08005119int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5120 struct drm_encoder *encoder)
5121{
5122 int i;
5123
Thierry Redingeb47fe82015-11-16 18:19:53 +01005124 /*
5125 * In the past, drivers have attempted to model the static association
5126 * of connector to encoder in simple connector/encoder devices using a
5127 * direct assignment of connector->encoder = encoder. This connection
5128 * is a logical one and the responsibility of the core, so drivers are
5129 * expected not to mess with this.
5130 *
5131 * Note that the error return should've been enough here, but a large
5132 * majority of drivers ignores the return value, so add in a big WARN
5133 * to get people's attention.
5134 */
5135 if (WARN_ON(connector->encoder))
5136 return -EINVAL;
5137
Dave Airlief453ba02008-11-07 14:05:41 -08005138 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5139 if (connector->encoder_ids[i] == 0) {
5140 connector->encoder_ids[i] = encoder->base.id;
5141 return 0;
5142 }
5143 }
5144 return -ENOMEM;
5145}
5146EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5147
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005148/**
5149 * drm_mode_crtc_set_gamma_size - set the gamma table size
5150 * @crtc: CRTC to set the gamma table size for
5151 * @gamma_size: size of the gamma table
5152 *
5153 * Drivers which support gamma tables should set this to the supported gamma
5154 * table size when initializing the CRTC. Currently the drm core only supports a
5155 * fixed gamma table size.
5156 *
5157 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005158 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005159 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005160int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005161 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005162{
5163 crtc->gamma_size = gamma_size;
5164
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005165 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5166 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005167 if (!crtc->gamma_store) {
5168 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005169 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005170 }
5171
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005172 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005173}
5174EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5175
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005176/**
5177 * drm_mode_gamma_set_ioctl - set the gamma table
5178 * @dev: DRM device
5179 * @data: ioctl data
5180 * @file_priv: DRM file info
5181 *
5182 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5183 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5184 *
5185 * Called by the user via ioctl.
5186 *
5187 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005188 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005189 */
Dave Airlief453ba02008-11-07 14:05:41 -08005190int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5191 void *data, struct drm_file *file_priv)
5192{
5193 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005194 struct drm_crtc *crtc;
5195 void *r_base, *g_base, *b_base;
5196 int size;
5197 int ret = 0;
5198
Dave Airliefb3b06c2011-02-08 13:55:21 +10005199 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5200 return -EINVAL;
5201
Daniel Vetter84849902012-12-02 00:28:11 +01005202 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005203 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5204 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005205 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005206 goto out;
5207 }
Dave Airlief453ba02008-11-07 14:05:41 -08005208
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005209 if (crtc->funcs->gamma_set == NULL) {
5210 ret = -ENOSYS;
5211 goto out;
5212 }
5213
Dave Airlief453ba02008-11-07 14:05:41 -08005214 /* memcpy into gamma store */
5215 if (crtc_lut->gamma_size != crtc->gamma_size) {
5216 ret = -EINVAL;
5217 goto out;
5218 }
5219
5220 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5221 r_base = crtc->gamma_store;
5222 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5223 ret = -EFAULT;
5224 goto out;
5225 }
5226
5227 g_base = r_base + size;
5228 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5229 ret = -EFAULT;
5230 goto out;
5231 }
5232
5233 b_base = g_base + size;
5234 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5235 ret = -EFAULT;
5236 goto out;
5237 }
5238
James Simmons72034252010-08-03 01:33:19 +01005239 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005240
5241out:
Daniel Vetter84849902012-12-02 00:28:11 +01005242 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005243 return ret;
5244
5245}
5246
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005247/**
5248 * drm_mode_gamma_get_ioctl - get the gamma table
5249 * @dev: DRM device
5250 * @data: ioctl data
5251 * @file_priv: DRM file info
5252 *
5253 * Copy the current gamma table into the storage provided. This also provides
5254 * the gamma table size the driver expects, which can be used to size the
5255 * allocated storage.
5256 *
5257 * Called by the user via ioctl.
5258 *
5259 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005260 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005261 */
Dave Airlief453ba02008-11-07 14:05:41 -08005262int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5263 void *data, struct drm_file *file_priv)
5264{
5265 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005266 struct drm_crtc *crtc;
5267 void *r_base, *g_base, *b_base;
5268 int size;
5269 int ret = 0;
5270
Dave Airliefb3b06c2011-02-08 13:55:21 +10005271 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5272 return -EINVAL;
5273
Daniel Vetter84849902012-12-02 00:28:11 +01005274 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005275 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5276 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005277 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005278 goto out;
5279 }
Dave Airlief453ba02008-11-07 14:05:41 -08005280
5281 /* memcpy into gamma store */
5282 if (crtc_lut->gamma_size != crtc->gamma_size) {
5283 ret = -EINVAL;
5284 goto out;
5285 }
5286
5287 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5288 r_base = crtc->gamma_store;
5289 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5290 ret = -EFAULT;
5291 goto out;
5292 }
5293
5294 g_base = r_base + size;
5295 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5296 ret = -EFAULT;
5297 goto out;
5298 }
5299
5300 b_base = g_base + size;
5301 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5302 ret = -EFAULT;
5303 goto out;
5304 }
5305out:
Daniel Vetter84849902012-12-02 00:28:11 +01005306 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005307 return ret;
5308}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005309
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005310/**
5311 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5312 * @dev: DRM device
5313 * @data: ioctl data
5314 * @file_priv: DRM file info
5315 *
5316 * This schedules an asynchronous update on a given CRTC, called page flip.
5317 * Optionally a drm event is generated to signal the completion of the event.
5318 * Generic drivers cannot assume that a pageflip with changed framebuffer
5319 * properties (including driver specific metadata like tiling layout) will work,
5320 * but some drivers support e.g. pixel format changes through the pageflip
5321 * ioctl.
5322 *
5323 * Called by the user via ioctl.
5324 *
5325 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005326 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005327 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005328int drm_mode_page_flip_ioctl(struct drm_device *dev,
5329 void *data, struct drm_file *file_priv)
5330{
5331 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005332 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005333 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005334 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005335 int ret = -EINVAL;
5336
5337 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5338 page_flip->reserved != 0)
5339 return -EINVAL;
5340
Keith Packard62f21042013-07-22 18:50:00 -07005341 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5342 return -EINVAL;
5343
Rob Clarka2b34e22013-10-05 16:36:52 -04005344 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5345 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005346 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005347
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005348 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005349 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005350 /* The framebuffer is currently unbound, presumably
5351 * due to a hotplug event, that userspace has not
5352 * yet discovered.
5353 */
5354 ret = -EBUSY;
5355 goto out;
5356 }
5357
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005358 if (crtc->funcs->page_flip == NULL)
5359 goto out;
5360
Daniel Vetter786b99e2012-12-02 21:53:40 +01005361 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005362 if (!fb) {
5363 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005364 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005365 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005366
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005367 if (crtc->state) {
5368 const struct drm_plane_state *state = crtc->primary->state;
5369
5370 ret = check_src_coords(state->src_x, state->src_y,
5371 state->src_w, state->src_h, fb);
5372 } else {
5373 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5374 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005375 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005376 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005377
Matt Roperf4510a22014-04-01 15:22:40 -07005378 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005379 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5380 ret = -EINVAL;
5381 goto out;
5382 }
5383
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005384 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005385 e = kzalloc(sizeof *e, GFP_KERNEL);
5386 if (!e) {
5387 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005388 goto out;
5389 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005390 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005391 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005392 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005393 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5394 if (ret) {
5395 kfree(e);
5396 goto out;
5397 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005398 }
5399
Daniel Vetter3d30a592014-07-27 13:42:42 +02005400 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005401 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005402 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005403 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5404 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005405 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005406 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005407 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005408 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005409 /* Unref only the old framebuffer. */
5410 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005411 }
5412
5413out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005414 if (fb)
5415 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005416 if (crtc->primary->old_fb)
5417 drm_framebuffer_unreference(crtc->primary->old_fb);
5418 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005419 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005420
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005421 return ret;
5422}
Chris Wilsoneb033552011-01-24 15:11:08 +00005423
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005424/**
5425 * drm_mode_config_reset - call ->reset callbacks
5426 * @dev: drm device
5427 *
5428 * This functions calls all the crtc's, encoder's and connector's ->reset
5429 * callback. Drivers can use this in e.g. their driver load or resume code to
5430 * reset hardware and software state.
5431 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005432void drm_mode_config_reset(struct drm_device *dev)
5433{
5434 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005435 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005436 struct drm_encoder *encoder;
5437 struct drm_connector *connector;
5438
Daniel Vettere4f62542015-07-09 23:44:35 +02005439 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005440 if (plane->funcs->reset)
5441 plane->funcs->reset(plane);
5442
Daniel Vettere4f62542015-07-09 23:44:35 +02005443 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005444 if (crtc->funcs->reset)
5445 crtc->funcs->reset(crtc);
5446
Daniel Vettere4f62542015-07-09 23:44:35 +02005447 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005448 if (encoder->funcs->reset)
5449 encoder->funcs->reset(encoder);
5450
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005451 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005452 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005453 if (connector->funcs->reset)
5454 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005455 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005456}
5457EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005458
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005459/**
5460 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5461 * @dev: DRM device
5462 * @data: ioctl data
5463 * @file_priv: DRM file info
5464 *
5465 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5466 * TTM or something else entirely) and returns the resulting buffer handle. This
5467 * handle can then be wrapped up into a framebuffer modeset object.
5468 *
5469 * Note that userspace is not allowed to use such objects for render
5470 * acceleration - drivers must create their own private ioctls for such a use
5471 * case.
5472 *
5473 * Called by the user via ioctl.
5474 *
5475 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005476 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005477 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005478int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5479 void *data, struct drm_file *file_priv)
5480{
5481 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005482 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005483
5484 if (!dev->driver->dumb_create)
5485 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005486 if (!args->width || !args->height || !args->bpp)
5487 return -EINVAL;
5488
5489 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005490 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005491 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005492 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005493 return -EINVAL;
5494 stride = cpp * args->width;
5495 if (args->height > 0xffffffffU / stride)
5496 return -EINVAL;
5497
5498 /* test for wrap-around */
5499 size = args->height * stride;
5500 if (PAGE_ALIGN(size) == 0)
5501 return -EINVAL;
5502
Thierry Redingf6085952014-11-03 11:14:14 +01005503 /*
5504 * handle, pitch and size are output parameters. Zero them out to
5505 * prevent drivers from accidentally using uninitialized data. Since
5506 * not all existing userspace is clearing these fields properly we
5507 * cannot reject IOCTL with garbage in them.
5508 */
5509 args->handle = 0;
5510 args->pitch = 0;
5511 args->size = 0;
5512
Dave Airlieff72145b2011-02-07 12:16:14 +10005513 return dev->driver->dumb_create(file_priv, dev, args);
5514}
5515
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005516/**
5517 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5518 * @dev: DRM device
5519 * @data: ioctl data
5520 * @file_priv: DRM file info
5521 *
5522 * Allocate an offset in the drm device node's address space to be able to
5523 * memory map a dumb buffer.
5524 *
5525 * Called by the user via ioctl.
5526 *
5527 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005528 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005529 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005530int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5531 void *data, struct drm_file *file_priv)
5532{
5533 struct drm_mode_map_dumb *args = data;
5534
5535 /* call driver ioctl to get mmap offset */
5536 if (!dev->driver->dumb_map_offset)
5537 return -ENOSYS;
5538
5539 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5540}
5541
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005542/**
5543 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5544 * @dev: DRM device
5545 * @data: ioctl data
5546 * @file_priv: DRM file info
5547 *
5548 * This destroys the userspace handle for the given dumb backing storage buffer.
5549 * Since buffer objects must be reference counted in the kernel a buffer object
5550 * won't be immediately freed if a framebuffer modeset object still uses it.
5551 *
5552 * Called by the user via ioctl.
5553 *
5554 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005555 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005556 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005557int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5558 void *data, struct drm_file *file_priv)
5559{
5560 struct drm_mode_destroy_dumb *args = data;
5561
5562 if (!dev->driver->dumb_destroy)
5563 return -ENOSYS;
5564
5565 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5566}
Dave Airlie248dbc22011-11-29 20:02:54 +00005567
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005568/**
5569 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5570 * @format: pixel format (DRM_FORMAT_*)
5571 * @depth: storage for the depth value
5572 * @bpp: storage for the bpp value
5573 *
5574 * This only supports RGB formats here for compat with code that doesn't use
5575 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005576 */
5577void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5578 int *bpp)
5579{
5580 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005581 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005582 case DRM_FORMAT_RGB332:
5583 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005584 *depth = 8;
5585 *bpp = 8;
5586 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005587 case DRM_FORMAT_XRGB1555:
5588 case DRM_FORMAT_XBGR1555:
5589 case DRM_FORMAT_RGBX5551:
5590 case DRM_FORMAT_BGRX5551:
5591 case DRM_FORMAT_ARGB1555:
5592 case DRM_FORMAT_ABGR1555:
5593 case DRM_FORMAT_RGBA5551:
5594 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005595 *depth = 15;
5596 *bpp = 16;
5597 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005598 case DRM_FORMAT_RGB565:
5599 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005600 *depth = 16;
5601 *bpp = 16;
5602 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005603 case DRM_FORMAT_RGB888:
5604 case DRM_FORMAT_BGR888:
5605 *depth = 24;
5606 *bpp = 24;
5607 break;
5608 case DRM_FORMAT_XRGB8888:
5609 case DRM_FORMAT_XBGR8888:
5610 case DRM_FORMAT_RGBX8888:
5611 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005612 *depth = 24;
5613 *bpp = 32;
5614 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005615 case DRM_FORMAT_XRGB2101010:
5616 case DRM_FORMAT_XBGR2101010:
5617 case DRM_FORMAT_RGBX1010102:
5618 case DRM_FORMAT_BGRX1010102:
5619 case DRM_FORMAT_ARGB2101010:
5620 case DRM_FORMAT_ABGR2101010:
5621 case DRM_FORMAT_RGBA1010102:
5622 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005623 *depth = 30;
5624 *bpp = 32;
5625 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005626 case DRM_FORMAT_ARGB8888:
5627 case DRM_FORMAT_ABGR8888:
5628 case DRM_FORMAT_RGBA8888:
5629 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005630 *depth = 32;
5631 *bpp = 32;
5632 break;
5633 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005634 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5635 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005636 *depth = 0;
5637 *bpp = 0;
5638 break;
5639 }
5640}
5641EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005642
5643/**
5644 * drm_format_num_planes - get the number of planes for format
5645 * @format: pixel format (DRM_FORMAT_*)
5646 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005647 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005648 * The number of planes used by the specified pixel format.
5649 */
5650int drm_format_num_planes(uint32_t format)
5651{
5652 switch (format) {
5653 case DRM_FORMAT_YUV410:
5654 case DRM_FORMAT_YVU410:
5655 case DRM_FORMAT_YUV411:
5656 case DRM_FORMAT_YVU411:
5657 case DRM_FORMAT_YUV420:
5658 case DRM_FORMAT_YVU420:
5659 case DRM_FORMAT_YUV422:
5660 case DRM_FORMAT_YVU422:
5661 case DRM_FORMAT_YUV444:
5662 case DRM_FORMAT_YVU444:
5663 return 3;
5664 case DRM_FORMAT_NV12:
5665 case DRM_FORMAT_NV21:
5666 case DRM_FORMAT_NV16:
5667 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005668 case DRM_FORMAT_NV24:
5669 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005670 return 2;
5671 default:
5672 return 1;
5673 }
5674}
5675EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005676
5677/**
5678 * drm_format_plane_cpp - determine the bytes per pixel value
5679 * @format: pixel format (DRM_FORMAT_*)
5680 * @plane: plane index
5681 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005682 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005683 * The bytes per pixel value for the specified plane.
5684 */
5685int drm_format_plane_cpp(uint32_t format, int plane)
5686{
5687 unsigned int depth;
5688 int bpp;
5689
5690 if (plane >= drm_format_num_planes(format))
5691 return 0;
5692
5693 switch (format) {
5694 case DRM_FORMAT_YUYV:
5695 case DRM_FORMAT_YVYU:
5696 case DRM_FORMAT_UYVY:
5697 case DRM_FORMAT_VYUY:
5698 return 2;
5699 case DRM_FORMAT_NV12:
5700 case DRM_FORMAT_NV21:
5701 case DRM_FORMAT_NV16:
5702 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005703 case DRM_FORMAT_NV24:
5704 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005705 return plane ? 2 : 1;
5706 case DRM_FORMAT_YUV410:
5707 case DRM_FORMAT_YVU410:
5708 case DRM_FORMAT_YUV411:
5709 case DRM_FORMAT_YVU411:
5710 case DRM_FORMAT_YUV420:
5711 case DRM_FORMAT_YVU420:
5712 case DRM_FORMAT_YUV422:
5713 case DRM_FORMAT_YVU422:
5714 case DRM_FORMAT_YUV444:
5715 case DRM_FORMAT_YVU444:
5716 return 1;
5717 default:
5718 drm_fb_get_bpp_depth(format, &depth, &bpp);
5719 return bpp >> 3;
5720 }
5721}
5722EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005723
5724/**
5725 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5726 * @format: pixel format (DRM_FORMAT_*)
5727 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005728 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005729 * The horizontal chroma subsampling factor for the
5730 * specified pixel format.
5731 */
5732int drm_format_horz_chroma_subsampling(uint32_t format)
5733{
5734 switch (format) {
5735 case DRM_FORMAT_YUV411:
5736 case DRM_FORMAT_YVU411:
5737 case DRM_FORMAT_YUV410:
5738 case DRM_FORMAT_YVU410:
5739 return 4;
5740 case DRM_FORMAT_YUYV:
5741 case DRM_FORMAT_YVYU:
5742 case DRM_FORMAT_UYVY:
5743 case DRM_FORMAT_VYUY:
5744 case DRM_FORMAT_NV12:
5745 case DRM_FORMAT_NV21:
5746 case DRM_FORMAT_NV16:
5747 case DRM_FORMAT_NV61:
5748 case DRM_FORMAT_YUV422:
5749 case DRM_FORMAT_YVU422:
5750 case DRM_FORMAT_YUV420:
5751 case DRM_FORMAT_YVU420:
5752 return 2;
5753 default:
5754 return 1;
5755 }
5756}
5757EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5758
5759/**
5760 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5761 * @format: pixel format (DRM_FORMAT_*)
5762 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005763 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005764 * The vertical chroma subsampling factor for the
5765 * specified pixel format.
5766 */
5767int drm_format_vert_chroma_subsampling(uint32_t format)
5768{
5769 switch (format) {
5770 case DRM_FORMAT_YUV410:
5771 case DRM_FORMAT_YVU410:
5772 return 4;
5773 case DRM_FORMAT_YUV420:
5774 case DRM_FORMAT_YVU420:
5775 case DRM_FORMAT_NV12:
5776 case DRM_FORMAT_NV21:
5777 return 2;
5778 default:
5779 return 1;
5780 }
5781}
5782EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005783
5784/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005785 * drm_format_plane_width - width of the plane given the first plane
5786 * @width: width of the first plane
5787 * @format: pixel format
5788 * @plane: plane index
5789 *
5790 * Returns:
5791 * The width of @plane, given that the width of the first plane is @width.
5792 */
5793int drm_format_plane_width(int width, uint32_t format, int plane)
5794{
5795 if (plane >= drm_format_num_planes(format))
5796 return 0;
5797
5798 if (plane == 0)
5799 return width;
5800
5801 return width / drm_format_horz_chroma_subsampling(format);
5802}
5803EXPORT_SYMBOL(drm_format_plane_width);
5804
5805/**
5806 * drm_format_plane_height - height of the plane given the first plane
5807 * @height: height of the first plane
5808 * @format: pixel format
5809 * @plane: plane index
5810 *
5811 * Returns:
5812 * The height of @plane, given that the height of the first plane is @height.
5813 */
5814int drm_format_plane_height(int height, uint32_t format, int plane)
5815{
5816 if (plane >= drm_format_num_planes(format))
5817 return 0;
5818
5819 if (plane == 0)
5820 return height;
5821
5822 return height / drm_format_vert_chroma_subsampling(format);
5823}
5824EXPORT_SYMBOL(drm_format_plane_height);
5825
5826/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305827 * drm_rotation_simplify() - Try to simplify the rotation
5828 * @rotation: Rotation to be simplified
5829 * @supported_rotations: Supported rotations
5830 *
5831 * Attempt to simplify the rotation to a form that is supported.
5832 * Eg. if the hardware supports everything except DRM_REFLECT_X
5833 * one could call this function like this:
5834 *
5835 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5836 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5837 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5838 *
5839 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5840 * transforms the hardware supports, this function may not
5841 * be able to produce a supported transform, so the caller should
5842 * check the result afterwards.
5843 */
5844unsigned int drm_rotation_simplify(unsigned int rotation,
5845 unsigned int supported_rotations)
5846{
5847 if (rotation & ~supported_rotations) {
5848 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005849 rotation = (rotation & DRM_REFLECT_MASK) |
5850 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305851 }
5852
5853 return rotation;
5854}
5855EXPORT_SYMBOL(drm_rotation_simplify);
5856
5857/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005858 * drm_mode_config_init - initialize DRM mode_configuration structure
5859 * @dev: DRM device
5860 *
5861 * Initialize @dev's mode_config structure, used for tracking the graphics
5862 * configuration of @dev.
5863 *
5864 * Since this initializes the modeset locks, no locking is possible. Which is no
5865 * problem, since this should happen single threaded at init time. It is the
5866 * driver's problem to ensure this guarantee.
5867 *
5868 */
5869void drm_mode_config_init(struct drm_device *dev)
5870{
5871 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005872 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005873 mutex_init(&dev->mode_config.idr_mutex);
5874 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005875 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005876 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5877 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5878 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5879 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5880 INIT_LIST_HEAD(&dev->mode_config.property_list);
5881 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5882 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5883 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005884 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005885 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005886
5887 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005888 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005889 drm_modeset_unlock_all(dev);
5890
5891 /* Just to be sure */
5892 dev->mode_config.num_fb = 0;
5893 dev->mode_config.num_connector = 0;
5894 dev->mode_config.num_crtc = 0;
5895 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005896 dev->mode_config.num_overlay_plane = 0;
5897 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005898}
5899EXPORT_SYMBOL(drm_mode_config_init);
5900
5901/**
5902 * drm_mode_config_cleanup - free up DRM mode_config info
5903 * @dev: DRM device
5904 *
5905 * Free up all the connectors and CRTCs associated with this DRM device, then
5906 * free up the framebuffers and associated buffer objects.
5907 *
5908 * Note that since this /should/ happen single-threaded at driver/device
5909 * teardown time, no locking is required. It's the driver's job to ensure that
5910 * this guarantee actually holds true.
5911 *
5912 * FIXME: cleanup any dangling user buffer objects too
5913 */
5914void drm_mode_config_cleanup(struct drm_device *dev)
5915{
5916 struct drm_connector *connector, *ot;
5917 struct drm_crtc *crtc, *ct;
5918 struct drm_encoder *encoder, *enct;
5919 struct drm_framebuffer *fb, *fbt;
5920 struct drm_property *property, *pt;
5921 struct drm_property_blob *blob, *bt;
5922 struct drm_plane *plane, *plt;
5923
5924 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5925 head) {
5926 encoder->funcs->destroy(encoder);
5927 }
5928
5929 list_for_each_entry_safe(connector, ot,
5930 &dev->mode_config.connector_list, head) {
5931 connector->funcs->destroy(connector);
5932 }
5933
5934 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5935 head) {
5936 drm_property_destroy(dev, property);
5937 }
5938
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005939 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5940 head) {
5941 plane->funcs->destroy(plane);
5942 }
5943
5944 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5945 crtc->funcs->destroy(crtc);
5946 }
5947
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005948 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005949 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005950 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005951 }
5952
5953 /*
5954 * Single-threaded teardown context, so it's not required to grab the
5955 * fb_lock to protect against concurrent fb_list access. Contrary, it
5956 * would actually deadlock with the drm_framebuffer_cleanup function.
5957 *
5958 * Also, if there are any framebuffers left, that's a driver leak now,
5959 * so politely WARN about this.
5960 */
5961 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5962 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005963 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005964 }
5965
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005966 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005967 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005968 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005969 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005970}
5971EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305972
5973struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5974 unsigned int supported_rotations)
5975{
5976 static const struct drm_prop_enum_list props[] = {
5977 { DRM_ROTATE_0, "rotate-0" },
5978 { DRM_ROTATE_90, "rotate-90" },
5979 { DRM_ROTATE_180, "rotate-180" },
5980 { DRM_ROTATE_270, "rotate-270" },
5981 { DRM_REFLECT_X, "reflect-x" },
5982 { DRM_REFLECT_Y, "reflect-y" },
5983 };
5984
5985 return drm_property_create_bitmask(dev, 0, "rotation",
5986 props, ARRAY_SIZE(props),
5987 supported_rotations);
5988}
5989EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005990
5991/**
5992 * DOC: Tile group
5993 *
5994 * Tile groups are used to represent tiled monitors with a unique
5995 * integer identifier. Tiled monitors using DisplayID v1.3 have
5996 * a unique 8-byte handle, we store this in a tile group, so we
5997 * have a common identifier for all tiles in a monitor group.
5998 */
5999static void drm_tile_group_free(struct kref *kref)
6000{
6001 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
6002 struct drm_device *dev = tg->dev;
6003 mutex_lock(&dev->mode_config.idr_mutex);
6004 idr_remove(&dev->mode_config.tile_idr, tg->id);
6005 mutex_unlock(&dev->mode_config.idr_mutex);
6006 kfree(tg);
6007}
6008
6009/**
6010 * drm_mode_put_tile_group - drop a reference to a tile group.
6011 * @dev: DRM device
6012 * @tg: tile group to drop reference to.
6013 *
6014 * drop reference to tile group and free if 0.
6015 */
6016void drm_mode_put_tile_group(struct drm_device *dev,
6017 struct drm_tile_group *tg)
6018{
6019 kref_put(&tg->refcount, drm_tile_group_free);
6020}
6021
6022/**
6023 * drm_mode_get_tile_group - get a reference to an existing tile group
6024 * @dev: DRM device
6025 * @topology: 8-bytes unique per monitor.
6026 *
6027 * Use the unique bytes to get a reference to an existing tile group.
6028 *
6029 * RETURNS:
6030 * tile group or NULL if not found.
6031 */
6032struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6033 char topology[8])
6034{
6035 struct drm_tile_group *tg;
6036 int id;
6037 mutex_lock(&dev->mode_config.idr_mutex);
6038 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6039 if (!memcmp(tg->group_data, topology, 8)) {
6040 if (!kref_get_unless_zero(&tg->refcount))
6041 tg = NULL;
6042 mutex_unlock(&dev->mode_config.idr_mutex);
6043 return tg;
6044 }
6045 }
6046 mutex_unlock(&dev->mode_config.idr_mutex);
6047 return NULL;
6048}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006049EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006050
6051/**
6052 * drm_mode_create_tile_group - create a tile group from a displayid description
6053 * @dev: DRM device
6054 * @topology: 8-bytes unique per monitor.
6055 *
6056 * Create a tile group for the unique monitor, and get a unique
6057 * identifier for the tile group.
6058 *
6059 * RETURNS:
6060 * new tile group or error.
6061 */
6062struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6063 char topology[8])
6064{
6065 struct drm_tile_group *tg;
6066 int ret;
6067
6068 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6069 if (!tg)
6070 return ERR_PTR(-ENOMEM);
6071
6072 kref_init(&tg->refcount);
6073 memcpy(tg->group_data, topology, 8);
6074 tg->dev = dev;
6075
6076 mutex_lock(&dev->mode_config.idr_mutex);
6077 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6078 if (ret >= 0) {
6079 tg->id = ret;
6080 } else {
6081 kfree(tg);
6082 tg = ERR_PTR(ret);
6083 }
6084
6085 mutex_unlock(&dev->mode_config.idr_mutex);
6086 return tg;
6087}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006088EXPORT_SYMBOL(drm_mode_create_tile_group);