blob: fd93e9c79d28f64398458c4418aba91544b10a21 [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>
Daniel Vetter3b96a0b2016-06-21 10:54:22 +020042#include <drm/drm_auth.h>
Dave Airlief453ba02008-11-07 14:05:41 -080043
Daniel Vetter8bd441b2014-01-23 15:35:24 +010044#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020045#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010046
Chris Wilson9a6f5132015-02-25 13:45:26 +000047static struct drm_framebuffer *
48internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020049 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +000050 struct drm_file *file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -070051
Dave Airlief453ba02008-11-07 14:05:41 -080052/* Avoid boilerplate. I'm tired of typing. */
53#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000054 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080055 { \
56 int i; \
57 for (i = 0; i < ARRAY_SIZE(list); i++) { \
58 if (list[i].type == val) \
59 return list[i].name; \
60 } \
61 return "(unknown)"; \
62 }
63
64/*
65 * Global properties
66 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010067static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
68 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080069 { DRM_MODE_DPMS_STANDBY, "Standby" },
70 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
71 { DRM_MODE_DPMS_OFF, "Off" }
72};
73
74DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75
Thierry Reding4dfd9092014-12-10 13:03:34 +010076static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040077 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
78 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
79 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
80};
81
Dave Airlief453ba02008-11-07 14:05:41 -080082/*
83 * Optional properties
84 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010085static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070086 { DRM_MODE_SCALE_NONE, "None" },
87 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
88 { DRM_MODE_SCALE_CENTER, "Center" },
89 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080090};
91
Vandana Kannanff587e42014-06-11 10:46:48 +053092static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
93 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
94 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
95 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
96};
97
Dave Airlief453ba02008-11-07 14:05:41 -080098/*
99 * Non-global properties, but "required" for certain connectors.
100 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100101static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800102 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
105};
106
107DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108
Thierry Reding4dfd9092014-12-10 13:03:34 +0100109static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800110 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
113};
114
115DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
116 drm_dvi_i_subconnector_enum_list)
117
Thierry Reding4dfd9092014-12-10 13:03:34 +0100118static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800119 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200123 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800124};
125
126DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127
Thierry Reding4dfd9092014-12-10 13:03:34 +0100128static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800129 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200133 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800134};
135
136DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
137 drm_tv_subconnector_enum_list)
138
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000139static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000140 { DRM_MODE_DIRTY_OFF, "Off" },
141 { DRM_MODE_DIRTY_ON, "On" },
142 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
143};
144
Dave Airlief453ba02008-11-07 14:05:41 -0800145struct drm_conn_prop_enum_list {
146 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000147 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400148 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800149};
150
151/*
152 * Connector and encoder types.
153 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100154static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
155 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400156 { DRM_MODE_CONNECTOR_VGA, "VGA" },
157 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
158 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
159 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
160 { DRM_MODE_CONNECTOR_Composite, "Composite" },
161 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
162 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
163 { DRM_MODE_CONNECTOR_Component, "Component" },
164 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
165 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
166 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
167 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
168 { DRM_MODE_CONNECTOR_TV, "TV" },
169 { DRM_MODE_CONNECTOR_eDP, "eDP" },
170 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300171 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700172 { DRM_MODE_CONNECTOR_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800173};
174
Thierry Reding4dfd9092014-12-10 13:03:34 +0100175static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
176 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800177 { DRM_MODE_ENCODER_DAC, "DAC" },
178 { DRM_MODE_ENCODER_TMDS, "TMDS" },
179 { DRM_MODE_ENCODER_LVDS, "LVDS" },
180 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200181 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300182 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000183 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700184 { DRM_MODE_ENCODER_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800185};
186
Thierry Reding4dfd9092014-12-10 13:03:34 +0100187static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800188 { SubPixelUnknown, "Unknown" },
189 { SubPixelHorizontalRGB, "Horizontal RGB" },
190 { SubPixelHorizontalBGR, "Horizontal BGR" },
191 { SubPixelVerticalRGB, "Vertical RGB" },
192 { SubPixelVerticalBGR, "Vertical BGR" },
193 { SubPixelNone, "None" },
194};
195
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400196void drm_connector_ida_init(void)
197{
198 int i;
199
200 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
201 ida_init(&drm_connector_enum_list[i].ida);
202}
203
204void drm_connector_ida_destroy(void)
205{
206 int i;
207
208 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
209 ida_destroy(&drm_connector_enum_list[i].ida);
210}
211
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100212/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100213 * drm_get_connector_status_name - return a string for connector status
214 * @status: connector status to compute name of
215 *
216 * In contrast to the other drm_get_*_name functions this one here returns a
217 * const pointer and hence is threadsafe.
218 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000219const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800220{
221 if (status == connector_status_connected)
222 return "connected";
223 else if (status == connector_status_disconnected)
224 return "disconnected";
225 else
226 return "unknown";
227}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000228EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800229
Jesse Barnesac1bb362014-02-10 15:32:44 -0800230/**
231 * drm_get_subpixel_order_name - return a string for a given subpixel enum
232 * @order: enum of subpixel_order
233 *
234 * Note you could abuse this and return something out of bounds, but that
235 * would be a caller error. No unscrubbed user data should make it here.
236 */
237const char *drm_get_subpixel_order_name(enum subpixel_order order)
238{
239 return drm_subpixel_enum_list[order].name;
240}
241EXPORT_SYMBOL(drm_get_subpixel_order_name);
242
Dave Airlie2ee39452014-07-24 11:53:45 +1000243/*
244 * Internal function to assign a slot in the object idr and optionally
245 * register the object into the idr.
246 */
247static int drm_mode_object_get_reg(struct drm_device *dev,
248 struct drm_mode_object *obj,
249 uint32_t obj_type,
Dave Airlied0f37cf2016-04-15 15:10:36 +1000250 bool register_obj,
251 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000252{
253 int ret;
254
255 mutex_lock(&dev->mode_config.idr_mutex);
256 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
257 if (ret >= 0) {
258 /*
259 * Set up the object linking under the protection of the idr
260 * lock so that other users can't see inconsistent state.
261 */
262 obj->id = ret;
263 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +1000264 if (obj_free_cb) {
265 obj->free_cb = obj_free_cb;
266 kref_init(&obj->refcount);
267 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000268 }
269 mutex_unlock(&dev->mode_config.idr_mutex);
270
271 return ret < 0 ? ret : 0;
272}
273
Dave Airlief453ba02008-11-07 14:05:41 -0800274/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100275 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800276 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100277 * @obj: object pointer, used to generate unique ID
278 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800279 *
Dave Airlief453ba02008-11-07 14:05:41 -0800280 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100281 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
282 * modeset identifiers are _not_ reference counted. Hence don't use this for
283 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800284 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100285 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200286 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800287 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100288int drm_mode_object_get(struct drm_device *dev,
289 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800290{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000291 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000292}
Dave Airlief453ba02008-11-07 14:05:41 -0800293
Dave Airlie2ee39452014-07-24 11:53:45 +1000294static void drm_mode_object_register(struct drm_device *dev,
295 struct drm_mode_object *obj)
296{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000297 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000298 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000299 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800300}
301
302/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000303 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800304 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100305 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800306 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000307 * Free @id from @dev's unique identifier pool.
308 * This function can be called multiple times, and guards against
309 * multiple removals.
310 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100311 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800312 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000313void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100314 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800315{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000316 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000317 if (object->id) {
318 idr_remove(&dev->mode_config.crtc_idr, object->id);
319 object->id = 0;
320 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000321 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800322}
323
Rob Clark98f75de2014-05-30 11:37:03 -0400324static struct drm_mode_object *_object_find(struct drm_device *dev,
325 uint32_t id, uint32_t type)
326{
327 struct drm_mode_object *obj = NULL;
328
329 mutex_lock(&dev->mode_config.idr_mutex);
330 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200331 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
332 obj = NULL;
333 if (obj && obj->id != id)
334 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000335
336 if (obj && obj->free_cb) {
337 if (!kref_get_unless_zero(&obj->refcount))
338 obj = NULL;
339 }
Rob Clark98f75de2014-05-30 11:37:03 -0400340 mutex_unlock(&dev->mode_config.idr_mutex);
341
342 return obj;
343}
344
Daniel Vetter786b99e2012-12-02 21:53:40 +0100345/**
346 * drm_mode_object_find - look up a drm object with static lifetime
347 * @dev: drm device
348 * @id: id of the mode object
349 * @type: type of the mode object
350 *
Daniel Vetter059814222016-04-22 22:10:27 +0200351 * This function is used to look up a modeset object. It will acquire a
352 * reference for reference counted objects. This reference must be dropped again
353 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100354 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200355struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
356 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800357{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000358 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800359
Rob Clark98f75de2014-05-30 11:37:03 -0400360 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800361 return obj;
362}
363EXPORT_SYMBOL(drm_mode_object_find);
364
Daniel Vetter059814222016-04-22 22:10:27 +0200365/**
366 * drm_mode_object_unreference - decr the object refcnt
367 * @obj: mode_object
368 *
369 * This functions decrements the object's refcount if it is a refcounted modeset
370 * object. It is a no-op on any other object. This is used to drop references
371 * acquired with drm_mode_object_reference().
372 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000373void drm_mode_object_unreference(struct drm_mode_object *obj)
374{
375 if (obj->free_cb) {
376 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
377 kref_put(&obj->refcount, obj->free_cb);
378 }
379}
380EXPORT_SYMBOL(drm_mode_object_unreference);
381
382/**
Daniel Vetter059814222016-04-22 22:10:27 +0200383 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000384 * @obj: mode_object
385 *
Daniel Vetter059814222016-04-22 22:10:27 +0200386 * This functions increments the object's refcount if it is a refcounted modeset
387 * object. It is a no-op on any other object. References should be dropped again
388 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000389 */
390void drm_mode_object_reference(struct drm_mode_object *obj)
391{
392 if (obj->free_cb) {
393 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
394 kref_get(&obj->refcount);
395 }
396}
397EXPORT_SYMBOL(drm_mode_object_reference);
398
Dave Airlief55f1f92016-04-15 15:10:33 +1000399static void drm_framebuffer_free(struct kref *kref)
400{
401 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000402 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000403 struct drm_device *dev = fb->dev;
404
405 /*
406 * The lookup idr holds a weak reference, which has not necessarily been
407 * removed at this point. Check for that.
408 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000409 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000410
411 fb->funcs->destroy(fb);
412}
413
Dave Airlief453ba02008-11-07 14:05:41 -0800414/**
Dave Airlief453ba02008-11-07 14:05:41 -0800415 * drm_framebuffer_init - initialize a framebuffer
416 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100417 * @fb: framebuffer to be initialized
418 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800419 *
Dave Airlief453ba02008-11-07 14:05:41 -0800420 * Allocates an ID for the framebuffer's parent mode object, sets its mode
421 * functions & device file and adds it to the master fd list.
422 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100423 * IMPORTANT:
424 * This functions publishes the fb and makes it available for concurrent access
425 * by other users. Which means by this point the fb _must_ be fully set up -
426 * since all the fb attributes are invariant over its lifetime, no further
427 * locking but only correct reference counting is required.
428 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100429 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200430 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800431 */
432int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
433 const struct drm_framebuffer_funcs *funcs)
434{
435 int ret;
436
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100437 INIT_LIST_HEAD(&fb->filp_head);
438 fb->dev = dev;
439 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000440
Dave Airlied0f37cf2016-04-15 15:10:36 +1000441 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000442 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200443 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100444 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800445
Dave Airlie9cd47422016-04-15 15:10:38 +1000446 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800447 dev->mode_config.num_fb++;
448 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000449 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800450
Dave Airlie9cd47422016-04-15 15:10:38 +1000451 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000452out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200453 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800454}
455EXPORT_SYMBOL(drm_framebuffer_init);
456
Rob Clarkf7eff602012-09-05 21:48:38 +0000457/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100458 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
459 * @dev: drm device
460 * @id: id of the fb object
461 *
462 * If successful, this grabs an additional reference to the framebuffer -
463 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100464 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100465 */
466struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
467 uint32_t id)
468{
Dave Airliecee26ac2016-04-15 15:10:37 +1000469 struct drm_mode_object *obj;
470 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100471
Dave Airliecee26ac2016-04-15 15:10:37 +1000472 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000473 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000474 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100475 return fb;
476}
477EXPORT_SYMBOL(drm_framebuffer_lookup);
478
479/**
Daniel Vetter36206362012-12-10 20:42:17 +0100480 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
481 * @fb: fb to unregister
482 *
483 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
484 * those used for fbdev. Note that the caller must hold a reference of it's own,
485 * i.e. the object may not be destroyed through this call (since it'll lead to a
486 * locking inversion).
487 */
488void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
489{
Daniel Vettera39a3572015-08-25 15:45:11 +0200490 struct drm_device *dev;
491
492 if (!fb)
493 return;
494
495 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100496
Daniel Vetter2b677e82012-12-10 21:16:05 +0100497 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000498 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100499}
500EXPORT_SYMBOL(drm_framebuffer_unregister_private);
501
502/**
Dave Airlief453ba02008-11-07 14:05:41 -0800503 * drm_framebuffer_cleanup - remove a framebuffer object
504 * @fb: framebuffer to remove
505 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100506 * Cleanup framebuffer. This function is intended to be used from the drivers
507 * ->destroy callback. It can also be used to clean up driver private
Daniel Vetter2e7a5702016-06-01 23:40:36 +0200508 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100509 *
510 * Note that this function does not remove the fb from active usuage - if it is
511 * still used anywhere, hilarity can ensue since userspace could call getfb on
512 * the id and get back -EINVAL. Obviously no concern at driver unload time.
513 *
514 * Also, the framebuffer will not be removed from the lookup idr - for
515 * user-created framebuffers this will happen in in the rmfb ioctl. For
516 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
517 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800518 */
519void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
520{
521 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100522
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100523 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000524 list_del(&fb->head);
525 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100526 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000527}
528EXPORT_SYMBOL(drm_framebuffer_cleanup);
529
530/**
531 * drm_framebuffer_remove - remove and unreference a framebuffer object
532 * @fb: framebuffer to remove
533 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000534 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100535 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100536 * passed-in framebuffer. Might take the modeset locks.
537 *
538 * Note that this function optimizes the cleanup away if the caller holds the
539 * last reference to the framebuffer. It is also guaranteed to not take the
540 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000541 */
542void drm_framebuffer_remove(struct drm_framebuffer *fb)
543{
Daniel Vettera39a3572015-08-25 15:45:11 +0200544 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800545 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800546 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000547 struct drm_mode_set set;
548 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800549
Daniel Vettera39a3572015-08-25 15:45:11 +0200550 if (!fb)
551 return;
552
553 dev = fb->dev;
554
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100555 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100556
Daniel Vetterb62584e2012-12-11 16:51:35 +0100557 /*
558 * drm ABI mandates that we remove any deleted framebuffers from active
559 * useage. But since most sane clients only remove framebuffers they no
560 * longer need, try to optimize this away.
561 *
562 * Since we're holding a reference ourselves, observing a refcount of 1
563 * means that we're the last holder and can skip it. Also, the refcount
564 * can never increase from 1 again, so we don't need any barriers or
565 * locks.
566 *
567 * Note that userspace could try to race with use and instate a new
568 * usage _after_ we've cleared all current ones. End result will be an
569 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
570 * in this manner.
571 */
Dave Airlie747a5982016-04-15 15:10:35 +1000572 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100573 drm_modeset_lock_all(dev);
574 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200575 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700576 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100577 /* should turn off the crtc */
578 memset(&set, 0, sizeof(struct drm_mode_set));
579 set.crtc = crtc;
580 set.fb = NULL;
581 ret = drm_mode_set_config_internal(&set);
582 if (ret)
583 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
584 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000585 }
Dave Airlief453ba02008-11-07 14:05:41 -0800586
Daniel Vetter6295d602015-07-09 23:44:25 +0200587 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300588 if (plane->fb == fb)
589 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800590 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100591 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800592 }
593
Rob Clarkf7eff602012-09-05 21:48:38 +0000594 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800595}
Rob Clarkf7eff602012-09-05 21:48:38 +0000596EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800597
Rob Clark51fd3712013-11-19 12:10:12 -0500598DEFINE_WW_CLASS(crtc_ww_class);
599
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200600static unsigned int drm_num_crtcs(struct drm_device *dev)
601{
602 unsigned int num = 0;
603 struct drm_crtc *tmp;
604
605 drm_for_each_crtc(tmp, dev) {
606 num++;
607 }
608
609 return num;
610}
611
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200612static int drm_crtc_register_all(struct drm_device *dev)
613{
614 struct drm_crtc *crtc;
615 int ret = 0;
616
617 drm_for_each_crtc(crtc, dev) {
618 if (crtc->funcs->late_register)
619 ret = crtc->funcs->late_register(crtc);
620 if (ret)
621 return ret;
622 }
623
624 return 0;
625}
626
627static void drm_crtc_unregister_all(struct drm_device *dev)
628{
629 struct drm_crtc *crtc;
630
631 drm_for_each_crtc(crtc, dev) {
632 if (crtc->funcs->early_unregister)
633 crtc->funcs->early_unregister(crtc);
634 }
635}
636
Dave Airlief453ba02008-11-07 14:05:41 -0800637/**
Matt Ropere13161a2014-04-01 15:22:38 -0700638 * drm_crtc_init_with_planes - Initialise a new CRTC object with
639 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800640 * @dev: DRM device
641 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700642 * @primary: Primary plane for CRTC
643 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800644 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200645 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800646 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000647 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200648 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100649 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200650 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800651 */
Matt Ropere13161a2014-04-01 15:22:38 -0700652int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
653 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700654 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200655 const struct drm_crtc_funcs *funcs,
656 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800657{
Rob Clark51fd3712013-11-19 12:10:12 -0500658 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200659 int ret;
660
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100661 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
662 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
663
Dave Airlief453ba02008-11-07 14:05:41 -0800664 crtc->dev = dev;
665 crtc->funcs = funcs;
666
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200667 INIT_LIST_HEAD(&crtc->commit_list);
668 spin_lock_init(&crtc->commit_lock);
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);
Chris Wilson490d3d12016-05-27 20:05:00 +0100693 crtc->index = 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
Chris Wilson490d3d12016-05-27 20:05:00 +0100723 /* Note that the crtc_list is considered to be static; should we
724 * remove the drm_crtc at runtime we would have to decrement all
725 * the indices on the drm_crtc after us in the crtc_list.
726 */
727
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000728 kfree(crtc->gamma_store);
729 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800730
Rob Clark51fd3712013-11-19 12:10:12 -0500731 drm_modeset_lock_fini(&crtc->mutex);
732
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000733 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800734 list_del(&crtc->head);
735 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100736
737 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
738 if (crtc->state && crtc->funcs->atomic_destroy_state)
739 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100740
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200741 kfree(crtc->name);
742
Thierry Redinga18c0af2014-12-10 11:38:49 +0100743 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800744}
745EXPORT_SYMBOL(drm_crtc_cleanup);
746
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100747/*
Dave Airlief453ba02008-11-07 14:05:41 -0800748 * drm_mode_remove - remove and free a mode
749 * @connector: connector list to modify
750 * @mode: mode to remove
751 *
Dave Airlief453ba02008-11-07 14:05:41 -0800752 * Remove @mode from @connector's mode list, then free it.
753 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100754static void drm_mode_remove(struct drm_connector *connector,
755 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800756{
757 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100758 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800759}
Dave Airlief453ba02008-11-07 14:05:41 -0800760
761/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200762 * drm_display_info_set_bus_formats - set the supported bus formats
763 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100764 * @formats: array containing the supported bus formats
765 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200766 *
767 * Store the supported bus formats in display info structure.
768 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
769 * a full list of available formats.
770 */
771int drm_display_info_set_bus_formats(struct drm_display_info *info,
772 const u32 *formats,
773 unsigned int num_formats)
774{
775 u32 *fmts = NULL;
776
777 if (!formats && num_formats)
778 return -EINVAL;
779
780 if (formats && num_formats) {
781 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
782 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300783 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200784 return -ENOMEM;
785 }
786
787 kfree(info->bus_formats);
788 info->bus_formats = fmts;
789 info->num_bus_formats = num_formats;
790
791 return 0;
792}
793EXPORT_SYMBOL(drm_display_info_set_bus_formats);
794
795/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200796 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
797 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200798 *
799 * The kernel supports per-connector configration of its consoles through
800 * use of the video= parameter. This function parses that option and
801 * extracts the user's specified mode (or enable/disable status) for a
802 * particular connector. This is typically only used during the early fbdev
803 * setup.
804 */
805static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
806{
807 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
808 char *option = NULL;
809
810 if (fb_get_options(connector->name, &option))
811 return;
812
813 if (!drm_mode_parse_command_line_for_connector(option,
814 connector,
815 mode))
816 return;
817
818 if (mode->force) {
819 const char *s;
820
821 switch (mode->force) {
822 case DRM_FORCE_OFF:
823 s = "OFF";
824 break;
825 case DRM_FORCE_ON_DIGITAL:
826 s = "ON - dig";
827 break;
828 default:
829 case DRM_FORCE_ON:
830 s = "ON";
831 break;
832 }
833
834 DRM_INFO("forcing %s connector %s\n", connector->name, s);
835 connector->force = mode->force;
836 }
837
838 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
839 connector->name,
840 mode->xres, mode->yres,
841 mode->refresh_specified ? mode->refresh : 60,
842 mode->rb ? " reduced blanking" : "",
843 mode->margins ? " with margins" : "",
844 mode->interlace ? " interlaced" : "");
845}
846
Dave Airlieb164d312016-04-27 11:10:09 +1000847static void drm_connector_free(struct kref *kref)
848{
849 struct drm_connector *connector =
850 container_of(kref, struct drm_connector, base.refcount);
851 struct drm_device *dev = connector->dev;
852
853 drm_mode_object_unregister(dev, &connector->base);
854 connector->funcs->destroy(connector);
855}
856
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200857/**
Dave Airlief453ba02008-11-07 14:05:41 -0800858 * drm_connector_init - Init a preallocated connector
859 * @dev: DRM device
860 * @connector: the connector to init
861 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100862 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800863 *
Dave Airlief453ba02008-11-07 14:05:41 -0800864 * Initialises a preallocated connector. Connectors should be
865 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200866 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100867 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200868 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800869 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200870int drm_connector_init(struct drm_device *dev,
871 struct drm_connector *connector,
872 const struct drm_connector_funcs *funcs,
873 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800874{
Rob Clarkae16c592014-12-18 16:01:54 -0500875 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200876 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400877 struct ida *connector_ida =
878 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200879
Daniel Vetter84849902012-12-02 00:28:11 +0100880 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800881
Dave Airlieb164d312016-04-27 11:10:09 +1000882 ret = drm_mode_object_get_reg(dev, &connector->base,
883 DRM_MODE_OBJECT_CONNECTOR,
884 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200885 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300886 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200887
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300888 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800889 connector->dev = dev;
890 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100891
892 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
893 if (connector->connector_id < 0) {
894 ret = connector->connector_id;
895 goto out_put;
896 }
897
Dave Airlief453ba02008-11-07 14:05:41 -0800898 connector->connector_type = connector_type;
899 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400900 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
901 if (connector->connector_type_id < 0) {
902 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100903 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400904 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300905 connector->name =
906 kasprintf(GFP_KERNEL, "%s-%d",
907 drm_connector_enum_list[connector_type].name,
908 connector->connector_type_id);
909 if (!connector->name) {
910 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100911 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300912 }
913
Dave Airlief453ba02008-11-07 14:05:41 -0800914 INIT_LIST_HEAD(&connector->probed_modes);
915 INIT_LIST_HEAD(&connector->modes);
916 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000917 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800918
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200919 drm_connector_get_cmdline_mode(connector);
920
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100921 /* We should add connectors at the end to avoid upsetting the connector
922 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500923 list_add_tail(&connector->head, &config->connector_list);
924 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800925
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200926 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500927 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500928 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200929 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800930
Rob Clark58495562012-10-11 20:50:56 -0500931 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500932 config->dpms_property, 0);
933
934 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
935 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
936 }
Dave Airlief453ba02008-11-07 14:05:41 -0800937
Thomas Wood30f65702014-06-18 17:52:32 +0100938 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100939out_put_type_id:
940 if (ret)
941 ida_remove(connector_ida, connector->connector_type_id);
942out_put_id:
943 if (ret)
944 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300945out_put:
946 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000947 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300948
949out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100950 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200951
952 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800953}
954EXPORT_SYMBOL(drm_connector_init);
955
956/**
957 * drm_connector_cleanup - cleans up an initialised connector
958 * @connector: connector to cleanup
959 *
Dave Airlief453ba02008-11-07 14:05:41 -0800960 * Cleans up the connector but doesn't free the object.
961 */
962void drm_connector_cleanup(struct drm_connector *connector)
963{
964 struct drm_device *dev = connector->dev;
965 struct drm_display_mode *mode, *t;
966
Chris Wilson80de3462016-06-15 13:17:49 +0100967 /* The connector should have been removed from userspace long before
968 * it is finally destroyed.
969 */
970 if (WARN_ON(connector->registered))
971 drm_connector_unregister(connector);
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
Chris Wilson40daac62016-06-15 13:17:48 +01001019 if (connector->registered)
1020 return 0;
1021
Thomas Wood30f65702014-06-18 17:52:32 +01001022 ret = drm_sysfs_connector_add(connector);
1023 if (ret)
1024 return ret;
1025
1026 ret = drm_debugfs_connector_add(connector);
1027 if (ret) {
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001028 goto err_sysfs;
1029 }
1030
1031 if (connector->funcs->late_register) {
1032 ret = connector->funcs->late_register(connector);
1033 if (ret)
1034 goto err_debugfs;
Thomas Wood30f65702014-06-18 17:52:32 +01001035 }
1036
Daniel Vetterfdf2c852016-05-06 14:55:02 +02001037 drm_mode_object_register(connector->dev, &connector->base);
1038
Chris Wilson40daac62016-06-15 13:17:48 +01001039 connector->registered = true;
Thomas Wood30f65702014-06-18 17:52:32 +01001040 return 0;
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001041
1042err_debugfs:
1043 drm_debugfs_connector_remove(connector);
1044err_sysfs:
1045 drm_sysfs_connector_remove(connector);
1046 return ret;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001047}
1048EXPORT_SYMBOL(drm_connector_register);
1049
1050/**
1051 * drm_connector_unregister - unregister a connector
1052 * @connector: the connector to unregister
1053 *
1054 * Unregister userspace interfaces for a connector
1055 */
1056void drm_connector_unregister(struct drm_connector *connector)
1057{
Chris Wilson40daac62016-06-15 13:17:48 +01001058 if (!connector->registered)
1059 return;
1060
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001061 if (connector->funcs->early_unregister)
1062 connector->funcs->early_unregister(connector);
1063
Thomas Wood34ea3d32014-05-29 16:57:41 +01001064 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001065 drm_debugfs_connector_remove(connector);
Chris Wilson40daac62016-06-15 13:17:48 +01001066
1067 connector->registered = false;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001068}
1069EXPORT_SYMBOL(drm_connector_unregister);
1070
Thomas Wood34ea3d32014-05-29 16:57:41 +01001071/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001072 * drm_connector_register_all - register all connectors
1073 * @dev: drm device
1074 *
1075 * This function registers all connectors in sysfs and other places so that
Chris Wilsone28cd4d2016-06-17 09:25:17 +01001076 * userspace can start to access them. drm_connector_register_all() is called
1077 * automatically from drm_dev_register() to complete the device registration,
1078 * if they don't call drm_connector_register() on each connector individually.
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001079 *
1080 * When a device is unplugged and should be removed from userspace access,
1081 * call drm_connector_unregister_all(), which is the inverse of this
1082 * function.
1083 *
1084 * Returns:
1085 * Zero on success, error code on failure.
1086 */
1087int drm_connector_register_all(struct drm_device *dev)
1088{
1089 struct drm_connector *connector;
1090 int ret;
1091
1092 mutex_lock(&dev->mode_config.mutex);
1093
1094 drm_for_each_connector(connector, dev) {
1095 ret = drm_connector_register(connector);
1096 if (ret)
1097 goto err;
1098 }
1099
1100 mutex_unlock(&dev->mode_config.mutex);
1101
1102 return 0;
1103
1104err:
1105 mutex_unlock(&dev->mode_config.mutex);
1106 drm_connector_unregister_all(dev);
1107 return ret;
1108}
1109EXPORT_SYMBOL(drm_connector_register_all);
1110
1111/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001112 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001113 * @dev: drm device
1114 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001115 * This functions unregisters all connectors from sysfs and other places so
1116 * that userspace can no longer access them. Drivers should call this as the
1117 * first step tearing down the device instace, or when the underlying
1118 * physical device disappeared (e.g. USB unplug), right before calling
1119 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001120 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001121void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001122{
1123 struct drm_connector *connector;
1124
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001125 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001126 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001127 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001128}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001129EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001130
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001131static int drm_encoder_register_all(struct drm_device *dev)
1132{
1133 struct drm_encoder *encoder;
1134 int ret = 0;
1135
1136 drm_for_each_encoder(encoder, dev) {
1137 if (encoder->funcs->late_register)
1138 ret = encoder->funcs->late_register(encoder);
1139 if (ret)
1140 return ret;
1141 }
1142
1143 return 0;
1144}
1145
1146static void drm_encoder_unregister_all(struct drm_device *dev)
1147{
1148 struct drm_encoder *encoder;
1149
1150 drm_for_each_encoder(encoder, dev) {
1151 if (encoder->funcs->early_unregister)
1152 encoder->funcs->early_unregister(encoder);
1153 }
1154}
1155
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001156/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001157 * drm_encoder_init - Init a preallocated encoder
1158 * @dev: drm device
1159 * @encoder: the encoder to init
1160 * @funcs: callbacks for this encoder
1161 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001162 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001163 *
1164 * Initialises a preallocated encoder. Encoder should be
1165 * subclassed as part of driver encoder objects.
1166 *
1167 * Returns:
1168 * Zero on success, error code on failure.
1169 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001170int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001171 struct drm_encoder *encoder,
1172 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001173 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001174{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001175 int ret;
1176
Daniel Vetter84849902012-12-02 00:28:11 +01001177 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001178
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001179 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1180 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001181 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001182
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001183 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001184 encoder->encoder_type = encoder_type;
1185 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001186 if (name) {
1187 va_list ap;
1188
1189 va_start(ap, name);
1190 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1191 va_end(ap);
1192 } else {
1193 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1194 drm_encoder_enum_list[encoder_type].name,
1195 encoder->base.id);
1196 }
Jani Nikulae5748942014-05-14 16:58:20 +03001197 if (!encoder->name) {
1198 ret = -ENOMEM;
1199 goto out_put;
1200 }
Dave Airlief453ba02008-11-07 14:05:41 -08001201
1202 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001203 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001204
Jani Nikulae5748942014-05-14 16:58:20 +03001205out_put:
1206 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001207 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001208
1209out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001210 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001211
1212 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001213}
1214EXPORT_SYMBOL(drm_encoder_init);
1215
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001216/**
1217 * drm_encoder_cleanup - cleans up an initialised encoder
1218 * @encoder: encoder to cleanup
1219 *
1220 * Cleans up the encoder but doesn't free the object.
1221 */
Dave Airlief453ba02008-11-07 14:05:41 -08001222void drm_encoder_cleanup(struct drm_encoder *encoder)
1223{
1224 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001225
Chris Wilson490d3d12016-05-27 20:05:00 +01001226 /* Note that the encoder_list is considered to be static; should we
1227 * remove the drm_encoder at runtime we would have to decrement all
1228 * the indices on the drm_encoder after us in the encoder_list.
1229 */
1230
Daniel Vetter84849902012-12-02 00:28:11 +01001231 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001232 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001233 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001234 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001235 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001236 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001237
1238 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001239}
1240EXPORT_SYMBOL(drm_encoder_cleanup);
1241
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001242static unsigned int drm_num_planes(struct drm_device *dev)
1243{
1244 unsigned int num = 0;
1245 struct drm_plane *tmp;
1246
1247 drm_for_each_plane(tmp, dev) {
1248 num++;
1249 }
1250
1251 return num;
1252}
1253
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001254/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001255 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001256 * @dev: DRM device
1257 * @plane: plane object to init
1258 * @possible_crtcs: bitmask of possible CRTCs
1259 * @funcs: callbacks for the new plane
1260 * @formats: array of supported formats (%DRM_FORMAT_*)
1261 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001262 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001263 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001264 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001265 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001266 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001267 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001268 * Zero on success, error code on failure.
1269 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001270int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1271 unsigned long possible_crtcs,
1272 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001273 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001274 enum drm_plane_type type,
1275 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001276{
Rob Clark6b4959f2014-12-18 16:01:53 -05001277 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001278 int ret;
1279
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001280 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1281 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001282 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001283
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001284 drm_modeset_lock_init(&plane->mutex);
1285
Rob Clark4d939142012-05-17 02:23:27 -06001286 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001287 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001288 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001289 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1290 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001291 if (!plane->format_types) {
1292 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001293 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001294 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001295 }
1296
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001297 if (name) {
1298 va_list ap;
1299
1300 va_start(ap, name);
1301 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1302 va_end(ap);
1303 } else {
1304 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1305 drm_num_planes(dev));
1306 }
1307 if (!plane->name) {
1308 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001309 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001310 return -ENOMEM;
1311 }
1312
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001313 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001314 plane->format_count = format_count;
1315 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001316 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001317
Rob Clark6b4959f2014-12-18 16:01:53 -05001318 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001319 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001320 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001321 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001322
Rob Clark9922ab52014-04-01 20:16:57 -04001323 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001324 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001325 plane->type);
1326
Rob Clark6b4959f2014-12-18 16:01:53 -05001327 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1328 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1329 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1330 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1331 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1332 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1333 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1334 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1335 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1336 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1337 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1338 }
1339
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001340 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001341}
Matt Roperdc415ff2014-04-01 15:22:36 -07001342EXPORT_SYMBOL(drm_universal_plane_init);
1343
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001344static int drm_plane_register_all(struct drm_device *dev)
1345{
1346 struct drm_plane *plane;
1347 int ret = 0;
1348
1349 drm_for_each_plane(plane, dev) {
1350 if (plane->funcs->late_register)
1351 ret = plane->funcs->late_register(plane);
1352 if (ret)
1353 return ret;
1354 }
1355
1356 return 0;
1357}
1358
1359static void drm_plane_unregister_all(struct drm_device *dev)
1360{
1361 struct drm_plane *plane;
1362
1363 drm_for_each_plane(plane, dev) {
1364 if (plane->funcs->early_unregister)
1365 plane->funcs->early_unregister(plane);
1366 }
1367}
1368
Matt Roperdc415ff2014-04-01 15:22:36 -07001369/**
1370 * drm_plane_init - Initialize a legacy plane
1371 * @dev: DRM device
1372 * @plane: plane object to init
1373 * @possible_crtcs: bitmask of possible CRTCs
1374 * @funcs: callbacks for the new plane
1375 * @formats: array of supported formats (%DRM_FORMAT_*)
1376 * @format_count: number of elements in @formats
1377 * @is_primary: plane type (primary vs overlay)
1378 *
1379 * Legacy API to initialize a DRM plane.
1380 *
1381 * New drivers should call drm_universal_plane_init() instead.
1382 *
1383 * Returns:
1384 * Zero on success, error code on failure.
1385 */
1386int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1387 unsigned long possible_crtcs,
1388 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001389 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001390 bool is_primary)
1391{
1392 enum drm_plane_type type;
1393
1394 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1395 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001396 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001397}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001398EXPORT_SYMBOL(drm_plane_init);
1399
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001400/**
1401 * drm_plane_cleanup - Clean up the core plane usage
1402 * @plane: plane to cleanup
1403 *
1404 * This function cleans up @plane and removes it from the DRM mode setting
1405 * core. Note that the function does *not* free the plane structure itself,
1406 * this is the responsibility of the caller.
1407 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001408void drm_plane_cleanup(struct drm_plane *plane)
1409{
1410 struct drm_device *dev = plane->dev;
1411
Daniel Vetter84849902012-12-02 00:28:11 +01001412 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001413 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001414 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001415
1416 BUG_ON(list_empty(&plane->head));
1417
Chris Wilson490d3d12016-05-27 20:05:00 +01001418 /* Note that the plane_list is considered to be static; should we
1419 * remove the drm_plane at runtime we would have to decrement all
1420 * the indices on the drm_plane after us in the plane_list.
1421 */
1422
Matt Roperdc415ff2014-04-01 15:22:36 -07001423 list_del(&plane->head);
1424 dev->mode_config.num_total_plane--;
1425 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1426 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001427 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001428
1429 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1430 if (plane->state && plane->funcs->atomic_destroy_state)
1431 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001432
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001433 kfree(plane->name);
1434
Thierry Redinga18c0af2014-12-10 11:38:49 +01001435 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001436}
1437EXPORT_SYMBOL(drm_plane_cleanup);
1438
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001439/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001440 * drm_plane_from_index - find the registered plane at an index
1441 * @dev: DRM device
1442 * @idx: index of registered plane to find for
1443 *
1444 * Given a plane index, return the registered plane from DRM device's
1445 * list of planes with matching index.
1446 */
1447struct drm_plane *
1448drm_plane_from_index(struct drm_device *dev, int idx)
1449{
1450 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001451
Chris Wilson490d3d12016-05-27 20:05:00 +01001452 drm_for_each_plane(plane, dev)
1453 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001454 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001455
Chandra Konduruf81338a2015-04-09 17:36:21 -07001456 return NULL;
1457}
1458EXPORT_SYMBOL(drm_plane_from_index);
1459
1460/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001461 * drm_plane_force_disable - Forcibly disable a plane
1462 * @plane: plane to disable
1463 *
1464 * Forces the plane to be disabled.
1465 *
1466 * Used when the plane's current framebuffer is destroyed,
1467 * and when restoring fbdev mode.
1468 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001469void drm_plane_force_disable(struct drm_plane *plane)
1470{
1471 int ret;
1472
Daniel Vetter3d30a592014-07-27 13:42:42 +02001473 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001474 return;
1475
Daniel Vetter3d30a592014-07-27 13:42:42 +02001476 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001477 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001478 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001479 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001480 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001481 return;
1482 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001483 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001484 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001485 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001486 plane->fb = NULL;
1487 plane->crtc = NULL;
1488}
1489EXPORT_SYMBOL(drm_plane_force_disable);
1490
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001491int drm_modeset_register_all(struct drm_device *dev)
1492{
1493 int ret;
1494
1495 ret = drm_plane_register_all(dev);
1496 if (ret)
1497 goto err_plane;
1498
1499 ret = drm_crtc_register_all(dev);
1500 if (ret)
1501 goto err_crtc;
1502
1503 ret = drm_encoder_register_all(dev);
1504 if (ret)
1505 goto err_encoder;
1506
1507 ret = drm_connector_register_all(dev);
1508 if (ret)
1509 goto err_connector;
1510
1511 return 0;
1512
1513err_connector:
1514 drm_encoder_unregister_all(dev);
1515err_encoder:
1516 drm_crtc_unregister_all(dev);
1517err_crtc:
1518 drm_plane_unregister_all(dev);
1519err_plane:
1520 return ret;
1521}
1522
1523void drm_modeset_unregister_all(struct drm_device *dev)
1524{
1525 drm_connector_unregister_all(dev);
1526 drm_encoder_unregister_all(dev);
1527 drm_crtc_unregister_all(dev);
1528 drm_plane_unregister_all(dev);
1529}
1530
Rob Clark6b4959f2014-12-18 16:01:53 -05001531static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001532{
Rob Clark356af0e2014-12-18 16:01:52 -05001533 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001534
1535 /*
1536 * Standard properties (apply to all connectors)
1537 */
Rob Clark356af0e2014-12-18 16:01:52 -05001538 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001539 DRM_MODE_PROP_IMMUTABLE,
1540 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001541 if (!prop)
1542 return -ENOMEM;
1543 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001544
Rob Clark356af0e2014-12-18 16:01:52 -05001545 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001546 "DPMS", drm_dpms_enum_list,
1547 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001548 if (!prop)
1549 return -ENOMEM;
1550 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001551
Rob Clark356af0e2014-12-18 16:01:52 -05001552 prop = drm_property_create(dev,
1553 DRM_MODE_PROP_BLOB |
1554 DRM_MODE_PROP_IMMUTABLE,
1555 "PATH", 0);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001559
Rob Clark356af0e2014-12-18 16:01:52 -05001560 prop = drm_property_create(dev,
1561 DRM_MODE_PROP_BLOB |
1562 DRM_MODE_PROP_IMMUTABLE,
1563 "TILE", 0);
1564 if (!prop)
1565 return -ENOMEM;
1566 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001567
Rob Clark6b4959f2014-12-18 16:01:53 -05001568 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001569 "type", drm_plane_type_enum_list,
1570 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001571 if (!prop)
1572 return -ENOMEM;
1573 dev->mode_config.plane_type_property = prop;
1574
1575 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1576 "SRC_X", 0, UINT_MAX);
1577 if (!prop)
1578 return -ENOMEM;
1579 dev->mode_config.prop_src_x = prop;
1580
1581 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1582 "SRC_Y", 0, UINT_MAX);
1583 if (!prop)
1584 return -ENOMEM;
1585 dev->mode_config.prop_src_y = prop;
1586
1587 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1588 "SRC_W", 0, UINT_MAX);
1589 if (!prop)
1590 return -ENOMEM;
1591 dev->mode_config.prop_src_w = prop;
1592
1593 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1594 "SRC_H", 0, UINT_MAX);
1595 if (!prop)
1596 return -ENOMEM;
1597 dev->mode_config.prop_src_h = prop;
1598
1599 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1600 "CRTC_X", INT_MIN, INT_MAX);
1601 if (!prop)
1602 return -ENOMEM;
1603 dev->mode_config.prop_crtc_x = prop;
1604
1605 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1606 "CRTC_Y", INT_MIN, INT_MAX);
1607 if (!prop)
1608 return -ENOMEM;
1609 dev->mode_config.prop_crtc_y = prop;
1610
1611 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1612 "CRTC_W", 0, INT_MAX);
1613 if (!prop)
1614 return -ENOMEM;
1615 dev->mode_config.prop_crtc_w = prop;
1616
1617 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1618 "CRTC_H", 0, INT_MAX);
1619 if (!prop)
1620 return -ENOMEM;
1621 dev->mode_config.prop_crtc_h = prop;
1622
1623 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1624 "FB_ID", DRM_MODE_OBJECT_FB);
1625 if (!prop)
1626 return -ENOMEM;
1627 dev->mode_config.prop_fb_id = prop;
1628
1629 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1630 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1631 if (!prop)
1632 return -ENOMEM;
1633 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001634
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001635 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1636 "ACTIVE");
1637 if (!prop)
1638 return -ENOMEM;
1639 dev->mode_config.prop_active = prop;
1640
Daniel Stone955f3c32015-05-25 19:11:52 +01001641 prop = drm_property_create(dev,
1642 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1643 "MODE_ID", 0);
1644 if (!prop)
1645 return -ENOMEM;
1646 dev->mode_config.prop_mode_id = prop;
1647
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001648 prop = drm_property_create(dev,
1649 DRM_MODE_PROP_BLOB,
1650 "DEGAMMA_LUT", 0);
1651 if (!prop)
1652 return -ENOMEM;
1653 dev->mode_config.degamma_lut_property = prop;
1654
1655 prop = drm_property_create_range(dev,
1656 DRM_MODE_PROP_IMMUTABLE,
1657 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1658 if (!prop)
1659 return -ENOMEM;
1660 dev->mode_config.degamma_lut_size_property = prop;
1661
1662 prop = drm_property_create(dev,
1663 DRM_MODE_PROP_BLOB,
1664 "CTM", 0);
1665 if (!prop)
1666 return -ENOMEM;
1667 dev->mode_config.ctm_property = prop;
1668
1669 prop = drm_property_create(dev,
1670 DRM_MODE_PROP_BLOB,
1671 "GAMMA_LUT", 0);
1672 if (!prop)
1673 return -ENOMEM;
1674 dev->mode_config.gamma_lut_property = prop;
1675
1676 prop = drm_property_create_range(dev,
1677 DRM_MODE_PROP_IMMUTABLE,
1678 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1679 if (!prop)
1680 return -ENOMEM;
1681 dev->mode_config.gamma_lut_size_property = prop;
1682
Rob Clark9922ab52014-04-01 20:16:57 -04001683 return 0;
1684}
1685
Dave Airlief453ba02008-11-07 14:05:41 -08001686/**
1687 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1688 * @dev: DRM device
1689 *
1690 * Called by a driver the first time a DVI-I connector is made.
1691 */
1692int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1693{
1694 struct drm_property *dvi_i_selector;
1695 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001696
1697 if (dev->mode_config.dvi_i_select_subconnector_property)
1698 return 0;
1699
1700 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001701 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001702 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001703 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001704 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001705 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1706
Sascha Hauer4a67d392012-02-06 10:58:17 +01001707 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001708 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001709 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001710 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001711 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1712
1713 return 0;
1714}
1715EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1716
1717/**
1718 * drm_create_tv_properties - create TV specific connector properties
1719 * @dev: DRM device
1720 * @num_modes: number of different TV formats (modes) supported
1721 * @modes: array of pointers to strings containing name of each format
1722 *
1723 * Called by a driver's TV initialization routine, this function creates
1724 * the TV specific connector properties for a given device. Caller is
1725 * responsible for allocating a list of format names and passing them to
1726 * this routine.
1727 */
Thierry Reding2f763312014-10-13 12:45:57 +02001728int drm_mode_create_tv_properties(struct drm_device *dev,
1729 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001730 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001731{
1732 struct drm_property *tv_selector;
1733 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001734 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001735
1736 if (dev->mode_config.tv_select_subconnector_property)
1737 return 0;
1738
1739 /*
1740 * Basic connector properties
1741 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001742 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001743 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001744 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001745 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001746 if (!tv_selector)
1747 goto nomem;
1748
Dave Airlief453ba02008-11-07 14:05:41 -08001749 dev->mode_config.tv_select_subconnector_property = tv_selector;
1750
1751 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001752 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1753 "subconnector",
1754 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001755 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001756 if (!tv_subconnector)
1757 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001758 dev->mode_config.tv_subconnector_property = tv_subconnector;
1759
1760 /*
1761 * Other, TV specific properties: margins & TV modes.
1762 */
1763 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001764 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001765 if (!dev->mode_config.tv_left_margin_property)
1766 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001767
1768 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001769 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001770 if (!dev->mode_config.tv_right_margin_property)
1771 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001772
1773 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001774 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001775 if (!dev->mode_config.tv_top_margin_property)
1776 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001777
1778 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001779 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001780 if (!dev->mode_config.tv_bottom_margin_property)
1781 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001782
1783 dev->mode_config.tv_mode_property =
1784 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1785 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001786 if (!dev->mode_config.tv_mode_property)
1787 goto nomem;
1788
Dave Airlief453ba02008-11-07 14:05:41 -08001789 for (i = 0; i < num_modes; i++)
1790 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1791 i, modes[i]);
1792
Francisco Jerezb6b79022009-08-02 04:19:20 +02001793 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001794 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001795 if (!dev->mode_config.tv_brightness_property)
1796 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001797
1798 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001799 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001800 if (!dev->mode_config.tv_contrast_property)
1801 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001802
1803 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001804 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001805 if (!dev->mode_config.tv_flicker_reduction_property)
1806 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001807
Francisco Jereza75f0232009-08-12 02:30:10 +02001808 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001809 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001810 if (!dev->mode_config.tv_overscan_property)
1811 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001812
1813 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001814 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001815 if (!dev->mode_config.tv_saturation_property)
1816 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001817
1818 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001819 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001820 if (!dev->mode_config.tv_hue_property)
1821 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001822
Dave Airlief453ba02008-11-07 14:05:41 -08001823 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001824nomem:
1825 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001826}
1827EXPORT_SYMBOL(drm_mode_create_tv_properties);
1828
1829/**
1830 * drm_mode_create_scaling_mode_property - create scaling mode property
1831 * @dev: DRM device
1832 *
1833 * Called by a driver the first time it's needed, must be attached to desired
1834 * connectors.
1835 */
1836int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1837{
1838 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001839
1840 if (dev->mode_config.scaling_mode_property)
1841 return 0;
1842
1843 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001844 drm_property_create_enum(dev, 0, "scaling mode",
1845 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001846 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001847
1848 dev->mode_config.scaling_mode_property = scaling_mode;
1849
1850 return 0;
1851}
1852EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1853
1854/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301855 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1856 * @dev: DRM device
1857 *
1858 * Called by a driver the first time it's needed, must be attached to desired
1859 * connectors.
1860 *
1861 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001862 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301863 */
1864int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1865{
1866 if (dev->mode_config.aspect_ratio_property)
1867 return 0;
1868
1869 dev->mode_config.aspect_ratio_property =
1870 drm_property_create_enum(dev, 0, "aspect ratio",
1871 drm_aspect_ratio_enum_list,
1872 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1873
1874 if (dev->mode_config.aspect_ratio_property == NULL)
1875 return -ENOMEM;
1876
1877 return 0;
1878}
1879EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1880
1881/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001882 * drm_mode_create_dirty_property - create dirty property
1883 * @dev: DRM device
1884 *
1885 * Called by a driver the first time it's needed, must be attached to desired
1886 * connectors.
1887 */
1888int drm_mode_create_dirty_info_property(struct drm_device *dev)
1889{
1890 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001891
1892 if (dev->mode_config.dirty_info_property)
1893 return 0;
1894
1895 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001896 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001897 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001898 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001899 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001900 dev->mode_config.dirty_info_property = dirty_info;
1901
1902 return 0;
1903}
1904EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1905
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001906/**
1907 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1908 * @dev: DRM device
1909 *
1910 * Create the the suggested x/y offset property for connectors.
1911 */
1912int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1913{
1914 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1915 return 0;
1916
1917 dev->mode_config.suggested_x_property =
1918 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1919
1920 dev->mode_config.suggested_y_property =
1921 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1922
1923 if (dev->mode_config.suggested_x_property == NULL ||
1924 dev->mode_config.suggested_y_property == NULL)
1925 return -ENOMEM;
1926 return 0;
1927}
1928EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1929
Dave Airlief453ba02008-11-07 14:05:41 -08001930/**
Dave Airlief453ba02008-11-07 14:05:41 -08001931 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001932 * @dev: drm device for the ioctl
1933 * @data: data pointer for the ioctl
1934 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001935 *
Dave Airlief453ba02008-11-07 14:05:41 -08001936 * Construct a set of configuration description structures and return
1937 * them to the user, including CRTC, connector and framebuffer configuration.
1938 *
1939 * Called by the user via ioctl.
1940 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001941 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001942 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001943 */
1944int drm_mode_getresources(struct drm_device *dev, void *data,
1945 struct drm_file *file_priv)
1946{
1947 struct drm_mode_card_res *card_res = data;
1948 struct list_head *lh;
1949 struct drm_framebuffer *fb;
1950 struct drm_connector *connector;
1951 struct drm_crtc *crtc;
1952 struct drm_encoder *encoder;
1953 int ret = 0;
1954 int connector_count = 0;
1955 int crtc_count = 0;
1956 int fb_count = 0;
1957 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001958 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001959 uint32_t __user *fb_id;
1960 uint32_t __user *crtc_id;
1961 uint32_t __user *connector_id;
1962 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001963
Dave Airliefb3b06c2011-02-08 13:55:21 +10001964 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1965 return -EINVAL;
1966
Dave Airlief453ba02008-11-07 14:05:41 -08001967
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001968 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001969 /*
1970 * For the non-control nodes we need to limit the list of resources
1971 * by IDs in the group list for this node
1972 */
1973 list_for_each(lh, &file_priv->fbs)
1974 fb_count++;
1975
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001976 /* handle this in 4 parts */
1977 /* FBs */
1978 if (card_res->count_fbs >= fb_count) {
1979 copied = 0;
1980 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1981 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1982 if (put_user(fb->base.id, fb_id + copied)) {
1983 mutex_unlock(&file_priv->fbs_lock);
1984 return -EFAULT;
1985 }
1986 copied++;
1987 }
1988 }
1989 card_res->count_fbs = fb_count;
1990 mutex_unlock(&file_priv->fbs_lock);
1991
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001992 /* mode_config.mutex protects the connector list against e.g. DP MST
1993 * connector hot-adding. CRTC/Plane lists are invariant. */
1994 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001995 drm_for_each_crtc(crtc, dev)
1996 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001997
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001998 drm_for_each_connector(connector, dev)
1999 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002000
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002001 drm_for_each_encoder(encoder, dev)
2002 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002003
2004 card_res->max_height = dev->mode_config.max_height;
2005 card_res->min_height = dev->mode_config.min_height;
2006 card_res->max_width = dev->mode_config.max_width;
2007 card_res->min_width = dev->mode_config.min_width;
2008
Dave Airlief453ba02008-11-07 14:05:41 -08002009 /* CRTCs */
2010 if (card_res->count_crtcs >= crtc_count) {
2011 copied = 0;
2012 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002013 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002014 if (put_user(crtc->base.id, crtc_id + copied)) {
2015 ret = -EFAULT;
2016 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002017 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002018 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002019 }
2020 }
2021 card_res->count_crtcs = crtc_count;
2022
2023 /* Encoders */
2024 if (card_res->count_encoders >= encoder_count) {
2025 copied = 0;
2026 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002027 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002028 if (put_user(encoder->base.id, encoder_id +
2029 copied)) {
2030 ret = -EFAULT;
2031 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002032 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002033 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002034 }
2035 }
2036 card_res->count_encoders = encoder_count;
2037
2038 /* Connectors */
2039 if (card_res->count_connectors >= connector_count) {
2040 copied = 0;
2041 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002042 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002043 if (put_user(connector->base.id,
2044 connector_id + copied)) {
2045 ret = -EFAULT;
2046 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002047 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002048 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002049 }
2050 }
2051 card_res->count_connectors = connector_count;
2052
Dave Airlief453ba02008-11-07 14:05:41 -08002053out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002054 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002055 return ret;
2056}
2057
2058/**
2059 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002060 * @dev: drm device for the ioctl
2061 * @data: data pointer for the ioctl
2062 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002063 *
Dave Airlief453ba02008-11-07 14:05:41 -08002064 * Construct a CRTC configuration structure to return to the user.
2065 *
2066 * Called by the user via ioctl.
2067 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002068 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002069 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002070 */
2071int drm_mode_getcrtc(struct drm_device *dev,
2072 void *data, struct drm_file *file_priv)
2073{
2074 struct drm_mode_crtc *crtc_resp = data;
2075 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002076
Dave Airliefb3b06c2011-02-08 13:55:21 +10002077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2078 return -EINVAL;
2079
Rob Clarka2b34e22013-10-05 16:36:52 -04002080 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002081 if (!crtc)
2082 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002083
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002084 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002085 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002086 if (crtc->primary->fb)
2087 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002088 else
2089 crtc_resp->fb_id = 0;
2090
Daniel Vetter31c946e2015-02-22 12:24:17 +01002091 if (crtc->state) {
2092 crtc_resp->x = crtc->primary->state->src_x >> 16;
2093 crtc_resp->y = crtc->primary->state->src_y >> 16;
2094 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002095 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002096 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002097
Daniel Vetter31c946e2015-02-22 12:24:17 +01002098 } else {
2099 crtc_resp->mode_valid = 0;
2100 }
Dave Airlief453ba02008-11-07 14:05:41 -08002101 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002102 crtc_resp->x = crtc->x;
2103 crtc_resp->y = crtc->y;
2104 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002105 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002106 crtc_resp->mode_valid = 1;
2107
2108 } else {
2109 crtc_resp->mode_valid = 0;
2110 }
Dave Airlief453ba02008-11-07 14:05:41 -08002111 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002112 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002113
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002114 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002115}
2116
Damien Lespiau61d8e322013-09-25 16:45:22 +01002117static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2118 const struct drm_file *file_priv)
2119{
2120 /*
2121 * If user-space hasn't configured the driver to expose the stereo 3D
2122 * modes, don't expose them.
2123 */
2124 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2125 return false;
2126
2127 return true;
2128}
2129
Daniel Vetterabd69c52014-11-25 23:50:05 +01002130static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2131{
2132 /* For atomic drivers only state objects are synchronously updated and
2133 * protected by modeset locks, so check those first. */
2134 if (connector->state)
2135 return connector->state->best_encoder;
2136 return connector->encoder;
2137}
2138
Rob Clark95cbf112014-12-18 16:01:49 -05002139/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002140static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002141 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2142 uint32_t *arg_count_props)
2143{
Rob Clark88a48e22014-12-18 16:01:50 -05002144 int props_count;
2145 int i, ret, copied;
2146
2147 props_count = obj->properties->count;
2148 if (!atomic)
2149 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002150
2151 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002152 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002153 struct drm_property *prop = obj->properties->properties[i];
2154 uint64_t val;
2155
Rob Clark88a48e22014-12-18 16:01:50 -05002156 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2157 continue;
2158
Rob Clark95cbf112014-12-18 16:01:49 -05002159 ret = drm_object_property_get_value(obj, prop, &val);
2160 if (ret)
2161 return ret;
2162
2163 if (put_user(prop->base.id, prop_ptr + copied))
2164 return -EFAULT;
2165
2166 if (put_user(val, prop_values + copied))
2167 return -EFAULT;
2168
2169 copied++;
2170 }
2171 }
2172 *arg_count_props = props_count;
2173
2174 return 0;
2175}
2176
Dave Airlief453ba02008-11-07 14:05:41 -08002177/**
2178 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002179 * @dev: drm device for the ioctl
2180 * @data: data pointer for the ioctl
2181 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002182 *
Dave Airlief453ba02008-11-07 14:05:41 -08002183 * Construct a connector configuration structure to return to the user.
2184 *
2185 * Called by the user via ioctl.
2186 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002187 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002188 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002189 */
2190int drm_mode_getconnector(struct drm_device *dev, void *data,
2191 struct drm_file *file_priv)
2192{
2193 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002194 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002195 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002196 struct drm_display_mode *mode;
2197 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002198 int encoders_count = 0;
2199 int ret = 0;
2200 int copied = 0;
2201 int i;
2202 struct drm_mode_modeinfo u_mode;
2203 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002204 uint32_t __user *encoder_ptr;
2205
Dave Airliefb3b06c2011-02-08 13:55:21 +10002206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2207 return -EINVAL;
2208
Dave Airlief453ba02008-11-07 14:05:41 -08002209 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2210
Daniel Vetter7b240562012-12-12 00:35:33 +01002211 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002212
Dave Airlieb164d312016-04-27 11:10:09 +10002213 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002214 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002215 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002216 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002217 }
Dave Airlief453ba02008-11-07 14:05:41 -08002218
Thierry Reding01073b02014-12-10 13:03:38 +01002219 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2220 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002221 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002222
2223 if (out_resp->count_modes == 0) {
2224 connector->funcs->fill_modes(connector,
2225 dev->mode_config.max_width,
2226 dev->mode_config.max_height);
2227 }
2228
2229 /* delayed so we get modes regardless of pre-fill_modes state */
2230 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002231 if (drm_mode_expose_to_userspace(mode, file_priv))
2232 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002233
2234 out_resp->connector_id = connector->base.id;
2235 out_resp->connector_type = connector->connector_type;
2236 out_resp->connector_type_id = connector->connector_type_id;
2237 out_resp->mm_width = connector->display_info.width_mm;
2238 out_resp->mm_height = connector->display_info.height_mm;
2239 out_resp->subpixel = connector->display_info.subpixel_order;
2240 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002241
2242 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002243 encoder = drm_connector_get_encoder(connector);
2244 if (encoder)
2245 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002246 else
2247 out_resp->encoder_id = 0;
2248
2249 /*
2250 * This ioctl is called twice, once to determine how much space is
2251 * needed, and the 2nd time to fill it.
2252 */
2253 if ((out_resp->count_modes >= mode_count) && mode_count) {
2254 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002255 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002256 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002257 if (!drm_mode_expose_to_userspace(mode, file_priv))
2258 continue;
2259
Daniel Stone934a8a82015-05-22 13:34:48 +01002260 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002261 if (copy_to_user(mode_ptr + copied,
2262 &u_mode, sizeof(u_mode))) {
2263 ret = -EFAULT;
2264 goto out;
2265 }
2266 copied++;
2267 }
2268 }
2269 out_resp->count_modes = mode_count;
2270
Rob Clark88a48e22014-12-18 16:01:50 -05002271 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002272 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2273 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2274 &out_resp->count_props);
2275 if (ret)
2276 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002277
2278 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2279 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002280 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002281 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2282 if (connector->encoder_ids[i] != 0) {
2283 if (put_user(connector->encoder_ids[i],
2284 encoder_ptr + copied)) {
2285 ret = -EFAULT;
2286 goto out;
2287 }
2288 copied++;
2289 }
2290 }
2291 }
2292 out_resp->count_encoders = encoders_count;
2293
2294out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002295 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002296
Dave Airlieb164d312016-04-27 11:10:09 +10002297 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002298out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002299 mutex_unlock(&dev->mode_config.mutex);
2300
Dave Airlief453ba02008-11-07 14:05:41 -08002301 return ret;
2302}
2303
Daniel Vetterabd69c52014-11-25 23:50:05 +01002304static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2305{
2306 struct drm_connector *connector;
2307 struct drm_device *dev = encoder->dev;
2308 bool uses_atomic = false;
2309
2310 /* For atomic drivers only state objects are synchronously updated and
2311 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002312 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002313 if (!connector->state)
2314 continue;
2315
2316 uses_atomic = true;
2317
2318 if (connector->state->best_encoder != encoder)
2319 continue;
2320
2321 return connector->state->crtc;
2322 }
2323
2324 /* Don't return stale data (e.g. pending async disable). */
2325 if (uses_atomic)
2326 return NULL;
2327
2328 return encoder->crtc;
2329}
2330
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002331/**
2332 * drm_mode_getencoder - get encoder configuration
2333 * @dev: drm device for the ioctl
2334 * @data: data pointer for the ioctl
2335 * @file_priv: drm file for the ioctl call
2336 *
2337 * Construct a encoder configuration structure to return to the user.
2338 *
2339 * Called by the user via ioctl.
2340 *
2341 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002342 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002343 */
Dave Airlief453ba02008-11-07 14:05:41 -08002344int drm_mode_getencoder(struct drm_device *dev, void *data,
2345 struct drm_file *file_priv)
2346{
2347 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002348 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002349 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002350
Dave Airliefb3b06c2011-02-08 13:55:21 +10002351 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2352 return -EINVAL;
2353
Rob Clarka2b34e22013-10-05 16:36:52 -04002354 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002355 if (!encoder)
2356 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002357
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002358 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002359 crtc = drm_encoder_get_crtc(encoder);
2360 if (crtc)
2361 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002362 else
2363 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002364 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2365
Dave Airlief453ba02008-11-07 14:05:41 -08002366 enc_resp->encoder_type = encoder->encoder_type;
2367 enc_resp->encoder_id = encoder->base.id;
2368 enc_resp->possible_crtcs = encoder->possible_crtcs;
2369 enc_resp->possible_clones = encoder->possible_clones;
2370
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002371 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002372}
2373
2374/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002375 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002376 * @dev: DRM device
2377 * @data: ioctl data
2378 * @file_priv: DRM file info
2379 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002380 * Construct a list of plane ids to return to the user.
2381 *
2382 * Called by the user via ioctl.
2383 *
2384 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002385 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002386 */
2387int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002388 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389{
2390 struct drm_mode_get_plane_res *plane_resp = data;
2391 struct drm_mode_config *config;
2392 struct drm_plane *plane;
2393 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002394 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002395 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002396
2397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2398 return -EINVAL;
2399
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002400 config = &dev->mode_config;
2401
Matt Roper681e7ec2014-04-01 15:22:42 -07002402 if (file_priv->universal_planes)
2403 num_planes = config->num_total_plane;
2404 else
2405 num_planes = config->num_overlay_plane;
2406
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002407 /*
2408 * This ioctl is called twice, once to determine how much space is
2409 * needed, and the 2nd time to fill it.
2410 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002411 if (num_planes &&
2412 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002413 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002415 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002416 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002417 /*
2418 * Unless userspace set the 'universal planes'
2419 * capability bit, only advertise overlays.
2420 */
2421 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2422 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002423 continue;
2424
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002425 if (put_user(plane->base.id, plane_ptr + copied))
2426 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002427 copied++;
2428 }
2429 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002430 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002431
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002432 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002433}
2434
2435/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002436 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002437 * @dev: DRM device
2438 * @data: ioctl data
2439 * @file_priv: DRM file info
2440 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002441 * Construct a plane configuration structure to return to the user.
2442 *
2443 * Called by the user via ioctl.
2444 *
2445 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002446 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002447 */
2448int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002449 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002450{
2451 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002452 struct drm_plane *plane;
2453 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002454
2455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2456 return -EINVAL;
2457
Rob Clarka2b34e22013-10-05 16:36:52 -04002458 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002459 if (!plane)
2460 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002461
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002462 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002463 if (plane->crtc)
2464 plane_resp->crtc_id = plane->crtc->base.id;
2465 else
2466 plane_resp->crtc_id = 0;
2467
2468 if (plane->fb)
2469 plane_resp->fb_id = plane->fb->base.id;
2470 else
2471 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002472 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002473
2474 plane_resp->plane_id = plane->base.id;
2475 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002476 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002477
2478 /*
2479 * This ioctl is called twice, once to determine how much space is
2480 * needed, and the 2nd time to fill it.
2481 */
2482 if (plane->format_count &&
2483 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002484 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002485 if (copy_to_user(format_ptr,
2486 plane->format_types,
2487 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002488 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002489 }
2490 }
2491 plane_resp->count_format_types = plane->format_count;
2492
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002493 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002494}
2495
Laurent Pinchartead86102015-03-05 02:25:43 +02002496/**
2497 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2498 * @plane: plane to check for format support
2499 * @format: the pixel format
2500 *
2501 * Returns:
2502 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2503 * otherwise.
2504 */
2505int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2506{
2507 unsigned int i;
2508
2509 for (i = 0; i < plane->format_count; i++) {
2510 if (format == plane->format_types[i])
2511 return 0;
2512 }
2513
2514 return -EINVAL;
2515}
2516
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002517static int check_src_coords(uint32_t src_x, uint32_t src_y,
2518 uint32_t src_w, uint32_t src_h,
2519 const struct drm_framebuffer *fb)
2520{
2521 unsigned int fb_width, fb_height;
2522
2523 fb_width = fb->width << 16;
2524 fb_height = fb->height << 16;
2525
2526 /* Make sure source coordinates are inside the fb. */
2527 if (src_w > fb_width ||
2528 src_x > fb_width - src_w ||
2529 src_h > fb_height ||
2530 src_y > fb_height - src_h) {
2531 DRM_DEBUG_KMS("Invalid source coordinates "
2532 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2533 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2534 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2535 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2536 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2537 return -ENOSPC;
2538 }
2539
2540 return 0;
2541}
2542
Matt Roperb36552b2014-06-10 08:28:09 -07002543/*
2544 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002545 *
Matt Roperb36552b2014-06-10 08:28:09 -07002546 * Note that we assume an extra reference has already been taken on fb. If the
2547 * update fails, this reference will be dropped before return; if it succeeds,
2548 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002549 *
Matt Roperb36552b2014-06-10 08:28:09 -07002550 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002551 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002552static int __setplane_internal(struct drm_plane *plane,
2553 struct drm_crtc *crtc,
2554 struct drm_framebuffer *fb,
2555 int32_t crtc_x, int32_t crtc_y,
2556 uint32_t crtc_w, uint32_t crtc_h,
2557 /* src_{x,y,w,h} values are 16.16 fixed point */
2558 uint32_t src_x, uint32_t src_y,
2559 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002560{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002561 int ret = 0;
2562
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002563 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002564 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002565 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002566 ret = plane->funcs->disable_plane(plane);
2567 if (!ret) {
2568 plane->crtc = NULL;
2569 plane->fb = NULL;
2570 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002571 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002572 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002573 goto out;
2574 }
2575
Matt Roper7f994f32014-05-29 08:06:51 -07002576 /* Check whether this plane is usable on this CRTC */
2577 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2578 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582
Ville Syrjälä62443be2011-12-20 00:06:47 +02002583 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002584 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2585 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002586 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2587 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002588 goto out;
2589 }
2590
Matt Roper3968be92015-04-13 11:06:13 -07002591 /* Give drivers some help against integer overflows */
2592 if (crtc_w > INT_MAX ||
2593 crtc_x > INT_MAX - (int32_t) crtc_w ||
2594 crtc_h > INT_MAX ||
2595 crtc_y > INT_MAX - (int32_t) crtc_h) {
2596 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2597 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002598 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002599 goto out;
2600 }
2601
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002602 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2603 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002604 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002605
Daniel Vetter3d30a592014-07-27 13:42:42 +02002606 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002607 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002608 crtc_x, crtc_y, crtc_w, crtc_h,
2609 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002610 if (!ret) {
2611 plane->crtc = crtc;
2612 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002613 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002614 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002615 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002616 }
2617
2618out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002619 if (fb)
2620 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002621 if (plane->old_fb)
2622 drm_framebuffer_unreference(plane->old_fb);
2623 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002624
2625 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002626}
Matt Roperb36552b2014-06-10 08:28:09 -07002627
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002628static int setplane_internal(struct drm_plane *plane,
2629 struct drm_crtc *crtc,
2630 struct drm_framebuffer *fb,
2631 int32_t crtc_x, int32_t crtc_y,
2632 uint32_t crtc_w, uint32_t crtc_h,
2633 /* src_{x,y,w,h} values are 16.16 fixed point */
2634 uint32_t src_x, uint32_t src_y,
2635 uint32_t src_w, uint32_t src_h)
2636{
2637 int ret;
2638
2639 drm_modeset_lock_all(plane->dev);
2640 ret = __setplane_internal(plane, crtc, fb,
2641 crtc_x, crtc_y, crtc_w, crtc_h,
2642 src_x, src_y, src_w, src_h);
2643 drm_modeset_unlock_all(plane->dev);
2644
2645 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002646}
2647
2648/**
2649 * drm_mode_setplane - configure a plane's configuration
2650 * @dev: DRM device
2651 * @data: ioctl data*
2652 * @file_priv: DRM file info
2653 *
2654 * Set plane configuration, including placement, fb, scaling, and other factors.
2655 * Or pass a NULL fb to disable (planes may be disabled without providing a
2656 * valid crtc).
2657 *
2658 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002659 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002660 */
2661int drm_mode_setplane(struct drm_device *dev, void *data,
2662 struct drm_file *file_priv)
2663{
2664 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002665 struct drm_plane *plane;
2666 struct drm_crtc *crtc = NULL;
2667 struct drm_framebuffer *fb = NULL;
2668
2669 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2670 return -EINVAL;
2671
Matt Roperb36552b2014-06-10 08:28:09 -07002672 /*
2673 * First, find the plane, crtc, and fb objects. If not available,
2674 * we don't bother to call the driver.
2675 */
Rob Clark933f6222014-11-25 20:33:11 -05002676 plane = drm_plane_find(dev, plane_req->plane_id);
2677 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002678 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2679 plane_req->plane_id);
2680 return -ENOENT;
2681 }
Matt Roperb36552b2014-06-10 08:28:09 -07002682
2683 if (plane_req->fb_id) {
2684 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2685 if (!fb) {
2686 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2687 plane_req->fb_id);
2688 return -ENOENT;
2689 }
2690
Rob Clark933f6222014-11-25 20:33:11 -05002691 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2692 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002693 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2694 plane_req->crtc_id);
2695 return -ENOENT;
2696 }
Matt Roperb36552b2014-06-10 08:28:09 -07002697 }
2698
Matt Roper161d0dc2014-06-10 08:28:10 -07002699 /*
2700 * setplane_internal will take care of deref'ing either the old or new
2701 * framebuffer depending on success.
2702 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002703 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002704 plane_req->crtc_x, plane_req->crtc_y,
2705 plane_req->crtc_w, plane_req->crtc_h,
2706 plane_req->src_x, plane_req->src_y,
2707 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002708}
2709
2710/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002711 * drm_mode_set_config_internal - helper to call ->set_config
2712 * @set: modeset config to set
2713 *
2714 * This is a little helper to wrap internal calls to the ->set_config driver
2715 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002716 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002717 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002718 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002719 */
2720int drm_mode_set_config_internal(struct drm_mode_set *set)
2721{
2722 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002723 struct drm_framebuffer *fb;
2724 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002725 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002726
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002727 /*
2728 * NOTE: ->set_config can also disable other crtcs (if we steal all
2729 * connectors from it), hence we need to refcount the fbs across all
2730 * crtcs. Atomic modeset will have saner semantics ...
2731 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002732 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002733 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002734
Daniel Vetterb0d12322012-12-11 01:07:12 +01002735 fb = set->fb;
2736
2737 ret = crtc->funcs->set_config(set);
2738 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002739 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002740 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002741 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002742
Daniel Vettere4f62542015-07-09 23:44:35 +02002743 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002744 if (tmp->primary->fb)
2745 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002746 if (tmp->primary->old_fb)
2747 drm_framebuffer_unreference(tmp->primary->old_fb);
2748 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002749 }
2750
2751 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002752}
2753EXPORT_SYMBOL(drm_mode_set_config_internal);
2754
Matt Roperaf936292014-04-01 15:22:34 -07002755/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002756 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2757 * @mode: mode to query
2758 * @hdisplay: hdisplay value to fill in
2759 * @vdisplay: vdisplay value to fill in
2760 *
2761 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2762 * the appropriate layout.
2763 */
2764void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2765 int *hdisplay, int *vdisplay)
2766{
2767 struct drm_display_mode adjusted;
2768
2769 drm_mode_copy(&adjusted, mode);
2770 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2771 *hdisplay = adjusted.crtc_hdisplay;
2772 *vdisplay = adjusted.crtc_vdisplay;
2773}
2774EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2775
2776/**
Matt Roperaf936292014-04-01 15:22:34 -07002777 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2778 * CRTC viewport
2779 * @crtc: CRTC that framebuffer will be displayed on
2780 * @x: x panning
2781 * @y: y panning
2782 * @mode: mode that framebuffer will be displayed under
2783 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002784 */
Matt Roperaf936292014-04-01 15:22:34 -07002785int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2786 int x, int y,
2787 const struct drm_display_mode *mode,
2788 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002789
2790{
2791 int hdisplay, vdisplay;
2792
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002793 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002794
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002795 if (crtc->state &&
2796 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2797 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002798 swap(hdisplay, vdisplay);
2799
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002800 return check_src_coords(x << 16, y << 16,
2801 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002802}
Matt Roperaf936292014-04-01 15:22:34 -07002803EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002804
Daniel Vetter2d13b672012-12-11 13:47:23 +01002805/**
Dave Airlief453ba02008-11-07 14:05:41 -08002806 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002807 * @dev: drm device for the ioctl
2808 * @data: data pointer for the ioctl
2809 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002810 *
Dave Airlief453ba02008-11-07 14:05:41 -08002811 * Build a new CRTC configuration based on user request.
2812 *
2813 * Called by the user via ioctl.
2814 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002815 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002816 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002817 */
2818int drm_mode_setcrtc(struct drm_device *dev, void *data,
2819 struct drm_file *file_priv)
2820{
2821 struct drm_mode_config *config = &dev->mode_config;
2822 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002823 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002824 struct drm_connector **connector_set = NULL, *connector;
2825 struct drm_framebuffer *fb = NULL;
2826 struct drm_display_mode *mode = NULL;
2827 struct drm_mode_set set;
2828 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002829 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002830 int i;
2831
Dave Airliefb3b06c2011-02-08 13:55:21 +10002832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2833 return -EINVAL;
2834
Zhao Junwang01447e92015-07-07 17:08:35 +08002835 /*
2836 * Universal plane src offsets are only 16.16, prevent havoc for
2837 * drivers using universal plane code internally.
2838 */
2839 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002840 return -ERANGE;
2841
Daniel Vetter84849902012-12-02 00:28:11 +01002842 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002843 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2844 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002845 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002846 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002847 goto out;
2848 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002849 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002850
2851 if (crtc_req->mode_valid) {
2852 /* If we have a mode we need a framebuffer. */
2853 /* If we pass -1, set the mode with the currently bound fb */
2854 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002855 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002856 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2857 ret = -EINVAL;
2858 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002859 }
Matt Roperf4510a22014-04-01 15:22:40 -07002860 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002861 /* Make refcounting symmetric with the lookup path. */
2862 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002863 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002864 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2865 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002866 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2867 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002868 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002869 goto out;
2870 }
Dave Airlief453ba02008-11-07 14:05:41 -08002871 }
2872
2873 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002874 if (!mode) {
2875 ret = -ENOMEM;
2876 goto out;
2877 }
2878
Daniel Stone934a8a82015-05-22 13:34:48 +01002879 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002880 if (ret) {
2881 DRM_DEBUG_KMS("Invalid mode\n");
2882 goto out;
2883 }
2884
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002885 /*
2886 * Check whether the primary plane supports the fb pixel format.
2887 * Drivers not implementing the universal planes API use a
2888 * default formats list provided by the DRM core which doesn't
2889 * match real hardware capabilities. Skip the check in that
2890 * case.
2891 */
2892 if (!crtc->primary->format_default) {
2893 ret = drm_plane_check_pixel_format(crtc->primary,
2894 fb->pixel_format);
2895 if (ret) {
2896 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2897 drm_get_format_name(fb->pixel_format));
2898 goto out;
2899 }
2900 }
2901
Damien Lespiauc11e9282013-09-25 16:45:30 +01002902 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2903 mode, fb);
2904 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002905 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002906
Dave Airlief453ba02008-11-07 14:05:41 -08002907 }
2908
2909 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002910 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002911 ret = -EINVAL;
2912 goto out;
2913 }
2914
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002915 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002916 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002917 crtc_req->count_connectors);
2918 ret = -EINVAL;
2919 goto out;
2920 }
2921
2922 if (crtc_req->count_connectors > 0) {
2923 u32 out_id;
2924
2925 /* Avoid unbounded kernel memory allocation */
2926 if (crtc_req->count_connectors > config->num_connector) {
2927 ret = -EINVAL;
2928 goto out;
2929 }
2930
Thierry Reding2f6c5382014-12-10 13:03:36 +01002931 connector_set = kmalloc_array(crtc_req->count_connectors,
2932 sizeof(struct drm_connector *),
2933 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002934 if (!connector_set) {
2935 ret = -ENOMEM;
2936 goto out;
2937 }
2938
2939 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002940 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002941 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002942 if (get_user(out_id, &set_connectors_ptr[i])) {
2943 ret = -EFAULT;
2944 goto out;
2945 }
2946
Dave Airlieb164d312016-04-27 11:10:09 +10002947 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002948 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002949 DRM_DEBUG_KMS("Connector id %d unknown\n",
2950 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002951 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002952 goto out;
2953 }
Jerome Glisse94401062010-07-15 15:43:25 -04002954 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2955 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002956 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002957
2958 connector_set[i] = connector;
2959 }
2960 }
2961
2962 set.crtc = crtc;
2963 set.x = crtc_req->x;
2964 set.y = crtc_req->y;
2965 set.mode = mode;
2966 set.connectors = connector_set;
2967 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002968 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002969 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002970
2971out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002972 if (fb)
2973 drm_framebuffer_unreference(fb);
2974
Dave Airlieb164d312016-04-27 11:10:09 +10002975 if (connector_set) {
2976 for (i = 0; i < crtc_req->count_connectors; i++) {
2977 if (connector_set[i])
2978 drm_connector_unreference(connector_set[i]);
2979 }
2980 }
Dave Airlief453ba02008-11-07 14:05:41 -08002981 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002982 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002983 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002984 return ret;
2985}
2986
Matt Roper161d0dc2014-06-10 08:28:10 -07002987/**
2988 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2989 * universal plane handler call
2990 * @crtc: crtc to update cursor for
2991 * @req: data pointer for the ioctl
2992 * @file_priv: drm file for the ioctl call
2993 *
2994 * Legacy cursor ioctl's work directly with driver buffer handles. To
2995 * translate legacy ioctl calls into universal plane handler calls, we need to
2996 * wrap the native buffer handle in a drm_framebuffer.
2997 *
2998 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2999 * buffer with a pitch of 4*width; the universal plane interface should be used
3000 * directly in cases where the hardware can support other buffer settings and
3001 * userspace wants to make use of these capabilities.
3002 *
3003 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003004 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07003005 */
3006static int drm_mode_cursor_universal(struct drm_crtc *crtc,
3007 struct drm_mode_cursor2 *req,
3008 struct drm_file *file_priv)
3009{
3010 struct drm_device *dev = crtc->dev;
3011 struct drm_framebuffer *fb = NULL;
3012 struct drm_mode_fb_cmd2 fbreq = {
3013 .width = req->width,
3014 .height = req->height,
3015 .pixel_format = DRM_FORMAT_ARGB8888,
3016 .pitches = { req->width * 4 },
3017 .handles = { req->handle },
3018 };
3019 int32_t crtc_x, crtc_y;
3020 uint32_t crtc_w = 0, crtc_h = 0;
3021 uint32_t src_w = 0, src_h = 0;
3022 int ret = 0;
3023
3024 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003025 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07003026
3027 /*
3028 * Obtain fb we'll be using (either new or existing) and take an extra
3029 * reference to it if fb != null. setplane will take care of dropping
3030 * the reference if the plane update fails.
3031 */
3032 if (req->flags & DRM_MODE_CURSOR_BO) {
3033 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00003034 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07003035 if (IS_ERR(fb)) {
3036 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
3037 return PTR_ERR(fb);
3038 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02003039 fb->hot_x = req->hot_x;
3040 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07003041 } else {
3042 fb = NULL;
3043 }
3044 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07003045 fb = crtc->cursor->fb;
3046 if (fb)
3047 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07003048 }
3049
3050 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3051 crtc_x = req->x;
3052 crtc_y = req->y;
3053 } else {
3054 crtc_x = crtc->cursor_x;
3055 crtc_y = crtc->cursor_y;
3056 }
3057
3058 if (fb) {
3059 crtc_w = fb->width;
3060 crtc_h = fb->height;
3061 src_w = fb->width << 16;
3062 src_h = fb->height << 16;
3063 }
3064
3065 /*
3066 * setplane_internal will take care of deref'ing either the old or new
3067 * framebuffer depending on success.
3068 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003069 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003070 crtc_x, crtc_y, crtc_w, crtc_h,
3071 0, 0, src_w, src_h);
3072
3073 /* Update successful; save new cursor position, if necessary */
3074 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3075 crtc->cursor_x = req->x;
3076 crtc->cursor_y = req->y;
3077 }
3078
3079 return ret;
3080}
3081
Dave Airlie4c813d42013-06-20 11:48:52 +10003082static int drm_mode_cursor_common(struct drm_device *dev,
3083 struct drm_mode_cursor2 *req,
3084 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003085{
Dave Airlief453ba02008-11-07 14:05:41 -08003086 struct drm_crtc *crtc;
3087 int ret = 0;
3088
Dave Airliefb3b06c2011-02-08 13:55:21 +10003089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3090 return -EINVAL;
3091
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003092 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003093 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003094
Rob Clarka2b34e22013-10-05 16:36:52 -04003095 crtc = drm_crtc_find(dev, req->crtc_id);
3096 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003097 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003098 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003099 }
Dave Airlief453ba02008-11-07 14:05:41 -08003100
Matt Roper161d0dc2014-06-10 08:28:10 -07003101 /*
3102 * If this crtc has a universal cursor plane, call that plane's update
3103 * handler rather than using legacy cursor handlers.
3104 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003105 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003106 if (crtc->cursor) {
3107 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3108 goto out;
3109 }
3110
Dave Airlief453ba02008-11-07 14:05:41 -08003111 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003112 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003113 ret = -ENXIO;
3114 goto out;
3115 }
3116 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003117 if (crtc->funcs->cursor_set2)
3118 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3119 req->width, req->height, req->hot_x, req->hot_y);
3120 else
3121 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3122 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003123 }
3124
3125 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3126 if (crtc->funcs->cursor_move) {
3127 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3128 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003129 ret = -EFAULT;
3130 goto out;
3131 }
3132 }
3133out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003134 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003135
Dave Airlief453ba02008-11-07 14:05:41 -08003136 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003137
3138}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003139
3140
3141/**
3142 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3143 * @dev: drm device for the ioctl
3144 * @data: data pointer for the ioctl
3145 * @file_priv: drm file for the ioctl call
3146 *
3147 * Set the cursor configuration based on user request.
3148 *
3149 * Called by the user via ioctl.
3150 *
3151 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003152 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003153 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003154int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003155 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003156{
3157 struct drm_mode_cursor *req = data;
3158 struct drm_mode_cursor2 new_req;
3159
3160 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3161 new_req.hot_x = new_req.hot_y = 0;
3162
3163 return drm_mode_cursor_common(dev, &new_req, file_priv);
3164}
3165
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003166/**
3167 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3168 * @dev: drm device for the ioctl
3169 * @data: data pointer for the ioctl
3170 * @file_priv: drm file for the ioctl call
3171 *
3172 * Set the cursor configuration based on user request. This implements the 2nd
3173 * version of the cursor ioctl, which allows userspace to additionally specify
3174 * the hotspot of the pointer.
3175 *
3176 * Called by the user via ioctl.
3177 *
3178 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003179 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003180 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003181int drm_mode_cursor2_ioctl(struct drm_device *dev,
3182 void *data, struct drm_file *file_priv)
3183{
3184 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003185
Dave Airlie4c813d42013-06-20 11:48:52 +10003186 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003187}
3188
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003189/**
3190 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3191 * @bpp: bits per pixels
3192 * @depth: bit depth per pixel
3193 *
3194 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3195 * Useful in fbdev emulation code, since that deals in those values.
3196 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003197uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3198{
3199 uint32_t fmt;
3200
3201 switch (bpp) {
3202 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003203 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003204 break;
3205 case 16:
3206 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003207 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003208 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003209 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003210 break;
3211 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003212 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003213 break;
3214 case 32:
3215 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003216 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003217 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003218 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003219 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003220 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003221 break;
3222 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003223 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3224 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003225 break;
3226 }
3227
3228 return fmt;
3229}
3230EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3231
Dave Airlief453ba02008-11-07 14:05:41 -08003232/**
3233 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003234 * @dev: drm device for the ioctl
3235 * @data: data pointer for the ioctl
3236 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003237 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003238 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003239 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003240 *
3241 * Called by the user via ioctl.
3242 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003243 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003244 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003245 */
3246int drm_mode_addfb(struct drm_device *dev,
3247 void *data, struct drm_file *file_priv)
3248{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003249 struct drm_mode_fb_cmd *or = data;
3250 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003251 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003252
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003253 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003254 r.fb_id = or->fb_id;
3255 r.width = or->width;
3256 r.height = or->height;
3257 r.pitches[0] = or->pitch;
3258 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3259 r.handles[0] = or->handle;
3260
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003261 ret = drm_mode_addfb2(dev, &r, file_priv);
3262 if (ret)
3263 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003264
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003265 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003266
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003267 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003268}
3269
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003270static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003271{
3272 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3273
3274 switch (format) {
3275 case DRM_FORMAT_C8:
3276 case DRM_FORMAT_RGB332:
3277 case DRM_FORMAT_BGR233:
3278 case DRM_FORMAT_XRGB4444:
3279 case DRM_FORMAT_XBGR4444:
3280 case DRM_FORMAT_RGBX4444:
3281 case DRM_FORMAT_BGRX4444:
3282 case DRM_FORMAT_ARGB4444:
3283 case DRM_FORMAT_ABGR4444:
3284 case DRM_FORMAT_RGBA4444:
3285 case DRM_FORMAT_BGRA4444:
3286 case DRM_FORMAT_XRGB1555:
3287 case DRM_FORMAT_XBGR1555:
3288 case DRM_FORMAT_RGBX5551:
3289 case DRM_FORMAT_BGRX5551:
3290 case DRM_FORMAT_ARGB1555:
3291 case DRM_FORMAT_ABGR1555:
3292 case DRM_FORMAT_RGBA5551:
3293 case DRM_FORMAT_BGRA5551:
3294 case DRM_FORMAT_RGB565:
3295 case DRM_FORMAT_BGR565:
3296 case DRM_FORMAT_RGB888:
3297 case DRM_FORMAT_BGR888:
3298 case DRM_FORMAT_XRGB8888:
3299 case DRM_FORMAT_XBGR8888:
3300 case DRM_FORMAT_RGBX8888:
3301 case DRM_FORMAT_BGRX8888:
3302 case DRM_FORMAT_ARGB8888:
3303 case DRM_FORMAT_ABGR8888:
3304 case DRM_FORMAT_RGBA8888:
3305 case DRM_FORMAT_BGRA8888:
3306 case DRM_FORMAT_XRGB2101010:
3307 case DRM_FORMAT_XBGR2101010:
3308 case DRM_FORMAT_RGBX1010102:
3309 case DRM_FORMAT_BGRX1010102:
3310 case DRM_FORMAT_ARGB2101010:
3311 case DRM_FORMAT_ABGR2101010:
3312 case DRM_FORMAT_RGBA1010102:
3313 case DRM_FORMAT_BGRA1010102:
3314 case DRM_FORMAT_YUYV:
3315 case DRM_FORMAT_YVYU:
3316 case DRM_FORMAT_UYVY:
3317 case DRM_FORMAT_VYUY:
3318 case DRM_FORMAT_AYUV:
3319 case DRM_FORMAT_NV12:
3320 case DRM_FORMAT_NV21:
3321 case DRM_FORMAT_NV16:
3322 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003323 case DRM_FORMAT_NV24:
3324 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003325 case DRM_FORMAT_YUV410:
3326 case DRM_FORMAT_YVU410:
3327 case DRM_FORMAT_YUV411:
3328 case DRM_FORMAT_YVU411:
3329 case DRM_FORMAT_YUV420:
3330 case DRM_FORMAT_YVU420:
3331 case DRM_FORMAT_YUV422:
3332 case DRM_FORMAT_YVU422:
3333 case DRM_FORMAT_YUV444:
3334 case DRM_FORMAT_YVU444:
3335 return 0;
3336 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003337 DRM_DEBUG_KMS("invalid pixel format %s\n",
3338 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003339 return -EINVAL;
3340 }
3341}
3342
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003343static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003344{
3345 int ret, hsub, vsub, num_planes, i;
3346
3347 ret = format_check(r);
3348 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003349 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3350 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003351 return ret;
3352 }
3353
3354 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3355 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3356 num_planes = drm_format_num_planes(r->pixel_format);
3357
3358 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003359 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003360 return -EINVAL;
3361 }
3362
3363 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003364 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003365 return -EINVAL;
3366 }
3367
3368 for (i = 0; i < num_planes; i++) {
3369 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003370 unsigned int height = r->height / (i != 0 ? vsub : 1);
3371 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003372
3373 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003374 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003375 return -EINVAL;
3376 }
3377
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003378 if ((uint64_t) width * cpp > UINT_MAX)
3379 return -ERANGE;
3380
3381 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3382 return -ERANGE;
3383
3384 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003385 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003386 return -EINVAL;
3387 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003388
3389 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3390 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3391 r->modifier[i], i);
3392 return -EINVAL;
3393 }
Rob Clark570655b2015-01-30 20:18:11 +05303394
3395 /* modifier specific checks: */
3396 switch (r->modifier[i]) {
3397 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3398 /* NOTE: the pitch restriction may be lifted later if it turns
3399 * out that no hw has this restriction:
3400 */
3401 if (r->pixel_format != DRM_FORMAT_NV12 ||
3402 width % 128 || height % 32 ||
3403 r->pitches[i] % 128) {
3404 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3405 return -EINVAL;
3406 }
3407 break;
3408
3409 default:
3410 break;
3411 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003412 }
3413
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003414 for (i = num_planes; i < 4; i++) {
3415 if (r->modifier[i]) {
3416 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3417 return -EINVAL;
3418 }
3419
3420 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3421 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3422 continue;
3423
3424 if (r->handles[i]) {
3425 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3426 return -EINVAL;
3427 }
3428
3429 if (r->pitches[i]) {
3430 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3431 return -EINVAL;
3432 }
3433
3434 if (r->offsets[i]) {
3435 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3436 return -EINVAL;
3437 }
3438 }
3439
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003440 return 0;
3441}
3442
Chris Wilson9a6f5132015-02-25 13:45:26 +00003443static struct drm_framebuffer *
3444internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003445 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003446 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003447{
3448 struct drm_mode_config *config = &dev->mode_config;
3449 struct drm_framebuffer *fb;
3450 int ret;
3451
Rob Clarke3eb3252015-02-05 14:41:52 +00003452 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003453 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3454 return ERR_PTR(-EINVAL);
3455 }
3456
3457 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3458 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3459 r->width, config->min_width, config->max_width);
3460 return ERR_PTR(-EINVAL);
3461 }
3462 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3463 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3464 r->height, config->min_height, config->max_height);
3465 return ERR_PTR(-EINVAL);
3466 }
3467
Rob Clarke3eb3252015-02-05 14:41:52 +00003468 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3469 !dev->mode_config.allow_fb_modifiers) {
3470 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3471 return ERR_PTR(-EINVAL);
3472 }
3473
Matt Roperc394c2b2014-06-10 08:28:08 -07003474 ret = framebuffer_check(r);
3475 if (ret)
3476 return ERR_PTR(ret);
3477
3478 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3479 if (IS_ERR(fb)) {
3480 DRM_DEBUG_KMS("could not create framebuffer\n");
3481 return fb;
3482 }
3483
Matt Roperc394c2b2014-06-10 08:28:08 -07003484 return fb;
3485}
3486
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003487/**
3488 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003489 * @dev: drm device for the ioctl
3490 * @data: data pointer for the ioctl
3491 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003492 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003493 * Add a new FB to the specified CRTC, given a user request with format. This is
3494 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3495 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003496 *
3497 * Called by the user via ioctl.
3498 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003499 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003500 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003501 */
3502int drm_mode_addfb2(struct drm_device *dev,
3503 void *data, struct drm_file *file_priv)
3504{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003505 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003506 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003507
Dave Airliefb3b06c2011-02-08 13:55:21 +10003508 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3509 return -EINVAL;
3510
Chris Wilson9a6f5132015-02-25 13:45:26 +00003511 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003512 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003513 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003514
Chris Wilson9a6f5132015-02-25 13:45:26 +00003515 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003516 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003517
3518 /* Transfer ownership to the filp for reaping on close */
3519 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003520 list_add(&fb->filp_head, &file_priv->fbs);
3521 mutex_unlock(&file_priv->fbs_lock);
3522
Matt Roperc394c2b2014-06-10 08:28:08 -07003523 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003524}
3525
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003526struct drm_mode_rmfb_work {
3527 struct work_struct work;
3528 struct list_head fbs;
3529};
3530
3531static void drm_mode_rmfb_work_fn(struct work_struct *w)
3532{
3533 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3534
3535 while (!list_empty(&arg->fbs)) {
3536 struct drm_framebuffer *fb =
3537 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3538
3539 list_del_init(&fb->filp_head);
3540 drm_framebuffer_remove(fb);
3541 }
3542}
3543
Dave Airlief453ba02008-11-07 14:05:41 -08003544/**
3545 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003546 * @dev: drm device for the ioctl
3547 * @data: data pointer for the ioctl
3548 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003549 *
Dave Airlief453ba02008-11-07 14:05:41 -08003550 * Remove the FB specified by the user.
3551 *
3552 * Called by the user via ioctl.
3553 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003554 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003555 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003556 */
3557int drm_mode_rmfb(struct drm_device *dev,
3558 void *data, struct drm_file *file_priv)
3559{
Dave Airlief453ba02008-11-07 14:05:41 -08003560 struct drm_framebuffer *fb = NULL;
3561 struct drm_framebuffer *fbl = NULL;
3562 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003563 int found = 0;
3564
Dave Airliefb3b06c2011-02-08 13:55:21 +10003565 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3566 return -EINVAL;
3567
Dave Airlie72fe90b2016-04-15 15:10:40 +10003568 fb = drm_framebuffer_lookup(dev, *id);
3569 if (!fb)
3570 return -ENOENT;
3571
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003572 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003573 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3574 if (fb == fbl)
3575 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003576 if (!found) {
3577 mutex_unlock(&file_priv->fbs_lock);
3578 goto fail_unref;
3579 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003580
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003581 list_del_init(&fb->filp_head);
3582 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003583
Dave Airlie72fe90b2016-04-15 15:10:40 +10003584 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003585 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003586
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003587 /*
3588 * we now own the reference that was stored in the fbs list
3589 *
3590 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3591 * so run this in a separate stack as there's no way to correctly
3592 * handle this after the fb is already removed from the lookup table.
3593 */
3594 if (drm_framebuffer_read_refcount(fb) > 1) {
3595 struct drm_mode_rmfb_work arg;
3596
3597 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3598 INIT_LIST_HEAD(&arg.fbs);
3599 list_add_tail(&fb->filp_head, &arg.fbs);
3600
3601 schedule_work(&arg.work);
3602 flush_work(&arg.work);
3603 destroy_work_on_stack(&arg.work);
3604 } else
3605 drm_framebuffer_unreference(fb);
3606
Daniel Vetter2b677e82012-12-10 21:16:05 +01003607 return 0;
3608
Dave Airlie72fe90b2016-04-15 15:10:40 +10003609fail_unref:
3610 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003611 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003612}
3613
3614/**
3615 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003616 * @dev: drm device for the ioctl
3617 * @data: data pointer for the ioctl
3618 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003619 *
Dave Airlief453ba02008-11-07 14:05:41 -08003620 * Lookup the FB given its ID and return info about it.
3621 *
3622 * Called by the user via ioctl.
3623 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003624 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003625 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003626 */
3627int drm_mode_getfb(struct drm_device *dev,
3628 void *data, struct drm_file *file_priv)
3629{
3630 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003631 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003632 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003633
Dave Airliefb3b06c2011-02-08 13:55:21 +10003634 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3635 return -EINVAL;
3636
Daniel Vetter786b99e2012-12-02 21:53:40 +01003637 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003638 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003639 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003640
3641 r->height = fb->height;
3642 r->width = fb->width;
3643 r->depth = fb->depth;
3644 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003645 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003646 if (fb->funcs->create_handle) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02003647 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003648 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003649 ret = fb->funcs->create_handle(fb, file_priv,
3650 &r->handle);
3651 } else {
3652 /* GET_FB() is an unprivileged ioctl so we must not
3653 * return a buffer-handle to non-master processes! For
3654 * backwards-compatibility reasons, we cannot make
3655 * GET_FB() privileged, so just return an invalid handle
3656 * for non-masters. */
3657 r->handle = 0;
3658 ret = 0;
3659 }
3660 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003661 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003662 }
Dave Airlief453ba02008-11-07 14:05:41 -08003663
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003664 drm_framebuffer_unreference(fb);
3665
Dave Airlief453ba02008-11-07 14:05:41 -08003666 return ret;
3667}
3668
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003669/**
3670 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3671 * @dev: drm device for the ioctl
3672 * @data: data pointer for the ioctl
3673 * @file_priv: drm file for the ioctl call
3674 *
3675 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3676 * rectangle list. Generic userspace which does frontbuffer rendering must call
3677 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3678 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3679 *
3680 * Modesetting drivers which always update the frontbuffer do not need to
3681 * implement the corresponding ->dirty framebuffer callback.
3682 *
3683 * Called by the user via ioctl.
3684 *
3685 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003686 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003687 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003688int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3689 void *data, struct drm_file *file_priv)
3690{
3691 struct drm_clip_rect __user *clips_ptr;
3692 struct drm_clip_rect *clips = NULL;
3693 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003694 struct drm_framebuffer *fb;
3695 unsigned flags;
3696 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003697 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003698
Dave Airliefb3b06c2011-02-08 13:55:21 +10003699 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3700 return -EINVAL;
3701
Daniel Vetter786b99e2012-12-02 21:53:40 +01003702 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003703 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003704 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003705
3706 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003707 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003708
3709 if (!num_clips != !clips_ptr) {
3710 ret = -EINVAL;
3711 goto out_err1;
3712 }
3713
3714 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3715
3716 /* If userspace annotates copy, clips must come in pairs */
3717 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3718 ret = -EINVAL;
3719 goto out_err1;
3720 }
3721
3722 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003723 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3724 ret = -EINVAL;
3725 goto out_err1;
3726 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003727 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003728 if (!clips) {
3729 ret = -ENOMEM;
3730 goto out_err1;
3731 }
3732
3733 ret = copy_from_user(clips, clips_ptr,
3734 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003735 if (ret) {
3736 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003737 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003738 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003739 }
3740
3741 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003742 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3743 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003744 } else {
3745 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003746 }
3747
3748out_err2:
3749 kfree(clips);
3750out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003751 drm_framebuffer_unreference(fb);
3752
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003753 return ret;
3754}
3755
Dave Airlief453ba02008-11-07 14:05:41 -08003756/**
3757 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003758 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003759 *
Dave Airlief453ba02008-11-07 14:05:41 -08003760 * Destroy all the FBs associated with @filp.
3761 *
3762 * Called by the user via ioctl.
3763 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003764 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003765 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003766 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003767void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003768{
Dave Airlief453ba02008-11-07 14:05:41 -08003769 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003770 struct drm_mode_rmfb_work arg;
3771
3772 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003773
Daniel Vetter1b116292014-09-24 21:51:06 +02003774 /*
3775 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003776 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003777 * avoid upsetting lockdep since the universal cursor code adds a
3778 * framebuffer while holding mutex locks.
3779 *
3780 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3781 * locks is impossible here since no one else but this function can get
3782 * at it any more.
3783 */
Dave Airlief453ba02008-11-07 14:05:41 -08003784 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003785 if (drm_framebuffer_read_refcount(fb) > 1) {
3786 list_move_tail(&fb->filp_head, &arg.fbs);
3787 } else {
3788 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003789
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003790 /* This drops the fpriv->fbs reference. */
3791 drm_framebuffer_unreference(fb);
3792 }
3793 }
3794
3795 if (!list_empty(&arg.fbs)) {
3796 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3797
3798 schedule_work(&arg.work);
3799 flush_work(&arg.work);
3800 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003801 }
Dave Airlief453ba02008-11-07 14:05:41 -08003802}
3803
Daniel Vetter81065542016-06-21 10:54:13 +02003804static bool drm_property_type_valid(struct drm_property *property)
3805{
3806 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
3807 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3808 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3809}
3810
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003811/**
3812 * drm_property_create - create a new property type
3813 * @dev: drm device
3814 * @flags: flags specifying the property type
3815 * @name: name of the property
3816 * @num_values: number of pre-defined values
3817 *
3818 * This creates a new generic drm property which can then be attached to a drm
3819 * object with drm_object_attach_property. The returned property object must be
3820 * freed with drm_property_destroy.
3821 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003822 * Note that the DRM core keeps a per-device list of properties and that, if
3823 * drm_mode_config_cleanup() is called, it will destroy all properties created
3824 * by the driver.
3825 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003826 * Returns:
3827 * A pointer to the newly created property on success, NULL on failure.
3828 */
Dave Airlief453ba02008-11-07 14:05:41 -08003829struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3830 const char *name, int num_values)
3831{
3832 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003833 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003834
3835 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3836 if (!property)
3837 return NULL;
3838
Rob Clark98f75de2014-05-30 11:37:03 -04003839 property->dev = dev;
3840
Dave Airlief453ba02008-11-07 14:05:41 -08003841 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003842 property->values = kcalloc(num_values, sizeof(uint64_t),
3843 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003844 if (!property->values)
3845 goto fail;
3846 }
3847
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003848 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3849 if (ret)
3850 goto fail;
3851
Dave Airlief453ba02008-11-07 14:05:41 -08003852 property->flags = flags;
3853 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003854 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003855
Vinson Lee471dd2e2011-11-10 11:55:40 -08003856 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003857 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003858 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3859 }
Dave Airlief453ba02008-11-07 14:05:41 -08003860
3861 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003862
3863 WARN_ON(!drm_property_type_valid(property));
3864
Dave Airlief453ba02008-11-07 14:05:41 -08003865 return property;
3866fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003867 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003868 kfree(property);
3869 return NULL;
3870}
3871EXPORT_SYMBOL(drm_property_create);
3872
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003873/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003874 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003875 * @dev: drm device
3876 * @flags: flags specifying the property type
3877 * @name: name of the property
3878 * @props: enumeration lists with property values
3879 * @num_values: number of pre-defined values
3880 *
3881 * This creates a new generic drm property which can then be attached to a drm
3882 * object with drm_object_attach_property. The returned property object must be
3883 * freed with drm_property_destroy.
3884 *
3885 * Userspace is only allowed to set one of the predefined values for enumeration
3886 * properties.
3887 *
3888 * Returns:
3889 * A pointer to the newly created property on success, NULL on failure.
3890 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003891struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3892 const char *name,
3893 const struct drm_prop_enum_list *props,
3894 int num_values)
3895{
3896 struct drm_property *property;
3897 int i, ret;
3898
3899 flags |= DRM_MODE_PROP_ENUM;
3900
3901 property = drm_property_create(dev, flags, name, num_values);
3902 if (!property)
3903 return NULL;
3904
3905 for (i = 0; i < num_values; i++) {
3906 ret = drm_property_add_enum(property, i,
3907 props[i].type,
3908 props[i].name);
3909 if (ret) {
3910 drm_property_destroy(dev, property);
3911 return NULL;
3912 }
3913 }
3914
3915 return property;
3916}
3917EXPORT_SYMBOL(drm_property_create_enum);
3918
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003919/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003920 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003921 * @dev: drm device
3922 * @flags: flags specifying the property type
3923 * @name: name of the property
3924 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003925 * @num_props: size of the @props array
3926 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003927 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003928 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3931 *
3932 * Compared to plain enumeration properties userspace is allowed to set any
3933 * or'ed together combination of the predefined property bitflag values
3934 *
3935 * Returns:
3936 * A pointer to the newly created property on success, NULL on failure.
3937 */
Rob Clark49e27542012-05-17 02:23:26 -06003938struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3939 int flags, const char *name,
3940 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303941 int num_props,
3942 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003943{
3944 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303945 int i, ret, index = 0;
3946 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003947
3948 flags |= DRM_MODE_PROP_BITMASK;
3949
3950 property = drm_property_create(dev, flags, name, num_values);
3951 if (!property)
3952 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303953 for (i = 0; i < num_props; i++) {
3954 if (!(supported_bits & (1ULL << props[i].type)))
3955 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003956
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303957 if (WARN_ON(index >= num_values)) {
3958 drm_property_destroy(dev, property);
3959 return NULL;
3960 }
3961
3962 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003963 props[i].type,
3964 props[i].name);
3965 if (ret) {
3966 drm_property_destroy(dev, property);
3967 return NULL;
3968 }
3969 }
3970
3971 return property;
3972}
3973EXPORT_SYMBOL(drm_property_create_bitmask);
3974
Rob Clarkebc44cf2012-09-12 22:22:31 -05003975static struct drm_property *property_create_range(struct drm_device *dev,
3976 int flags, const char *name,
3977 uint64_t min, uint64_t max)
3978{
3979 struct drm_property *property;
3980
3981 property = drm_property_create(dev, flags, name, 2);
3982 if (!property)
3983 return NULL;
3984
3985 property->values[0] = min;
3986 property->values[1] = max;
3987
3988 return property;
3989}
3990
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003991/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003992 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003993 * @dev: drm device
3994 * @flags: flags specifying the property type
3995 * @name: name of the property
3996 * @min: minimum value of the property
3997 * @max: maximum value of the property
3998 *
3999 * This creates a new generic drm property which can then be attached to a drm
4000 * object with drm_object_attach_property. The returned property object must be
4001 * freed with drm_property_destroy.
4002 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004003 * Userspace is allowed to set any unsigned integer value in the (min, max)
4004 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004005 *
4006 * Returns:
4007 * A pointer to the newly created property on success, NULL on failure.
4008 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01004009struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
4010 const char *name,
4011 uint64_t min, uint64_t max)
4012{
Rob Clarkebc44cf2012-09-12 22:22:31 -05004013 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
4014 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01004015}
4016EXPORT_SYMBOL(drm_property_create_range);
4017
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004018/**
4019 * drm_property_create_signed_range - create a new signed ranged property type
4020 * @dev: drm device
4021 * @flags: flags specifying the property type
4022 * @name: name of the property
4023 * @min: minimum value of the property
4024 * @max: maximum value of the property
4025 *
4026 * This creates a new generic drm property which can then be attached to a drm
4027 * object with drm_object_attach_property. The returned property object must be
4028 * freed with drm_property_destroy.
4029 *
4030 * Userspace is allowed to set any signed integer value in the (min, max)
4031 * range inclusive.
4032 *
4033 * Returns:
4034 * A pointer to the newly created property on success, NULL on failure.
4035 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05004036struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
4037 int flags, const char *name,
4038 int64_t min, int64_t max)
4039{
4040 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
4041 name, I642U64(min), I642U64(max));
4042}
4043EXPORT_SYMBOL(drm_property_create_signed_range);
4044
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004045/**
4046 * drm_property_create_object - create a new object property type
4047 * @dev: drm device
4048 * @flags: flags specifying the property type
4049 * @name: name of the property
4050 * @type: object type from DRM_MODE_OBJECT_* defines
4051 *
4052 * This creates a new generic drm property which can then be attached to a drm
4053 * object with drm_object_attach_property. The returned property object must be
4054 * freed with drm_property_destroy.
4055 *
4056 * Userspace is only allowed to set this to any property value of the given
4057 * @type. Only useful for atomic properties, which is enforced.
4058 *
4059 * Returns:
4060 * A pointer to the newly created property on success, NULL on failure.
4061 */
Rob Clark98f75de2014-05-30 11:37:03 -04004062struct drm_property *drm_property_create_object(struct drm_device *dev,
4063 int flags, const char *name, uint32_t type)
4064{
4065 struct drm_property *property;
4066
4067 flags |= DRM_MODE_PROP_OBJECT;
4068
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004069 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
4070 return NULL;
4071
Rob Clark98f75de2014-05-30 11:37:03 -04004072 property = drm_property_create(dev, flags, name, 1);
4073 if (!property)
4074 return NULL;
4075
4076 property->values[0] = type;
4077
4078 return property;
4079}
4080EXPORT_SYMBOL(drm_property_create_object);
4081
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004082/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004083 * drm_property_create_bool - create a new boolean property type
4084 * @dev: drm device
4085 * @flags: flags specifying the property type
4086 * @name: name of the property
4087 *
4088 * This creates a new generic drm property which can then be attached to a drm
4089 * object with drm_object_attach_property. The returned property object must be
4090 * freed with drm_property_destroy.
4091 *
4092 * This is implemented as a ranged property with only {0, 1} as valid values.
4093 *
4094 * Returns:
4095 * A pointer to the newly created property on success, NULL on failure.
4096 */
4097struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
4098 const char *name)
4099{
4100 return drm_property_create_range(dev, flags, name, 0, 1);
4101}
4102EXPORT_SYMBOL(drm_property_create_bool);
4103
4104/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004105 * drm_property_add_enum - add a possible value to an enumeration property
4106 * @property: enumeration property to change
4107 * @index: index of the new enumeration
4108 * @value: value of the new enumeration
4109 * @name: symbolic name of the new enumeration
4110 *
4111 * This functions adds enumerations to a property.
4112 *
4113 * It's use is deprecated, drivers should use one of the more specific helpers
4114 * to directly create the property with all enumerations already attached.
4115 *
4116 * Returns:
4117 * Zero on success, error code on failure.
4118 */
Dave Airlief453ba02008-11-07 14:05:41 -08004119int drm_property_add_enum(struct drm_property *property, int index,
4120 uint64_t value, const char *name)
4121{
4122 struct drm_property_enum *prop_enum;
4123
Rob Clark5ea22f22014-05-30 11:34:01 -04004124 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4125 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004126 return -EINVAL;
4127
4128 /*
4129 * Bitmask enum properties have the additional constraint of values
4130 * from 0 to 63
4131 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004132 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4133 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004134 return -EINVAL;
4135
Daniel Vetter3758b342014-11-19 18:38:10 +01004136 if (!list_empty(&property->enum_list)) {
4137 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004138 if (prop_enum->value == value) {
4139 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4140 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4141 return 0;
4142 }
4143 }
4144 }
4145
4146 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4147 if (!prop_enum)
4148 return -ENOMEM;
4149
4150 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4151 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4152 prop_enum->value = value;
4153
4154 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004155 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004156 return 0;
4157}
4158EXPORT_SYMBOL(drm_property_add_enum);
4159
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004160/**
4161 * drm_property_destroy - destroy a drm property
4162 * @dev: drm device
4163 * @property: property to destry
4164 *
4165 * This function frees a property including any attached resources like
4166 * enumeration values.
4167 */
Dave Airlief453ba02008-11-07 14:05:41 -08004168void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4169{
4170 struct drm_property_enum *prop_enum, *pt;
4171
Daniel Vetter3758b342014-11-19 18:38:10 +01004172 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004173 list_del(&prop_enum->head);
4174 kfree(prop_enum);
4175 }
4176
4177 if (property->num_values)
4178 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004179 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004180 list_del(&property->head);
4181 kfree(property);
4182}
4183EXPORT_SYMBOL(drm_property_destroy);
4184
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004185/**
4186 * drm_object_attach_property - attach a property to a modeset object
4187 * @obj: drm modeset object
4188 * @property: property to attach
4189 * @init_val: initial value of the property
4190 *
4191 * This attaches the given property to the modeset object with the given initial
4192 * value. Currently this function cannot fail since the properties are stored in
4193 * a statically sized array.
4194 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004195void drm_object_attach_property(struct drm_mode_object *obj,
4196 struct drm_property *property,
4197 uint64_t init_val)
4198{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004199 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004200
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004201 if (count == DRM_OBJECT_MAX_PROPERTY) {
4202 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4203 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4204 "you see this message on the same object type.\n",
4205 obj->type);
4206 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004207 }
4208
Rob Clarkb17cd752014-12-16 18:05:30 -05004209 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004210 obj->properties->values[count] = init_val;
4211 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004212 if (property->flags & DRM_MODE_PROP_ATOMIC)
4213 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004214}
4215EXPORT_SYMBOL(drm_object_attach_property);
4216
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004217/**
4218 * drm_object_property_set_value - set the value of a property
4219 * @obj: drm mode object to set property value for
4220 * @property: property to set
4221 * @val: value the property should be set to
4222 *
4223 * This functions sets a given property on a given object. This function only
4224 * changes the software state of the property, it does not call into the
4225 * driver's ->set_property callback.
4226 *
4227 * Returns:
4228 * Zero on success, error code on failure.
4229 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004230int drm_object_property_set_value(struct drm_mode_object *obj,
4231 struct drm_property *property, uint64_t val)
4232{
4233 int i;
4234
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004235 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004236 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004237 obj->properties->values[i] = val;
4238 return 0;
4239 }
4240 }
4241
4242 return -EINVAL;
4243}
4244EXPORT_SYMBOL(drm_object_property_set_value);
4245
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004246/**
4247 * drm_object_property_get_value - retrieve the value of a property
4248 * @obj: drm mode object to get property value from
4249 * @property: property to retrieve
4250 * @val: storage for the property value
4251 *
4252 * This function retrieves the softare state of the given property for the given
4253 * property. Since there is no driver callback to retrieve the current property
4254 * value this might be out of sync with the hardware, depending upon the driver
4255 * and property.
4256 *
4257 * Returns:
4258 * Zero on success, error code on failure.
4259 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004260int drm_object_property_get_value(struct drm_mode_object *obj,
4261 struct drm_property *property, uint64_t *val)
4262{
4263 int i;
4264
Rob Clark88a48e22014-12-18 16:01:50 -05004265 /* read-only properties bypass atomic mechanism and still store
4266 * their value in obj->properties->values[].. mostly to avoid
4267 * having to deal w/ EDID and similar props in atomic paths:
4268 */
4269 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4270 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4271 return drm_atomic_get_property(obj, property, val);
4272
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004273 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004274 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004275 *val = obj->properties->values[i];
4276 return 0;
4277 }
4278 }
4279
4280 return -EINVAL;
4281}
4282EXPORT_SYMBOL(drm_object_property_get_value);
4283
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004284/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004285 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004286 * @dev: DRM device
4287 * @data: ioctl data
4288 * @file_priv: DRM file info
4289 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004290 * This function retrieves the metadata for a given property, like the different
4291 * possible values for an enum property or the limits for a range property.
4292 *
4293 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004294 *
4295 * Called by the user via ioctl.
4296 *
4297 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004298 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004299 */
Dave Airlief453ba02008-11-07 14:05:41 -08004300int drm_mode_getproperty_ioctl(struct drm_device *dev,
4301 void *data, struct drm_file *file_priv)
4302{
Dave Airlief453ba02008-11-07 14:05:41 -08004303 struct drm_mode_get_property *out_resp = data;
4304 struct drm_property *property;
4305 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004306 int value_count = 0;
4307 int ret = 0, i;
4308 int copied;
4309 struct drm_property_enum *prop_enum;
4310 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004311 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004312
Dave Airliefb3b06c2011-02-08 13:55:21 +10004313 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4314 return -EINVAL;
4315
Daniel Vetter84849902012-12-02 00:28:11 +01004316 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004317 property = drm_property_find(dev, out_resp->prop_id);
4318 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004319 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004320 goto done;
4321 }
Dave Airlief453ba02008-11-07 14:05:41 -08004322
Rob Clark5ea22f22014-05-30 11:34:01 -04004323 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4324 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004325 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004326 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004327 }
4328
4329 value_count = property->num_values;
4330
4331 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4332 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4333 out_resp->flags = property->flags;
4334
4335 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004336 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004337 for (i = 0; i < value_count; i++) {
4338 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4339 ret = -EFAULT;
4340 goto done;
4341 }
4342 }
4343 }
4344 out_resp->count_values = value_count;
4345
Rob Clark5ea22f22014-05-30 11:34:01 -04004346 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4347 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004348 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4349 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004350 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004351 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004352
4353 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4354 ret = -EFAULT;
4355 goto done;
4356 }
4357
4358 if (copy_to_user(&enum_ptr[copied].name,
4359 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4360 ret = -EFAULT;
4361 goto done;
4362 }
4363 copied++;
4364 }
4365 }
4366 out_resp->count_enum_blobs = enum_count;
4367 }
4368
Daniel Vetter3758b342014-11-19 18:38:10 +01004369 /*
4370 * NOTE: The idea seems to have been to use this to read all the blob
4371 * property values. But nothing ever added them to the corresponding
4372 * list, userspace always used the special-purpose get_blob ioctl to
4373 * read the value for a blob property. It also doesn't make a lot of
4374 * sense to return values here when everything else is just metadata for
4375 * the property itself.
4376 */
4377 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4378 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004379done:
Daniel Vetter84849902012-12-02 00:28:11 +01004380 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004381 return ret;
4382}
4383
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004384static void drm_property_free_blob(struct kref *kref)
4385{
4386 struct drm_property_blob *blob =
4387 container_of(kref, struct drm_property_blob, base.refcount);
4388
4389 mutex_lock(&blob->dev->mode_config.blob_lock);
4390 list_del(&blob->head_global);
4391 mutex_unlock(&blob->dev->mode_config.blob_lock);
4392
4393 drm_mode_object_unregister(blob->dev, &blob->base);
4394
4395 kfree(blob);
4396}
4397
Daniel Stone99531d92015-05-22 13:34:49 +01004398/**
4399 * drm_property_create_blob - Create new blob property
4400 *
4401 * Creates a new blob property for a specified DRM device, optionally
4402 * copying data.
4403 *
4404 * @dev: DRM device to create property for
4405 * @length: Length to allocate for blob data
4406 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004407 *
4408 * Returns:
4409 * New blob property with a single reference on success, or an ERR_PTR
4410 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004411 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004412struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004413drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004414 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004415{
4416 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004417 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004418
Dan Carpenter9ac09342015-10-29 16:37:54 +03004419 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004420 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004421
4422 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4423 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004424 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004425
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004426 /* This must be explicitly initialised, so we can safely call list_del
4427 * on it in the removal handler, even if it isn't in a file list. */
4428 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004429 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004430 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004431
Daniel Stone99531d92015-05-22 13:34:49 +01004432 if (data)
4433 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004434
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004435 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4436 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004437 if (ret) {
4438 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004439 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004440 }
4441
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004442 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004443 list_add_tail(&blob->head_global,
4444 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004445 mutex_unlock(&dev->mode_config.blob_lock);
4446
Dave Airlief453ba02008-11-07 14:05:41 -08004447 return blob;
4448}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004449EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004450
Daniel Stone6bcacf52015-04-20 19:22:55 +01004451/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004452 * drm_property_unreference_blob - Unreference a blob property
4453 *
4454 * Drop a reference on a blob property. May free the object.
4455 *
Daniel Stonef102c162015-05-22 13:34:44 +01004456 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004457 */
4458void drm_property_unreference_blob(struct drm_property_blob *blob)
4459{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004460 if (!blob)
4461 return;
4462
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004463 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004464}
4465EXPORT_SYMBOL(drm_property_unreference_blob);
4466
4467/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004468 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4469 * @dev: DRM device
4470 * @file_priv: destroy all blobs owned by this file handle
4471 */
4472void drm_property_destroy_user_blobs(struct drm_device *dev,
4473 struct drm_file *file_priv)
4474{
4475 struct drm_property_blob *blob, *bt;
4476
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004477 /*
4478 * When the file gets released that means no one else can access the
4479 * blob list any more, so no need to grab dev->blob_lock.
4480 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004481 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4482 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004483 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004484 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004485}
4486
4487/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004488 * drm_property_reference_blob - Take a reference on an existing property
4489 *
4490 * Take a new reference on an existing blob property.
4491 *
Daniel Stonef102c162015-05-22 13:34:44 +01004492 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004493 */
4494struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4495{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004496 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004497 return blob;
4498}
4499EXPORT_SYMBOL(drm_property_reference_blob);
4500
Daniel Stone6bcacf52015-04-20 19:22:55 +01004501/**
4502 * drm_property_lookup_blob - look up a blob property and take a reference
4503 * @dev: drm device
4504 * @id: id of the blob property
4505 *
4506 * If successful, this takes an additional reference to the blob property.
4507 * callers need to make sure to eventually unreference the returned property
4508 * again, using @drm_property_unreference_blob.
4509 */
4510struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4511 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004512{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004513 struct drm_mode_object *obj;
4514 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004515
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004516 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4517 if (obj)
4518 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004519 return blob;
4520}
4521EXPORT_SYMBOL(drm_property_lookup_blob);
4522
4523/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004524 * drm_property_replace_global_blob - atomically replace existing blob property
4525 * @dev: drm device
4526 * @replace: location of blob property pointer to be replaced
4527 * @length: length of data for new blob, or 0 for no data
4528 * @data: content for new blob, or NULL for no data
4529 * @obj_holds_id: optional object for property holding blob ID
4530 * @prop_holds_id: optional property holding blob ID
4531 * @return 0 on success or error on failure
4532 *
4533 * This function will atomically replace a global property in the blob list,
4534 * optionally updating a property which holds the ID of that property. It is
4535 * guaranteed to be atomic: no caller will be allowed to see intermediate
4536 * results, and either the entire operation will succeed and clean up the
4537 * previous property, or it will fail and the state will be unchanged.
4538 *
4539 * If length is 0 or data is NULL, no new blob will be created, and the holding
4540 * property, if specified, will be set to 0.
4541 *
4542 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4543 * by holding the relevant modesetting object lock for its parent.
4544 *
4545 * For example, a drm_connector has a 'PATH' property, which contains the ID
4546 * of a blob property with the value of the MST path information. Calling this
4547 * function with replace pointing to the connector's path_blob_ptr, length and
4548 * data set for the new path information, obj_holds_id set to the connector's
4549 * base object, and prop_holds_id set to the path property name, will perform
4550 * a completely atomic update. The access to path_blob_ptr is protected by the
4551 * caller holding a lock on the connector.
4552 */
4553static int drm_property_replace_global_blob(struct drm_device *dev,
4554 struct drm_property_blob **replace,
4555 size_t length,
4556 const void *data,
4557 struct drm_mode_object *obj_holds_id,
4558 struct drm_property *prop_holds_id)
4559{
4560 struct drm_property_blob *new_blob = NULL;
4561 struct drm_property_blob *old_blob = NULL;
4562 int ret;
4563
4564 WARN_ON(replace == NULL);
4565
4566 old_blob = *replace;
4567
4568 if (length && data) {
4569 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004570 if (IS_ERR(new_blob))
4571 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004572 }
4573
4574 /* This does not need to be synchronised with blob_lock, as the
4575 * get_properties ioctl locks all modesetting objects, and
4576 * obj_holds_id must be locked before calling here, so we cannot
4577 * have its value out of sync with the list membership modified
4578 * below under blob_lock. */
4579 if (obj_holds_id) {
4580 ret = drm_object_property_set_value(obj_holds_id,
4581 prop_holds_id,
4582 new_blob ?
4583 new_blob->base.id : 0);
4584 if (ret != 0)
4585 goto err_created;
4586 }
4587
Markus Elfringec530822015-07-05 21:55:10 +02004588 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004589 *replace = new_blob;
4590
4591 return 0;
4592
4593err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004594 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004595 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004596}
4597
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004598/**
4599 * drm_mode_getblob_ioctl - get the contents of a blob property value
4600 * @dev: DRM device
4601 * @data: ioctl data
4602 * @file_priv: DRM file info
4603 *
4604 * This function retrieves the contents of a blob property. The value stored in
4605 * an object's blob property is just a normal modeset object id.
4606 *
4607 * Called by the user via ioctl.
4608 *
4609 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004610 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004611 */
Dave Airlief453ba02008-11-07 14:05:41 -08004612int drm_mode_getblob_ioctl(struct drm_device *dev,
4613 void *data, struct drm_file *file_priv)
4614{
Dave Airlief453ba02008-11-07 14:05:41 -08004615 struct drm_mode_get_blob *out_resp = data;
4616 struct drm_property_blob *blob;
4617 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004618 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004619
Dave Airliefb3b06c2011-02-08 13:55:21 +10004620 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4621 return -EINVAL;
4622
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004623 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4624 if (!blob)
4625 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004626
4627 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004628 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004629 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004630 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004631 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004632 }
4633 }
4634 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004635unref:
4636 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004637
Dave Airlief453ba02008-11-07 14:05:41 -08004638 return ret;
4639}
4640
Dave Airliecc7096f2014-10-22 12:03:04 +10004641/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004642 * drm_mode_createblob_ioctl - create a new blob property
4643 * @dev: DRM device
4644 * @data: ioctl data
4645 * @file_priv: DRM file info
4646 *
4647 * This function creates a new blob property with user-defined values. In order
4648 * to give us sensible validation and checking when creating, rather than at
4649 * every potential use, we also require a type to be provided upfront.
4650 *
4651 * Called by the user via ioctl.
4652 *
4653 * Returns:
4654 * Zero on success, negative errno on failure.
4655 */
4656int drm_mode_createblob_ioctl(struct drm_device *dev,
4657 void *data, struct drm_file *file_priv)
4658{
4659 struct drm_mode_create_blob *out_resp = data;
4660 struct drm_property_blob *blob;
4661 void __user *blob_ptr;
4662 int ret = 0;
4663
4664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4665 return -EINVAL;
4666
4667 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4668 if (IS_ERR(blob))
4669 return PTR_ERR(blob);
4670
4671 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4672 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4673 ret = -EFAULT;
4674 goto out_blob;
4675 }
4676
4677 /* Dropping the lock between create_blob and our access here is safe
4678 * as only the same file_priv can remove the blob; at this point, it is
4679 * not associated with any file_priv. */
4680 mutex_lock(&dev->mode_config.blob_lock);
4681 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004682 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004683 mutex_unlock(&dev->mode_config.blob_lock);
4684
4685 return 0;
4686
4687out_blob:
4688 drm_property_unreference_blob(blob);
4689 return ret;
4690}
4691
4692/**
4693 * drm_mode_destroyblob_ioctl - destroy a user blob property
4694 * @dev: DRM device
4695 * @data: ioctl data
4696 * @file_priv: DRM file info
4697 *
4698 * Destroy an existing user-defined blob property.
4699 *
4700 * Called by the user via ioctl.
4701 *
4702 * Returns:
4703 * Zero on success, negative errno on failure.
4704 */
4705int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4706 void *data, struct drm_file *file_priv)
4707{
4708 struct drm_mode_destroy_blob *out_resp = data;
4709 struct drm_property_blob *blob = NULL, *bt;
4710 bool found = false;
4711 int ret = 0;
4712
4713 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4714 return -EINVAL;
4715
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004716 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4717 if (!blob)
4718 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004719
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004720 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004721 /* Ensure the property was actually created by this user. */
4722 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4723 if (bt == blob) {
4724 found = true;
4725 break;
4726 }
4727 }
4728
4729 if (!found) {
4730 ret = -EPERM;
4731 goto err;
4732 }
4733
4734 /* We must drop head_file here, because we may not be the last
4735 * reference on the blob. */
4736 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004737 mutex_unlock(&dev->mode_config.blob_lock);
4738
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004739 /* One reference from lookup, and one from the filp. */
4740 drm_property_unreference_blob(blob);
4741 drm_property_unreference_blob(blob);
4742
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004743 return 0;
4744
4745err:
4746 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004747 drm_property_unreference_blob(blob);
4748
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004749 return ret;
4750}
4751
4752/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004753 * drm_mode_connector_set_path_property - set tile property on connector
4754 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004755 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004756 *
4757 * This creates a property to expose to userspace to specify a
4758 * connector path. This is mainly used for DisplayPort MST where
4759 * connectors have a topology and we want to allow userspace to give
4760 * them more meaningful names.
4761 *
4762 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004763 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004764 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004765int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004766 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004767{
4768 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004769 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004770
Daniel Stoned2ed3432015-04-20 19:22:53 +01004771 ret = drm_property_replace_global_blob(dev,
4772 &connector->path_blob_ptr,
4773 strlen(path) + 1,
4774 path,
4775 &connector->base,
4776 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004777 return ret;
4778}
4779EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4780
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004781/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004782 * drm_mode_connector_set_tile_property - set tile property on connector
4783 * @connector: connector to set property on.
4784 *
4785 * This looks up the tile information for a connector, and creates a
4786 * property for userspace to parse if it exists. The property is of
4787 * the form of 8 integers using ':' as a separator.
4788 *
4789 * Returns:
4790 * Zero on success, errno on failure.
4791 */
4792int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4793{
4794 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004795 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004796 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004797
4798 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004799 ret = drm_property_replace_global_blob(dev,
4800 &connector->tile_blob_ptr,
4801 0,
4802 NULL,
4803 &connector->base,
4804 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004805 return ret;
4806 }
4807
4808 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4809 connector->tile_group->id, connector->tile_is_single_monitor,
4810 connector->num_h_tile, connector->num_v_tile,
4811 connector->tile_h_loc, connector->tile_v_loc,
4812 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004813
Daniel Stoned2ed3432015-04-20 19:22:53 +01004814 ret = drm_property_replace_global_blob(dev,
4815 &connector->tile_blob_ptr,
4816 strlen(tile) + 1,
4817 tile,
4818 &connector->base,
4819 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004820 return ret;
4821}
4822EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4823
4824/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004825 * drm_mode_connector_update_edid_property - update the edid property of a connector
4826 * @connector: drm connector
4827 * @edid: new value of the edid property
4828 *
4829 * This function creates a new blob modeset object and assigns its id to the
4830 * connector's edid property.
4831 *
4832 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004833 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004834 */
Dave Airlief453ba02008-11-07 14:05:41 -08004835int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004836 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004837{
4838 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004839 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004840 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004841
Thomas Wood4cf2b282014-06-18 17:52:33 +01004842 /* ignore requests to set edid when overridden */
4843 if (connector->override_edid)
4844 return 0;
4845
Daniel Stoned2ed3432015-04-20 19:22:53 +01004846 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004847 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004848
Daniel Stoned2ed3432015-04-20 19:22:53 +01004849 ret = drm_property_replace_global_blob(dev,
4850 &connector->edid_blob_ptr,
4851 size,
4852 edid,
4853 &connector->base,
4854 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004855 return ret;
4856}
4857EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4858
Rob Clark3843e712014-12-16 18:05:29 -05004859/* Some properties could refer to dynamic refcnt'd objects, or things that
4860 * need special locking to handle lifetime issues (ie. to ensure the prop
4861 * value doesn't become invalid part way through the property update due to
4862 * race). The value returned by reference via 'obj' should be passed back
4863 * to drm_property_change_valid_put() after the property is set (and the
4864 * object to which the property is attached has a chance to take it's own
4865 * reference).
4866 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004867bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004868 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004869{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004870 int i;
4871
Paulo Zanoni26a34812012-05-15 18:08:59 -03004872 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4873 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004874
Rob Clark3843e712014-12-16 18:05:29 -05004875 *ref = NULL;
4876
Rob Clark5ea22f22014-05-30 11:34:01 -04004877 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004878 if (value < property->values[0] || value > property->values[1])
4879 return false;
4880 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004881 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4882 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004883
Rob Clarkebc44cf2012-09-12 22:22:31 -05004884 if (svalue < U642I64(property->values[0]) ||
4885 svalue > U642I64(property->values[1]))
4886 return false;
4887 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004888 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004889 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004890
Rob Clark49e27542012-05-17 02:23:26 -06004891 for (i = 0; i < property->num_values; i++)
4892 valid_mask |= (1ULL << property->values[i]);
4893 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004894 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004895 struct drm_property_blob *blob;
4896
4897 if (value == 0)
4898 return true;
4899
4900 blob = drm_property_lookup_blob(property->dev, value);
4901 if (blob) {
4902 *ref = &blob->base;
4903 return true;
4904 } else {
4905 return false;
4906 }
Rob Clark98f75de2014-05-30 11:37:03 -04004907 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004908 /* a zero value for an object property translates to null: */
4909 if (value == 0)
4910 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004911
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004912 *ref = _object_find(property->dev, value, property->values[0]);
4913 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004914 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004915
4916 for (i = 0; i < property->num_values; i++)
4917 if (property->values[i] == value)
4918 return true;
4919 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004920}
4921
Rob Clarkd34f20d2014-12-18 16:01:56 -05004922void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004923 struct drm_mode_object *ref)
4924{
4925 if (!ref)
4926 return;
4927
4928 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004929 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004930 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4931 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004932}
4933
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004934/**
4935 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4936 * @dev: DRM device
4937 * @data: ioctl data
4938 * @file_priv: DRM file info
4939 *
4940 * This function sets the current value for a connectors's property. It also
4941 * calls into a driver's ->set_property callback to update the hardware state
4942 *
4943 * Called by the user via ioctl.
4944 *
4945 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004946 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004947 */
Dave Airlief453ba02008-11-07 14:05:41 -08004948int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4949 void *data, struct drm_file *file_priv)
4950{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004951 struct drm_mode_connector_set_property *conn_set_prop = data;
4952 struct drm_mode_obj_set_property obj_set_prop = {
4953 .value = conn_set_prop->value,
4954 .prop_id = conn_set_prop->prop_id,
4955 .obj_id = conn_set_prop->connector_id,
4956 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4957 };
Dave Airlief453ba02008-11-07 14:05:41 -08004958
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004959 /* It does all the locking and checking we need */
4960 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004961}
4962
Paulo Zanonic5431882012-05-15 18:09:02 -03004963static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4964 struct drm_property *property,
4965 uint64_t value)
4966{
4967 int ret = -EINVAL;
4968 struct drm_connector *connector = obj_to_connector(obj);
4969
4970 /* Do DPMS ourselves */
4971 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004972 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004973 } else if (connector->funcs->set_property)
4974 ret = connector->funcs->set_property(connector, property, value);
4975
4976 /* store the property value if successful */
4977 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004978 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004979 return ret;
4980}
4981
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004982static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4983 struct drm_property *property,
4984 uint64_t value)
4985{
4986 int ret = -EINVAL;
4987 struct drm_crtc *crtc = obj_to_crtc(obj);
4988
4989 if (crtc->funcs->set_property)
4990 ret = crtc->funcs->set_property(crtc, property, value);
4991 if (!ret)
4992 drm_object_property_set_value(obj, property, value);
4993
4994 return ret;
4995}
4996
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004997/**
4998 * drm_mode_plane_set_obj_prop - set the value of a property
4999 * @plane: drm plane object to set property value for
5000 * @property: property to set
5001 * @value: value the property should be set to
5002 *
5003 * This functions sets a given property on a given plane object. This function
5004 * calls the driver's ->set_property callback and changes the software state of
5005 * the property if the callback succeeds.
5006 *
5007 * Returns:
5008 * Zero on success, error code on failure.
5009 */
5010int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
5011 struct drm_property *property,
5012 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06005013{
5014 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005015 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06005016
5017 if (plane->funcs->set_property)
5018 ret = plane->funcs->set_property(plane, property, value);
5019 if (!ret)
5020 drm_object_property_set_value(obj, property, value);
5021
5022 return ret;
5023}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005024EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06005025
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005026/**
Daniel Vetter1a498632014-11-19 18:38:09 +01005027 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005028 * @dev: DRM device
5029 * @data: ioctl data
5030 * @file_priv: DRM file info
5031 *
5032 * This function retrieves the current value for an object's property. Compared
5033 * to the connector specific ioctl this one is extended to also work on crtc and
5034 * plane objects.
5035 *
5036 * Called by the user via ioctl.
5037 *
5038 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005039 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005040 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005041int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5042 struct drm_file *file_priv)
5043{
5044 struct drm_mode_obj_get_properties *arg = data;
5045 struct drm_mode_object *obj;
5046 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03005047
5048 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5049 return -EINVAL;
5050
Daniel Vetter84849902012-12-02 00:28:11 +01005051 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005052
5053 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5054 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005055 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005056 goto out;
5057 }
5058 if (!obj->properties) {
5059 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02005060 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005061 }
5062
Rob Clark88a48e22014-12-18 16:01:50 -05005063 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005064 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5065 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5066 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005067
Daniel Vetter1649c332016-04-22 22:10:28 +02005068out_unref:
5069 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005070out:
Daniel Vetter84849902012-12-02 00:28:11 +01005071 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005072 return ret;
5073}
5074
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005075/**
5076 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5077 * @dev: DRM device
5078 * @data: ioctl data
5079 * @file_priv: DRM file info
5080 *
5081 * This function sets the current value for an object's property. It also calls
5082 * into a driver's ->set_property callback to update the hardware state.
5083 * Compared to the connector specific ioctl this one is extended to also work on
5084 * crtc and plane objects.
5085 *
5086 * Called by the user via ioctl.
5087 *
5088 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005089 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005090 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005091int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5092 struct drm_file *file_priv)
5093{
5094 struct drm_mode_obj_set_property *arg = data;
5095 struct drm_mode_object *arg_obj;
5096 struct drm_mode_object *prop_obj;
5097 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005098 int i, ret = -EINVAL;
5099 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005100
5101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5102 return -EINVAL;
5103
Daniel Vetter84849902012-12-02 00:28:11 +01005104 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005105
5106 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005107 if (!arg_obj) {
5108 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005109 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005110 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005111 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02005112 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005113
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005114 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005115 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005116 break;
5117
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005118 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02005119 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005120
5121 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5122 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005123 if (!prop_obj) {
5124 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02005125 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005126 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005127 property = obj_to_property(prop_obj);
5128
Rob Clark3843e712014-12-16 18:05:29 -05005129 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10005130 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005131
5132 switch (arg_obj->type) {
5133 case DRM_MODE_OBJECT_CONNECTOR:
5134 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5135 arg->value);
5136 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005137 case DRM_MODE_OBJECT_CRTC:
5138 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5139 break;
Rob Clark4d939142012-05-17 02:23:27 -06005140 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005141 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5142 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005143 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005144 }
5145
Rob Clark3843e712014-12-16 18:05:29 -05005146 drm_property_change_valid_put(property, ref);
5147
Daniel Vetter1649c332016-04-22 22:10:28 +02005148out_unref:
5149 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005150out:
Daniel Vetter84849902012-12-02 00:28:11 +01005151 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005152 return ret;
5153}
5154
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005155/**
5156 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5157 * @connector: connector to attach
5158 * @encoder: encoder to attach @connector to
5159 *
5160 * This function links up a connector to an encoder. Note that the routing
5161 * restrictions between encoders and crtcs are exposed to userspace through the
5162 * possible_clones and possible_crtcs bitmasks.
5163 *
5164 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005165 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005166 */
Dave Airlief453ba02008-11-07 14:05:41 -08005167int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5168 struct drm_encoder *encoder)
5169{
5170 int i;
5171
Thierry Redingeb47fe82015-11-16 18:19:53 +01005172 /*
5173 * In the past, drivers have attempted to model the static association
5174 * of connector to encoder in simple connector/encoder devices using a
5175 * direct assignment of connector->encoder = encoder. This connection
5176 * is a logical one and the responsibility of the core, so drivers are
5177 * expected not to mess with this.
5178 *
5179 * Note that the error return should've been enough here, but a large
5180 * majority of drivers ignores the return value, so add in a big WARN
5181 * to get people's attention.
5182 */
5183 if (WARN_ON(connector->encoder))
5184 return -EINVAL;
5185
Dave Airlief453ba02008-11-07 14:05:41 -08005186 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5187 if (connector->encoder_ids[i] == 0) {
5188 connector->encoder_ids[i] = encoder->base.id;
5189 return 0;
5190 }
5191 }
5192 return -ENOMEM;
5193}
5194EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5195
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005196/**
5197 * drm_mode_crtc_set_gamma_size - set the gamma table size
5198 * @crtc: CRTC to set the gamma table size for
5199 * @gamma_size: size of the gamma table
5200 *
5201 * Drivers which support gamma tables should set this to the supported gamma
5202 * table size when initializing the CRTC. Currently the drm core only supports a
5203 * fixed gamma table size.
5204 *
5205 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005206 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005207 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005208int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005209 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005210{
Daniel Vettercf48e292016-03-30 11:51:16 +02005211 uint16_t *r_base, *g_base, *b_base;
5212 int i;
5213
Dave Airlief453ba02008-11-07 14:05:41 -08005214 crtc->gamma_size = gamma_size;
5215
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005216 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5217 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005218 if (!crtc->gamma_store) {
5219 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005220 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005221 }
5222
Daniel Vettercf48e292016-03-30 11:51:16 +02005223 r_base = crtc->gamma_store;
5224 g_base = r_base + gamma_size;
5225 b_base = g_base + gamma_size;
5226 for (i = 0; i < gamma_size; i++) {
5227 r_base[i] = i << 8;
5228 g_base[i] = i << 8;
5229 b_base[i] = i << 8;
5230 }
5231
5232
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005233 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005234}
5235EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5236
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005237/**
5238 * drm_mode_gamma_set_ioctl - set the gamma table
5239 * @dev: DRM device
5240 * @data: ioctl data
5241 * @file_priv: DRM file info
5242 *
5243 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5244 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5245 *
5246 * Called by the user via ioctl.
5247 *
5248 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005249 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005250 */
Dave Airlief453ba02008-11-07 14:05:41 -08005251int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5252 void *data, struct drm_file *file_priv)
5253{
5254 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005255 struct drm_crtc *crtc;
5256 void *r_base, *g_base, *b_base;
5257 int size;
5258 int ret = 0;
5259
Dave Airliefb3b06c2011-02-08 13:55:21 +10005260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5261 return -EINVAL;
5262
Daniel Vetter84849902012-12-02 00:28:11 +01005263 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005264 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5265 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005266 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005267 goto out;
5268 }
Dave Airlief453ba02008-11-07 14:05:41 -08005269
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005270 if (crtc->funcs->gamma_set == NULL) {
5271 ret = -ENOSYS;
5272 goto out;
5273 }
5274
Dave Airlief453ba02008-11-07 14:05:41 -08005275 /* memcpy into gamma store */
5276 if (crtc_lut->gamma_size != crtc->gamma_size) {
5277 ret = -EINVAL;
5278 goto out;
5279 }
5280
5281 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5282 r_base = crtc->gamma_store;
5283 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5284 ret = -EFAULT;
5285 goto out;
5286 }
5287
5288 g_base = r_base + size;
5289 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5290 ret = -EFAULT;
5291 goto out;
5292 }
5293
5294 b_base = g_base + size;
5295 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5296 ret = -EFAULT;
5297 goto out;
5298 }
5299
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02005300 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005301
5302out:
Daniel Vetter84849902012-12-02 00:28:11 +01005303 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005304 return ret;
5305
5306}
5307
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005308/**
5309 * drm_mode_gamma_get_ioctl - get the gamma table
5310 * @dev: DRM device
5311 * @data: ioctl data
5312 * @file_priv: DRM file info
5313 *
5314 * Copy the current gamma table into the storage provided. This also provides
5315 * the gamma table size the driver expects, which can be used to size the
5316 * allocated storage.
5317 *
5318 * Called by the user via ioctl.
5319 *
5320 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005321 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005322 */
Dave Airlief453ba02008-11-07 14:05:41 -08005323int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5324 void *data, struct drm_file *file_priv)
5325{
5326 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005327 struct drm_crtc *crtc;
5328 void *r_base, *g_base, *b_base;
5329 int size;
5330 int ret = 0;
5331
Dave Airliefb3b06c2011-02-08 13:55:21 +10005332 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5333 return -EINVAL;
5334
Daniel Vetter84849902012-12-02 00:28:11 +01005335 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005336 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5337 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005338 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005339 goto out;
5340 }
Dave Airlief453ba02008-11-07 14:05:41 -08005341
5342 /* memcpy into gamma store */
5343 if (crtc_lut->gamma_size != crtc->gamma_size) {
5344 ret = -EINVAL;
5345 goto out;
5346 }
5347
5348 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5349 r_base = crtc->gamma_store;
5350 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5351 ret = -EFAULT;
5352 goto out;
5353 }
5354
5355 g_base = r_base + size;
5356 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5357 ret = -EFAULT;
5358 goto out;
5359 }
5360
5361 b_base = g_base + size;
5362 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5363 ret = -EFAULT;
5364 goto out;
5365 }
5366out:
Daniel Vetter84849902012-12-02 00:28:11 +01005367 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005368 return ret;
5369}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005370
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005371/**
5372 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5373 * @dev: DRM device
5374 * @data: ioctl data
5375 * @file_priv: DRM file info
5376 *
5377 * This schedules an asynchronous update on a given CRTC, called page flip.
5378 * Optionally a drm event is generated to signal the completion of the event.
5379 * Generic drivers cannot assume that a pageflip with changed framebuffer
5380 * properties (including driver specific metadata like tiling layout) will work,
5381 * but some drivers support e.g. pixel format changes through the pageflip
5382 * ioctl.
5383 *
5384 * Called by the user via ioctl.
5385 *
5386 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005387 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005388 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005389int drm_mode_page_flip_ioctl(struct drm_device *dev,
5390 void *data, struct drm_file *file_priv)
5391{
5392 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005393 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005394 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005395 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005396 int ret = -EINVAL;
5397
5398 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5399 page_flip->reserved != 0)
5400 return -EINVAL;
5401
Keith Packard62f21042013-07-22 18:50:00 -07005402 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5403 return -EINVAL;
5404
Rob Clarka2b34e22013-10-05 16:36:52 -04005405 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5406 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005407 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005408
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005409 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005410 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005411 /* The framebuffer is currently unbound, presumably
5412 * due to a hotplug event, that userspace has not
5413 * yet discovered.
5414 */
5415 ret = -EBUSY;
5416 goto out;
5417 }
5418
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005419 if (crtc->funcs->page_flip == NULL)
5420 goto out;
5421
Daniel Vetter786b99e2012-12-02 21:53:40 +01005422 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005423 if (!fb) {
5424 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005425 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005426 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005427
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005428 if (crtc->state) {
5429 const struct drm_plane_state *state = crtc->primary->state;
5430
5431 ret = check_src_coords(state->src_x, state->src_y,
5432 state->src_w, state->src_h, fb);
5433 } else {
5434 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5435 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005436 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005437 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005438
Matt Roperf4510a22014-04-01 15:22:40 -07005439 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005440 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5441 ret = -EINVAL;
5442 goto out;
5443 }
5444
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005445 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005446 e = kzalloc(sizeof *e, GFP_KERNEL);
5447 if (!e) {
5448 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005449 goto out;
5450 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005451 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005452 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005453 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005454 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5455 if (ret) {
5456 kfree(e);
5457 goto out;
5458 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005459 }
5460
Daniel Vetter3d30a592014-07-27 13:42:42 +02005461 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005462 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005463 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005464 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5465 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005466 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005467 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005468 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005469 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005470 /* Unref only the old framebuffer. */
5471 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005472 }
5473
5474out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005475 if (fb)
5476 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005477 if (crtc->primary->old_fb)
5478 drm_framebuffer_unreference(crtc->primary->old_fb);
5479 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005480 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005481
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005482 return ret;
5483}
Chris Wilsoneb033552011-01-24 15:11:08 +00005484
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005485/**
5486 * drm_mode_config_reset - call ->reset callbacks
5487 * @dev: drm device
5488 *
5489 * This functions calls all the crtc's, encoder's and connector's ->reset
5490 * callback. Drivers can use this in e.g. their driver load or resume code to
5491 * reset hardware and software state.
5492 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005493void drm_mode_config_reset(struct drm_device *dev)
5494{
5495 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005496 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005497 struct drm_encoder *encoder;
5498 struct drm_connector *connector;
5499
Daniel Vettere4f62542015-07-09 23:44:35 +02005500 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005501 if (plane->funcs->reset)
5502 plane->funcs->reset(plane);
5503
Daniel Vettere4f62542015-07-09 23:44:35 +02005504 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005505 if (crtc->funcs->reset)
5506 crtc->funcs->reset(crtc);
5507
Daniel Vettere4f62542015-07-09 23:44:35 +02005508 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005509 if (encoder->funcs->reset)
5510 encoder->funcs->reset(encoder);
5511
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005512 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005513 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005514 if (connector->funcs->reset)
5515 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005516 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005517}
5518EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005519
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005520/**
5521 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5522 * @dev: DRM device
5523 * @data: ioctl data
5524 * @file_priv: DRM file info
5525 *
5526 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5527 * TTM or something else entirely) and returns the resulting buffer handle. This
5528 * handle can then be wrapped up into a framebuffer modeset object.
5529 *
5530 * Note that userspace is not allowed to use such objects for render
5531 * acceleration - drivers must create their own private ioctls for such a use
5532 * case.
5533 *
5534 * Called by the user via ioctl.
5535 *
5536 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005537 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005538 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005539int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5540 void *data, struct drm_file *file_priv)
5541{
5542 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005543 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005544
5545 if (!dev->driver->dumb_create)
5546 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005547 if (!args->width || !args->height || !args->bpp)
5548 return -EINVAL;
5549
5550 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005551 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005552 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005553 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005554 return -EINVAL;
5555 stride = cpp * args->width;
5556 if (args->height > 0xffffffffU / stride)
5557 return -EINVAL;
5558
5559 /* test for wrap-around */
5560 size = args->height * stride;
5561 if (PAGE_ALIGN(size) == 0)
5562 return -EINVAL;
5563
Thierry Redingf6085952014-11-03 11:14:14 +01005564 /*
5565 * handle, pitch and size are output parameters. Zero them out to
5566 * prevent drivers from accidentally using uninitialized data. Since
5567 * not all existing userspace is clearing these fields properly we
5568 * cannot reject IOCTL with garbage in them.
5569 */
5570 args->handle = 0;
5571 args->pitch = 0;
5572 args->size = 0;
5573
Dave Airlieff72145b2011-02-07 12:16:14 +10005574 return dev->driver->dumb_create(file_priv, dev, args);
5575}
5576
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005577/**
5578 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5579 * @dev: DRM device
5580 * @data: ioctl data
5581 * @file_priv: DRM file info
5582 *
5583 * Allocate an offset in the drm device node's address space to be able to
5584 * memory map a dumb buffer.
5585 *
5586 * Called by the user via ioctl.
5587 *
5588 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005589 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005590 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005591int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5592 void *data, struct drm_file *file_priv)
5593{
5594 struct drm_mode_map_dumb *args = data;
5595
5596 /* call driver ioctl to get mmap offset */
5597 if (!dev->driver->dumb_map_offset)
5598 return -ENOSYS;
5599
5600 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5601}
5602
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005603/**
5604 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5605 * @dev: DRM device
5606 * @data: ioctl data
5607 * @file_priv: DRM file info
5608 *
5609 * This destroys the userspace handle for the given dumb backing storage buffer.
5610 * Since buffer objects must be reference counted in the kernel a buffer object
5611 * won't be immediately freed if a framebuffer modeset object still uses it.
5612 *
5613 * Called by the user via ioctl.
5614 *
5615 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005616 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005617 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005618int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5619 void *data, struct drm_file *file_priv)
5620{
5621 struct drm_mode_destroy_dumb *args = data;
5622
5623 if (!dev->driver->dumb_destroy)
5624 return -ENOSYS;
5625
5626 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5627}
Dave Airlie248dbc22011-11-29 20:02:54 +00005628
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005629/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305630 * drm_rotation_simplify() - Try to simplify the rotation
5631 * @rotation: Rotation to be simplified
5632 * @supported_rotations: Supported rotations
5633 *
5634 * Attempt to simplify the rotation to a form that is supported.
5635 * Eg. if the hardware supports everything except DRM_REFLECT_X
5636 * one could call this function like this:
5637 *
5638 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5639 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5640 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5641 *
5642 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5643 * transforms the hardware supports, this function may not
5644 * be able to produce a supported transform, so the caller should
5645 * check the result afterwards.
5646 */
5647unsigned int drm_rotation_simplify(unsigned int rotation,
5648 unsigned int supported_rotations)
5649{
5650 if (rotation & ~supported_rotations) {
5651 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005652 rotation = (rotation & DRM_REFLECT_MASK) |
5653 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305654 }
5655
5656 return rotation;
5657}
5658EXPORT_SYMBOL(drm_rotation_simplify);
5659
5660/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005661 * drm_mode_config_init - initialize DRM mode_configuration structure
5662 * @dev: DRM device
5663 *
5664 * Initialize @dev's mode_config structure, used for tracking the graphics
5665 * configuration of @dev.
5666 *
5667 * Since this initializes the modeset locks, no locking is possible. Which is no
5668 * problem, since this should happen single threaded at init time. It is the
5669 * driver's problem to ensure this guarantee.
5670 *
5671 */
5672void drm_mode_config_init(struct drm_device *dev)
5673{
5674 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005675 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005676 mutex_init(&dev->mode_config.idr_mutex);
5677 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005678 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005679 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5680 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5681 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5682 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5683 INIT_LIST_HEAD(&dev->mode_config.property_list);
5684 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5685 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5686 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005687 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005688 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005689
5690 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005691 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005692 drm_modeset_unlock_all(dev);
5693
5694 /* Just to be sure */
5695 dev->mode_config.num_fb = 0;
5696 dev->mode_config.num_connector = 0;
5697 dev->mode_config.num_crtc = 0;
5698 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005699 dev->mode_config.num_overlay_plane = 0;
5700 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005701}
5702EXPORT_SYMBOL(drm_mode_config_init);
5703
5704/**
5705 * drm_mode_config_cleanup - free up DRM mode_config info
5706 * @dev: DRM device
5707 *
5708 * Free up all the connectors and CRTCs associated with this DRM device, then
5709 * free up the framebuffers and associated buffer objects.
5710 *
5711 * Note that since this /should/ happen single-threaded at driver/device
5712 * teardown time, no locking is required. It's the driver's job to ensure that
5713 * this guarantee actually holds true.
5714 *
5715 * FIXME: cleanup any dangling user buffer objects too
5716 */
5717void drm_mode_config_cleanup(struct drm_device *dev)
5718{
5719 struct drm_connector *connector, *ot;
5720 struct drm_crtc *crtc, *ct;
5721 struct drm_encoder *encoder, *enct;
5722 struct drm_framebuffer *fb, *fbt;
5723 struct drm_property *property, *pt;
5724 struct drm_property_blob *blob, *bt;
5725 struct drm_plane *plane, *plt;
5726
5727 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5728 head) {
5729 encoder->funcs->destroy(encoder);
5730 }
5731
5732 list_for_each_entry_safe(connector, ot,
5733 &dev->mode_config.connector_list, head) {
5734 connector->funcs->destroy(connector);
5735 }
5736
5737 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5738 head) {
5739 drm_property_destroy(dev, property);
5740 }
5741
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005742 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5743 head) {
5744 plane->funcs->destroy(plane);
5745 }
5746
5747 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5748 crtc->funcs->destroy(crtc);
5749 }
5750
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005751 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005752 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005753 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005754 }
5755
5756 /*
5757 * Single-threaded teardown context, so it's not required to grab the
5758 * fb_lock to protect against concurrent fb_list access. Contrary, it
5759 * would actually deadlock with the drm_framebuffer_cleanup function.
5760 *
5761 * Also, if there are any framebuffers left, that's a driver leak now,
5762 * so politely WARN about this.
5763 */
5764 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5765 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005766 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005767 }
5768
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005769 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005770 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005771 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005772 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005773}
5774EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305775
5776struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5777 unsigned int supported_rotations)
5778{
5779 static const struct drm_prop_enum_list props[] = {
5780 { DRM_ROTATE_0, "rotate-0" },
5781 { DRM_ROTATE_90, "rotate-90" },
5782 { DRM_ROTATE_180, "rotate-180" },
5783 { DRM_ROTATE_270, "rotate-270" },
5784 { DRM_REFLECT_X, "reflect-x" },
5785 { DRM_REFLECT_Y, "reflect-y" },
5786 };
5787
5788 return drm_property_create_bitmask(dev, 0, "rotation",
5789 props, ARRAY_SIZE(props),
5790 supported_rotations);
5791}
5792EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005793
5794/**
5795 * DOC: Tile group
5796 *
5797 * Tile groups are used to represent tiled monitors with a unique
5798 * integer identifier. Tiled monitors using DisplayID v1.3 have
5799 * a unique 8-byte handle, we store this in a tile group, so we
5800 * have a common identifier for all tiles in a monitor group.
5801 */
5802static void drm_tile_group_free(struct kref *kref)
5803{
5804 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5805 struct drm_device *dev = tg->dev;
5806 mutex_lock(&dev->mode_config.idr_mutex);
5807 idr_remove(&dev->mode_config.tile_idr, tg->id);
5808 mutex_unlock(&dev->mode_config.idr_mutex);
5809 kfree(tg);
5810}
5811
5812/**
5813 * drm_mode_put_tile_group - drop a reference to a tile group.
5814 * @dev: DRM device
5815 * @tg: tile group to drop reference to.
5816 *
5817 * drop reference to tile group and free if 0.
5818 */
5819void drm_mode_put_tile_group(struct drm_device *dev,
5820 struct drm_tile_group *tg)
5821{
5822 kref_put(&tg->refcount, drm_tile_group_free);
5823}
5824
5825/**
5826 * drm_mode_get_tile_group - get a reference to an existing tile group
5827 * @dev: DRM device
5828 * @topology: 8-bytes unique per monitor.
5829 *
5830 * Use the unique bytes to get a reference to an existing tile group.
5831 *
5832 * RETURNS:
5833 * tile group or NULL if not found.
5834 */
5835struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5836 char topology[8])
5837{
5838 struct drm_tile_group *tg;
5839 int id;
5840 mutex_lock(&dev->mode_config.idr_mutex);
5841 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5842 if (!memcmp(tg->group_data, topology, 8)) {
5843 if (!kref_get_unless_zero(&tg->refcount))
5844 tg = NULL;
5845 mutex_unlock(&dev->mode_config.idr_mutex);
5846 return tg;
5847 }
5848 }
5849 mutex_unlock(&dev->mode_config.idr_mutex);
5850 return NULL;
5851}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005852EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005853
5854/**
5855 * drm_mode_create_tile_group - create a tile group from a displayid description
5856 * @dev: DRM device
5857 * @topology: 8-bytes unique per monitor.
5858 *
5859 * Create a tile group for the unique monitor, and get a unique
5860 * identifier for the tile group.
5861 *
5862 * RETURNS:
5863 * new tile group or error.
5864 */
5865struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5866 char topology[8])
5867{
5868 struct drm_tile_group *tg;
5869 int ret;
5870
5871 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5872 if (!tg)
5873 return ERR_PTR(-ENOMEM);
5874
5875 kref_init(&tg->refcount);
5876 memcpy(tg->group_data, topology, 8);
5877 tg->dev = dev;
5878
5879 mutex_lock(&dev->mode_config.idr_mutex);
5880 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5881 if (ret >= 0) {
5882 tg->id = ret;
5883 } else {
5884 kfree(tg);
5885 tg = ERR_PTR(ret);
5886 }
5887
5888 mutex_unlock(&dev->mode_config.idr_mutex);
5889 return tg;
5890}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005891EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005892
5893/**
5894 * drm_crtc_enable_color_mgmt - enable color management properties
5895 * @crtc: DRM CRTC
5896 * @degamma_lut_size: the size of the degamma lut (before CSC)
5897 * @has_ctm: whether to attach ctm_property for CSC matrix
5898 * @gamma_lut_size: the size of the gamma lut (after CSC)
5899 *
5900 * This function lets the driver enable the color correction
5901 * properties on a CRTC. This includes 3 degamma, csc and gamma
5902 * properties that userspace can set and 2 size properties to inform
5903 * the userspace of the lut sizes. Each of the properties are
5904 * optional. The gamma and degamma properties are only attached if
5905 * their size is not 0 and ctm_property is only attached if has_ctm is
5906 * true.
5907 */
5908void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5909 uint degamma_lut_size,
5910 bool has_ctm,
5911 uint gamma_lut_size)
5912{
5913 struct drm_device *dev = crtc->dev;
5914 struct drm_mode_config *config = &dev->mode_config;
5915
5916 if (degamma_lut_size) {
5917 drm_object_attach_property(&crtc->base,
5918 config->degamma_lut_property, 0);
5919 drm_object_attach_property(&crtc->base,
5920 config->degamma_lut_size_property,
5921 degamma_lut_size);
5922 }
5923
5924 if (has_ctm)
5925 drm_object_attach_property(&crtc->base,
5926 config->ctm_property, 0);
5927
5928 if (gamma_lut_size) {
5929 drm_object_attach_property(&crtc->base,
5930 config->gamma_lut_property, 0);
5931 drm_object_attach_property(&crtc->base,
5932 config->gamma_lut_size_property,
5933 gamma_lut_size);
5934 }
5935}
5936EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);