blob: 4e5b015a5e3aa8b647b55fddd788409ded1fda2b [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;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000363
364 if (obj && obj->free_cb) {
365 if (!kref_get_unless_zero(&obj->refcount))
366 obj = NULL;
367 }
Rob Clark98f75de2014-05-30 11:37:03 -0400368 mutex_unlock(&dev->mode_config.idr_mutex);
369
370 return obj;
371}
372
Daniel Vetter786b99e2012-12-02 21:53:40 +0100373/**
374 * drm_mode_object_find - look up a drm object with static lifetime
375 * @dev: drm device
376 * @id: id of the mode object
377 * @type: type of the mode object
378 *
Daniel Vetter059814222016-04-22 22:10:27 +0200379 * This function is used to look up a modeset object. It will acquire a
380 * reference for reference counted objects. This reference must be dropped again
381 * by callind drm_mode_object_unreference().
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
Rob Clark98f75de2014-05-30 11:37:03 -0400388 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800389 return obj;
390}
391EXPORT_SYMBOL(drm_mode_object_find);
392
Daniel Vetter059814222016-04-22 22:10:27 +0200393/**
394 * drm_mode_object_unreference - decr the object refcnt
395 * @obj: mode_object
396 *
397 * This functions decrements the object's refcount if it is a refcounted modeset
398 * object. It is a no-op on any other object. This is used to drop references
399 * acquired with drm_mode_object_reference().
400 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000401void drm_mode_object_unreference(struct drm_mode_object *obj)
402{
403 if (obj->free_cb) {
404 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
405 kref_put(&obj->refcount, obj->free_cb);
406 }
407}
408EXPORT_SYMBOL(drm_mode_object_unreference);
409
410/**
Daniel Vetter059814222016-04-22 22:10:27 +0200411 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000412 * @obj: mode_object
413 *
Daniel Vetter059814222016-04-22 22:10:27 +0200414 * This functions increments the object's refcount if it is a refcounted modeset
415 * object. It is a no-op on any other object. References should be dropped again
416 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000417 */
418void drm_mode_object_reference(struct drm_mode_object *obj)
419{
420 if (obj->free_cb) {
421 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
422 kref_get(&obj->refcount);
423 }
424}
425EXPORT_SYMBOL(drm_mode_object_reference);
426
Dave Airlief55f1f92016-04-15 15:10:33 +1000427static void drm_framebuffer_free(struct kref *kref)
428{
429 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000430 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000431 struct drm_device *dev = fb->dev;
432
433 /*
434 * The lookup idr holds a weak reference, which has not necessarily been
435 * removed at this point. Check for that.
436 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000437 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000438
439 fb->funcs->destroy(fb);
440}
441
Dave Airlief453ba02008-11-07 14:05:41 -0800442/**
Dave Airlief453ba02008-11-07 14:05:41 -0800443 * drm_framebuffer_init - initialize a framebuffer
444 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100445 * @fb: framebuffer to be initialized
446 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800447 *
Dave Airlief453ba02008-11-07 14:05:41 -0800448 * Allocates an ID for the framebuffer's parent mode object, sets its mode
449 * functions & device file and adds it to the master fd list.
450 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100451 * IMPORTANT:
452 * This functions publishes the fb and makes it available for concurrent access
453 * by other users. Which means by this point the fb _must_ be fully set up -
454 * since all the fb attributes are invariant over its lifetime, no further
455 * locking but only correct reference counting is required.
456 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100457 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200458 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800459 */
460int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
461 const struct drm_framebuffer_funcs *funcs)
462{
463 int ret;
464
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100465 INIT_LIST_HEAD(&fb->filp_head);
466 fb->dev = dev;
467 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000468
Dave Airlied0f37cf2016-04-15 15:10:36 +1000469 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000470 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200471 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100472 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800473
Dave Airlie9cd47422016-04-15 15:10:38 +1000474 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800475 dev->mode_config.num_fb++;
476 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000477 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800478
Dave Airlie9cd47422016-04-15 15:10:38 +1000479 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000480out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200481 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800482}
483EXPORT_SYMBOL(drm_framebuffer_init);
484
Rob Clarkf7eff602012-09-05 21:48:38 +0000485/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100486 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
487 * @dev: drm device
488 * @id: id of the fb object
489 *
490 * If successful, this grabs an additional reference to the framebuffer -
491 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100492 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100493 */
494struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
495 uint32_t id)
496{
Dave Airliecee26ac2016-04-15 15:10:37 +1000497 struct drm_mode_object *obj;
498 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100499
Dave Airliecee26ac2016-04-15 15:10:37 +1000500 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000501 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000502 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100503 return fb;
504}
505EXPORT_SYMBOL(drm_framebuffer_lookup);
506
507/**
Daniel Vetter36206362012-12-10 20:42:17 +0100508 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
509 * @fb: fb to unregister
510 *
511 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
512 * those used for fbdev. Note that the caller must hold a reference of it's own,
513 * i.e. the object may not be destroyed through this call (since it'll lead to a
514 * locking inversion).
515 */
516void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
517{
Daniel Vettera39a3572015-08-25 15:45:11 +0200518 struct drm_device *dev;
519
520 if (!fb)
521 return;
522
523 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100524
Daniel Vetter2b677e82012-12-10 21:16:05 +0100525 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000526 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100527}
528EXPORT_SYMBOL(drm_framebuffer_unregister_private);
529
530/**
Dave Airlief453ba02008-11-07 14:05:41 -0800531 * drm_framebuffer_cleanup - remove a framebuffer object
532 * @fb: framebuffer to remove
533 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100534 * Cleanup framebuffer. This function is intended to be used from the drivers
535 * ->destroy callback. It can also be used to clean up driver private
536 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100537 *
538 * Note that this function does not remove the fb from active usuage - if it is
539 * still used anywhere, hilarity can ensue since userspace could call getfb on
540 * the id and get back -EINVAL. Obviously no concern at driver unload time.
541 *
542 * Also, the framebuffer will not be removed from the lookup idr - for
543 * user-created framebuffers this will happen in in the rmfb ioctl. For
544 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
545 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800546 */
547void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
548{
549 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100550
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100551 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000552 list_del(&fb->head);
553 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100554 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000555}
556EXPORT_SYMBOL(drm_framebuffer_cleanup);
557
558/**
559 * drm_framebuffer_remove - remove and unreference a framebuffer object
560 * @fb: framebuffer to remove
561 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000562 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100563 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100564 * passed-in framebuffer. Might take the modeset locks.
565 *
566 * Note that this function optimizes the cleanup away if the caller holds the
567 * last reference to the framebuffer. It is also guaranteed to not take the
568 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000569 */
570void drm_framebuffer_remove(struct drm_framebuffer *fb)
571{
Daniel Vettera39a3572015-08-25 15:45:11 +0200572 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800573 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800574 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000575 struct drm_mode_set set;
576 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800577
Daniel Vettera39a3572015-08-25 15:45:11 +0200578 if (!fb)
579 return;
580
581 dev = fb->dev;
582
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100583 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100584
Daniel Vetterb62584e2012-12-11 16:51:35 +0100585 /*
586 * drm ABI mandates that we remove any deleted framebuffers from active
587 * useage. But since most sane clients only remove framebuffers they no
588 * longer need, try to optimize this away.
589 *
590 * Since we're holding a reference ourselves, observing a refcount of 1
591 * means that we're the last holder and can skip it. Also, the refcount
592 * can never increase from 1 again, so we don't need any barriers or
593 * locks.
594 *
595 * Note that userspace could try to race with use and instate a new
596 * usage _after_ we've cleared all current ones. End result will be an
597 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
598 * in this manner.
599 */
Dave Airlie747a5982016-04-15 15:10:35 +1000600 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100601 drm_modeset_lock_all(dev);
602 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200603 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700604 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100605 /* should turn off the crtc */
606 memset(&set, 0, sizeof(struct drm_mode_set));
607 set.crtc = crtc;
608 set.fb = NULL;
609 ret = drm_mode_set_config_internal(&set);
610 if (ret)
611 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
612 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000613 }
Dave Airlief453ba02008-11-07 14:05:41 -0800614
Daniel Vetter6295d602015-07-09 23:44:25 +0200615 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300616 if (plane->fb == fb)
617 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800618 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100619 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800620 }
621
Rob Clarkf7eff602012-09-05 21:48:38 +0000622 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800623}
Rob Clarkf7eff602012-09-05 21:48:38 +0000624EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800625
Rob Clark51fd3712013-11-19 12:10:12 -0500626DEFINE_WW_CLASS(crtc_ww_class);
627
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200628static unsigned int drm_num_crtcs(struct drm_device *dev)
629{
630 unsigned int num = 0;
631 struct drm_crtc *tmp;
632
633 drm_for_each_crtc(tmp, dev) {
634 num++;
635 }
636
637 return num;
638}
639
Dave Airlief453ba02008-11-07 14:05:41 -0800640/**
Matt Ropere13161a2014-04-01 15:22:38 -0700641 * drm_crtc_init_with_planes - Initialise a new CRTC object with
642 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800643 * @dev: DRM device
644 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700645 * @primary: Primary plane for CRTC
646 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800647 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200648 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800649 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000650 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200651 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100652 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200653 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800654 */
Matt Ropere13161a2014-04-01 15:22:38 -0700655int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
656 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700657 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200658 const struct drm_crtc_funcs *funcs,
659 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800660{
Rob Clark51fd3712013-11-19 12:10:12 -0500661 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200662 int ret;
663
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100664 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
665 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
666
Dave Airlief453ba02008-11-07 14:05:41 -0800667 crtc->dev = dev;
668 crtc->funcs = funcs;
669
Rob Clark51fd3712013-11-19 12:10:12 -0500670 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200671 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
672 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100673 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800674
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200675 if (name) {
676 va_list ap;
677
678 va_start(ap, name);
679 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
680 va_end(ap);
681 } else {
682 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
683 drm_num_crtcs(dev));
684 }
685 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000686 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200687 return -ENOMEM;
688 }
689
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300690 crtc->base.properties = &crtc->properties;
691
Rob Clark51fd3712013-11-19 12:10:12 -0500692 list_add_tail(&crtc->head, &config->crtc_list);
693 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200694
Matt Ropere13161a2014-04-01 15:22:38 -0700695 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700696 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700697 if (primary)
698 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700699 if (cursor)
700 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700701
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100702 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
703 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100704 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100705 }
706
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100707 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800708}
Matt Ropere13161a2014-04-01 15:22:38 -0700709EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800710
711/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000712 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800713 * @crtc: CRTC to cleanup
714 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000715 * This function cleans up @crtc and removes it from the DRM mode setting
716 * core. Note that the function does *not* free the crtc structure itself,
717 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800718 */
719void drm_crtc_cleanup(struct drm_crtc *crtc)
720{
721 struct drm_device *dev = crtc->dev;
722
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000723 kfree(crtc->gamma_store);
724 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800725
Rob Clark51fd3712013-11-19 12:10:12 -0500726 drm_modeset_lock_fini(&crtc->mutex);
727
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000728 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800729 list_del(&crtc->head);
730 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100731
732 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
733 if (crtc->state && crtc->funcs->atomic_destroy_state)
734 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100735
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200736 kfree(crtc->name);
737
Thierry Redinga18c0af2014-12-10 11:38:49 +0100738 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800739}
740EXPORT_SYMBOL(drm_crtc_cleanup);
741
742/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000743 * drm_crtc_index - find the index of a registered CRTC
744 * @crtc: CRTC to find index for
745 *
746 * Given a registered CRTC, return the index of that CRTC within a DRM
747 * device's list of CRTCs.
748 */
749unsigned int drm_crtc_index(struct drm_crtc *crtc)
750{
751 unsigned int index = 0;
752 struct drm_crtc *tmp;
753
Daniel Vettere4f62542015-07-09 23:44:35 +0200754 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000755 if (tmp == crtc)
756 return index;
757
758 index++;
759 }
760
761 BUG();
762}
763EXPORT_SYMBOL(drm_crtc_index);
764
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100765/*
Dave Airlief453ba02008-11-07 14:05:41 -0800766 * drm_mode_remove - remove and free a mode
767 * @connector: connector list to modify
768 * @mode: mode to remove
769 *
Dave Airlief453ba02008-11-07 14:05:41 -0800770 * Remove @mode from @connector's mode list, then free it.
771 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100772static void drm_mode_remove(struct drm_connector *connector,
773 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800774{
775 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100776 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800777}
Dave Airlief453ba02008-11-07 14:05:41 -0800778
779/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200780 * drm_display_info_set_bus_formats - set the supported bus formats
781 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100782 * @formats: array containing the supported bus formats
783 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200784 *
785 * Store the supported bus formats in display info structure.
786 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
787 * a full list of available formats.
788 */
789int drm_display_info_set_bus_formats(struct drm_display_info *info,
790 const u32 *formats,
791 unsigned int num_formats)
792{
793 u32 *fmts = NULL;
794
795 if (!formats && num_formats)
796 return -EINVAL;
797
798 if (formats && num_formats) {
799 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
800 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300801 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200802 return -ENOMEM;
803 }
804
805 kfree(info->bus_formats);
806 info->bus_formats = fmts;
807 info->num_bus_formats = num_formats;
808
809 return 0;
810}
811EXPORT_SYMBOL(drm_display_info_set_bus_formats);
812
813/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200814 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
815 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200816 *
817 * The kernel supports per-connector configration of its consoles through
818 * use of the video= parameter. This function parses that option and
819 * extracts the user's specified mode (or enable/disable status) for a
820 * particular connector. This is typically only used during the early fbdev
821 * setup.
822 */
823static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
824{
825 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
826 char *option = NULL;
827
828 if (fb_get_options(connector->name, &option))
829 return;
830
831 if (!drm_mode_parse_command_line_for_connector(option,
832 connector,
833 mode))
834 return;
835
836 if (mode->force) {
837 const char *s;
838
839 switch (mode->force) {
840 case DRM_FORCE_OFF:
841 s = "OFF";
842 break;
843 case DRM_FORCE_ON_DIGITAL:
844 s = "ON - dig";
845 break;
846 default:
847 case DRM_FORCE_ON:
848 s = "ON";
849 break;
850 }
851
852 DRM_INFO("forcing %s connector %s\n", connector->name, s);
853 connector->force = mode->force;
854 }
855
856 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
857 connector->name,
858 mode->xres, mode->yres,
859 mode->refresh_specified ? mode->refresh : 60,
860 mode->rb ? " reduced blanking" : "",
861 mode->margins ? " with margins" : "",
862 mode->interlace ? " interlaced" : "");
863}
864
865/**
Dave Airlief453ba02008-11-07 14:05:41 -0800866 * drm_connector_init - Init a preallocated connector
867 * @dev: DRM device
868 * @connector: the connector to init
869 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100870 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800871 *
Dave Airlief453ba02008-11-07 14:05:41 -0800872 * Initialises a preallocated connector. Connectors should be
873 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200874 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100875 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200876 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800877 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200878int drm_connector_init(struct drm_device *dev,
879 struct drm_connector *connector,
880 const struct drm_connector_funcs *funcs,
881 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800882{
Rob Clarkae16c592014-12-18 16:01:54 -0500883 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200884 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400885 struct ida *connector_ida =
886 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200887
Daniel Vetter84849902012-12-02 00:28:11 +0100888 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800889
Dave Airlied0f37cf2016-04-15 15:10:36 +1000890 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200891 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300892 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200893
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300894 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800895 connector->dev = dev;
896 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100897
898 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
899 if (connector->connector_id < 0) {
900 ret = connector->connector_id;
901 goto out_put;
902 }
903
Dave Airlief453ba02008-11-07 14:05:41 -0800904 connector->connector_type = connector_type;
905 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400906 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
907 if (connector->connector_type_id < 0) {
908 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100909 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400910 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300911 connector->name =
912 kasprintf(GFP_KERNEL, "%s-%d",
913 drm_connector_enum_list[connector_type].name,
914 connector->connector_type_id);
915 if (!connector->name) {
916 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100917 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300918 }
919
Dave Airlief453ba02008-11-07 14:05:41 -0800920 INIT_LIST_HEAD(&connector->probed_modes);
921 INIT_LIST_HEAD(&connector->modes);
922 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000923 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800924
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200925 drm_connector_get_cmdline_mode(connector);
926
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100927 /* We should add connectors at the end to avoid upsetting the connector
928 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500929 list_add_tail(&connector->head, &config->connector_list);
930 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800931
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200932 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500933 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500934 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200935 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800936
Rob Clark58495562012-10-11 20:50:56 -0500937 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500938 config->dpms_property, 0);
939
940 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
941 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
942 }
Dave Airlief453ba02008-11-07 14:05:41 -0800943
Thomas Wood30f65702014-06-18 17:52:32 +0100944 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100945out_put_type_id:
946 if (ret)
947 ida_remove(connector_ida, connector->connector_type_id);
948out_put_id:
949 if (ret)
950 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300951out_put:
952 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000953 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300954
955out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100956 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200957
958 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800959}
960EXPORT_SYMBOL(drm_connector_init);
961
962/**
963 * drm_connector_cleanup - cleans up an initialised connector
964 * @connector: connector to cleanup
965 *
Dave Airlief453ba02008-11-07 14:05:41 -0800966 * Cleans up the connector but doesn't free the object.
967 */
968void drm_connector_cleanup(struct drm_connector *connector)
969{
970 struct drm_device *dev = connector->dev;
971 struct drm_display_mode *mode, *t;
972
Dave Airlie40d9b042014-10-20 16:29:33 +1000973 if (connector->tile_group) {
974 drm_mode_put_tile_group(dev, connector->tile_group);
975 connector->tile_group = NULL;
976 }
977
Dave Airlief453ba02008-11-07 14:05:41 -0800978 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
979 drm_mode_remove(connector, mode);
980
981 list_for_each_entry_safe(mode, t, &connector->modes, head)
982 drm_mode_remove(connector, mode);
983
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400984 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
985 connector->connector_type_id);
986
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100987 ida_remove(&dev->mode_config.connector_ida,
988 connector->connector_id);
989
Boris Brezillonb5571e92014-07-22 12:09:10 +0200990 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000991 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300992 kfree(connector->name);
993 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800994 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000995 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100996
997 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
998 if (connector->state && connector->funcs->atomic_destroy_state)
999 connector->funcs->atomic_destroy_state(connector,
1000 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001001
1002 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001003}
1004EXPORT_SYMBOL(drm_connector_cleanup);
1005
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001006/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001007 * drm_connector_register - register a connector
1008 * @connector: the connector to register
1009 *
1010 * Register userspace interfaces for a connector
1011 *
1012 * Returns:
1013 * Zero on success, error code on failure.
1014 */
1015int drm_connector_register(struct drm_connector *connector)
1016{
Thomas Wood30f65702014-06-18 17:52:32 +01001017 int ret;
1018
Dave Airlie2ee39452014-07-24 11:53:45 +10001019 drm_mode_object_register(connector->dev, &connector->base);
1020
Thomas Wood30f65702014-06-18 17:52:32 +01001021 ret = drm_sysfs_connector_add(connector);
1022 if (ret)
1023 return ret;
1024
1025 ret = drm_debugfs_connector_add(connector);
1026 if (ret) {
1027 drm_sysfs_connector_remove(connector);
1028 return ret;
1029 }
1030
1031 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001032}
1033EXPORT_SYMBOL(drm_connector_register);
1034
1035/**
1036 * drm_connector_unregister - unregister a connector
1037 * @connector: the connector to unregister
1038 *
1039 * Unregister userspace interfaces for a connector
1040 */
1041void drm_connector_unregister(struct drm_connector *connector)
1042{
1043 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001044 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001045}
1046EXPORT_SYMBOL(drm_connector_unregister);
1047
Thomas Wood34ea3d32014-05-29 16:57:41 +01001048/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001049 * drm_connector_register_all - register all connectors
1050 * @dev: drm device
1051 *
1052 * This function registers all connectors in sysfs and other places so that
1053 * userspace can start to access them. Drivers can call it after calling
1054 * drm_dev_register() to complete the device registration, if they don't call
1055 * drm_connector_register() on each connector individually.
1056 *
1057 * When a device is unplugged and should be removed from userspace access,
1058 * call drm_connector_unregister_all(), which is the inverse of this
1059 * function.
1060 *
1061 * Returns:
1062 * Zero on success, error code on failure.
1063 */
1064int drm_connector_register_all(struct drm_device *dev)
1065{
1066 struct drm_connector *connector;
1067 int ret;
1068
1069 mutex_lock(&dev->mode_config.mutex);
1070
1071 drm_for_each_connector(connector, dev) {
1072 ret = drm_connector_register(connector);
1073 if (ret)
1074 goto err;
1075 }
1076
1077 mutex_unlock(&dev->mode_config.mutex);
1078
1079 return 0;
1080
1081err:
1082 mutex_unlock(&dev->mode_config.mutex);
1083 drm_connector_unregister_all(dev);
1084 return ret;
1085}
1086EXPORT_SYMBOL(drm_connector_register_all);
1087
1088/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001089 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001090 * @dev: drm device
1091 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001092 * This functions unregisters all connectors from sysfs and other places so
1093 * that userspace can no longer access them. Drivers should call this as the
1094 * first step tearing down the device instace, or when the underlying
1095 * physical device disappeared (e.g. USB unplug), right before calling
1096 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001097 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001098void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001099{
1100 struct drm_connector *connector;
1101
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001102 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001103 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001104 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001105}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001106EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001107
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001108/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001109 * drm_encoder_init - Init a preallocated encoder
1110 * @dev: drm device
1111 * @encoder: the encoder to init
1112 * @funcs: callbacks for this encoder
1113 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001114 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001115 *
1116 * Initialises a preallocated encoder. Encoder should be
1117 * subclassed as part of driver encoder objects.
1118 *
1119 * Returns:
1120 * Zero on success, error code on failure.
1121 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001122int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001123 struct drm_encoder *encoder,
1124 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001125 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001126{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001127 int ret;
1128
Daniel Vetter84849902012-12-02 00:28:11 +01001129 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001130
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001131 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1132 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001133 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001134
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001135 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001136 encoder->encoder_type = encoder_type;
1137 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001138 if (name) {
1139 va_list ap;
1140
1141 va_start(ap, name);
1142 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1143 va_end(ap);
1144 } else {
1145 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1146 drm_encoder_enum_list[encoder_type].name,
1147 encoder->base.id);
1148 }
Jani Nikulae5748942014-05-14 16:58:20 +03001149 if (!encoder->name) {
1150 ret = -ENOMEM;
1151 goto out_put;
1152 }
Dave Airlief453ba02008-11-07 14:05:41 -08001153
1154 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1155 dev->mode_config.num_encoder++;
1156
Jani Nikulae5748942014-05-14 16:58:20 +03001157out_put:
1158 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001159 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001160
1161out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001162 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001163
1164 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001165}
1166EXPORT_SYMBOL(drm_encoder_init);
1167
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001168/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001169 * drm_encoder_index - find the index of a registered encoder
1170 * @encoder: encoder to find index for
1171 *
1172 * Given a registered encoder, return the index of that encoder within a DRM
1173 * device's list of encoders.
1174 */
1175unsigned int drm_encoder_index(struct drm_encoder *encoder)
1176{
1177 unsigned int index = 0;
1178 struct drm_encoder *tmp;
1179
1180 drm_for_each_encoder(tmp, encoder->dev) {
1181 if (tmp == encoder)
1182 return index;
1183
1184 index++;
1185 }
1186
1187 BUG();
1188}
1189EXPORT_SYMBOL(drm_encoder_index);
1190
1191/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001192 * drm_encoder_cleanup - cleans up an initialised encoder
1193 * @encoder: encoder to cleanup
1194 *
1195 * Cleans up the encoder but doesn't free the object.
1196 */
Dave Airlief453ba02008-11-07 14:05:41 -08001197void drm_encoder_cleanup(struct drm_encoder *encoder)
1198{
1199 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001200
Daniel Vetter84849902012-12-02 00:28:11 +01001201 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001202 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001203 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001204 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001205 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001206 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001207
1208 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001209}
1210EXPORT_SYMBOL(drm_encoder_cleanup);
1211
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001212static unsigned int drm_num_planes(struct drm_device *dev)
1213{
1214 unsigned int num = 0;
1215 struct drm_plane *tmp;
1216
1217 drm_for_each_plane(tmp, dev) {
1218 num++;
1219 }
1220
1221 return num;
1222}
1223
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001224/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001225 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001226 * @dev: DRM device
1227 * @plane: plane object to init
1228 * @possible_crtcs: bitmask of possible CRTCs
1229 * @funcs: callbacks for the new plane
1230 * @formats: array of supported formats (%DRM_FORMAT_*)
1231 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001232 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001233 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001234 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001235 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001236 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001237 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001238 * Zero on success, error code on failure.
1239 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001240int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1241 unsigned long possible_crtcs,
1242 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001243 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001244 enum drm_plane_type type,
1245 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001246{
Rob Clark6b4959f2014-12-18 16:01:53 -05001247 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001248 int ret;
1249
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001250 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1251 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001252 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001253
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001254 drm_modeset_lock_init(&plane->mutex);
1255
Rob Clark4d939142012-05-17 02:23:27 -06001256 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001257 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001258 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001259 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1260 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001261 if (!plane->format_types) {
1262 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001263 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001264 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001265 }
1266
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001267 if (name) {
1268 va_list ap;
1269
1270 va_start(ap, name);
1271 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1272 va_end(ap);
1273 } else {
1274 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1275 drm_num_planes(dev));
1276 }
1277 if (!plane->name) {
1278 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001279 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001280 return -ENOMEM;
1281 }
1282
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001283 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001284 plane->format_count = format_count;
1285 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001286 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001287
Rob Clark6b4959f2014-12-18 16:01:53 -05001288 list_add_tail(&plane->head, &config->plane_list);
1289 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001290 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001291 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001292
Rob Clark9922ab52014-04-01 20:16:57 -04001293 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001294 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001295 plane->type);
1296
Rob Clark6b4959f2014-12-18 16:01:53 -05001297 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1298 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1299 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1300 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1301 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1302 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1303 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1304 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1305 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1306 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1307 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1308 }
1309
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001310 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001311}
Matt Roperdc415ff2014-04-01 15:22:36 -07001312EXPORT_SYMBOL(drm_universal_plane_init);
1313
1314/**
1315 * drm_plane_init - Initialize a legacy plane
1316 * @dev: DRM device
1317 * @plane: plane object to init
1318 * @possible_crtcs: bitmask of possible CRTCs
1319 * @funcs: callbacks for the new plane
1320 * @formats: array of supported formats (%DRM_FORMAT_*)
1321 * @format_count: number of elements in @formats
1322 * @is_primary: plane type (primary vs overlay)
1323 *
1324 * Legacy API to initialize a DRM plane.
1325 *
1326 * New drivers should call drm_universal_plane_init() instead.
1327 *
1328 * Returns:
1329 * Zero on success, error code on failure.
1330 */
1331int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1332 unsigned long possible_crtcs,
1333 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001334 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001335 bool is_primary)
1336{
1337 enum drm_plane_type type;
1338
1339 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1340 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001341 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001342}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001343EXPORT_SYMBOL(drm_plane_init);
1344
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001345/**
1346 * drm_plane_cleanup - Clean up the core plane usage
1347 * @plane: plane to cleanup
1348 *
1349 * This function cleans up @plane and removes it from the DRM mode setting
1350 * core. Note that the function does *not* free the plane structure itself,
1351 * this is the responsibility of the caller.
1352 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001353void drm_plane_cleanup(struct drm_plane *plane)
1354{
1355 struct drm_device *dev = plane->dev;
1356
Daniel Vetter84849902012-12-02 00:28:11 +01001357 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001358 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001359 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001360
1361 BUG_ON(list_empty(&plane->head));
1362
1363 list_del(&plane->head);
1364 dev->mode_config.num_total_plane--;
1365 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1366 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001367 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001368
1369 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1370 if (plane->state && plane->funcs->atomic_destroy_state)
1371 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001372
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001373 kfree(plane->name);
1374
Thierry Redinga18c0af2014-12-10 11:38:49 +01001375 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001376}
1377EXPORT_SYMBOL(drm_plane_cleanup);
1378
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001379/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001380 * drm_plane_index - find the index of a registered plane
1381 * @plane: plane to find index for
1382 *
1383 * Given a registered plane, return the index of that CRTC within a DRM
1384 * device's list of planes.
1385 */
1386unsigned int drm_plane_index(struct drm_plane *plane)
1387{
1388 unsigned int index = 0;
1389 struct drm_plane *tmp;
1390
Daniel Vettere4f62542015-07-09 23:44:35 +02001391 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001392 if (tmp == plane)
1393 return index;
1394
1395 index++;
1396 }
1397
1398 BUG();
1399}
1400EXPORT_SYMBOL(drm_plane_index);
1401
1402/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001403 * drm_plane_from_index - find the registered plane at an index
1404 * @dev: DRM device
1405 * @idx: index of registered plane to find for
1406 *
1407 * Given a plane index, return the registered plane from DRM device's
1408 * list of planes with matching index.
1409 */
1410struct drm_plane *
1411drm_plane_from_index(struct drm_device *dev, int idx)
1412{
1413 struct drm_plane *plane;
1414 unsigned int i = 0;
1415
Daniel Vetter6295d602015-07-09 23:44:25 +02001416 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001417 if (i == idx)
1418 return plane;
1419 i++;
1420 }
1421 return NULL;
1422}
1423EXPORT_SYMBOL(drm_plane_from_index);
1424
1425/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001426 * drm_plane_force_disable - Forcibly disable a plane
1427 * @plane: plane to disable
1428 *
1429 * Forces the plane to be disabled.
1430 *
1431 * Used when the plane's current framebuffer is destroyed,
1432 * and when restoring fbdev mode.
1433 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001434void drm_plane_force_disable(struct drm_plane *plane)
1435{
1436 int ret;
1437
Daniel Vetter3d30a592014-07-27 13:42:42 +02001438 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001439 return;
1440
Daniel Vetter3d30a592014-07-27 13:42:42 +02001441 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001442 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001443 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001444 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001445 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001446 return;
1447 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001448 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001449 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001450 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001451 plane->fb = NULL;
1452 plane->crtc = NULL;
1453}
1454EXPORT_SYMBOL(drm_plane_force_disable);
1455
Rob Clark6b4959f2014-12-18 16:01:53 -05001456static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001457{
Rob Clark356af0e2014-12-18 16:01:52 -05001458 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001459
1460 /*
1461 * Standard properties (apply to all connectors)
1462 */
Rob Clark356af0e2014-12-18 16:01:52 -05001463 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001464 DRM_MODE_PROP_IMMUTABLE,
1465 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001466 if (!prop)
1467 return -ENOMEM;
1468 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001469
Rob Clark356af0e2014-12-18 16:01:52 -05001470 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001471 "DPMS", drm_dpms_enum_list,
1472 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001473 if (!prop)
1474 return -ENOMEM;
1475 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001476
Rob Clark356af0e2014-12-18 16:01:52 -05001477 prop = drm_property_create(dev,
1478 DRM_MODE_PROP_BLOB |
1479 DRM_MODE_PROP_IMMUTABLE,
1480 "PATH", 0);
1481 if (!prop)
1482 return -ENOMEM;
1483 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001484
Rob Clark356af0e2014-12-18 16:01:52 -05001485 prop = drm_property_create(dev,
1486 DRM_MODE_PROP_BLOB |
1487 DRM_MODE_PROP_IMMUTABLE,
1488 "TILE", 0);
1489 if (!prop)
1490 return -ENOMEM;
1491 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001492
Rob Clark6b4959f2014-12-18 16:01:53 -05001493 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001494 "type", drm_plane_type_enum_list,
1495 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001496 if (!prop)
1497 return -ENOMEM;
1498 dev->mode_config.plane_type_property = prop;
1499
1500 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1501 "SRC_X", 0, UINT_MAX);
1502 if (!prop)
1503 return -ENOMEM;
1504 dev->mode_config.prop_src_x = prop;
1505
1506 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1507 "SRC_Y", 0, UINT_MAX);
1508 if (!prop)
1509 return -ENOMEM;
1510 dev->mode_config.prop_src_y = prop;
1511
1512 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1513 "SRC_W", 0, UINT_MAX);
1514 if (!prop)
1515 return -ENOMEM;
1516 dev->mode_config.prop_src_w = prop;
1517
1518 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1519 "SRC_H", 0, UINT_MAX);
1520 if (!prop)
1521 return -ENOMEM;
1522 dev->mode_config.prop_src_h = prop;
1523
1524 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1525 "CRTC_X", INT_MIN, INT_MAX);
1526 if (!prop)
1527 return -ENOMEM;
1528 dev->mode_config.prop_crtc_x = prop;
1529
1530 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1531 "CRTC_Y", INT_MIN, INT_MAX);
1532 if (!prop)
1533 return -ENOMEM;
1534 dev->mode_config.prop_crtc_y = prop;
1535
1536 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1537 "CRTC_W", 0, INT_MAX);
1538 if (!prop)
1539 return -ENOMEM;
1540 dev->mode_config.prop_crtc_w = prop;
1541
1542 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1543 "CRTC_H", 0, INT_MAX);
1544 if (!prop)
1545 return -ENOMEM;
1546 dev->mode_config.prop_crtc_h = prop;
1547
1548 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1549 "FB_ID", DRM_MODE_OBJECT_FB);
1550 if (!prop)
1551 return -ENOMEM;
1552 dev->mode_config.prop_fb_id = prop;
1553
1554 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1555 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001559
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001560 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1561 "ACTIVE");
1562 if (!prop)
1563 return -ENOMEM;
1564 dev->mode_config.prop_active = prop;
1565
Daniel Stone955f3c32015-05-25 19:11:52 +01001566 prop = drm_property_create(dev,
1567 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1568 "MODE_ID", 0);
1569 if (!prop)
1570 return -ENOMEM;
1571 dev->mode_config.prop_mode_id = prop;
1572
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001573 prop = drm_property_create(dev,
1574 DRM_MODE_PROP_BLOB,
1575 "DEGAMMA_LUT", 0);
1576 if (!prop)
1577 return -ENOMEM;
1578 dev->mode_config.degamma_lut_property = prop;
1579
1580 prop = drm_property_create_range(dev,
1581 DRM_MODE_PROP_IMMUTABLE,
1582 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1583 if (!prop)
1584 return -ENOMEM;
1585 dev->mode_config.degamma_lut_size_property = prop;
1586
1587 prop = drm_property_create(dev,
1588 DRM_MODE_PROP_BLOB,
1589 "CTM", 0);
1590 if (!prop)
1591 return -ENOMEM;
1592 dev->mode_config.ctm_property = prop;
1593
1594 prop = drm_property_create(dev,
1595 DRM_MODE_PROP_BLOB,
1596 "GAMMA_LUT", 0);
1597 if (!prop)
1598 return -ENOMEM;
1599 dev->mode_config.gamma_lut_property = prop;
1600
1601 prop = drm_property_create_range(dev,
1602 DRM_MODE_PROP_IMMUTABLE,
1603 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1604 if (!prop)
1605 return -ENOMEM;
1606 dev->mode_config.gamma_lut_size_property = prop;
1607
Rob Clark9922ab52014-04-01 20:16:57 -04001608 return 0;
1609}
1610
Dave Airlief453ba02008-11-07 14:05:41 -08001611/**
1612 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1613 * @dev: DRM device
1614 *
1615 * Called by a driver the first time a DVI-I connector is made.
1616 */
1617int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1618{
1619 struct drm_property *dvi_i_selector;
1620 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001621
1622 if (dev->mode_config.dvi_i_select_subconnector_property)
1623 return 0;
1624
1625 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001626 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001627 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001628 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001629 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001630 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1631
Sascha Hauer4a67d392012-02-06 10:58:17 +01001632 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001633 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001634 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001635 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001636 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1637
1638 return 0;
1639}
1640EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1641
1642/**
1643 * drm_create_tv_properties - create TV specific connector properties
1644 * @dev: DRM device
1645 * @num_modes: number of different TV formats (modes) supported
1646 * @modes: array of pointers to strings containing name of each format
1647 *
1648 * Called by a driver's TV initialization routine, this function creates
1649 * the TV specific connector properties for a given device. Caller is
1650 * responsible for allocating a list of format names and passing them to
1651 * this routine.
1652 */
Thierry Reding2f763312014-10-13 12:45:57 +02001653int drm_mode_create_tv_properties(struct drm_device *dev,
1654 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001655 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001656{
1657 struct drm_property *tv_selector;
1658 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001659 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001660
1661 if (dev->mode_config.tv_select_subconnector_property)
1662 return 0;
1663
1664 /*
1665 * Basic connector properties
1666 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001667 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001668 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001669 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001670 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001671 if (!tv_selector)
1672 goto nomem;
1673
Dave Airlief453ba02008-11-07 14:05:41 -08001674 dev->mode_config.tv_select_subconnector_property = tv_selector;
1675
1676 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001677 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1678 "subconnector",
1679 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001680 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001681 if (!tv_subconnector)
1682 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001683 dev->mode_config.tv_subconnector_property = tv_subconnector;
1684
1685 /*
1686 * Other, TV specific properties: margins & TV modes.
1687 */
1688 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001689 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001690 if (!dev->mode_config.tv_left_margin_property)
1691 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001692
1693 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001694 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001695 if (!dev->mode_config.tv_right_margin_property)
1696 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001697
1698 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001699 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001700 if (!dev->mode_config.tv_top_margin_property)
1701 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001702
1703 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001704 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001705 if (!dev->mode_config.tv_bottom_margin_property)
1706 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001707
1708 dev->mode_config.tv_mode_property =
1709 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1710 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001711 if (!dev->mode_config.tv_mode_property)
1712 goto nomem;
1713
Dave Airlief453ba02008-11-07 14:05:41 -08001714 for (i = 0; i < num_modes; i++)
1715 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1716 i, modes[i]);
1717
Francisco Jerezb6b79022009-08-02 04:19:20 +02001718 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001719 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001720 if (!dev->mode_config.tv_brightness_property)
1721 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001722
1723 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001724 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001725 if (!dev->mode_config.tv_contrast_property)
1726 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001727
1728 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001729 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001730 if (!dev->mode_config.tv_flicker_reduction_property)
1731 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001732
Francisco Jereza75f0232009-08-12 02:30:10 +02001733 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001734 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001735 if (!dev->mode_config.tv_overscan_property)
1736 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001737
1738 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001739 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001740 if (!dev->mode_config.tv_saturation_property)
1741 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001742
1743 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001744 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001745 if (!dev->mode_config.tv_hue_property)
1746 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001747
Dave Airlief453ba02008-11-07 14:05:41 -08001748 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001749nomem:
1750 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001751}
1752EXPORT_SYMBOL(drm_mode_create_tv_properties);
1753
1754/**
1755 * drm_mode_create_scaling_mode_property - create scaling mode property
1756 * @dev: DRM device
1757 *
1758 * Called by a driver the first time it's needed, must be attached to desired
1759 * connectors.
1760 */
1761int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1762{
1763 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001764
1765 if (dev->mode_config.scaling_mode_property)
1766 return 0;
1767
1768 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001769 drm_property_create_enum(dev, 0, "scaling mode",
1770 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001771 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001772
1773 dev->mode_config.scaling_mode_property = scaling_mode;
1774
1775 return 0;
1776}
1777EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1778
1779/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301780 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1781 * @dev: DRM device
1782 *
1783 * Called by a driver the first time it's needed, must be attached to desired
1784 * connectors.
1785 *
1786 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001787 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301788 */
1789int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1790{
1791 if (dev->mode_config.aspect_ratio_property)
1792 return 0;
1793
1794 dev->mode_config.aspect_ratio_property =
1795 drm_property_create_enum(dev, 0, "aspect ratio",
1796 drm_aspect_ratio_enum_list,
1797 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1798
1799 if (dev->mode_config.aspect_ratio_property == NULL)
1800 return -ENOMEM;
1801
1802 return 0;
1803}
1804EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1805
1806/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001807 * drm_mode_create_dirty_property - create dirty property
1808 * @dev: DRM device
1809 *
1810 * Called by a driver the first time it's needed, must be attached to desired
1811 * connectors.
1812 */
1813int drm_mode_create_dirty_info_property(struct drm_device *dev)
1814{
1815 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001816
1817 if (dev->mode_config.dirty_info_property)
1818 return 0;
1819
1820 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001821 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001822 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001823 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001824 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001825 dev->mode_config.dirty_info_property = dirty_info;
1826
1827 return 0;
1828}
1829EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1830
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001831/**
1832 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1833 * @dev: DRM device
1834 *
1835 * Create the the suggested x/y offset property for connectors.
1836 */
1837int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1838{
1839 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1840 return 0;
1841
1842 dev->mode_config.suggested_x_property =
1843 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1844
1845 dev->mode_config.suggested_y_property =
1846 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1847
1848 if (dev->mode_config.suggested_x_property == NULL ||
1849 dev->mode_config.suggested_y_property == NULL)
1850 return -ENOMEM;
1851 return 0;
1852}
1853EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1854
Dave Airlief453ba02008-11-07 14:05:41 -08001855/**
Dave Airlief453ba02008-11-07 14:05:41 -08001856 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001857 * @dev: drm device for the ioctl
1858 * @data: data pointer for the ioctl
1859 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001860 *
Dave Airlief453ba02008-11-07 14:05:41 -08001861 * Construct a set of configuration description structures and return
1862 * them to the user, including CRTC, connector and framebuffer configuration.
1863 *
1864 * Called by the user via ioctl.
1865 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001866 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001867 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001868 */
1869int drm_mode_getresources(struct drm_device *dev, void *data,
1870 struct drm_file *file_priv)
1871{
1872 struct drm_mode_card_res *card_res = data;
1873 struct list_head *lh;
1874 struct drm_framebuffer *fb;
1875 struct drm_connector *connector;
1876 struct drm_crtc *crtc;
1877 struct drm_encoder *encoder;
1878 int ret = 0;
1879 int connector_count = 0;
1880 int crtc_count = 0;
1881 int fb_count = 0;
1882 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001883 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001884 uint32_t __user *fb_id;
1885 uint32_t __user *crtc_id;
1886 uint32_t __user *connector_id;
1887 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001888
Dave Airliefb3b06c2011-02-08 13:55:21 +10001889 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1890 return -EINVAL;
1891
Dave Airlief453ba02008-11-07 14:05:41 -08001892
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001893 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001894 /*
1895 * For the non-control nodes we need to limit the list of resources
1896 * by IDs in the group list for this node
1897 */
1898 list_for_each(lh, &file_priv->fbs)
1899 fb_count++;
1900
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001901 /* handle this in 4 parts */
1902 /* FBs */
1903 if (card_res->count_fbs >= fb_count) {
1904 copied = 0;
1905 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1906 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1907 if (put_user(fb->base.id, fb_id + copied)) {
1908 mutex_unlock(&file_priv->fbs_lock);
1909 return -EFAULT;
1910 }
1911 copied++;
1912 }
1913 }
1914 card_res->count_fbs = fb_count;
1915 mutex_unlock(&file_priv->fbs_lock);
1916
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001917 /* mode_config.mutex protects the connector list against e.g. DP MST
1918 * connector hot-adding. CRTC/Plane lists are invariant. */
1919 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001920 drm_for_each_crtc(crtc, dev)
1921 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001922
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001923 drm_for_each_connector(connector, dev)
1924 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001925
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001926 drm_for_each_encoder(encoder, dev)
1927 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001928
1929 card_res->max_height = dev->mode_config.max_height;
1930 card_res->min_height = dev->mode_config.min_height;
1931 card_res->max_width = dev->mode_config.max_width;
1932 card_res->min_width = dev->mode_config.min_width;
1933
Dave Airlief453ba02008-11-07 14:05:41 -08001934 /* CRTCs */
1935 if (card_res->count_crtcs >= crtc_count) {
1936 copied = 0;
1937 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001938 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001939 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1940 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001941 if (put_user(crtc->base.id, crtc_id + copied)) {
1942 ret = -EFAULT;
1943 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001944 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001945 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001946 }
1947 }
1948 card_res->count_crtcs = crtc_count;
1949
1950 /* Encoders */
1951 if (card_res->count_encoders >= encoder_count) {
1952 copied = 0;
1953 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001954 drm_for_each_encoder(encoder, dev) {
1955 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1956 encoder->name);
1957 if (put_user(encoder->base.id, encoder_id +
1958 copied)) {
1959 ret = -EFAULT;
1960 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001961 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001962 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001963 }
1964 }
1965 card_res->count_encoders = encoder_count;
1966
1967 /* Connectors */
1968 if (card_res->count_connectors >= connector_count) {
1969 copied = 0;
1970 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001971 drm_for_each_connector(connector, dev) {
1972 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1973 connector->base.id,
1974 connector->name);
1975 if (put_user(connector->base.id,
1976 connector_id + copied)) {
1977 ret = -EFAULT;
1978 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001979 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001980 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001981 }
1982 }
1983 card_res->count_connectors = connector_count;
1984
Jerome Glisse94401062010-07-15 15:43:25 -04001985 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001986 card_res->count_connectors, card_res->count_encoders);
1987
1988out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001989 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001990 return ret;
1991}
1992
1993/**
1994 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001995 * @dev: drm device for the ioctl
1996 * @data: data pointer for the ioctl
1997 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001998 *
Dave Airlief453ba02008-11-07 14:05:41 -08001999 * Construct a CRTC configuration structure to return to the user.
2000 *
2001 * Called by the user via ioctl.
2002 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002003 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002004 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002005 */
2006int drm_mode_getcrtc(struct drm_device *dev,
2007 void *data, struct drm_file *file_priv)
2008{
2009 struct drm_mode_crtc *crtc_resp = data;
2010 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002011
Dave Airliefb3b06c2011-02-08 13:55:21 +10002012 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2013 return -EINVAL;
2014
Rob Clarka2b34e22013-10-05 16:36:52 -04002015 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002016 if (!crtc)
2017 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002018
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002019 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002020 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002021 if (crtc->primary->fb)
2022 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002023 else
2024 crtc_resp->fb_id = 0;
2025
Daniel Vetter31c946e2015-02-22 12:24:17 +01002026 if (crtc->state) {
2027 crtc_resp->x = crtc->primary->state->src_x >> 16;
2028 crtc_resp->y = crtc->primary->state->src_y >> 16;
2029 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002030 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002031 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002032
Daniel Vetter31c946e2015-02-22 12:24:17 +01002033 } else {
2034 crtc_resp->mode_valid = 0;
2035 }
Dave Airlief453ba02008-11-07 14:05:41 -08002036 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002037 crtc_resp->x = crtc->x;
2038 crtc_resp->y = crtc->y;
2039 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002040 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002041 crtc_resp->mode_valid = 1;
2042
2043 } else {
2044 crtc_resp->mode_valid = 0;
2045 }
Dave Airlief453ba02008-11-07 14:05:41 -08002046 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002047 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002048
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002049 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002050}
2051
Damien Lespiau61d8e322013-09-25 16:45:22 +01002052static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2053 const struct drm_file *file_priv)
2054{
2055 /*
2056 * If user-space hasn't configured the driver to expose the stereo 3D
2057 * modes, don't expose them.
2058 */
2059 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2060 return false;
2061
2062 return true;
2063}
2064
Daniel Vetterabd69c52014-11-25 23:50:05 +01002065static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2066{
2067 /* For atomic drivers only state objects are synchronously updated and
2068 * protected by modeset locks, so check those first. */
2069 if (connector->state)
2070 return connector->state->best_encoder;
2071 return connector->encoder;
2072}
2073
Rob Clark95cbf112014-12-18 16:01:49 -05002074/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002075static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002076 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2077 uint32_t *arg_count_props)
2078{
Rob Clark88a48e22014-12-18 16:01:50 -05002079 int props_count;
2080 int i, ret, copied;
2081
2082 props_count = obj->properties->count;
2083 if (!atomic)
2084 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002085
2086 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002087 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002088 struct drm_property *prop = obj->properties->properties[i];
2089 uint64_t val;
2090
Rob Clark88a48e22014-12-18 16:01:50 -05002091 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2092 continue;
2093
Rob Clark95cbf112014-12-18 16:01:49 -05002094 ret = drm_object_property_get_value(obj, prop, &val);
2095 if (ret)
2096 return ret;
2097
2098 if (put_user(prop->base.id, prop_ptr + copied))
2099 return -EFAULT;
2100
2101 if (put_user(val, prop_values + copied))
2102 return -EFAULT;
2103
2104 copied++;
2105 }
2106 }
2107 *arg_count_props = props_count;
2108
2109 return 0;
2110}
2111
Dave Airlief453ba02008-11-07 14:05:41 -08002112/**
2113 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002114 * @dev: drm device for the ioctl
2115 * @data: data pointer for the ioctl
2116 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002117 *
Dave Airlief453ba02008-11-07 14:05:41 -08002118 * Construct a connector configuration structure to return to the user.
2119 *
2120 * Called by the user via ioctl.
2121 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002122 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002123 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002124 */
2125int drm_mode_getconnector(struct drm_device *dev, void *data,
2126 struct drm_file *file_priv)
2127{
2128 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002129 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002130 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002131 struct drm_display_mode *mode;
2132 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002133 int encoders_count = 0;
2134 int ret = 0;
2135 int copied = 0;
2136 int i;
2137 struct drm_mode_modeinfo u_mode;
2138 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002139 uint32_t __user *encoder_ptr;
2140
Dave Airliefb3b06c2011-02-08 13:55:21 +10002141 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2142 return -EINVAL;
2143
Dave Airlief453ba02008-11-07 14:05:41 -08002144 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2145
Jerome Glisse94401062010-07-15 15:43:25 -04002146 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002147
Daniel Vetter7b240562012-12-12 00:35:33 +01002148 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002149
Rob Clarka2b34e22013-10-05 16:36:52 -04002150 connector = drm_connector_find(dev, out_resp->connector_id);
2151 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002152 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002153 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002154 }
Dave Airlief453ba02008-11-07 14:05:41 -08002155
Thierry Reding01073b02014-12-10 13:03:38 +01002156 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2157 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002158 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002159
2160 if (out_resp->count_modes == 0) {
2161 connector->funcs->fill_modes(connector,
2162 dev->mode_config.max_width,
2163 dev->mode_config.max_height);
2164 }
2165
2166 /* delayed so we get modes regardless of pre-fill_modes state */
2167 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002168 if (drm_mode_expose_to_userspace(mode, file_priv))
2169 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002170
2171 out_resp->connector_id = connector->base.id;
2172 out_resp->connector_type = connector->connector_type;
2173 out_resp->connector_type_id = connector->connector_type_id;
2174 out_resp->mm_width = connector->display_info.width_mm;
2175 out_resp->mm_height = connector->display_info.height_mm;
2176 out_resp->subpixel = connector->display_info.subpixel_order;
2177 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002178
2179 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002180 encoder = drm_connector_get_encoder(connector);
2181 if (encoder)
2182 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002183 else
2184 out_resp->encoder_id = 0;
2185
2186 /*
2187 * This ioctl is called twice, once to determine how much space is
2188 * needed, and the 2nd time to fill it.
2189 */
2190 if ((out_resp->count_modes >= mode_count) && mode_count) {
2191 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002192 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002193 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002194 if (!drm_mode_expose_to_userspace(mode, file_priv))
2195 continue;
2196
Daniel Stone934a8a82015-05-22 13:34:48 +01002197 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002198 if (copy_to_user(mode_ptr + copied,
2199 &u_mode, sizeof(u_mode))) {
2200 ret = -EFAULT;
2201 goto out;
2202 }
2203 copied++;
2204 }
2205 }
2206 out_resp->count_modes = mode_count;
2207
Rob Clark88a48e22014-12-18 16:01:50 -05002208 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002209 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2210 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2211 &out_resp->count_props);
2212 if (ret)
2213 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002214
2215 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2216 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002217 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002218 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2219 if (connector->encoder_ids[i] != 0) {
2220 if (put_user(connector->encoder_ids[i],
2221 encoder_ptr + copied)) {
2222 ret = -EFAULT;
2223 goto out;
2224 }
2225 copied++;
2226 }
2227 }
2228 }
2229 out_resp->count_encoders = encoders_count;
2230
2231out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002232 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002233
2234out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002235 mutex_unlock(&dev->mode_config.mutex);
2236
Dave Airlief453ba02008-11-07 14:05:41 -08002237 return ret;
2238}
2239
Daniel Vetterabd69c52014-11-25 23:50:05 +01002240static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2241{
2242 struct drm_connector *connector;
2243 struct drm_device *dev = encoder->dev;
2244 bool uses_atomic = false;
2245
2246 /* For atomic drivers only state objects are synchronously updated and
2247 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002248 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002249 if (!connector->state)
2250 continue;
2251
2252 uses_atomic = true;
2253
2254 if (connector->state->best_encoder != encoder)
2255 continue;
2256
2257 return connector->state->crtc;
2258 }
2259
2260 /* Don't return stale data (e.g. pending async disable). */
2261 if (uses_atomic)
2262 return NULL;
2263
2264 return encoder->crtc;
2265}
2266
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002267/**
2268 * drm_mode_getencoder - get encoder configuration
2269 * @dev: drm device for the ioctl
2270 * @data: data pointer for the ioctl
2271 * @file_priv: drm file for the ioctl call
2272 *
2273 * Construct a encoder configuration structure to return to the user.
2274 *
2275 * Called by the user via ioctl.
2276 *
2277 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002278 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002279 */
Dave Airlief453ba02008-11-07 14:05:41 -08002280int drm_mode_getencoder(struct drm_device *dev, void *data,
2281 struct drm_file *file_priv)
2282{
2283 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002284 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002285 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002286
Dave Airliefb3b06c2011-02-08 13:55:21 +10002287 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2288 return -EINVAL;
2289
Rob Clarka2b34e22013-10-05 16:36:52 -04002290 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002291 if (!encoder)
2292 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002293
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002294 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002295 crtc = drm_encoder_get_crtc(encoder);
2296 if (crtc)
2297 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002298 else
2299 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002300 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2301
Dave Airlief453ba02008-11-07 14:05:41 -08002302 enc_resp->encoder_type = encoder->encoder_type;
2303 enc_resp->encoder_id = encoder->base.id;
2304 enc_resp->possible_crtcs = encoder->possible_crtcs;
2305 enc_resp->possible_clones = encoder->possible_clones;
2306
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002307 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002308}
2309
2310/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002311 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002312 * @dev: DRM device
2313 * @data: ioctl data
2314 * @file_priv: DRM file info
2315 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002316 * Construct a list of plane ids to return to the user.
2317 *
2318 * Called by the user via ioctl.
2319 *
2320 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002321 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002322 */
2323int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002324 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325{
2326 struct drm_mode_get_plane_res *plane_resp = data;
2327 struct drm_mode_config *config;
2328 struct drm_plane *plane;
2329 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002330 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002331 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002332
2333 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2334 return -EINVAL;
2335
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002336 config = &dev->mode_config;
2337
Matt Roper681e7ec2014-04-01 15:22:42 -07002338 if (file_priv->universal_planes)
2339 num_planes = config->num_total_plane;
2340 else
2341 num_planes = config->num_overlay_plane;
2342
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002343 /*
2344 * This ioctl is called twice, once to determine how much space is
2345 * needed, and the 2nd time to fill it.
2346 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002347 if (num_planes &&
2348 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002349 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002350
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002351 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002352 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002353 /*
2354 * Unless userspace set the 'universal planes'
2355 * capability bit, only advertise overlays.
2356 */
2357 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2358 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002359 continue;
2360
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002361 if (put_user(plane->base.id, plane_ptr + copied))
2362 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002363 copied++;
2364 }
2365 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002366 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002367
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002368 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002369}
2370
2371/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002372 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002373 * @dev: DRM device
2374 * @data: ioctl data
2375 * @file_priv: DRM file info
2376 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002377 * Construct a plane configuration structure to return to the user.
2378 *
2379 * Called by the user via ioctl.
2380 *
2381 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002382 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002383 */
2384int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002385 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002386{
2387 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002388 struct drm_plane *plane;
2389 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002390
2391 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2392 return -EINVAL;
2393
Rob Clarka2b34e22013-10-05 16:36:52 -04002394 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002395 if (!plane)
2396 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002397
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002398 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002399 if (plane->crtc)
2400 plane_resp->crtc_id = plane->crtc->base.id;
2401 else
2402 plane_resp->crtc_id = 0;
2403
2404 if (plane->fb)
2405 plane_resp->fb_id = plane->fb->base.id;
2406 else
2407 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002408 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002409
2410 plane_resp->plane_id = plane->base.id;
2411 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002412 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002413
2414 /*
2415 * This ioctl is called twice, once to determine how much space is
2416 * needed, and the 2nd time to fill it.
2417 */
2418 if (plane->format_count &&
2419 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002420 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002421 if (copy_to_user(format_ptr,
2422 plane->format_types,
2423 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002424 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002425 }
2426 }
2427 plane_resp->count_format_types = plane->format_count;
2428
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002429 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002430}
2431
Laurent Pinchartead86102015-03-05 02:25:43 +02002432/**
2433 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2434 * @plane: plane to check for format support
2435 * @format: the pixel format
2436 *
2437 * Returns:
2438 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2439 * otherwise.
2440 */
2441int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2442{
2443 unsigned int i;
2444
2445 for (i = 0; i < plane->format_count; i++) {
2446 if (format == plane->format_types[i])
2447 return 0;
2448 }
2449
2450 return -EINVAL;
2451}
2452
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002453static int check_src_coords(uint32_t src_x, uint32_t src_y,
2454 uint32_t src_w, uint32_t src_h,
2455 const struct drm_framebuffer *fb)
2456{
2457 unsigned int fb_width, fb_height;
2458
2459 fb_width = fb->width << 16;
2460 fb_height = fb->height << 16;
2461
2462 /* Make sure source coordinates are inside the fb. */
2463 if (src_w > fb_width ||
2464 src_x > fb_width - src_w ||
2465 src_h > fb_height ||
2466 src_y > fb_height - src_h) {
2467 DRM_DEBUG_KMS("Invalid source coordinates "
2468 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2469 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2470 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2471 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2472 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2473 return -ENOSPC;
2474 }
2475
2476 return 0;
2477}
2478
Matt Roperb36552b2014-06-10 08:28:09 -07002479/*
2480 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002481 *
Matt Roperb36552b2014-06-10 08:28:09 -07002482 * Note that we assume an extra reference has already been taken on fb. If the
2483 * update fails, this reference will be dropped before return; if it succeeds,
2484 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002485 *
Matt Roperb36552b2014-06-10 08:28:09 -07002486 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002487 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002488static int __setplane_internal(struct drm_plane *plane,
2489 struct drm_crtc *crtc,
2490 struct drm_framebuffer *fb,
2491 int32_t crtc_x, int32_t crtc_y,
2492 uint32_t crtc_w, uint32_t crtc_h,
2493 /* src_{x,y,w,h} values are 16.16 fixed point */
2494 uint32_t src_x, uint32_t src_y,
2495 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002496{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002497 int ret = 0;
2498
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002499 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002500 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002501 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002502 ret = plane->funcs->disable_plane(plane);
2503 if (!ret) {
2504 plane->crtc = NULL;
2505 plane->fb = NULL;
2506 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002507 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002508 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002509 goto out;
2510 }
2511
Matt Roper7f994f32014-05-29 08:06:51 -07002512 /* Check whether this plane is usable on this CRTC */
2513 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2514 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2515 ret = -EINVAL;
2516 goto out;
2517 }
2518
Ville Syrjälä62443be2011-12-20 00:06:47 +02002519 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002520 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2521 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002522 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2523 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002524 goto out;
2525 }
2526
Matt Roper3968be92015-04-13 11:06:13 -07002527 /* Give drivers some help against integer overflows */
2528 if (crtc_w > INT_MAX ||
2529 crtc_x > INT_MAX - (int32_t) crtc_w ||
2530 crtc_h > INT_MAX ||
2531 crtc_y > INT_MAX - (int32_t) crtc_h) {
2532 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2533 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002534 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002535 goto out;
2536 }
2537
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002538 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2539 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002540 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002541
Daniel Vetter3d30a592014-07-27 13:42:42 +02002542 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002543 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002544 crtc_x, crtc_y, crtc_w, crtc_h,
2545 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002546 if (!ret) {
2547 plane->crtc = crtc;
2548 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002549 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002550 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002551 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002552 }
2553
2554out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002555 if (fb)
2556 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002557 if (plane->old_fb)
2558 drm_framebuffer_unreference(plane->old_fb);
2559 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002560
2561 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002562}
Matt Roperb36552b2014-06-10 08:28:09 -07002563
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002564static int setplane_internal(struct drm_plane *plane,
2565 struct drm_crtc *crtc,
2566 struct drm_framebuffer *fb,
2567 int32_t crtc_x, int32_t crtc_y,
2568 uint32_t crtc_w, uint32_t crtc_h,
2569 /* src_{x,y,w,h} values are 16.16 fixed point */
2570 uint32_t src_x, uint32_t src_y,
2571 uint32_t src_w, uint32_t src_h)
2572{
2573 int ret;
2574
2575 drm_modeset_lock_all(plane->dev);
2576 ret = __setplane_internal(plane, crtc, fb,
2577 crtc_x, crtc_y, crtc_w, crtc_h,
2578 src_x, src_y, src_w, src_h);
2579 drm_modeset_unlock_all(plane->dev);
2580
2581 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002582}
2583
2584/**
2585 * drm_mode_setplane - configure a plane's configuration
2586 * @dev: DRM device
2587 * @data: ioctl data*
2588 * @file_priv: DRM file info
2589 *
2590 * Set plane configuration, including placement, fb, scaling, and other factors.
2591 * Or pass a NULL fb to disable (planes may be disabled without providing a
2592 * valid crtc).
2593 *
2594 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002595 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002596 */
2597int drm_mode_setplane(struct drm_device *dev, void *data,
2598 struct drm_file *file_priv)
2599{
2600 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002601 struct drm_plane *plane;
2602 struct drm_crtc *crtc = NULL;
2603 struct drm_framebuffer *fb = NULL;
2604
2605 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2606 return -EINVAL;
2607
Matt Roperb36552b2014-06-10 08:28:09 -07002608 /*
2609 * First, find the plane, crtc, and fb objects. If not available,
2610 * we don't bother to call the driver.
2611 */
Rob Clark933f6222014-11-25 20:33:11 -05002612 plane = drm_plane_find(dev, plane_req->plane_id);
2613 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002614 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2615 plane_req->plane_id);
2616 return -ENOENT;
2617 }
Matt Roperb36552b2014-06-10 08:28:09 -07002618
2619 if (plane_req->fb_id) {
2620 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2621 if (!fb) {
2622 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2623 plane_req->fb_id);
2624 return -ENOENT;
2625 }
2626
Rob Clark933f6222014-11-25 20:33:11 -05002627 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2628 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002629 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2630 plane_req->crtc_id);
2631 return -ENOENT;
2632 }
Matt Roperb36552b2014-06-10 08:28:09 -07002633 }
2634
Matt Roper161d0dc2014-06-10 08:28:10 -07002635 /*
2636 * setplane_internal will take care of deref'ing either the old or new
2637 * framebuffer depending on success.
2638 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002639 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002640 plane_req->crtc_x, plane_req->crtc_y,
2641 plane_req->crtc_w, plane_req->crtc_h,
2642 plane_req->src_x, plane_req->src_y,
2643 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002644}
2645
2646/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002647 * drm_mode_set_config_internal - helper to call ->set_config
2648 * @set: modeset config to set
2649 *
2650 * This is a little helper to wrap internal calls to the ->set_config driver
2651 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002652 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002653 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002654 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002655 */
2656int drm_mode_set_config_internal(struct drm_mode_set *set)
2657{
2658 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002659 struct drm_framebuffer *fb;
2660 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002661 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002662
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002663 /*
2664 * NOTE: ->set_config can also disable other crtcs (if we steal all
2665 * connectors from it), hence we need to refcount the fbs across all
2666 * crtcs. Atomic modeset will have saner semantics ...
2667 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002668 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002669 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002670
Daniel Vetterb0d12322012-12-11 01:07:12 +01002671 fb = set->fb;
2672
2673 ret = crtc->funcs->set_config(set);
2674 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002675 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002676 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002677 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002678
Daniel Vettere4f62542015-07-09 23:44:35 +02002679 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002680 if (tmp->primary->fb)
2681 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002682 if (tmp->primary->old_fb)
2683 drm_framebuffer_unreference(tmp->primary->old_fb);
2684 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002685 }
2686
2687 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002688}
2689EXPORT_SYMBOL(drm_mode_set_config_internal);
2690
Matt Roperaf936292014-04-01 15:22:34 -07002691/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002692 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2693 * @mode: mode to query
2694 * @hdisplay: hdisplay value to fill in
2695 * @vdisplay: vdisplay value to fill in
2696 *
2697 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2698 * the appropriate layout.
2699 */
2700void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2701 int *hdisplay, int *vdisplay)
2702{
2703 struct drm_display_mode adjusted;
2704
2705 drm_mode_copy(&adjusted, mode);
2706 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2707 *hdisplay = adjusted.crtc_hdisplay;
2708 *vdisplay = adjusted.crtc_vdisplay;
2709}
2710EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2711
2712/**
Matt Roperaf936292014-04-01 15:22:34 -07002713 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2714 * CRTC viewport
2715 * @crtc: CRTC that framebuffer will be displayed on
2716 * @x: x panning
2717 * @y: y panning
2718 * @mode: mode that framebuffer will be displayed under
2719 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002720 */
Matt Roperaf936292014-04-01 15:22:34 -07002721int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2722 int x, int y,
2723 const struct drm_display_mode *mode,
2724 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002725
2726{
2727 int hdisplay, vdisplay;
2728
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002729 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002730
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002731 if (crtc->state &&
2732 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2733 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002734 swap(hdisplay, vdisplay);
2735
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002736 return check_src_coords(x << 16, y << 16,
2737 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002738}
Matt Roperaf936292014-04-01 15:22:34 -07002739EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002740
Daniel Vetter2d13b672012-12-11 13:47:23 +01002741/**
Dave Airlief453ba02008-11-07 14:05:41 -08002742 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002743 * @dev: drm device for the ioctl
2744 * @data: data pointer for the ioctl
2745 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002746 *
Dave Airlief453ba02008-11-07 14:05:41 -08002747 * Build a new CRTC configuration based on user request.
2748 *
2749 * Called by the user via ioctl.
2750 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002751 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002752 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002753 */
2754int drm_mode_setcrtc(struct drm_device *dev, void *data,
2755 struct drm_file *file_priv)
2756{
2757 struct drm_mode_config *config = &dev->mode_config;
2758 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002759 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002760 struct drm_connector **connector_set = NULL, *connector;
2761 struct drm_framebuffer *fb = NULL;
2762 struct drm_display_mode *mode = NULL;
2763 struct drm_mode_set set;
2764 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002765 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002766 int i;
2767
Dave Airliefb3b06c2011-02-08 13:55:21 +10002768 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2769 return -EINVAL;
2770
Zhao Junwang01447e92015-07-07 17:08:35 +08002771 /*
2772 * Universal plane src offsets are only 16.16, prevent havoc for
2773 * drivers using universal plane code internally.
2774 */
2775 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002776 return -ERANGE;
2777
Daniel Vetter84849902012-12-02 00:28:11 +01002778 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002779 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2780 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002781 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002782 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002783 goto out;
2784 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002785 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002786
2787 if (crtc_req->mode_valid) {
2788 /* If we have a mode we need a framebuffer. */
2789 /* If we pass -1, set the mode with the currently bound fb */
2790 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002791 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002792 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2793 ret = -EINVAL;
2794 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002795 }
Matt Roperf4510a22014-04-01 15:22:40 -07002796 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002797 /* Make refcounting symmetric with the lookup path. */
2798 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002799 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002800 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2801 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002802 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2803 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002804 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002805 goto out;
2806 }
Dave Airlief453ba02008-11-07 14:05:41 -08002807 }
2808
2809 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002810 if (!mode) {
2811 ret = -ENOMEM;
2812 goto out;
2813 }
2814
Daniel Stone934a8a82015-05-22 13:34:48 +01002815 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002816 if (ret) {
2817 DRM_DEBUG_KMS("Invalid mode\n");
2818 goto out;
2819 }
2820
Dave Airlief453ba02008-11-07 14:05:41 -08002821 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002822
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002823 /*
2824 * Check whether the primary plane supports the fb pixel format.
2825 * Drivers not implementing the universal planes API use a
2826 * default formats list provided by the DRM core which doesn't
2827 * match real hardware capabilities. Skip the check in that
2828 * case.
2829 */
2830 if (!crtc->primary->format_default) {
2831 ret = drm_plane_check_pixel_format(crtc->primary,
2832 fb->pixel_format);
2833 if (ret) {
2834 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2835 drm_get_format_name(fb->pixel_format));
2836 goto out;
2837 }
2838 }
2839
Damien Lespiauc11e9282013-09-25 16:45:30 +01002840 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2841 mode, fb);
2842 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002843 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002844
Dave Airlief453ba02008-11-07 14:05:41 -08002845 }
2846
2847 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002848 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002849 ret = -EINVAL;
2850 goto out;
2851 }
2852
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002853 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002854 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002855 crtc_req->count_connectors);
2856 ret = -EINVAL;
2857 goto out;
2858 }
2859
2860 if (crtc_req->count_connectors > 0) {
2861 u32 out_id;
2862
2863 /* Avoid unbounded kernel memory allocation */
2864 if (crtc_req->count_connectors > config->num_connector) {
2865 ret = -EINVAL;
2866 goto out;
2867 }
2868
Thierry Reding2f6c5382014-12-10 13:03:36 +01002869 connector_set = kmalloc_array(crtc_req->count_connectors,
2870 sizeof(struct drm_connector *),
2871 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002872 if (!connector_set) {
2873 ret = -ENOMEM;
2874 goto out;
2875 }
2876
2877 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002878 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002879 if (get_user(out_id, &set_connectors_ptr[i])) {
2880 ret = -EFAULT;
2881 goto out;
2882 }
2883
Rob Clarka2b34e22013-10-05 16:36:52 -04002884 connector = drm_connector_find(dev, out_id);
2885 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002886 DRM_DEBUG_KMS("Connector id %d unknown\n",
2887 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002888 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002889 goto out;
2890 }
Jerome Glisse94401062010-07-15 15:43:25 -04002891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2892 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002893 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002894
2895 connector_set[i] = connector;
2896 }
2897 }
2898
2899 set.crtc = crtc;
2900 set.x = crtc_req->x;
2901 set.y = crtc_req->y;
2902 set.mode = mode;
2903 set.connectors = connector_set;
2904 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002905 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002906 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002907
2908out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002909 if (fb)
2910 drm_framebuffer_unreference(fb);
2911
Dave Airlief453ba02008-11-07 14:05:41 -08002912 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002913 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002914 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002915 return ret;
2916}
2917
Matt Roper161d0dc2014-06-10 08:28:10 -07002918/**
2919 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2920 * universal plane handler call
2921 * @crtc: crtc to update cursor for
2922 * @req: data pointer for the ioctl
2923 * @file_priv: drm file for the ioctl call
2924 *
2925 * Legacy cursor ioctl's work directly with driver buffer handles. To
2926 * translate legacy ioctl calls into universal plane handler calls, we need to
2927 * wrap the native buffer handle in a drm_framebuffer.
2928 *
2929 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2930 * buffer with a pitch of 4*width; the universal plane interface should be used
2931 * directly in cases where the hardware can support other buffer settings and
2932 * userspace wants to make use of these capabilities.
2933 *
2934 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002935 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002936 */
2937static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2938 struct drm_mode_cursor2 *req,
2939 struct drm_file *file_priv)
2940{
2941 struct drm_device *dev = crtc->dev;
2942 struct drm_framebuffer *fb = NULL;
2943 struct drm_mode_fb_cmd2 fbreq = {
2944 .width = req->width,
2945 .height = req->height,
2946 .pixel_format = DRM_FORMAT_ARGB8888,
2947 .pitches = { req->width * 4 },
2948 .handles = { req->handle },
2949 };
2950 int32_t crtc_x, crtc_y;
2951 uint32_t crtc_w = 0, crtc_h = 0;
2952 uint32_t src_w = 0, src_h = 0;
2953 int ret = 0;
2954
2955 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002956 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002957
2958 /*
2959 * Obtain fb we'll be using (either new or existing) and take an extra
2960 * reference to it if fb != null. setplane will take care of dropping
2961 * the reference if the plane update fails.
2962 */
2963 if (req->flags & DRM_MODE_CURSOR_BO) {
2964 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002965 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002966 if (IS_ERR(fb)) {
2967 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2968 return PTR_ERR(fb);
2969 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002970 } else {
2971 fb = NULL;
2972 }
2973 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002974 fb = crtc->cursor->fb;
2975 if (fb)
2976 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002977 }
2978
2979 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2980 crtc_x = req->x;
2981 crtc_y = req->y;
2982 } else {
2983 crtc_x = crtc->cursor_x;
2984 crtc_y = crtc->cursor_y;
2985 }
2986
2987 if (fb) {
2988 crtc_w = fb->width;
2989 crtc_h = fb->height;
2990 src_w = fb->width << 16;
2991 src_h = fb->height << 16;
2992 }
2993
2994 /*
2995 * setplane_internal will take care of deref'ing either the old or new
2996 * framebuffer depending on success.
2997 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002998 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002999 crtc_x, crtc_y, crtc_w, crtc_h,
3000 0, 0, src_w, src_h);
3001
3002 /* Update successful; save new cursor position, if necessary */
3003 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3004 crtc->cursor_x = req->x;
3005 crtc->cursor_y = req->y;
3006 }
3007
3008 return ret;
3009}
3010
Dave Airlie4c813d42013-06-20 11:48:52 +10003011static int drm_mode_cursor_common(struct drm_device *dev,
3012 struct drm_mode_cursor2 *req,
3013 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003014{
Dave Airlief453ba02008-11-07 14:05:41 -08003015 struct drm_crtc *crtc;
3016 int ret = 0;
3017
Dave Airliefb3b06c2011-02-08 13:55:21 +10003018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3019 return -EINVAL;
3020
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003021 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003022 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003023
Rob Clarka2b34e22013-10-05 16:36:52 -04003024 crtc = drm_crtc_find(dev, req->crtc_id);
3025 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003026 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003027 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003028 }
Dave Airlief453ba02008-11-07 14:05:41 -08003029
Matt Roper161d0dc2014-06-10 08:28:10 -07003030 /*
3031 * If this crtc has a universal cursor plane, call that plane's update
3032 * handler rather than using legacy cursor handlers.
3033 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003034 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003035 if (crtc->cursor) {
3036 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3037 goto out;
3038 }
3039
Dave Airlief453ba02008-11-07 14:05:41 -08003040 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003041 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003042 ret = -ENXIO;
3043 goto out;
3044 }
3045 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003046 if (crtc->funcs->cursor_set2)
3047 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3048 req->width, req->height, req->hot_x, req->hot_y);
3049 else
3050 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3051 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003052 }
3053
3054 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3055 if (crtc->funcs->cursor_move) {
3056 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3057 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003058 ret = -EFAULT;
3059 goto out;
3060 }
3061 }
3062out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003063 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003064
Dave Airlief453ba02008-11-07 14:05:41 -08003065 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003066
3067}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003068
3069
3070/**
3071 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3072 * @dev: drm device for the ioctl
3073 * @data: data pointer for the ioctl
3074 * @file_priv: drm file for the ioctl call
3075 *
3076 * Set the cursor configuration based on user request.
3077 *
3078 * Called by the user via ioctl.
3079 *
3080 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003081 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003082 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003083int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003084 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003085{
3086 struct drm_mode_cursor *req = data;
3087 struct drm_mode_cursor2 new_req;
3088
3089 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3090 new_req.hot_x = new_req.hot_y = 0;
3091
3092 return drm_mode_cursor_common(dev, &new_req, file_priv);
3093}
3094
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003095/**
3096 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3097 * @dev: drm device for the ioctl
3098 * @data: data pointer for the ioctl
3099 * @file_priv: drm file for the ioctl call
3100 *
3101 * Set the cursor configuration based on user request. This implements the 2nd
3102 * version of the cursor ioctl, which allows userspace to additionally specify
3103 * the hotspot of the pointer.
3104 *
3105 * Called by the user via ioctl.
3106 *
3107 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003108 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003109 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003110int drm_mode_cursor2_ioctl(struct drm_device *dev,
3111 void *data, struct drm_file *file_priv)
3112{
3113 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003114
Dave Airlie4c813d42013-06-20 11:48:52 +10003115 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003116}
3117
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003118/**
3119 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3120 * @bpp: bits per pixels
3121 * @depth: bit depth per pixel
3122 *
3123 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3124 * Useful in fbdev emulation code, since that deals in those values.
3125 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003126uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3127{
3128 uint32_t fmt;
3129
3130 switch (bpp) {
3131 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003132 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003133 break;
3134 case 16:
3135 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003136 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003137 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003138 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003139 break;
3140 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003141 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003142 break;
3143 case 32:
3144 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003145 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003146 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003147 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003148 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003149 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003150 break;
3151 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003152 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3153 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003154 break;
3155 }
3156
3157 return fmt;
3158}
3159EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3160
Dave Airlief453ba02008-11-07 14:05:41 -08003161/**
3162 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003163 * @dev: drm device for the ioctl
3164 * @data: data pointer for the ioctl
3165 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003166 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003167 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003168 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003169 *
3170 * Called by the user via ioctl.
3171 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003172 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003173 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003174 */
3175int drm_mode_addfb(struct drm_device *dev,
3176 void *data, struct drm_file *file_priv)
3177{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003178 struct drm_mode_fb_cmd *or = data;
3179 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003180 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003181
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003182 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003183 r.fb_id = or->fb_id;
3184 r.width = or->width;
3185 r.height = or->height;
3186 r.pitches[0] = or->pitch;
3187 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3188 r.handles[0] = or->handle;
3189
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003190 ret = drm_mode_addfb2(dev, &r, file_priv);
3191 if (ret)
3192 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003193
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003194 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003195
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003196 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003197}
3198
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003199static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003200{
3201 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3202
3203 switch (format) {
3204 case DRM_FORMAT_C8:
3205 case DRM_FORMAT_RGB332:
3206 case DRM_FORMAT_BGR233:
3207 case DRM_FORMAT_XRGB4444:
3208 case DRM_FORMAT_XBGR4444:
3209 case DRM_FORMAT_RGBX4444:
3210 case DRM_FORMAT_BGRX4444:
3211 case DRM_FORMAT_ARGB4444:
3212 case DRM_FORMAT_ABGR4444:
3213 case DRM_FORMAT_RGBA4444:
3214 case DRM_FORMAT_BGRA4444:
3215 case DRM_FORMAT_XRGB1555:
3216 case DRM_FORMAT_XBGR1555:
3217 case DRM_FORMAT_RGBX5551:
3218 case DRM_FORMAT_BGRX5551:
3219 case DRM_FORMAT_ARGB1555:
3220 case DRM_FORMAT_ABGR1555:
3221 case DRM_FORMAT_RGBA5551:
3222 case DRM_FORMAT_BGRA5551:
3223 case DRM_FORMAT_RGB565:
3224 case DRM_FORMAT_BGR565:
3225 case DRM_FORMAT_RGB888:
3226 case DRM_FORMAT_BGR888:
3227 case DRM_FORMAT_XRGB8888:
3228 case DRM_FORMAT_XBGR8888:
3229 case DRM_FORMAT_RGBX8888:
3230 case DRM_FORMAT_BGRX8888:
3231 case DRM_FORMAT_ARGB8888:
3232 case DRM_FORMAT_ABGR8888:
3233 case DRM_FORMAT_RGBA8888:
3234 case DRM_FORMAT_BGRA8888:
3235 case DRM_FORMAT_XRGB2101010:
3236 case DRM_FORMAT_XBGR2101010:
3237 case DRM_FORMAT_RGBX1010102:
3238 case DRM_FORMAT_BGRX1010102:
3239 case DRM_FORMAT_ARGB2101010:
3240 case DRM_FORMAT_ABGR2101010:
3241 case DRM_FORMAT_RGBA1010102:
3242 case DRM_FORMAT_BGRA1010102:
3243 case DRM_FORMAT_YUYV:
3244 case DRM_FORMAT_YVYU:
3245 case DRM_FORMAT_UYVY:
3246 case DRM_FORMAT_VYUY:
3247 case DRM_FORMAT_AYUV:
3248 case DRM_FORMAT_NV12:
3249 case DRM_FORMAT_NV21:
3250 case DRM_FORMAT_NV16:
3251 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003252 case DRM_FORMAT_NV24:
3253 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003254 case DRM_FORMAT_YUV410:
3255 case DRM_FORMAT_YVU410:
3256 case DRM_FORMAT_YUV411:
3257 case DRM_FORMAT_YVU411:
3258 case DRM_FORMAT_YUV420:
3259 case DRM_FORMAT_YVU420:
3260 case DRM_FORMAT_YUV422:
3261 case DRM_FORMAT_YVU422:
3262 case DRM_FORMAT_YUV444:
3263 case DRM_FORMAT_YVU444:
3264 return 0;
3265 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003266 DRM_DEBUG_KMS("invalid pixel format %s\n",
3267 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003268 return -EINVAL;
3269 }
3270}
3271
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003272static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003273{
3274 int ret, hsub, vsub, num_planes, i;
3275
3276 ret = format_check(r);
3277 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003278 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3279 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003280 return ret;
3281 }
3282
3283 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3284 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3285 num_planes = drm_format_num_planes(r->pixel_format);
3286
3287 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003288 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003289 return -EINVAL;
3290 }
3291
3292 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003293 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003294 return -EINVAL;
3295 }
3296
3297 for (i = 0; i < num_planes; i++) {
3298 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003299 unsigned int height = r->height / (i != 0 ? vsub : 1);
3300 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003301
3302 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003303 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003304 return -EINVAL;
3305 }
3306
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003307 if ((uint64_t) width * cpp > UINT_MAX)
3308 return -ERANGE;
3309
3310 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3311 return -ERANGE;
3312
3313 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003314 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003315 return -EINVAL;
3316 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003317
3318 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3319 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3320 r->modifier[i], i);
3321 return -EINVAL;
3322 }
Rob Clark570655b2015-01-30 20:18:11 +05303323
3324 /* modifier specific checks: */
3325 switch (r->modifier[i]) {
3326 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3327 /* NOTE: the pitch restriction may be lifted later if it turns
3328 * out that no hw has this restriction:
3329 */
3330 if (r->pixel_format != DRM_FORMAT_NV12 ||
3331 width % 128 || height % 32 ||
3332 r->pitches[i] % 128) {
3333 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3334 return -EINVAL;
3335 }
3336 break;
3337
3338 default:
3339 break;
3340 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003341 }
3342
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003343 for (i = num_planes; i < 4; i++) {
3344 if (r->modifier[i]) {
3345 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3346 return -EINVAL;
3347 }
3348
3349 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3350 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3351 continue;
3352
3353 if (r->handles[i]) {
3354 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3355 return -EINVAL;
3356 }
3357
3358 if (r->pitches[i]) {
3359 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3360 return -EINVAL;
3361 }
3362
3363 if (r->offsets[i]) {
3364 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3365 return -EINVAL;
3366 }
3367 }
3368
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003369 return 0;
3370}
3371
Chris Wilson9a6f5132015-02-25 13:45:26 +00003372static struct drm_framebuffer *
3373internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003374 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003375 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003376{
3377 struct drm_mode_config *config = &dev->mode_config;
3378 struct drm_framebuffer *fb;
3379 int ret;
3380
Rob Clarke3eb3252015-02-05 14:41:52 +00003381 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003382 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3383 return ERR_PTR(-EINVAL);
3384 }
3385
3386 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3387 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3388 r->width, config->min_width, config->max_width);
3389 return ERR_PTR(-EINVAL);
3390 }
3391 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3392 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3393 r->height, config->min_height, config->max_height);
3394 return ERR_PTR(-EINVAL);
3395 }
3396
Rob Clarke3eb3252015-02-05 14:41:52 +00003397 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3398 !dev->mode_config.allow_fb_modifiers) {
3399 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3400 return ERR_PTR(-EINVAL);
3401 }
3402
Matt Roperc394c2b2014-06-10 08:28:08 -07003403 ret = framebuffer_check(r);
3404 if (ret)
3405 return ERR_PTR(ret);
3406
3407 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3408 if (IS_ERR(fb)) {
3409 DRM_DEBUG_KMS("could not create framebuffer\n");
3410 return fb;
3411 }
3412
Matt Roperc394c2b2014-06-10 08:28:08 -07003413 return fb;
3414}
3415
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003416/**
3417 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003418 * @dev: drm device for the ioctl
3419 * @data: data pointer for the ioctl
3420 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003421 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003422 * Add a new FB to the specified CRTC, given a user request with format. This is
3423 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3424 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003425 *
3426 * Called by the user via ioctl.
3427 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003428 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003429 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003430 */
3431int drm_mode_addfb2(struct drm_device *dev,
3432 void *data, struct drm_file *file_priv)
3433{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003434 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003435 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003436
Dave Airliefb3b06c2011-02-08 13:55:21 +10003437 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3438 return -EINVAL;
3439
Chris Wilson9a6f5132015-02-25 13:45:26 +00003440 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003441 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003442 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003443
Chris Wilson9a6f5132015-02-25 13:45:26 +00003444 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003445 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003446
3447 /* Transfer ownership to the filp for reaping on close */
3448 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003449 list_add(&fb->filp_head, &file_priv->fbs);
3450 mutex_unlock(&file_priv->fbs_lock);
3451
Matt Roperc394c2b2014-06-10 08:28:08 -07003452 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003453}
3454
3455/**
3456 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003457 * @dev: drm device for the ioctl
3458 * @data: data pointer for the ioctl
3459 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003460 *
Dave Airlief453ba02008-11-07 14:05:41 -08003461 * Remove the FB specified by the user.
3462 *
3463 * Called by the user via ioctl.
3464 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003465 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003466 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003467 */
3468int drm_mode_rmfb(struct drm_device *dev,
3469 void *data, struct drm_file *file_priv)
3470{
Dave Airlief453ba02008-11-07 14:05:41 -08003471 struct drm_framebuffer *fb = NULL;
3472 struct drm_framebuffer *fbl = NULL;
3473 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003474 int found = 0;
3475
Dave Airliefb3b06c2011-02-08 13:55:21 +10003476 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3477 return -EINVAL;
3478
Dave Airlie72fe90b2016-04-15 15:10:40 +10003479 fb = drm_framebuffer_lookup(dev, *id);
3480 if (!fb)
3481 return -ENOENT;
3482
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003483 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003484 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3485 if (fb == fbl)
3486 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003487 if (!found) {
3488 mutex_unlock(&file_priv->fbs_lock);
3489 goto fail_unref;
3490 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003491
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003492 list_del_init(&fb->filp_head);
3493 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003494
Dave Airlie72fe90b2016-04-15 15:10:40 +10003495 /* we now own the reference that was stored in the fbs list */
3496 drm_framebuffer_unreference(fb);
3497
3498 /* drop the reference we picked up in framebuffer lookup */
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
Dave Airlie72fe90b2016-04-15 15:10:40 +10003503fail_unref:
3504 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003505 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003506}
3507
3508/**
3509 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003510 * @dev: drm device for the ioctl
3511 * @data: data pointer for the ioctl
3512 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003513 *
Dave Airlief453ba02008-11-07 14:05:41 -08003514 * Lookup the FB given its ID and return info about it.
3515 *
3516 * Called by the user via ioctl.
3517 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003518 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003519 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003520 */
3521int drm_mode_getfb(struct drm_device *dev,
3522 void *data, struct drm_file *file_priv)
3523{
3524 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003525 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003526 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003527
Dave Airliefb3b06c2011-02-08 13:55:21 +10003528 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3529 return -EINVAL;
3530
Daniel Vetter786b99e2012-12-02 21:53:40 +01003531 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003532 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003533 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003534
3535 r->height = fb->height;
3536 r->width = fb->width;
3537 r->depth = fb->depth;
3538 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003539 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003540 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003541 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003542 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003543 ret = fb->funcs->create_handle(fb, file_priv,
3544 &r->handle);
3545 } else {
3546 /* GET_FB() is an unprivileged ioctl so we must not
3547 * return a buffer-handle to non-master processes! For
3548 * backwards-compatibility reasons, we cannot make
3549 * GET_FB() privileged, so just return an invalid handle
3550 * for non-masters. */
3551 r->handle = 0;
3552 ret = 0;
3553 }
3554 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003555 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003556 }
Dave Airlief453ba02008-11-07 14:05:41 -08003557
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003558 drm_framebuffer_unreference(fb);
3559
Dave Airlief453ba02008-11-07 14:05:41 -08003560 return ret;
3561}
3562
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003563/**
3564 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3565 * @dev: drm device for the ioctl
3566 * @data: data pointer for the ioctl
3567 * @file_priv: drm file for the ioctl call
3568 *
3569 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3570 * rectangle list. Generic userspace which does frontbuffer rendering must call
3571 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3572 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3573 *
3574 * Modesetting drivers which always update the frontbuffer do not need to
3575 * implement the corresponding ->dirty framebuffer callback.
3576 *
3577 * Called by the user via ioctl.
3578 *
3579 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003580 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003581 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003582int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3583 void *data, struct drm_file *file_priv)
3584{
3585 struct drm_clip_rect __user *clips_ptr;
3586 struct drm_clip_rect *clips = NULL;
3587 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003588 struct drm_framebuffer *fb;
3589 unsigned flags;
3590 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003591 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003592
Dave Airliefb3b06c2011-02-08 13:55:21 +10003593 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3594 return -EINVAL;
3595
Daniel Vetter786b99e2012-12-02 21:53:40 +01003596 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003597 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003598 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003599
3600 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003601 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003602
3603 if (!num_clips != !clips_ptr) {
3604 ret = -EINVAL;
3605 goto out_err1;
3606 }
3607
3608 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3609
3610 /* If userspace annotates copy, clips must come in pairs */
3611 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3612 ret = -EINVAL;
3613 goto out_err1;
3614 }
3615
3616 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003617 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3618 ret = -EINVAL;
3619 goto out_err1;
3620 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003621 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003622 if (!clips) {
3623 ret = -ENOMEM;
3624 goto out_err1;
3625 }
3626
3627 ret = copy_from_user(clips, clips_ptr,
3628 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003629 if (ret) {
3630 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003631 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003632 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003633 }
3634
3635 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003636 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3637 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003638 } else {
3639 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003640 }
3641
3642out_err2:
3643 kfree(clips);
3644out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003645 drm_framebuffer_unreference(fb);
3646
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003647 return ret;
3648}
3649
3650
Dave Airlief453ba02008-11-07 14:05:41 -08003651/**
3652 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003653 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003654 *
Dave Airlief453ba02008-11-07 14:05:41 -08003655 * Destroy all the FBs associated with @filp.
3656 *
3657 * Called by the user via ioctl.
3658 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003659 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003660 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003661 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003662void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003663{
Dave Airlief453ba02008-11-07 14:05:41 -08003664 struct drm_framebuffer *fb, *tfb;
3665
Daniel Vetter1b116292014-09-24 21:51:06 +02003666 /*
3667 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003668 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003669 * avoid upsetting lockdep since the universal cursor code adds a
3670 * framebuffer while holding mutex locks.
3671 *
3672 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3673 * locks is impossible here since no one else but this function can get
3674 * at it any more.
3675 */
Dave Airlief453ba02008-11-07 14:05:41 -08003676 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003677 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003678
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003679 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003680 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003681 }
Dave Airlief453ba02008-11-07 14:05:41 -08003682}
3683
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003684/**
3685 * drm_property_create - create a new property type
3686 * @dev: drm device
3687 * @flags: flags specifying the property type
3688 * @name: name of the property
3689 * @num_values: number of pre-defined values
3690 *
3691 * This creates a new generic drm property which can then be attached to a drm
3692 * object with drm_object_attach_property. The returned property object must be
3693 * freed with drm_property_destroy.
3694 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003695 * Note that the DRM core keeps a per-device list of properties and that, if
3696 * drm_mode_config_cleanup() is called, it will destroy all properties created
3697 * by the driver.
3698 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003699 * Returns:
3700 * A pointer to the newly created property on success, NULL on failure.
3701 */
Dave Airlief453ba02008-11-07 14:05:41 -08003702struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3703 const char *name, int num_values)
3704{
3705 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003706 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003707
3708 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3709 if (!property)
3710 return NULL;
3711
Rob Clark98f75de2014-05-30 11:37:03 -04003712 property->dev = dev;
3713
Dave Airlief453ba02008-11-07 14:05:41 -08003714 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003715 property->values = kcalloc(num_values, sizeof(uint64_t),
3716 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003717 if (!property->values)
3718 goto fail;
3719 }
3720
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003721 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3722 if (ret)
3723 goto fail;
3724
Dave Airlief453ba02008-11-07 14:05:41 -08003725 property->flags = flags;
3726 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003727 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003728
Vinson Lee471dd2e2011-11-10 11:55:40 -08003729 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003730 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003731 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3732 }
Dave Airlief453ba02008-11-07 14:05:41 -08003733
3734 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003735
3736 WARN_ON(!drm_property_type_valid(property));
3737
Dave Airlief453ba02008-11-07 14:05:41 -08003738 return property;
3739fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003740 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003741 kfree(property);
3742 return NULL;
3743}
3744EXPORT_SYMBOL(drm_property_create);
3745
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003746/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003747 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003748 * @dev: drm device
3749 * @flags: flags specifying the property type
3750 * @name: name of the property
3751 * @props: enumeration lists with property values
3752 * @num_values: number of pre-defined values
3753 *
3754 * This creates a new generic drm property which can then be attached to a drm
3755 * object with drm_object_attach_property. The returned property object must be
3756 * freed with drm_property_destroy.
3757 *
3758 * Userspace is only allowed to set one of the predefined values for enumeration
3759 * properties.
3760 *
3761 * Returns:
3762 * A pointer to the newly created property on success, NULL on failure.
3763 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003764struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3765 const char *name,
3766 const struct drm_prop_enum_list *props,
3767 int num_values)
3768{
3769 struct drm_property *property;
3770 int i, ret;
3771
3772 flags |= DRM_MODE_PROP_ENUM;
3773
3774 property = drm_property_create(dev, flags, name, num_values);
3775 if (!property)
3776 return NULL;
3777
3778 for (i = 0; i < num_values; i++) {
3779 ret = drm_property_add_enum(property, i,
3780 props[i].type,
3781 props[i].name);
3782 if (ret) {
3783 drm_property_destroy(dev, property);
3784 return NULL;
3785 }
3786 }
3787
3788 return property;
3789}
3790EXPORT_SYMBOL(drm_property_create_enum);
3791
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003792/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003793 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003794 * @dev: drm device
3795 * @flags: flags specifying the property type
3796 * @name: name of the property
3797 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003798 * @num_props: size of the @props array
3799 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003800 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003801 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003802 * object with drm_object_attach_property. The returned property object must be
3803 * freed with drm_property_destroy.
3804 *
3805 * Compared to plain enumeration properties userspace is allowed to set any
3806 * or'ed together combination of the predefined property bitflag values
3807 *
3808 * Returns:
3809 * A pointer to the newly created property on success, NULL on failure.
3810 */
Rob Clark49e27542012-05-17 02:23:26 -06003811struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3812 int flags, const char *name,
3813 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303814 int num_props,
3815 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003816{
3817 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303818 int i, ret, index = 0;
3819 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003820
3821 flags |= DRM_MODE_PROP_BITMASK;
3822
3823 property = drm_property_create(dev, flags, name, num_values);
3824 if (!property)
3825 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303826 for (i = 0; i < num_props; i++) {
3827 if (!(supported_bits & (1ULL << props[i].type)))
3828 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003829
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303830 if (WARN_ON(index >= num_values)) {
3831 drm_property_destroy(dev, property);
3832 return NULL;
3833 }
3834
3835 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003836 props[i].type,
3837 props[i].name);
3838 if (ret) {
3839 drm_property_destroy(dev, property);
3840 return NULL;
3841 }
3842 }
3843
3844 return property;
3845}
3846EXPORT_SYMBOL(drm_property_create_bitmask);
3847
Rob Clarkebc44cf2012-09-12 22:22:31 -05003848static struct drm_property *property_create_range(struct drm_device *dev,
3849 int flags, const char *name,
3850 uint64_t min, uint64_t max)
3851{
3852 struct drm_property *property;
3853
3854 property = drm_property_create(dev, flags, name, 2);
3855 if (!property)
3856 return NULL;
3857
3858 property->values[0] = min;
3859 property->values[1] = max;
3860
3861 return property;
3862}
3863
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003864/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003865 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003866 * @dev: drm device
3867 * @flags: flags specifying the property type
3868 * @name: name of the property
3869 * @min: minimum value of the property
3870 * @max: maximum value of the property
3871 *
3872 * This creates a new generic drm property which can then be attached to a drm
3873 * object with drm_object_attach_property. The returned property object must be
3874 * freed with drm_property_destroy.
3875 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003876 * Userspace is allowed to set any unsigned integer value in the (min, max)
3877 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003878 *
3879 * Returns:
3880 * A pointer to the newly created property on success, NULL on failure.
3881 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003882struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3883 const char *name,
3884 uint64_t min, uint64_t max)
3885{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003886 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3887 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003888}
3889EXPORT_SYMBOL(drm_property_create_range);
3890
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003891/**
3892 * drm_property_create_signed_range - create a new signed ranged property type
3893 * @dev: drm device
3894 * @flags: flags specifying the property type
3895 * @name: name of the property
3896 * @min: minimum value of the property
3897 * @max: maximum value of the property
3898 *
3899 * This creates a new generic drm property which can then be attached to a drm
3900 * object with drm_object_attach_property. The returned property object must be
3901 * freed with drm_property_destroy.
3902 *
3903 * Userspace is allowed to set any signed integer value in the (min, max)
3904 * range inclusive.
3905 *
3906 * Returns:
3907 * A pointer to the newly created property on success, NULL on failure.
3908 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003909struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3910 int flags, const char *name,
3911 int64_t min, int64_t max)
3912{
3913 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3914 name, I642U64(min), I642U64(max));
3915}
3916EXPORT_SYMBOL(drm_property_create_signed_range);
3917
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003918/**
3919 * drm_property_create_object - create a new object property type
3920 * @dev: drm device
3921 * @flags: flags specifying the property type
3922 * @name: name of the property
3923 * @type: object type from DRM_MODE_OBJECT_* defines
3924 *
3925 * This creates a new generic drm property which can then be attached to a drm
3926 * object with drm_object_attach_property. The returned property object must be
3927 * freed with drm_property_destroy.
3928 *
3929 * Userspace is only allowed to set this to any property value of the given
3930 * @type. Only useful for atomic properties, which is enforced.
3931 *
3932 * Returns:
3933 * A pointer to the newly created property on success, NULL on failure.
3934 */
Rob Clark98f75de2014-05-30 11:37:03 -04003935struct drm_property *drm_property_create_object(struct drm_device *dev,
3936 int flags, const char *name, uint32_t type)
3937{
3938 struct drm_property *property;
3939
3940 flags |= DRM_MODE_PROP_OBJECT;
3941
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003942 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3943 return NULL;
3944
Rob Clark98f75de2014-05-30 11:37:03 -04003945 property = drm_property_create(dev, flags, name, 1);
3946 if (!property)
3947 return NULL;
3948
3949 property->values[0] = type;
3950
3951 return property;
3952}
3953EXPORT_SYMBOL(drm_property_create_object);
3954
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003955/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003956 * drm_property_create_bool - create a new boolean property type
3957 * @dev: drm device
3958 * @flags: flags specifying the property type
3959 * @name: name of the property
3960 *
3961 * This creates a new generic drm property which can then be attached to a drm
3962 * object with drm_object_attach_property. The returned property object must be
3963 * freed with drm_property_destroy.
3964 *
3965 * This is implemented as a ranged property with only {0, 1} as valid values.
3966 *
3967 * Returns:
3968 * A pointer to the newly created property on success, NULL on failure.
3969 */
3970struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3971 const char *name)
3972{
3973 return drm_property_create_range(dev, flags, name, 0, 1);
3974}
3975EXPORT_SYMBOL(drm_property_create_bool);
3976
3977/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003978 * drm_property_add_enum - add a possible value to an enumeration property
3979 * @property: enumeration property to change
3980 * @index: index of the new enumeration
3981 * @value: value of the new enumeration
3982 * @name: symbolic name of the new enumeration
3983 *
3984 * This functions adds enumerations to a property.
3985 *
3986 * It's use is deprecated, drivers should use one of the more specific helpers
3987 * to directly create the property with all enumerations already attached.
3988 *
3989 * Returns:
3990 * Zero on success, error code on failure.
3991 */
Dave Airlief453ba02008-11-07 14:05:41 -08003992int drm_property_add_enum(struct drm_property *property, int index,
3993 uint64_t value, const char *name)
3994{
3995 struct drm_property_enum *prop_enum;
3996
Rob Clark5ea22f22014-05-30 11:34:01 -04003997 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3998 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003999 return -EINVAL;
4000
4001 /*
4002 * Bitmask enum properties have the additional constraint of values
4003 * from 0 to 63
4004 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004005 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4006 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004007 return -EINVAL;
4008
Daniel Vetter3758b342014-11-19 18:38:10 +01004009 if (!list_empty(&property->enum_list)) {
4010 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004011 if (prop_enum->value == value) {
4012 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4013 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4014 return 0;
4015 }
4016 }
4017 }
4018
4019 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4020 if (!prop_enum)
4021 return -ENOMEM;
4022
4023 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4024 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4025 prop_enum->value = value;
4026
4027 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004028 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004029 return 0;
4030}
4031EXPORT_SYMBOL(drm_property_add_enum);
4032
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004033/**
4034 * drm_property_destroy - destroy a drm property
4035 * @dev: drm device
4036 * @property: property to destry
4037 *
4038 * This function frees a property including any attached resources like
4039 * enumeration values.
4040 */
Dave Airlief453ba02008-11-07 14:05:41 -08004041void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4042{
4043 struct drm_property_enum *prop_enum, *pt;
4044
Daniel Vetter3758b342014-11-19 18:38:10 +01004045 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004046 list_del(&prop_enum->head);
4047 kfree(prop_enum);
4048 }
4049
4050 if (property->num_values)
4051 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004052 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004053 list_del(&property->head);
4054 kfree(property);
4055}
4056EXPORT_SYMBOL(drm_property_destroy);
4057
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004058/**
4059 * drm_object_attach_property - attach a property to a modeset object
4060 * @obj: drm modeset object
4061 * @property: property to attach
4062 * @init_val: initial value of the property
4063 *
4064 * This attaches the given property to the modeset object with the given initial
4065 * value. Currently this function cannot fail since the properties are stored in
4066 * a statically sized array.
4067 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004068void drm_object_attach_property(struct drm_mode_object *obj,
4069 struct drm_property *property,
4070 uint64_t init_val)
4071{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004072 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004073
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004074 if (count == DRM_OBJECT_MAX_PROPERTY) {
4075 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4076 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4077 "you see this message on the same object type.\n",
4078 obj->type);
4079 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004080 }
4081
Rob Clarkb17cd752014-12-16 18:05:30 -05004082 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004083 obj->properties->values[count] = init_val;
4084 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004085 if (property->flags & DRM_MODE_PROP_ATOMIC)
4086 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004087}
4088EXPORT_SYMBOL(drm_object_attach_property);
4089
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004090/**
4091 * drm_object_property_set_value - set the value of a property
4092 * @obj: drm mode object to set property value for
4093 * @property: property to set
4094 * @val: value the property should be set to
4095 *
4096 * This functions sets a given property on a given object. This function only
4097 * changes the software state of the property, it does not call into the
4098 * driver's ->set_property callback.
4099 *
4100 * Returns:
4101 * Zero on success, error code on failure.
4102 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004103int drm_object_property_set_value(struct drm_mode_object *obj,
4104 struct drm_property *property, uint64_t val)
4105{
4106 int i;
4107
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004108 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004109 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004110 obj->properties->values[i] = val;
4111 return 0;
4112 }
4113 }
4114
4115 return -EINVAL;
4116}
4117EXPORT_SYMBOL(drm_object_property_set_value);
4118
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004119/**
4120 * drm_object_property_get_value - retrieve the value of a property
4121 * @obj: drm mode object to get property value from
4122 * @property: property to retrieve
4123 * @val: storage for the property value
4124 *
4125 * This function retrieves the softare state of the given property for the given
4126 * property. Since there is no driver callback to retrieve the current property
4127 * value this might be out of sync with the hardware, depending upon the driver
4128 * and property.
4129 *
4130 * Returns:
4131 * Zero on success, error code on failure.
4132 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004133int drm_object_property_get_value(struct drm_mode_object *obj,
4134 struct drm_property *property, uint64_t *val)
4135{
4136 int i;
4137
Rob Clark88a48e22014-12-18 16:01:50 -05004138 /* read-only properties bypass atomic mechanism and still store
4139 * their value in obj->properties->values[].. mostly to avoid
4140 * having to deal w/ EDID and similar props in atomic paths:
4141 */
4142 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4143 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4144 return drm_atomic_get_property(obj, property, val);
4145
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004146 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004147 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004148 *val = obj->properties->values[i];
4149 return 0;
4150 }
4151 }
4152
4153 return -EINVAL;
4154}
4155EXPORT_SYMBOL(drm_object_property_get_value);
4156
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004157/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004158 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004159 * @dev: DRM device
4160 * @data: ioctl data
4161 * @file_priv: DRM file info
4162 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004163 * This function retrieves the metadata for a given property, like the different
4164 * possible values for an enum property or the limits for a range property.
4165 *
4166 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004167 *
4168 * Called by the user via ioctl.
4169 *
4170 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004171 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004172 */
Dave Airlief453ba02008-11-07 14:05:41 -08004173int drm_mode_getproperty_ioctl(struct drm_device *dev,
4174 void *data, struct drm_file *file_priv)
4175{
Dave Airlief453ba02008-11-07 14:05:41 -08004176 struct drm_mode_get_property *out_resp = data;
4177 struct drm_property *property;
4178 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004179 int value_count = 0;
4180 int ret = 0, i;
4181 int copied;
4182 struct drm_property_enum *prop_enum;
4183 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004184 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004185
Dave Airliefb3b06c2011-02-08 13:55:21 +10004186 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4187 return -EINVAL;
4188
Daniel Vetter84849902012-12-02 00:28:11 +01004189 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004190 property = drm_property_find(dev, out_resp->prop_id);
4191 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004192 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004193 goto done;
4194 }
Dave Airlief453ba02008-11-07 14:05:41 -08004195
Rob Clark5ea22f22014-05-30 11:34:01 -04004196 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4197 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004198 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004199 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004200 }
4201
4202 value_count = property->num_values;
4203
4204 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4205 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4206 out_resp->flags = property->flags;
4207
4208 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004209 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004210 for (i = 0; i < value_count; i++) {
4211 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4212 ret = -EFAULT;
4213 goto done;
4214 }
4215 }
4216 }
4217 out_resp->count_values = value_count;
4218
Rob Clark5ea22f22014-05-30 11:34:01 -04004219 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4220 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004221 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4222 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004223 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004224 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004225
4226 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4227 ret = -EFAULT;
4228 goto done;
4229 }
4230
4231 if (copy_to_user(&enum_ptr[copied].name,
4232 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4233 ret = -EFAULT;
4234 goto done;
4235 }
4236 copied++;
4237 }
4238 }
4239 out_resp->count_enum_blobs = enum_count;
4240 }
4241
Daniel Vetter3758b342014-11-19 18:38:10 +01004242 /*
4243 * NOTE: The idea seems to have been to use this to read all the blob
4244 * property values. But nothing ever added them to the corresponding
4245 * list, userspace always used the special-purpose get_blob ioctl to
4246 * read the value for a blob property. It also doesn't make a lot of
4247 * sense to return values here when everything else is just metadata for
4248 * the property itself.
4249 */
4250 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4251 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004252done:
Daniel Vetter84849902012-12-02 00:28:11 +01004253 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004254 return ret;
4255}
4256
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004257static void drm_property_free_blob(struct kref *kref)
4258{
4259 struct drm_property_blob *blob =
4260 container_of(kref, struct drm_property_blob, base.refcount);
4261
4262 mutex_lock(&blob->dev->mode_config.blob_lock);
4263 list_del(&blob->head_global);
4264 mutex_unlock(&blob->dev->mode_config.blob_lock);
4265
4266 drm_mode_object_unregister(blob->dev, &blob->base);
4267
4268 kfree(blob);
4269}
4270
Daniel Stone99531d92015-05-22 13:34:49 +01004271/**
4272 * drm_property_create_blob - Create new blob property
4273 *
4274 * Creates a new blob property for a specified DRM device, optionally
4275 * copying data.
4276 *
4277 * @dev: DRM device to create property for
4278 * @length: Length to allocate for blob data
4279 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004280 *
4281 * Returns:
4282 * New blob property with a single reference on success, or an ERR_PTR
4283 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004284 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004285struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004286drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004287 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004288{
4289 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004290 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004291
Dan Carpenter9ac09342015-10-29 16:37:54 +03004292 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004293 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004294
4295 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4296 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004297 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004298
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004299 /* This must be explicitly initialised, so we can safely call list_del
4300 * on it in the removal handler, even if it isn't in a file list. */
4301 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004302 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004303 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004304
Daniel Stone99531d92015-05-22 13:34:49 +01004305 if (data)
4306 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004307
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004308 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4309 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004310 if (ret) {
4311 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004312 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004313 }
4314
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004315 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004316 list_add_tail(&blob->head_global,
4317 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004318 mutex_unlock(&dev->mode_config.blob_lock);
4319
Dave Airlief453ba02008-11-07 14:05:41 -08004320 return blob;
4321}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004322EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004323
Daniel Stone6bcacf52015-04-20 19:22:55 +01004324/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004325 * drm_property_unreference_blob - Unreference a blob property
4326 *
4327 * Drop a reference on a blob property. May free the object.
4328 *
Daniel Stonef102c162015-05-22 13:34:44 +01004329 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004330 */
4331void drm_property_unreference_blob(struct drm_property_blob *blob)
4332{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004333 if (!blob)
4334 return;
4335
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004336 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004337}
4338EXPORT_SYMBOL(drm_property_unreference_blob);
4339
4340/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004341 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4342 * @dev: DRM device
4343 * @file_priv: destroy all blobs owned by this file handle
4344 */
4345void drm_property_destroy_user_blobs(struct drm_device *dev,
4346 struct drm_file *file_priv)
4347{
4348 struct drm_property_blob *blob, *bt;
4349
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004350 /*
4351 * When the file gets released that means no one else can access the
4352 * blob list any more, so no need to grab dev->blob_lock.
4353 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004354 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4355 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004356 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004357 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004358}
4359
4360/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004361 * drm_property_reference_blob - Take a reference on an existing property
4362 *
4363 * Take a new reference on an existing blob property.
4364 *
Daniel Stonef102c162015-05-22 13:34:44 +01004365 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004366 */
4367struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4368{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004369 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004370 return blob;
4371}
4372EXPORT_SYMBOL(drm_property_reference_blob);
4373
Daniel Stone6bcacf52015-04-20 19:22:55 +01004374/**
4375 * drm_property_lookup_blob - look up a blob property and take a reference
4376 * @dev: drm device
4377 * @id: id of the blob property
4378 *
4379 * If successful, this takes an additional reference to the blob property.
4380 * callers need to make sure to eventually unreference the returned property
4381 * again, using @drm_property_unreference_blob.
4382 */
4383struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4384 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004385{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004386 struct drm_mode_object *obj;
4387 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004388
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004389 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4390 if (obj)
4391 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004392 return blob;
4393}
4394EXPORT_SYMBOL(drm_property_lookup_blob);
4395
4396/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004397 * drm_property_replace_global_blob - atomically replace existing blob property
4398 * @dev: drm device
4399 * @replace: location of blob property pointer to be replaced
4400 * @length: length of data for new blob, or 0 for no data
4401 * @data: content for new blob, or NULL for no data
4402 * @obj_holds_id: optional object for property holding blob ID
4403 * @prop_holds_id: optional property holding blob ID
4404 * @return 0 on success or error on failure
4405 *
4406 * This function will atomically replace a global property in the blob list,
4407 * optionally updating a property which holds the ID of that property. It is
4408 * guaranteed to be atomic: no caller will be allowed to see intermediate
4409 * results, and either the entire operation will succeed and clean up the
4410 * previous property, or it will fail and the state will be unchanged.
4411 *
4412 * If length is 0 or data is NULL, no new blob will be created, and the holding
4413 * property, if specified, will be set to 0.
4414 *
4415 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4416 * by holding the relevant modesetting object lock for its parent.
4417 *
4418 * For example, a drm_connector has a 'PATH' property, which contains the ID
4419 * of a blob property with the value of the MST path information. Calling this
4420 * function with replace pointing to the connector's path_blob_ptr, length and
4421 * data set for the new path information, obj_holds_id set to the connector's
4422 * base object, and prop_holds_id set to the path property name, will perform
4423 * a completely atomic update. The access to path_blob_ptr is protected by the
4424 * caller holding a lock on the connector.
4425 */
4426static int drm_property_replace_global_blob(struct drm_device *dev,
4427 struct drm_property_blob **replace,
4428 size_t length,
4429 const void *data,
4430 struct drm_mode_object *obj_holds_id,
4431 struct drm_property *prop_holds_id)
4432{
4433 struct drm_property_blob *new_blob = NULL;
4434 struct drm_property_blob *old_blob = NULL;
4435 int ret;
4436
4437 WARN_ON(replace == NULL);
4438
4439 old_blob = *replace;
4440
4441 if (length && data) {
4442 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004443 if (IS_ERR(new_blob))
4444 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004445 }
4446
4447 /* This does not need to be synchronised with blob_lock, as the
4448 * get_properties ioctl locks all modesetting objects, and
4449 * obj_holds_id must be locked before calling here, so we cannot
4450 * have its value out of sync with the list membership modified
4451 * below under blob_lock. */
4452 if (obj_holds_id) {
4453 ret = drm_object_property_set_value(obj_holds_id,
4454 prop_holds_id,
4455 new_blob ?
4456 new_blob->base.id : 0);
4457 if (ret != 0)
4458 goto err_created;
4459 }
4460
Markus Elfringec530822015-07-05 21:55:10 +02004461 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004462 *replace = new_blob;
4463
4464 return 0;
4465
4466err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004467 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004468 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004469}
4470
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004471/**
4472 * drm_mode_getblob_ioctl - get the contents of a blob property value
4473 * @dev: DRM device
4474 * @data: ioctl data
4475 * @file_priv: DRM file info
4476 *
4477 * This function retrieves the contents of a blob property. The value stored in
4478 * an object's blob property is just a normal modeset object id.
4479 *
4480 * Called by the user via ioctl.
4481 *
4482 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004483 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004484 */
Dave Airlief453ba02008-11-07 14:05:41 -08004485int drm_mode_getblob_ioctl(struct drm_device *dev,
4486 void *data, struct drm_file *file_priv)
4487{
Dave Airlief453ba02008-11-07 14:05:41 -08004488 struct drm_mode_get_blob *out_resp = data;
4489 struct drm_property_blob *blob;
4490 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004491 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004492
Dave Airliefb3b06c2011-02-08 13:55:21 +10004493 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4494 return -EINVAL;
4495
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004496 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4497 if (!blob)
4498 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004499
4500 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004501 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004502 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004503 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004504 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004505 }
4506 }
4507 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004508unref:
4509 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004510
Dave Airlief453ba02008-11-07 14:05:41 -08004511 return ret;
4512}
4513
Dave Airliecc7096f2014-10-22 12:03:04 +10004514/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004515 * drm_mode_createblob_ioctl - create a new blob property
4516 * @dev: DRM device
4517 * @data: ioctl data
4518 * @file_priv: DRM file info
4519 *
4520 * This function creates a new blob property with user-defined values. In order
4521 * to give us sensible validation and checking when creating, rather than at
4522 * every potential use, we also require a type to be provided upfront.
4523 *
4524 * Called by the user via ioctl.
4525 *
4526 * Returns:
4527 * Zero on success, negative errno on failure.
4528 */
4529int drm_mode_createblob_ioctl(struct drm_device *dev,
4530 void *data, struct drm_file *file_priv)
4531{
4532 struct drm_mode_create_blob *out_resp = data;
4533 struct drm_property_blob *blob;
4534 void __user *blob_ptr;
4535 int ret = 0;
4536
4537 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4538 return -EINVAL;
4539
4540 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4541 if (IS_ERR(blob))
4542 return PTR_ERR(blob);
4543
4544 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4545 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4546 ret = -EFAULT;
4547 goto out_blob;
4548 }
4549
4550 /* Dropping the lock between create_blob and our access here is safe
4551 * as only the same file_priv can remove the blob; at this point, it is
4552 * not associated with any file_priv. */
4553 mutex_lock(&dev->mode_config.blob_lock);
4554 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004555 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004556 mutex_unlock(&dev->mode_config.blob_lock);
4557
4558 return 0;
4559
4560out_blob:
4561 drm_property_unreference_blob(blob);
4562 return ret;
4563}
4564
4565/**
4566 * drm_mode_destroyblob_ioctl - destroy a user blob property
4567 * @dev: DRM device
4568 * @data: ioctl data
4569 * @file_priv: DRM file info
4570 *
4571 * Destroy an existing user-defined blob property.
4572 *
4573 * Called by the user via ioctl.
4574 *
4575 * Returns:
4576 * Zero on success, negative errno on failure.
4577 */
4578int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4579 void *data, struct drm_file *file_priv)
4580{
4581 struct drm_mode_destroy_blob *out_resp = data;
4582 struct drm_property_blob *blob = NULL, *bt;
4583 bool found = false;
4584 int ret = 0;
4585
4586 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4587 return -EINVAL;
4588
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004589 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4590 if (!blob)
4591 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004592
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004593 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004594 /* Ensure the property was actually created by this user. */
4595 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4596 if (bt == blob) {
4597 found = true;
4598 break;
4599 }
4600 }
4601
4602 if (!found) {
4603 ret = -EPERM;
4604 goto err;
4605 }
4606
4607 /* We must drop head_file here, because we may not be the last
4608 * reference on the blob. */
4609 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004610 mutex_unlock(&dev->mode_config.blob_lock);
4611
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004612 /* One reference from lookup, and one from the filp. */
4613 drm_property_unreference_blob(blob);
4614 drm_property_unreference_blob(blob);
4615
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004616 return 0;
4617
4618err:
4619 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004620 drm_property_unreference_blob(blob);
4621
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004622 return ret;
4623}
4624
4625/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004626 * drm_mode_connector_set_path_property - set tile property on connector
4627 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004628 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004629 *
4630 * This creates a property to expose to userspace to specify a
4631 * connector path. This is mainly used for DisplayPort MST where
4632 * connectors have a topology and we want to allow userspace to give
4633 * them more meaningful names.
4634 *
4635 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004636 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004637 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004638int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004639 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004640{
4641 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004642 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004643
Daniel Stoned2ed3432015-04-20 19:22:53 +01004644 ret = drm_property_replace_global_blob(dev,
4645 &connector->path_blob_ptr,
4646 strlen(path) + 1,
4647 path,
4648 &connector->base,
4649 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004650 return ret;
4651}
4652EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4653
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004654/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004655 * drm_mode_connector_set_tile_property - set tile property on connector
4656 * @connector: connector to set property on.
4657 *
4658 * This looks up the tile information for a connector, and creates a
4659 * property for userspace to parse if it exists. The property is of
4660 * the form of 8 integers using ':' as a separator.
4661 *
4662 * Returns:
4663 * Zero on success, errno on failure.
4664 */
4665int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4666{
4667 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004668 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004669 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004670
4671 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004672 ret = drm_property_replace_global_blob(dev,
4673 &connector->tile_blob_ptr,
4674 0,
4675 NULL,
4676 &connector->base,
4677 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004678 return ret;
4679 }
4680
4681 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4682 connector->tile_group->id, connector->tile_is_single_monitor,
4683 connector->num_h_tile, connector->num_v_tile,
4684 connector->tile_h_loc, connector->tile_v_loc,
4685 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004686
Daniel Stoned2ed3432015-04-20 19:22:53 +01004687 ret = drm_property_replace_global_blob(dev,
4688 &connector->tile_blob_ptr,
4689 strlen(tile) + 1,
4690 tile,
4691 &connector->base,
4692 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004693 return ret;
4694}
4695EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4696
4697/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004698 * drm_mode_connector_update_edid_property - update the edid property of a connector
4699 * @connector: drm connector
4700 * @edid: new value of the edid property
4701 *
4702 * This function creates a new blob modeset object and assigns its id to the
4703 * connector's edid property.
4704 *
4705 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004706 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004707 */
Dave Airlief453ba02008-11-07 14:05:41 -08004708int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004709 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004710{
4711 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004712 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004713 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004714
Thomas Wood4cf2b282014-06-18 17:52:33 +01004715 /* ignore requests to set edid when overridden */
4716 if (connector->override_edid)
4717 return 0;
4718
Daniel Stoned2ed3432015-04-20 19:22:53 +01004719 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004720 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004721
Daniel Stoned2ed3432015-04-20 19:22:53 +01004722 ret = drm_property_replace_global_blob(dev,
4723 &connector->edid_blob_ptr,
4724 size,
4725 edid,
4726 &connector->base,
4727 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004728 return ret;
4729}
4730EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4731
Rob Clark3843e712014-12-16 18:05:29 -05004732/* Some properties could refer to dynamic refcnt'd objects, or things that
4733 * need special locking to handle lifetime issues (ie. to ensure the prop
4734 * value doesn't become invalid part way through the property update due to
4735 * race). The value returned by reference via 'obj' should be passed back
4736 * to drm_property_change_valid_put() after the property is set (and the
4737 * object to which the property is attached has a chance to take it's own
4738 * reference).
4739 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004740bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004741 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004742{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004743 int i;
4744
Paulo Zanoni26a34812012-05-15 18:08:59 -03004745 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4746 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004747
Rob Clark3843e712014-12-16 18:05:29 -05004748 *ref = NULL;
4749
Rob Clark5ea22f22014-05-30 11:34:01 -04004750 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004751 if (value < property->values[0] || value > property->values[1])
4752 return false;
4753 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004754 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4755 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004756
Rob Clarkebc44cf2012-09-12 22:22:31 -05004757 if (svalue < U642I64(property->values[0]) ||
4758 svalue > U642I64(property->values[1]))
4759 return false;
4760 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004761 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004762 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004763
Rob Clark49e27542012-05-17 02:23:26 -06004764 for (i = 0; i < property->num_values; i++)
4765 valid_mask |= (1ULL << property->values[i]);
4766 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004767 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004768 struct drm_property_blob *blob;
4769
4770 if (value == 0)
4771 return true;
4772
4773 blob = drm_property_lookup_blob(property->dev, value);
4774 if (blob) {
4775 *ref = &blob->base;
4776 return true;
4777 } else {
4778 return false;
4779 }
Rob Clark98f75de2014-05-30 11:37:03 -04004780 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004781 /* a zero value for an object property translates to null: */
4782 if (value == 0)
4783 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004784
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004785 return _object_find(property->dev, value, property->values[0]) != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004786 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004787
4788 for (i = 0; i < property->num_values; i++)
4789 if (property->values[i] == value)
4790 return true;
4791 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004792}
4793
Rob Clarkd34f20d2014-12-18 16:01:56 -05004794void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004795 struct drm_mode_object *ref)
4796{
4797 if (!ref)
4798 return;
4799
4800 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004801 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004802 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4803 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004804}
4805
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004806/**
4807 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4808 * @dev: DRM device
4809 * @data: ioctl data
4810 * @file_priv: DRM file info
4811 *
4812 * This function sets the current value for a connectors's property. It also
4813 * calls into a driver's ->set_property callback to update the hardware state
4814 *
4815 * Called by the user via ioctl.
4816 *
4817 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004818 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004819 */
Dave Airlief453ba02008-11-07 14:05:41 -08004820int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4821 void *data, struct drm_file *file_priv)
4822{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004823 struct drm_mode_connector_set_property *conn_set_prop = data;
4824 struct drm_mode_obj_set_property obj_set_prop = {
4825 .value = conn_set_prop->value,
4826 .prop_id = conn_set_prop->prop_id,
4827 .obj_id = conn_set_prop->connector_id,
4828 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4829 };
Dave Airlief453ba02008-11-07 14:05:41 -08004830
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004831 /* It does all the locking and checking we need */
4832 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004833}
4834
Paulo Zanonic5431882012-05-15 18:09:02 -03004835static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4836 struct drm_property *property,
4837 uint64_t value)
4838{
4839 int ret = -EINVAL;
4840 struct drm_connector *connector = obj_to_connector(obj);
4841
4842 /* Do DPMS ourselves */
4843 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004844 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004845 } else if (connector->funcs->set_property)
4846 ret = connector->funcs->set_property(connector, property, value);
4847
4848 /* store the property value if successful */
4849 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004850 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004851 return ret;
4852}
4853
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004854static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4855 struct drm_property *property,
4856 uint64_t value)
4857{
4858 int ret = -EINVAL;
4859 struct drm_crtc *crtc = obj_to_crtc(obj);
4860
4861 if (crtc->funcs->set_property)
4862 ret = crtc->funcs->set_property(crtc, property, value);
4863 if (!ret)
4864 drm_object_property_set_value(obj, property, value);
4865
4866 return ret;
4867}
4868
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004869/**
4870 * drm_mode_plane_set_obj_prop - set the value of a property
4871 * @plane: drm plane object to set property value for
4872 * @property: property to set
4873 * @value: value the property should be set to
4874 *
4875 * This functions sets a given property on a given plane object. This function
4876 * calls the driver's ->set_property callback and changes the software state of
4877 * the property if the callback succeeds.
4878 *
4879 * Returns:
4880 * Zero on success, error code on failure.
4881 */
4882int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4883 struct drm_property *property,
4884 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004885{
4886 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004887 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004888
4889 if (plane->funcs->set_property)
4890 ret = plane->funcs->set_property(plane, property, value);
4891 if (!ret)
4892 drm_object_property_set_value(obj, property, value);
4893
4894 return ret;
4895}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004896EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004897
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004898/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004899 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004900 * @dev: DRM device
4901 * @data: ioctl data
4902 * @file_priv: DRM file info
4903 *
4904 * This function retrieves the current value for an object's property. Compared
4905 * to the connector specific ioctl this one is extended to also work on crtc and
4906 * plane objects.
4907 *
4908 * Called by the user via ioctl.
4909 *
4910 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004911 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004912 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004913int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4914 struct drm_file *file_priv)
4915{
4916 struct drm_mode_obj_get_properties *arg = data;
4917 struct drm_mode_object *obj;
4918 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004919
4920 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4921 return -EINVAL;
4922
Daniel Vetter84849902012-12-02 00:28:11 +01004923 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004924
4925 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4926 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004927 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004928 goto out;
4929 }
4930 if (!obj->properties) {
4931 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004932 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004933 }
4934
Rob Clark88a48e22014-12-18 16:01:50 -05004935 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004936 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4937 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4938 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004939
Daniel Vetter1649c332016-04-22 22:10:28 +02004940out_unref:
4941 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004942out:
Daniel Vetter84849902012-12-02 00:28:11 +01004943 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004944 return ret;
4945}
4946
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004947/**
4948 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4949 * @dev: DRM device
4950 * @data: ioctl data
4951 * @file_priv: DRM file info
4952 *
4953 * This function sets the current value for an object's property. It also calls
4954 * into a driver's ->set_property callback to update the hardware state.
4955 * Compared to the connector specific ioctl this one is extended to also work on
4956 * crtc and plane objects.
4957 *
4958 * Called by the user via ioctl.
4959 *
4960 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004961 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004962 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004963int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4964 struct drm_file *file_priv)
4965{
4966 struct drm_mode_obj_set_property *arg = data;
4967 struct drm_mode_object *arg_obj;
4968 struct drm_mode_object *prop_obj;
4969 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004970 int i, ret = -EINVAL;
4971 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004972
4973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4974 return -EINVAL;
4975
Daniel Vetter84849902012-12-02 00:28:11 +01004976 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004977
4978 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004979 if (!arg_obj) {
4980 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004981 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004982 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004983 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02004984 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004985
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004986 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004987 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004988 break;
4989
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004990 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02004991 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004992
4993 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4994 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004995 if (!prop_obj) {
4996 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02004997 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004998 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004999 property = obj_to_property(prop_obj);
5000
Rob Clark3843e712014-12-16 18:05:29 -05005001 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005002 goto out;
5003
5004 switch (arg_obj->type) {
5005 case DRM_MODE_OBJECT_CONNECTOR:
5006 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5007 arg->value);
5008 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005009 case DRM_MODE_OBJECT_CRTC:
5010 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5011 break;
Rob Clark4d939142012-05-17 02:23:27 -06005012 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005013 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5014 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005015 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005016 }
5017
Rob Clark3843e712014-12-16 18:05:29 -05005018 drm_property_change_valid_put(property, ref);
5019
Daniel Vetter1649c332016-04-22 22:10:28 +02005020out_unref:
5021 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005022out:
Daniel Vetter84849902012-12-02 00:28:11 +01005023 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005024 return ret;
5025}
5026
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005027/**
5028 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5029 * @connector: connector to attach
5030 * @encoder: encoder to attach @connector to
5031 *
5032 * This function links up a connector to an encoder. Note that the routing
5033 * restrictions between encoders and crtcs are exposed to userspace through the
5034 * possible_clones and possible_crtcs bitmasks.
5035 *
5036 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005037 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005038 */
Dave Airlief453ba02008-11-07 14:05:41 -08005039int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5040 struct drm_encoder *encoder)
5041{
5042 int i;
5043
Thierry Redingeb47fe82015-11-16 18:19:53 +01005044 /*
5045 * In the past, drivers have attempted to model the static association
5046 * of connector to encoder in simple connector/encoder devices using a
5047 * direct assignment of connector->encoder = encoder. This connection
5048 * is a logical one and the responsibility of the core, so drivers are
5049 * expected not to mess with this.
5050 *
5051 * Note that the error return should've been enough here, but a large
5052 * majority of drivers ignores the return value, so add in a big WARN
5053 * to get people's attention.
5054 */
5055 if (WARN_ON(connector->encoder))
5056 return -EINVAL;
5057
Dave Airlief453ba02008-11-07 14:05:41 -08005058 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5059 if (connector->encoder_ids[i] == 0) {
5060 connector->encoder_ids[i] = encoder->base.id;
5061 return 0;
5062 }
5063 }
5064 return -ENOMEM;
5065}
5066EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5067
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005068/**
5069 * drm_mode_crtc_set_gamma_size - set the gamma table size
5070 * @crtc: CRTC to set the gamma table size for
5071 * @gamma_size: size of the gamma table
5072 *
5073 * Drivers which support gamma tables should set this to the supported gamma
5074 * table size when initializing the CRTC. Currently the drm core only supports a
5075 * fixed gamma table size.
5076 *
5077 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005078 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005079 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005080int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005081 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005082{
5083 crtc->gamma_size = gamma_size;
5084
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005085 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5086 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005087 if (!crtc->gamma_store) {
5088 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005089 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005090 }
5091
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005092 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005093}
5094EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005096/**
5097 * drm_mode_gamma_set_ioctl - set the gamma table
5098 * @dev: DRM device
5099 * @data: ioctl data
5100 * @file_priv: DRM file info
5101 *
5102 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5103 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5104 *
5105 * Called by the user via ioctl.
5106 *
5107 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005108 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005109 */
Dave Airlief453ba02008-11-07 14:05:41 -08005110int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5111 void *data, struct drm_file *file_priv)
5112{
5113 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005114 struct drm_crtc *crtc;
5115 void *r_base, *g_base, *b_base;
5116 int size;
5117 int ret = 0;
5118
Dave Airliefb3b06c2011-02-08 13:55:21 +10005119 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5120 return -EINVAL;
5121
Daniel Vetter84849902012-12-02 00:28:11 +01005122 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005123 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5124 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005125 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005126 goto out;
5127 }
Dave Airlief453ba02008-11-07 14:05:41 -08005128
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005129 if (crtc->funcs->gamma_set == NULL) {
5130 ret = -ENOSYS;
5131 goto out;
5132 }
5133
Dave Airlief453ba02008-11-07 14:05:41 -08005134 /* memcpy into gamma store */
5135 if (crtc_lut->gamma_size != crtc->gamma_size) {
5136 ret = -EINVAL;
5137 goto out;
5138 }
5139
5140 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5141 r_base = crtc->gamma_store;
5142 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5143 ret = -EFAULT;
5144 goto out;
5145 }
5146
5147 g_base = r_base + size;
5148 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5149 ret = -EFAULT;
5150 goto out;
5151 }
5152
5153 b_base = g_base + size;
5154 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5155 ret = -EFAULT;
5156 goto out;
5157 }
5158
James Simmons72034252010-08-03 01:33:19 +01005159 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005160
5161out:
Daniel Vetter84849902012-12-02 00:28:11 +01005162 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005163 return ret;
5164
5165}
5166
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005167/**
5168 * drm_mode_gamma_get_ioctl - get the gamma table
5169 * @dev: DRM device
5170 * @data: ioctl data
5171 * @file_priv: DRM file info
5172 *
5173 * Copy the current gamma table into the storage provided. This also provides
5174 * the gamma table size the driver expects, which can be used to size the
5175 * allocated storage.
5176 *
5177 * Called by the user via ioctl.
5178 *
5179 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005180 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005181 */
Dave Airlief453ba02008-11-07 14:05:41 -08005182int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5183 void *data, struct drm_file *file_priv)
5184{
5185 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005186 struct drm_crtc *crtc;
5187 void *r_base, *g_base, *b_base;
5188 int size;
5189 int ret = 0;
5190
Dave Airliefb3b06c2011-02-08 13:55:21 +10005191 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5192 return -EINVAL;
5193
Daniel Vetter84849902012-12-02 00:28:11 +01005194 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005195 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5196 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005197 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005198 goto out;
5199 }
Dave Airlief453ba02008-11-07 14:05:41 -08005200
5201 /* memcpy into gamma store */
5202 if (crtc_lut->gamma_size != crtc->gamma_size) {
5203 ret = -EINVAL;
5204 goto out;
5205 }
5206
5207 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5208 r_base = crtc->gamma_store;
5209 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5210 ret = -EFAULT;
5211 goto out;
5212 }
5213
5214 g_base = r_base + size;
5215 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5216 ret = -EFAULT;
5217 goto out;
5218 }
5219
5220 b_base = g_base + size;
5221 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5222 ret = -EFAULT;
5223 goto out;
5224 }
5225out:
Daniel Vetter84849902012-12-02 00:28:11 +01005226 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005227 return ret;
5228}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005229
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005230/**
5231 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5232 * @dev: DRM device
5233 * @data: ioctl data
5234 * @file_priv: DRM file info
5235 *
5236 * This schedules an asynchronous update on a given CRTC, called page flip.
5237 * Optionally a drm event is generated to signal the completion of the event.
5238 * Generic drivers cannot assume that a pageflip with changed framebuffer
5239 * properties (including driver specific metadata like tiling layout) will work,
5240 * but some drivers support e.g. pixel format changes through the pageflip
5241 * ioctl.
5242 *
5243 * Called by the user via ioctl.
5244 *
5245 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005246 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005247 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005248int drm_mode_page_flip_ioctl(struct drm_device *dev,
5249 void *data, struct drm_file *file_priv)
5250{
5251 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005252 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005253 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005254 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005255 int ret = -EINVAL;
5256
5257 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5258 page_flip->reserved != 0)
5259 return -EINVAL;
5260
Keith Packard62f21042013-07-22 18:50:00 -07005261 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5262 return -EINVAL;
5263
Rob Clarka2b34e22013-10-05 16:36:52 -04005264 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5265 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005266 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005267
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005268 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005269 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005270 /* The framebuffer is currently unbound, presumably
5271 * due to a hotplug event, that userspace has not
5272 * yet discovered.
5273 */
5274 ret = -EBUSY;
5275 goto out;
5276 }
5277
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005278 if (crtc->funcs->page_flip == NULL)
5279 goto out;
5280
Daniel Vetter786b99e2012-12-02 21:53:40 +01005281 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005282 if (!fb) {
5283 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005284 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005285 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005286
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005287 if (crtc->state) {
5288 const struct drm_plane_state *state = crtc->primary->state;
5289
5290 ret = check_src_coords(state->src_x, state->src_y,
5291 state->src_w, state->src_h, fb);
5292 } else {
5293 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5294 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005295 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005296 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005297
Matt Roperf4510a22014-04-01 15:22:40 -07005298 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005299 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5300 ret = -EINVAL;
5301 goto out;
5302 }
5303
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005304 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005305 e = kzalloc(sizeof *e, GFP_KERNEL);
5306 if (!e) {
5307 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005308 goto out;
5309 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005310 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005311 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005312 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005313 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5314 if (ret) {
5315 kfree(e);
5316 goto out;
5317 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005318 }
5319
Daniel Vetter3d30a592014-07-27 13:42:42 +02005320 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005321 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005322 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005323 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5324 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005325 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005326 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005327 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005328 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005329 /* Unref only the old framebuffer. */
5330 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005331 }
5332
5333out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005334 if (fb)
5335 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005336 if (crtc->primary->old_fb)
5337 drm_framebuffer_unreference(crtc->primary->old_fb);
5338 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005339 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005340
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005341 return ret;
5342}
Chris Wilsoneb033552011-01-24 15:11:08 +00005343
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005344/**
5345 * drm_mode_config_reset - call ->reset callbacks
5346 * @dev: drm device
5347 *
5348 * This functions calls all the crtc's, encoder's and connector's ->reset
5349 * callback. Drivers can use this in e.g. their driver load or resume code to
5350 * reset hardware and software state.
5351 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005352void drm_mode_config_reset(struct drm_device *dev)
5353{
5354 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005355 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005356 struct drm_encoder *encoder;
5357 struct drm_connector *connector;
5358
Daniel Vettere4f62542015-07-09 23:44:35 +02005359 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005360 if (plane->funcs->reset)
5361 plane->funcs->reset(plane);
5362
Daniel Vettere4f62542015-07-09 23:44:35 +02005363 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005364 if (crtc->funcs->reset)
5365 crtc->funcs->reset(crtc);
5366
Daniel Vettere4f62542015-07-09 23:44:35 +02005367 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005368 if (encoder->funcs->reset)
5369 encoder->funcs->reset(encoder);
5370
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005371 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005372 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005373 if (connector->funcs->reset)
5374 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005375 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005376}
5377EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005378
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005379/**
5380 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5381 * @dev: DRM device
5382 * @data: ioctl data
5383 * @file_priv: DRM file info
5384 *
5385 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5386 * TTM or something else entirely) and returns the resulting buffer handle. This
5387 * handle can then be wrapped up into a framebuffer modeset object.
5388 *
5389 * Note that userspace is not allowed to use such objects for render
5390 * acceleration - drivers must create their own private ioctls for such a use
5391 * case.
5392 *
5393 * Called by the user via ioctl.
5394 *
5395 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005396 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005397 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005398int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5399 void *data, struct drm_file *file_priv)
5400{
5401 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005402 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005403
5404 if (!dev->driver->dumb_create)
5405 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005406 if (!args->width || !args->height || !args->bpp)
5407 return -EINVAL;
5408
5409 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005410 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005411 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005412 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005413 return -EINVAL;
5414 stride = cpp * args->width;
5415 if (args->height > 0xffffffffU / stride)
5416 return -EINVAL;
5417
5418 /* test for wrap-around */
5419 size = args->height * stride;
5420 if (PAGE_ALIGN(size) == 0)
5421 return -EINVAL;
5422
Thierry Redingf6085952014-11-03 11:14:14 +01005423 /*
5424 * handle, pitch and size are output parameters. Zero them out to
5425 * prevent drivers from accidentally using uninitialized data. Since
5426 * not all existing userspace is clearing these fields properly we
5427 * cannot reject IOCTL with garbage in them.
5428 */
5429 args->handle = 0;
5430 args->pitch = 0;
5431 args->size = 0;
5432
Dave Airlieff72145b2011-02-07 12:16:14 +10005433 return dev->driver->dumb_create(file_priv, dev, args);
5434}
5435
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005436/**
5437 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5438 * @dev: DRM device
5439 * @data: ioctl data
5440 * @file_priv: DRM file info
5441 *
5442 * Allocate an offset in the drm device node's address space to be able to
5443 * memory map a dumb buffer.
5444 *
5445 * Called by the user via ioctl.
5446 *
5447 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005448 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005449 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005450int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5451 void *data, struct drm_file *file_priv)
5452{
5453 struct drm_mode_map_dumb *args = data;
5454
5455 /* call driver ioctl to get mmap offset */
5456 if (!dev->driver->dumb_map_offset)
5457 return -ENOSYS;
5458
5459 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5460}
5461
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005462/**
5463 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5464 * @dev: DRM device
5465 * @data: ioctl data
5466 * @file_priv: DRM file info
5467 *
5468 * This destroys the userspace handle for the given dumb backing storage buffer.
5469 * Since buffer objects must be reference counted in the kernel a buffer object
5470 * won't be immediately freed if a framebuffer modeset object still uses it.
5471 *
5472 * Called by the user via ioctl.
5473 *
5474 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005475 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005476 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005477int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5478 void *data, struct drm_file *file_priv)
5479{
5480 struct drm_mode_destroy_dumb *args = data;
5481
5482 if (!dev->driver->dumb_destroy)
5483 return -ENOSYS;
5484
5485 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5486}
Dave Airlie248dbc22011-11-29 20:02:54 +00005487
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005488/**
5489 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5490 * @format: pixel format (DRM_FORMAT_*)
5491 * @depth: storage for the depth value
5492 * @bpp: storage for the bpp value
5493 *
5494 * This only supports RGB formats here for compat with code that doesn't use
5495 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005496 */
5497void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5498 int *bpp)
5499{
5500 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005501 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005502 case DRM_FORMAT_RGB332:
5503 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005504 *depth = 8;
5505 *bpp = 8;
5506 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005507 case DRM_FORMAT_XRGB1555:
5508 case DRM_FORMAT_XBGR1555:
5509 case DRM_FORMAT_RGBX5551:
5510 case DRM_FORMAT_BGRX5551:
5511 case DRM_FORMAT_ARGB1555:
5512 case DRM_FORMAT_ABGR1555:
5513 case DRM_FORMAT_RGBA5551:
5514 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005515 *depth = 15;
5516 *bpp = 16;
5517 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005518 case DRM_FORMAT_RGB565:
5519 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005520 *depth = 16;
5521 *bpp = 16;
5522 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005523 case DRM_FORMAT_RGB888:
5524 case DRM_FORMAT_BGR888:
5525 *depth = 24;
5526 *bpp = 24;
5527 break;
5528 case DRM_FORMAT_XRGB8888:
5529 case DRM_FORMAT_XBGR8888:
5530 case DRM_FORMAT_RGBX8888:
5531 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005532 *depth = 24;
5533 *bpp = 32;
5534 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005535 case DRM_FORMAT_XRGB2101010:
5536 case DRM_FORMAT_XBGR2101010:
5537 case DRM_FORMAT_RGBX1010102:
5538 case DRM_FORMAT_BGRX1010102:
5539 case DRM_FORMAT_ARGB2101010:
5540 case DRM_FORMAT_ABGR2101010:
5541 case DRM_FORMAT_RGBA1010102:
5542 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005543 *depth = 30;
5544 *bpp = 32;
5545 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005546 case DRM_FORMAT_ARGB8888:
5547 case DRM_FORMAT_ABGR8888:
5548 case DRM_FORMAT_RGBA8888:
5549 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005550 *depth = 32;
5551 *bpp = 32;
5552 break;
5553 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005554 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5555 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005556 *depth = 0;
5557 *bpp = 0;
5558 break;
5559 }
5560}
5561EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005562
5563/**
5564 * drm_format_num_planes - get the number of planes for format
5565 * @format: pixel format (DRM_FORMAT_*)
5566 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005567 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005568 * The number of planes used by the specified pixel format.
5569 */
5570int drm_format_num_planes(uint32_t format)
5571{
5572 switch (format) {
5573 case DRM_FORMAT_YUV410:
5574 case DRM_FORMAT_YVU410:
5575 case DRM_FORMAT_YUV411:
5576 case DRM_FORMAT_YVU411:
5577 case DRM_FORMAT_YUV420:
5578 case DRM_FORMAT_YVU420:
5579 case DRM_FORMAT_YUV422:
5580 case DRM_FORMAT_YVU422:
5581 case DRM_FORMAT_YUV444:
5582 case DRM_FORMAT_YVU444:
5583 return 3;
5584 case DRM_FORMAT_NV12:
5585 case DRM_FORMAT_NV21:
5586 case DRM_FORMAT_NV16:
5587 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005588 case DRM_FORMAT_NV24:
5589 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005590 return 2;
5591 default:
5592 return 1;
5593 }
5594}
5595EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005596
5597/**
5598 * drm_format_plane_cpp - determine the bytes per pixel value
5599 * @format: pixel format (DRM_FORMAT_*)
5600 * @plane: plane index
5601 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005602 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005603 * The bytes per pixel value for the specified plane.
5604 */
5605int drm_format_plane_cpp(uint32_t format, int plane)
5606{
5607 unsigned int depth;
5608 int bpp;
5609
5610 if (plane >= drm_format_num_planes(format))
5611 return 0;
5612
5613 switch (format) {
5614 case DRM_FORMAT_YUYV:
5615 case DRM_FORMAT_YVYU:
5616 case DRM_FORMAT_UYVY:
5617 case DRM_FORMAT_VYUY:
5618 return 2;
5619 case DRM_FORMAT_NV12:
5620 case DRM_FORMAT_NV21:
5621 case DRM_FORMAT_NV16:
5622 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005623 case DRM_FORMAT_NV24:
5624 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005625 return plane ? 2 : 1;
5626 case DRM_FORMAT_YUV410:
5627 case DRM_FORMAT_YVU410:
5628 case DRM_FORMAT_YUV411:
5629 case DRM_FORMAT_YVU411:
5630 case DRM_FORMAT_YUV420:
5631 case DRM_FORMAT_YVU420:
5632 case DRM_FORMAT_YUV422:
5633 case DRM_FORMAT_YVU422:
5634 case DRM_FORMAT_YUV444:
5635 case DRM_FORMAT_YVU444:
5636 return 1;
5637 default:
5638 drm_fb_get_bpp_depth(format, &depth, &bpp);
5639 return bpp >> 3;
5640 }
5641}
5642EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005643
5644/**
5645 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5646 * @format: pixel format (DRM_FORMAT_*)
5647 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005648 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005649 * The horizontal chroma subsampling factor for the
5650 * specified pixel format.
5651 */
5652int drm_format_horz_chroma_subsampling(uint32_t format)
5653{
5654 switch (format) {
5655 case DRM_FORMAT_YUV411:
5656 case DRM_FORMAT_YVU411:
5657 case DRM_FORMAT_YUV410:
5658 case DRM_FORMAT_YVU410:
5659 return 4;
5660 case DRM_FORMAT_YUYV:
5661 case DRM_FORMAT_YVYU:
5662 case DRM_FORMAT_UYVY:
5663 case DRM_FORMAT_VYUY:
5664 case DRM_FORMAT_NV12:
5665 case DRM_FORMAT_NV21:
5666 case DRM_FORMAT_NV16:
5667 case DRM_FORMAT_NV61:
5668 case DRM_FORMAT_YUV422:
5669 case DRM_FORMAT_YVU422:
5670 case DRM_FORMAT_YUV420:
5671 case DRM_FORMAT_YVU420:
5672 return 2;
5673 default:
5674 return 1;
5675 }
5676}
5677EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5678
5679/**
5680 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5681 * @format: pixel format (DRM_FORMAT_*)
5682 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005683 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005684 * The vertical chroma subsampling factor for the
5685 * specified pixel format.
5686 */
5687int drm_format_vert_chroma_subsampling(uint32_t format)
5688{
5689 switch (format) {
5690 case DRM_FORMAT_YUV410:
5691 case DRM_FORMAT_YVU410:
5692 return 4;
5693 case DRM_FORMAT_YUV420:
5694 case DRM_FORMAT_YVU420:
5695 case DRM_FORMAT_NV12:
5696 case DRM_FORMAT_NV21:
5697 return 2;
5698 default:
5699 return 1;
5700 }
5701}
5702EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005703
5704/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005705 * drm_format_plane_width - width of the plane given the first plane
5706 * @width: width of the first plane
5707 * @format: pixel format
5708 * @plane: plane index
5709 *
5710 * Returns:
5711 * The width of @plane, given that the width of the first plane is @width.
5712 */
5713int drm_format_plane_width(int width, uint32_t format, int plane)
5714{
5715 if (plane >= drm_format_num_planes(format))
5716 return 0;
5717
5718 if (plane == 0)
5719 return width;
5720
5721 return width / drm_format_horz_chroma_subsampling(format);
5722}
5723EXPORT_SYMBOL(drm_format_plane_width);
5724
5725/**
5726 * drm_format_plane_height - height of the plane given the first plane
5727 * @height: height of the first plane
5728 * @format: pixel format
5729 * @plane: plane index
5730 *
5731 * Returns:
5732 * The height of @plane, given that the height of the first plane is @height.
5733 */
5734int drm_format_plane_height(int height, uint32_t format, int plane)
5735{
5736 if (plane >= drm_format_num_planes(format))
5737 return 0;
5738
5739 if (plane == 0)
5740 return height;
5741
5742 return height / drm_format_vert_chroma_subsampling(format);
5743}
5744EXPORT_SYMBOL(drm_format_plane_height);
5745
5746/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305747 * drm_rotation_simplify() - Try to simplify the rotation
5748 * @rotation: Rotation to be simplified
5749 * @supported_rotations: Supported rotations
5750 *
5751 * Attempt to simplify the rotation to a form that is supported.
5752 * Eg. if the hardware supports everything except DRM_REFLECT_X
5753 * one could call this function like this:
5754 *
5755 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5756 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5757 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5758 *
5759 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5760 * transforms the hardware supports, this function may not
5761 * be able to produce a supported transform, so the caller should
5762 * check the result afterwards.
5763 */
5764unsigned int drm_rotation_simplify(unsigned int rotation,
5765 unsigned int supported_rotations)
5766{
5767 if (rotation & ~supported_rotations) {
5768 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005769 rotation = (rotation & DRM_REFLECT_MASK) |
5770 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305771 }
5772
5773 return rotation;
5774}
5775EXPORT_SYMBOL(drm_rotation_simplify);
5776
5777/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005778 * drm_mode_config_init - initialize DRM mode_configuration structure
5779 * @dev: DRM device
5780 *
5781 * Initialize @dev's mode_config structure, used for tracking the graphics
5782 * configuration of @dev.
5783 *
5784 * Since this initializes the modeset locks, no locking is possible. Which is no
5785 * problem, since this should happen single threaded at init time. It is the
5786 * driver's problem to ensure this guarantee.
5787 *
5788 */
5789void drm_mode_config_init(struct drm_device *dev)
5790{
5791 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005792 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005793 mutex_init(&dev->mode_config.idr_mutex);
5794 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005795 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005796 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5797 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5798 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5799 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5800 INIT_LIST_HEAD(&dev->mode_config.property_list);
5801 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5802 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5803 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005804 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005805 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005806
5807 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005808 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005809 drm_modeset_unlock_all(dev);
5810
5811 /* Just to be sure */
5812 dev->mode_config.num_fb = 0;
5813 dev->mode_config.num_connector = 0;
5814 dev->mode_config.num_crtc = 0;
5815 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005816 dev->mode_config.num_overlay_plane = 0;
5817 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005818}
5819EXPORT_SYMBOL(drm_mode_config_init);
5820
5821/**
5822 * drm_mode_config_cleanup - free up DRM mode_config info
5823 * @dev: DRM device
5824 *
5825 * Free up all the connectors and CRTCs associated with this DRM device, then
5826 * free up the framebuffers and associated buffer objects.
5827 *
5828 * Note that since this /should/ happen single-threaded at driver/device
5829 * teardown time, no locking is required. It's the driver's job to ensure that
5830 * this guarantee actually holds true.
5831 *
5832 * FIXME: cleanup any dangling user buffer objects too
5833 */
5834void drm_mode_config_cleanup(struct drm_device *dev)
5835{
5836 struct drm_connector *connector, *ot;
5837 struct drm_crtc *crtc, *ct;
5838 struct drm_encoder *encoder, *enct;
5839 struct drm_framebuffer *fb, *fbt;
5840 struct drm_property *property, *pt;
5841 struct drm_property_blob *blob, *bt;
5842 struct drm_plane *plane, *plt;
5843
5844 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5845 head) {
5846 encoder->funcs->destroy(encoder);
5847 }
5848
5849 list_for_each_entry_safe(connector, ot,
5850 &dev->mode_config.connector_list, head) {
5851 connector->funcs->destroy(connector);
5852 }
5853
5854 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5855 head) {
5856 drm_property_destroy(dev, property);
5857 }
5858
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005859 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5860 head) {
5861 plane->funcs->destroy(plane);
5862 }
5863
5864 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5865 crtc->funcs->destroy(crtc);
5866 }
5867
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005868 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005869 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005870 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005871 }
5872
5873 /*
5874 * Single-threaded teardown context, so it's not required to grab the
5875 * fb_lock to protect against concurrent fb_list access. Contrary, it
5876 * would actually deadlock with the drm_framebuffer_cleanup function.
5877 *
5878 * Also, if there are any framebuffers left, that's a driver leak now,
5879 * so politely WARN about this.
5880 */
5881 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5882 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005883 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005884 }
5885
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005886 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005887 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005888 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005889 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005890}
5891EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305892
5893struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5894 unsigned int supported_rotations)
5895{
5896 static const struct drm_prop_enum_list props[] = {
5897 { DRM_ROTATE_0, "rotate-0" },
5898 { DRM_ROTATE_90, "rotate-90" },
5899 { DRM_ROTATE_180, "rotate-180" },
5900 { DRM_ROTATE_270, "rotate-270" },
5901 { DRM_REFLECT_X, "reflect-x" },
5902 { DRM_REFLECT_Y, "reflect-y" },
5903 };
5904
5905 return drm_property_create_bitmask(dev, 0, "rotation",
5906 props, ARRAY_SIZE(props),
5907 supported_rotations);
5908}
5909EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005910
5911/**
5912 * DOC: Tile group
5913 *
5914 * Tile groups are used to represent tiled monitors with a unique
5915 * integer identifier. Tiled monitors using DisplayID v1.3 have
5916 * a unique 8-byte handle, we store this in a tile group, so we
5917 * have a common identifier for all tiles in a monitor group.
5918 */
5919static void drm_tile_group_free(struct kref *kref)
5920{
5921 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5922 struct drm_device *dev = tg->dev;
5923 mutex_lock(&dev->mode_config.idr_mutex);
5924 idr_remove(&dev->mode_config.tile_idr, tg->id);
5925 mutex_unlock(&dev->mode_config.idr_mutex);
5926 kfree(tg);
5927}
5928
5929/**
5930 * drm_mode_put_tile_group - drop a reference to a tile group.
5931 * @dev: DRM device
5932 * @tg: tile group to drop reference to.
5933 *
5934 * drop reference to tile group and free if 0.
5935 */
5936void drm_mode_put_tile_group(struct drm_device *dev,
5937 struct drm_tile_group *tg)
5938{
5939 kref_put(&tg->refcount, drm_tile_group_free);
5940}
5941
5942/**
5943 * drm_mode_get_tile_group - get a reference to an existing tile group
5944 * @dev: DRM device
5945 * @topology: 8-bytes unique per monitor.
5946 *
5947 * Use the unique bytes to get a reference to an existing tile group.
5948 *
5949 * RETURNS:
5950 * tile group or NULL if not found.
5951 */
5952struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5953 char topology[8])
5954{
5955 struct drm_tile_group *tg;
5956 int id;
5957 mutex_lock(&dev->mode_config.idr_mutex);
5958 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5959 if (!memcmp(tg->group_data, topology, 8)) {
5960 if (!kref_get_unless_zero(&tg->refcount))
5961 tg = NULL;
5962 mutex_unlock(&dev->mode_config.idr_mutex);
5963 return tg;
5964 }
5965 }
5966 mutex_unlock(&dev->mode_config.idr_mutex);
5967 return NULL;
5968}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005969EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005970
5971/**
5972 * drm_mode_create_tile_group - create a tile group from a displayid description
5973 * @dev: DRM device
5974 * @topology: 8-bytes unique per monitor.
5975 *
5976 * Create a tile group for the unique monitor, and get a unique
5977 * identifier for the tile group.
5978 *
5979 * RETURNS:
5980 * new tile group or error.
5981 */
5982struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5983 char topology[8])
5984{
5985 struct drm_tile_group *tg;
5986 int ret;
5987
5988 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5989 if (!tg)
5990 return ERR_PTR(-ENOMEM);
5991
5992 kref_init(&tg->refcount);
5993 memcpy(tg->group_data, topology, 8);
5994 tg->dev = dev;
5995
5996 mutex_lock(&dev->mode_config.idr_mutex);
5997 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5998 if (ret >= 0) {
5999 tg->id = ret;
6000 } else {
6001 kfree(tg);
6002 tg = ERR_PTR(ret);
6003 }
6004
6005 mutex_unlock(&dev->mode_config.idr_mutex);
6006 return tg;
6007}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006008EXPORT_SYMBOL(drm_mode_create_tile_group);