blob: 93ea5ddd1e49b4e9699520c04e321b90540727d9 [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 Airlied0f37cf62016-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 Airlied0f37cf62016-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 Airlied0f37cf62016-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 Airlied0f37cf62016-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 Airlied0f37cf62016-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 Airlied0f37cf62016-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
Lukas Wunner6a0d9522016-06-08 18:47:27 +0200399/**
400 * drm_crtc_force_disable - Forcibly turn off a CRTC
401 * @crtc: CRTC to turn off
402 *
403 * Returns:
404 * Zero on success, error code on failure.
405 */
406int drm_crtc_force_disable(struct drm_crtc *crtc)
407{
408 struct drm_mode_set set = {
409 .crtc = crtc,
410 };
411
412 return drm_mode_set_config_internal(&set);
413}
414EXPORT_SYMBOL(drm_crtc_force_disable);
415
416/**
417 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
418 * @dev: DRM device whose CRTCs to turn off
419 *
420 * Drivers may want to call this on unload to ensure that all displays are
421 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
422 *
423 * Returns:
424 * Zero on success, error code on failure.
425 */
426int drm_crtc_force_disable_all(struct drm_device *dev)
427{
428 struct drm_crtc *crtc;
429 int ret = 0;
430
431 drm_modeset_lock_all(dev);
432 drm_for_each_crtc(crtc, dev)
433 if (crtc->enabled) {
434 ret = drm_crtc_force_disable(crtc);
435 if (ret)
436 goto out;
437 }
438out:
439 drm_modeset_unlock_all(dev);
440 return ret;
441}
442EXPORT_SYMBOL(drm_crtc_force_disable_all);
443
Dave Airlief55f1f92016-04-15 15:10:33 +1000444static void drm_framebuffer_free(struct kref *kref)
445{
446 struct drm_framebuffer *fb =
Dave Airlied0f37cf62016-04-15 15:10:36 +1000447 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000448 struct drm_device *dev = fb->dev;
449
450 /*
451 * The lookup idr holds a weak reference, which has not necessarily been
452 * removed at this point. Check for that.
453 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000454 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000455
456 fb->funcs->destroy(fb);
457}
458
Dave Airlief453ba02008-11-07 14:05:41 -0800459/**
Dave Airlief453ba02008-11-07 14:05:41 -0800460 * drm_framebuffer_init - initialize a framebuffer
461 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100462 * @fb: framebuffer to be initialized
463 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800464 *
Dave Airlief453ba02008-11-07 14:05:41 -0800465 * Allocates an ID for the framebuffer's parent mode object, sets its mode
466 * functions & device file and adds it to the master fd list.
467 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100468 * IMPORTANT:
469 * This functions publishes the fb and makes it available for concurrent access
470 * by other users. Which means by this point the fb _must_ be fully set up -
471 * since all the fb attributes are invariant over its lifetime, no further
472 * locking but only correct reference counting is required.
473 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100474 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200475 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800476 */
477int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
478 const struct drm_framebuffer_funcs *funcs)
479{
480 int ret;
481
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100482 INIT_LIST_HEAD(&fb->filp_head);
483 fb->dev = dev;
484 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000485
Dave Airlied0f37cf62016-04-15 15:10:36 +1000486 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000487 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200488 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100489 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800490
Dave Airlie9cd47422016-04-15 15:10:38 +1000491 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800492 dev->mode_config.num_fb++;
493 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000494 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800495
Dave Airlie9cd47422016-04-15 15:10:38 +1000496 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000497out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200498 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800499}
500EXPORT_SYMBOL(drm_framebuffer_init);
501
Rob Clarkf7eff602012-09-05 21:48:38 +0000502/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100503 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
504 * @dev: drm device
505 * @id: id of the fb object
506 *
507 * If successful, this grabs an additional reference to the framebuffer -
508 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100509 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100510 */
511struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
512 uint32_t id)
513{
Dave Airliecee26ac2016-04-15 15:10:37 +1000514 struct drm_mode_object *obj;
515 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100516
Dave Airliecee26ac2016-04-15 15:10:37 +1000517 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000518 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000519 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100520 return fb;
521}
522EXPORT_SYMBOL(drm_framebuffer_lookup);
523
524/**
Daniel Vetter36206362012-12-10 20:42:17 +0100525 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
526 * @fb: fb to unregister
527 *
528 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
529 * those used for fbdev. Note that the caller must hold a reference of it's own,
530 * i.e. the object may not be destroyed through this call (since it'll lead to a
531 * locking inversion).
532 */
533void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
534{
Daniel Vettera39a3572015-08-25 15:45:11 +0200535 struct drm_device *dev;
536
537 if (!fb)
538 return;
539
540 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100541
Daniel Vetter2b677e82012-12-10 21:16:05 +0100542 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000543 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100544}
545EXPORT_SYMBOL(drm_framebuffer_unregister_private);
546
547/**
Dave Airlief453ba02008-11-07 14:05:41 -0800548 * drm_framebuffer_cleanup - remove a framebuffer object
549 * @fb: framebuffer to remove
550 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100551 * Cleanup framebuffer. This function is intended to be used from the drivers
552 * ->destroy callback. It can also be used to clean up driver private
Daniel Vetter2e7a5702016-06-01 23:40:36 +0200553 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100554 *
555 * Note that this function does not remove the fb from active usuage - if it is
556 * still used anywhere, hilarity can ensue since userspace could call getfb on
557 * the id and get back -EINVAL. Obviously no concern at driver unload time.
558 *
559 * Also, the framebuffer will not be removed from the lookup idr - for
560 * user-created framebuffers this will happen in in the rmfb ioctl. For
561 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
562 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800563 */
564void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
565{
566 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100567
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100568 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000569 list_del(&fb->head);
570 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100571 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000572}
573EXPORT_SYMBOL(drm_framebuffer_cleanup);
574
575/**
576 * drm_framebuffer_remove - remove and unreference a framebuffer object
577 * @fb: framebuffer to remove
578 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000579 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100580 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100581 * passed-in framebuffer. Might take the modeset locks.
582 *
583 * Note that this function optimizes the cleanup away if the caller holds the
584 * last reference to the framebuffer. It is also guaranteed to not take the
585 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000586 */
587void drm_framebuffer_remove(struct drm_framebuffer *fb)
588{
Daniel Vettera39a3572015-08-25 15:45:11 +0200589 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800590 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800591 struct drm_plane *plane;
Dave Airlief453ba02008-11-07 14:05:41 -0800592
Daniel Vettera39a3572015-08-25 15:45:11 +0200593 if (!fb)
594 return;
595
596 dev = fb->dev;
597
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100598 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100599
Daniel Vetterb62584e2012-12-11 16:51:35 +0100600 /*
601 * drm ABI mandates that we remove any deleted framebuffers from active
602 * useage. But since most sane clients only remove framebuffers they no
603 * longer need, try to optimize this away.
604 *
605 * Since we're holding a reference ourselves, observing a refcount of 1
606 * means that we're the last holder and can skip it. Also, the refcount
607 * can never increase from 1 again, so we don't need any barriers or
608 * locks.
609 *
610 * Note that userspace could try to race with use and instate a new
611 * usage _after_ we've cleared all current ones. End result will be an
612 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
613 * in this manner.
614 */
Dave Airlie747a5982016-04-15 15:10:35 +1000615 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100616 drm_modeset_lock_all(dev);
617 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200618 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700619 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100620 /* should turn off the crtc */
Lukas Wunner3e31d242016-06-08 18:47:27 +0200621 if (drm_crtc_force_disable(crtc))
Daniel Vetterb62584e2012-12-11 16:51:35 +0100622 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
623 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000624 }
Dave Airlief453ba02008-11-07 14:05:41 -0800625
Daniel Vetter6295d602015-07-09 23:44:25 +0200626 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300627 if (plane->fb == fb)
628 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800629 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100630 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800631 }
632
Rob Clarkf7eff602012-09-05 21:48:38 +0000633 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800634}
Rob Clarkf7eff602012-09-05 21:48:38 +0000635EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800636
Rob Clark51fd3712013-11-19 12:10:12 -0500637DEFINE_WW_CLASS(crtc_ww_class);
638
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200639static unsigned int drm_num_crtcs(struct drm_device *dev)
640{
641 unsigned int num = 0;
642 struct drm_crtc *tmp;
643
644 drm_for_each_crtc(tmp, dev) {
645 num++;
646 }
647
648 return num;
649}
650
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200651static int drm_crtc_register_all(struct drm_device *dev)
652{
653 struct drm_crtc *crtc;
654 int ret = 0;
655
656 drm_for_each_crtc(crtc, dev) {
657 if (crtc->funcs->late_register)
658 ret = crtc->funcs->late_register(crtc);
659 if (ret)
660 return ret;
661 }
662
663 return 0;
664}
665
666static void drm_crtc_unregister_all(struct drm_device *dev)
667{
668 struct drm_crtc *crtc;
669
670 drm_for_each_crtc(crtc, dev) {
671 if (crtc->funcs->early_unregister)
672 crtc->funcs->early_unregister(crtc);
673 }
674}
675
Dave Airlief453ba02008-11-07 14:05:41 -0800676/**
Matt Ropere13161a2014-04-01 15:22:38 -0700677 * drm_crtc_init_with_planes - Initialise a new CRTC object with
678 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800679 * @dev: DRM device
680 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700681 * @primary: Primary plane for CRTC
682 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800683 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200684 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800685 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000686 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200687 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100688 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200689 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800690 */
Matt Ropere13161a2014-04-01 15:22:38 -0700691int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
692 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700693 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200694 const struct drm_crtc_funcs *funcs,
695 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800696{
Rob Clark51fd3712013-11-19 12:10:12 -0500697 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200698 int ret;
699
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100700 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
701 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
702
Dave Airlief453ba02008-11-07 14:05:41 -0800703 crtc->dev = dev;
704 crtc->funcs = funcs;
705
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200706 INIT_LIST_HEAD(&crtc->commit_list);
707 spin_lock_init(&crtc->commit_lock);
708
Rob Clark51fd3712013-11-19 12:10:12 -0500709 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200710 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
711 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100712 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800713
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200714 if (name) {
715 va_list ap;
716
717 va_start(ap, name);
718 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
719 va_end(ap);
720 } else {
721 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
722 drm_num_crtcs(dev));
723 }
724 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000725 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200726 return -ENOMEM;
727 }
728
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300729 crtc->base.properties = &crtc->properties;
730
Rob Clark51fd3712013-11-19 12:10:12 -0500731 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100732 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200733
Matt Ropere13161a2014-04-01 15:22:38 -0700734 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700735 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700736 if (primary)
737 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700738 if (cursor)
739 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700740
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100741 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
742 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100743 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100744 }
745
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100746 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800747}
Matt Ropere13161a2014-04-01 15:22:38 -0700748EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800749
750/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000751 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800752 * @crtc: CRTC to cleanup
753 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000754 * This function cleans up @crtc and removes it from the DRM mode setting
755 * core. Note that the function does *not* free the crtc structure itself,
756 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800757 */
758void drm_crtc_cleanup(struct drm_crtc *crtc)
759{
760 struct drm_device *dev = crtc->dev;
761
Chris Wilson490d3d12016-05-27 20:05:00 +0100762 /* Note that the crtc_list is considered to be static; should we
763 * remove the drm_crtc at runtime we would have to decrement all
764 * the indices on the drm_crtc after us in the crtc_list.
765 */
766
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000767 kfree(crtc->gamma_store);
768 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800769
Rob Clark51fd3712013-11-19 12:10:12 -0500770 drm_modeset_lock_fini(&crtc->mutex);
771
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000772 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800773 list_del(&crtc->head);
774 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100775
776 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
777 if (crtc->state && crtc->funcs->atomic_destroy_state)
778 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100779
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200780 kfree(crtc->name);
781
Thierry Redinga18c0af2014-12-10 11:38:49 +0100782 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800783}
784EXPORT_SYMBOL(drm_crtc_cleanup);
785
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100786/*
Dave Airlief453ba02008-11-07 14:05:41 -0800787 * drm_mode_remove - remove and free a mode
788 * @connector: connector list to modify
789 * @mode: mode to remove
790 *
Dave Airlief453ba02008-11-07 14:05:41 -0800791 * Remove @mode from @connector's mode list, then free it.
792 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100793static void drm_mode_remove(struct drm_connector *connector,
794 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800795{
796 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100797 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800798}
Dave Airlief453ba02008-11-07 14:05:41 -0800799
800/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200801 * drm_display_info_set_bus_formats - set the supported bus formats
802 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100803 * @formats: array containing the supported bus formats
804 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200805 *
806 * Store the supported bus formats in display info structure.
807 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
808 * a full list of available formats.
809 */
810int drm_display_info_set_bus_formats(struct drm_display_info *info,
811 const u32 *formats,
812 unsigned int num_formats)
813{
814 u32 *fmts = NULL;
815
816 if (!formats && num_formats)
817 return -EINVAL;
818
819 if (formats && num_formats) {
820 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
821 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300822 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200823 return -ENOMEM;
824 }
825
826 kfree(info->bus_formats);
827 info->bus_formats = fmts;
828 info->num_bus_formats = num_formats;
829
830 return 0;
831}
832EXPORT_SYMBOL(drm_display_info_set_bus_formats);
833
834/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200835 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
836 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200837 *
838 * The kernel supports per-connector configration of its consoles through
839 * use of the video= parameter. This function parses that option and
840 * extracts the user's specified mode (or enable/disable status) for a
841 * particular connector. This is typically only used during the early fbdev
842 * setup.
843 */
844static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
845{
846 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
847 char *option = NULL;
848
849 if (fb_get_options(connector->name, &option))
850 return;
851
852 if (!drm_mode_parse_command_line_for_connector(option,
853 connector,
854 mode))
855 return;
856
857 if (mode->force) {
858 const char *s;
859
860 switch (mode->force) {
861 case DRM_FORCE_OFF:
862 s = "OFF";
863 break;
864 case DRM_FORCE_ON_DIGITAL:
865 s = "ON - dig";
866 break;
867 default:
868 case DRM_FORCE_ON:
869 s = "ON";
870 break;
871 }
872
873 DRM_INFO("forcing %s connector %s\n", connector->name, s);
874 connector->force = mode->force;
875 }
876
877 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
878 connector->name,
879 mode->xres, mode->yres,
880 mode->refresh_specified ? mode->refresh : 60,
881 mode->rb ? " reduced blanking" : "",
882 mode->margins ? " with margins" : "",
883 mode->interlace ? " interlaced" : "");
884}
885
Dave Airlieb164d312016-04-27 11:10:09 +1000886static void drm_connector_free(struct kref *kref)
887{
888 struct drm_connector *connector =
889 container_of(kref, struct drm_connector, base.refcount);
890 struct drm_device *dev = connector->dev;
891
892 drm_mode_object_unregister(dev, &connector->base);
893 connector->funcs->destroy(connector);
894}
895
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200896/**
Dave Airlief453ba02008-11-07 14:05:41 -0800897 * drm_connector_init - Init a preallocated connector
898 * @dev: DRM device
899 * @connector: the connector to init
900 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100901 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800902 *
Dave Airlief453ba02008-11-07 14:05:41 -0800903 * Initialises a preallocated connector. Connectors should be
904 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200905 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100906 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200907 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800908 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200909int drm_connector_init(struct drm_device *dev,
910 struct drm_connector *connector,
911 const struct drm_connector_funcs *funcs,
912 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800913{
Rob Clarkae16c592014-12-18 16:01:54 -0500914 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200915 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400916 struct ida *connector_ida =
917 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200918
Daniel Vetter84849902012-12-02 00:28:11 +0100919 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800920
Dave Airlieb164d312016-04-27 11:10:09 +1000921 ret = drm_mode_object_get_reg(dev, &connector->base,
922 DRM_MODE_OBJECT_CONNECTOR,
923 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200924 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300925 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200926
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300927 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800928 connector->dev = dev;
929 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100930
Daniel Vetter69425592016-07-19 18:25:01 +0200931 ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
932 if (ret < 0)
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100933 goto out_put;
Daniel Vetter69425592016-07-19 18:25:01 +0200934 connector->index = ret;
935 ret = 0;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100936
Dave Airlief453ba02008-11-07 14:05:41 -0800937 connector->connector_type = connector_type;
938 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400939 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
940 if (connector->connector_type_id < 0) {
941 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100942 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400943 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300944 connector->name =
945 kasprintf(GFP_KERNEL, "%s-%d",
946 drm_connector_enum_list[connector_type].name,
947 connector->connector_type_id);
948 if (!connector->name) {
949 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100950 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300951 }
952
Dave Airlief453ba02008-11-07 14:05:41 -0800953 INIT_LIST_HEAD(&connector->probed_modes);
954 INIT_LIST_HEAD(&connector->modes);
955 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000956 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800957
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200958 drm_connector_get_cmdline_mode(connector);
959
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100960 /* We should add connectors at the end to avoid upsetting the connector
961 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500962 list_add_tail(&connector->head, &config->connector_list);
963 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800964
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200965 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500966 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500967 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200968 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800969
Rob Clark58495562012-10-11 20:50:56 -0500970 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500971 config->dpms_property, 0);
972
973 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
974 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
975 }
Dave Airlief453ba02008-11-07 14:05:41 -0800976
Thomas Wood30f65702014-06-18 17:52:32 +0100977 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100978out_put_type_id:
979 if (ret)
980 ida_remove(connector_ida, connector->connector_type_id);
981out_put_id:
982 if (ret)
Daniel Vetter69425592016-07-19 18:25:01 +0200983 ida_remove(&config->connector_ida, connector->index);
Jani Nikula2abdd312014-05-14 16:58:19 +0300984out_put:
985 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000986 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300987
988out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100989 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200990
991 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800992}
993EXPORT_SYMBOL(drm_connector_init);
994
995/**
996 * drm_connector_cleanup - cleans up an initialised connector
997 * @connector: connector to cleanup
998 *
Dave Airlief453ba02008-11-07 14:05:41 -0800999 * Cleans up the connector but doesn't free the object.
1000 */
1001void drm_connector_cleanup(struct drm_connector *connector)
1002{
1003 struct drm_device *dev = connector->dev;
1004 struct drm_display_mode *mode, *t;
1005
Chris Wilson80de3462016-06-15 13:17:49 +01001006 /* The connector should have been removed from userspace long before
1007 * it is finally destroyed.
1008 */
1009 if (WARN_ON(connector->registered))
1010 drm_connector_unregister(connector);
1011
Dave Airlie40d9b042014-10-20 16:29:33 +10001012 if (connector->tile_group) {
1013 drm_mode_put_tile_group(dev, connector->tile_group);
1014 connector->tile_group = NULL;
1015 }
1016
Dave Airlief453ba02008-11-07 14:05:41 -08001017 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
1018 drm_mode_remove(connector, mode);
1019
1020 list_for_each_entry_safe(mode, t, &connector->modes, head)
1021 drm_mode_remove(connector, mode);
1022
Ilia Mirkinb21e3af2013-08-07 22:34:48 -04001023 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
1024 connector->connector_type_id);
1025
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001026 ida_remove(&dev->mode_config.connector_ida,
Daniel Vetter69425592016-07-19 18:25:01 +02001027 connector->index);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001028
Boris Brezillonb5571e92014-07-22 12:09:10 +02001029 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001030 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +03001031 kfree(connector->name);
1032 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001033 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001034 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001035
1036 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1037 if (connector->state && connector->funcs->atomic_destroy_state)
1038 connector->funcs->atomic_destroy_state(connector,
1039 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001040
1041 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001042}
1043EXPORT_SYMBOL(drm_connector_cleanup);
1044
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001045/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001046 * drm_connector_register - register a connector
1047 * @connector: the connector to register
1048 *
1049 * Register userspace interfaces for a connector
1050 *
1051 * Returns:
1052 * Zero on success, error code on failure.
1053 */
1054int drm_connector_register(struct drm_connector *connector)
1055{
Thomas Wood30f65702014-06-18 17:52:32 +01001056 int ret;
1057
Chris Wilson40daac62016-06-15 13:17:48 +01001058 if (connector->registered)
1059 return 0;
1060
Thomas Wood30f65702014-06-18 17:52:32 +01001061 ret = drm_sysfs_connector_add(connector);
1062 if (ret)
1063 return ret;
1064
1065 ret = drm_debugfs_connector_add(connector);
1066 if (ret) {
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001067 goto err_sysfs;
1068 }
1069
1070 if (connector->funcs->late_register) {
1071 ret = connector->funcs->late_register(connector);
1072 if (ret)
1073 goto err_debugfs;
Thomas Wood30f65702014-06-18 17:52:32 +01001074 }
1075
Daniel Vetterfdf2c852016-05-06 14:55:02 +02001076 drm_mode_object_register(connector->dev, &connector->base);
1077
Chris Wilson40daac62016-06-15 13:17:48 +01001078 connector->registered = true;
Thomas Wood30f65702014-06-18 17:52:32 +01001079 return 0;
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001080
1081err_debugfs:
1082 drm_debugfs_connector_remove(connector);
1083err_sysfs:
1084 drm_sysfs_connector_remove(connector);
1085 return ret;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001086}
1087EXPORT_SYMBOL(drm_connector_register);
1088
1089/**
1090 * drm_connector_unregister - unregister a connector
1091 * @connector: the connector to unregister
1092 *
1093 * Unregister userspace interfaces for a connector
1094 */
1095void drm_connector_unregister(struct drm_connector *connector)
1096{
Chris Wilson40daac62016-06-15 13:17:48 +01001097 if (!connector->registered)
1098 return;
1099
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001100 if (connector->funcs->early_unregister)
1101 connector->funcs->early_unregister(connector);
1102
Thomas Wood34ea3d32014-05-29 16:57:41 +01001103 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001104 drm_debugfs_connector_remove(connector);
Chris Wilson40daac62016-06-15 13:17:48 +01001105
1106 connector->registered = false;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001107}
1108EXPORT_SYMBOL(drm_connector_unregister);
1109
Chris Wilson61005982016-07-13 17:39:07 +01001110static void drm_connector_unregister_all(struct drm_device *dev)
1111{
1112 struct drm_connector *connector;
1113
1114 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1115 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1116 drm_connector_unregister(connector);
1117}
1118
Chris Wilson041401f2016-06-24 15:36:18 +01001119static int drm_connector_register_all(struct drm_device *dev)
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001120{
1121 struct drm_connector *connector;
1122 int ret;
1123
Daniel Vetter5c6c2012016-08-04 18:35:48 +02001124 /* FIXME: taking the mode config mutex ends up in a clash with
1125 * fbcon/backlight registration */
1126 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001127 ret = drm_connector_register(connector);
1128 if (ret)
1129 goto err;
1130 }
1131
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001132 return 0;
1133
1134err:
1135 mutex_unlock(&dev->mode_config.mutex);
1136 drm_connector_unregister_all(dev);
1137 return ret;
1138}
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001139
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001140static int drm_encoder_register_all(struct drm_device *dev)
1141{
1142 struct drm_encoder *encoder;
1143 int ret = 0;
1144
1145 drm_for_each_encoder(encoder, dev) {
1146 if (encoder->funcs->late_register)
1147 ret = encoder->funcs->late_register(encoder);
1148 if (ret)
1149 return ret;
1150 }
1151
1152 return 0;
1153}
1154
1155static void drm_encoder_unregister_all(struct drm_device *dev)
1156{
1157 struct drm_encoder *encoder;
1158
1159 drm_for_each_encoder(encoder, dev) {
1160 if (encoder->funcs->early_unregister)
1161 encoder->funcs->early_unregister(encoder);
1162 }
1163}
1164
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001165/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001166 * drm_encoder_init - Init a preallocated encoder
1167 * @dev: drm device
1168 * @encoder: the encoder to init
1169 * @funcs: callbacks for this encoder
1170 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001171 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001172 *
1173 * Initialises a preallocated encoder. Encoder should be
1174 * subclassed as part of driver encoder objects.
1175 *
1176 * Returns:
1177 * Zero on success, error code on failure.
1178 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001179int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001180 struct drm_encoder *encoder,
1181 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001182 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001183{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001184 int ret;
1185
Daniel Vetter84849902012-12-02 00:28:11 +01001186 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001187
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001188 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1189 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001190 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001191
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001192 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001193 encoder->encoder_type = encoder_type;
1194 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001195 if (name) {
1196 va_list ap;
1197
1198 va_start(ap, name);
1199 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1200 va_end(ap);
1201 } else {
1202 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1203 drm_encoder_enum_list[encoder_type].name,
1204 encoder->base.id);
1205 }
Jani Nikulae5748942014-05-14 16:58:20 +03001206 if (!encoder->name) {
1207 ret = -ENOMEM;
1208 goto out_put;
1209 }
Dave Airlief453ba02008-11-07 14:05:41 -08001210
1211 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001212 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001213
Jani Nikulae5748942014-05-14 16:58:20 +03001214out_put:
1215 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001216 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001217
1218out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001219 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001220
1221 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001222}
1223EXPORT_SYMBOL(drm_encoder_init);
1224
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001225/**
1226 * drm_encoder_cleanup - cleans up an initialised encoder
1227 * @encoder: encoder to cleanup
1228 *
1229 * Cleans up the encoder but doesn't free the object.
1230 */
Dave Airlief453ba02008-11-07 14:05:41 -08001231void drm_encoder_cleanup(struct drm_encoder *encoder)
1232{
1233 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001234
Chris Wilson490d3d12016-05-27 20:05:00 +01001235 /* Note that the encoder_list is considered to be static; should we
1236 * remove the drm_encoder at runtime we would have to decrement all
1237 * the indices on the drm_encoder after us in the encoder_list.
1238 */
1239
Daniel Vetter84849902012-12-02 00:28:11 +01001240 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001241 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001242 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001243 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001244 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001245 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001246
1247 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001248}
1249EXPORT_SYMBOL(drm_encoder_cleanup);
1250
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001251static unsigned int drm_num_planes(struct drm_device *dev)
1252{
1253 unsigned int num = 0;
1254 struct drm_plane *tmp;
1255
1256 drm_for_each_plane(tmp, dev) {
1257 num++;
1258 }
1259
1260 return num;
1261}
1262
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001263/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001264 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001265 * @dev: DRM device
1266 * @plane: plane object to init
1267 * @possible_crtcs: bitmask of possible CRTCs
1268 * @funcs: callbacks for the new plane
1269 * @formats: array of supported formats (%DRM_FORMAT_*)
1270 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001271 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001272 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001273 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001274 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001275 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001276 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001277 * Zero on success, error code on failure.
1278 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001279int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1280 unsigned long possible_crtcs,
1281 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001282 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001283 enum drm_plane_type type,
1284 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001285{
Rob Clark6b4959f2014-12-18 16:01:53 -05001286 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001287 int ret;
1288
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001289 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1290 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001291 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001292
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001293 drm_modeset_lock_init(&plane->mutex);
1294
Rob Clark4d939142012-05-17 02:23:27 -06001295 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001296 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001297 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001298 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1299 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001300 if (!plane->format_types) {
1301 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001302 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001303 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001304 }
1305
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001306 if (name) {
1307 va_list ap;
1308
1309 va_start(ap, name);
1310 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1311 va_end(ap);
1312 } else {
1313 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1314 drm_num_planes(dev));
1315 }
1316 if (!plane->name) {
1317 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001318 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001319 return -ENOMEM;
1320 }
1321
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001322 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001323 plane->format_count = format_count;
1324 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001325 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001326
Rob Clark6b4959f2014-12-18 16:01:53 -05001327 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001328 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001329 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001330 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001331
Rob Clark9922ab52014-04-01 20:16:57 -04001332 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001333 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001334 plane->type);
1335
Rob Clark6b4959f2014-12-18 16:01:53 -05001336 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1337 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1338 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1339 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1340 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1341 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1342 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1343 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1344 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1345 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1346 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1347 }
1348
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001349 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001350}
Matt Roperdc415ff2014-04-01 15:22:36 -07001351EXPORT_SYMBOL(drm_universal_plane_init);
1352
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001353static int drm_plane_register_all(struct drm_device *dev)
1354{
1355 struct drm_plane *plane;
1356 int ret = 0;
1357
1358 drm_for_each_plane(plane, dev) {
1359 if (plane->funcs->late_register)
1360 ret = plane->funcs->late_register(plane);
1361 if (ret)
1362 return ret;
1363 }
1364
1365 return 0;
1366}
1367
1368static void drm_plane_unregister_all(struct drm_device *dev)
1369{
1370 struct drm_plane *plane;
1371
1372 drm_for_each_plane(plane, dev) {
1373 if (plane->funcs->early_unregister)
1374 plane->funcs->early_unregister(plane);
1375 }
1376}
1377
Matt Roperdc415ff2014-04-01 15:22:36 -07001378/**
1379 * drm_plane_init - Initialize a legacy plane
1380 * @dev: DRM device
1381 * @plane: plane object to init
1382 * @possible_crtcs: bitmask of possible CRTCs
1383 * @funcs: callbacks for the new plane
1384 * @formats: array of supported formats (%DRM_FORMAT_*)
1385 * @format_count: number of elements in @formats
1386 * @is_primary: plane type (primary vs overlay)
1387 *
1388 * Legacy API to initialize a DRM plane.
1389 *
1390 * New drivers should call drm_universal_plane_init() instead.
1391 *
1392 * Returns:
1393 * Zero on success, error code on failure.
1394 */
1395int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1396 unsigned long possible_crtcs,
1397 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001398 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001399 bool is_primary)
1400{
1401 enum drm_plane_type type;
1402
1403 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1404 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001405 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001406}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001407EXPORT_SYMBOL(drm_plane_init);
1408
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001409/**
1410 * drm_plane_cleanup - Clean up the core plane usage
1411 * @plane: plane to cleanup
1412 *
1413 * This function cleans up @plane and removes it from the DRM mode setting
1414 * core. Note that the function does *not* free the plane structure itself,
1415 * this is the responsibility of the caller.
1416 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001417void drm_plane_cleanup(struct drm_plane *plane)
1418{
1419 struct drm_device *dev = plane->dev;
1420
Daniel Vetter84849902012-12-02 00:28:11 +01001421 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001422 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001423 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001424
1425 BUG_ON(list_empty(&plane->head));
1426
Chris Wilson490d3d12016-05-27 20:05:00 +01001427 /* Note that the plane_list is considered to be static; should we
1428 * remove the drm_plane at runtime we would have to decrement all
1429 * the indices on the drm_plane after us in the plane_list.
1430 */
1431
Matt Roperdc415ff2014-04-01 15:22:36 -07001432 list_del(&plane->head);
1433 dev->mode_config.num_total_plane--;
1434 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1435 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001436 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001437
1438 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1439 if (plane->state && plane->funcs->atomic_destroy_state)
1440 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001441
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001442 kfree(plane->name);
1443
Thierry Redinga18c0af2014-12-10 11:38:49 +01001444 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001445}
1446EXPORT_SYMBOL(drm_plane_cleanup);
1447
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001448/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001449 * drm_plane_from_index - find the registered plane at an index
1450 * @dev: DRM device
1451 * @idx: index of registered plane to find for
1452 *
1453 * Given a plane index, return the registered plane from DRM device's
1454 * list of planes with matching index.
1455 */
1456struct drm_plane *
1457drm_plane_from_index(struct drm_device *dev, int idx)
1458{
1459 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001460
Chris Wilson490d3d12016-05-27 20:05:00 +01001461 drm_for_each_plane(plane, dev)
1462 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001463 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001464
Chandra Konduruf81338a2015-04-09 17:36:21 -07001465 return NULL;
1466}
1467EXPORT_SYMBOL(drm_plane_from_index);
1468
1469/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001470 * drm_plane_force_disable - Forcibly disable a plane
1471 * @plane: plane to disable
1472 *
1473 * Forces the plane to be disabled.
1474 *
1475 * Used when the plane's current framebuffer is destroyed,
1476 * and when restoring fbdev mode.
1477 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001478void drm_plane_force_disable(struct drm_plane *plane)
1479{
1480 int ret;
1481
Daniel Vetter3d30a592014-07-27 13:42:42 +02001482 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001483 return;
1484
Daniel Vetter3d30a592014-07-27 13:42:42 +02001485 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001486 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001487 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001488 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001489 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001490 return;
1491 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001492 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001493 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001494 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001495 plane->fb = NULL;
1496 plane->crtc = NULL;
1497}
1498EXPORT_SYMBOL(drm_plane_force_disable);
1499
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001500int drm_modeset_register_all(struct drm_device *dev)
1501{
1502 int ret;
1503
1504 ret = drm_plane_register_all(dev);
1505 if (ret)
1506 goto err_plane;
1507
1508 ret = drm_crtc_register_all(dev);
1509 if (ret)
1510 goto err_crtc;
1511
1512 ret = drm_encoder_register_all(dev);
1513 if (ret)
1514 goto err_encoder;
1515
1516 ret = drm_connector_register_all(dev);
1517 if (ret)
1518 goto err_connector;
1519
1520 return 0;
1521
1522err_connector:
1523 drm_encoder_unregister_all(dev);
1524err_encoder:
1525 drm_crtc_unregister_all(dev);
1526err_crtc:
1527 drm_plane_unregister_all(dev);
1528err_plane:
1529 return ret;
1530}
1531
1532void drm_modeset_unregister_all(struct drm_device *dev)
1533{
1534 drm_connector_unregister_all(dev);
1535 drm_encoder_unregister_all(dev);
1536 drm_crtc_unregister_all(dev);
1537 drm_plane_unregister_all(dev);
1538}
1539
Rob Clark6b4959f2014-12-18 16:01:53 -05001540static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001541{
Rob Clark356af0e2014-12-18 16:01:52 -05001542 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001543
1544 /*
1545 * Standard properties (apply to all connectors)
1546 */
Rob Clark356af0e2014-12-18 16:01:52 -05001547 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001548 DRM_MODE_PROP_IMMUTABLE,
1549 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001550 if (!prop)
1551 return -ENOMEM;
1552 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001553
Rob Clark356af0e2014-12-18 16:01:52 -05001554 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001555 "DPMS", drm_dpms_enum_list,
1556 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001557 if (!prop)
1558 return -ENOMEM;
1559 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001560
Rob Clark356af0e2014-12-18 16:01:52 -05001561 prop = drm_property_create(dev,
1562 DRM_MODE_PROP_BLOB |
1563 DRM_MODE_PROP_IMMUTABLE,
1564 "PATH", 0);
1565 if (!prop)
1566 return -ENOMEM;
1567 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001568
Rob Clark356af0e2014-12-18 16:01:52 -05001569 prop = drm_property_create(dev,
1570 DRM_MODE_PROP_BLOB |
1571 DRM_MODE_PROP_IMMUTABLE,
1572 "TILE", 0);
1573 if (!prop)
1574 return -ENOMEM;
1575 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001576
Rob Clark6b4959f2014-12-18 16:01:53 -05001577 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001578 "type", drm_plane_type_enum_list,
1579 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001580 if (!prop)
1581 return -ENOMEM;
1582 dev->mode_config.plane_type_property = prop;
1583
1584 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1585 "SRC_X", 0, UINT_MAX);
1586 if (!prop)
1587 return -ENOMEM;
1588 dev->mode_config.prop_src_x = prop;
1589
1590 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1591 "SRC_Y", 0, UINT_MAX);
1592 if (!prop)
1593 return -ENOMEM;
1594 dev->mode_config.prop_src_y = prop;
1595
1596 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1597 "SRC_W", 0, UINT_MAX);
1598 if (!prop)
1599 return -ENOMEM;
1600 dev->mode_config.prop_src_w = prop;
1601
1602 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1603 "SRC_H", 0, UINT_MAX);
1604 if (!prop)
1605 return -ENOMEM;
1606 dev->mode_config.prop_src_h = prop;
1607
1608 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1609 "CRTC_X", INT_MIN, INT_MAX);
1610 if (!prop)
1611 return -ENOMEM;
1612 dev->mode_config.prop_crtc_x = prop;
1613
1614 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1615 "CRTC_Y", INT_MIN, INT_MAX);
1616 if (!prop)
1617 return -ENOMEM;
1618 dev->mode_config.prop_crtc_y = prop;
1619
1620 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1621 "CRTC_W", 0, INT_MAX);
1622 if (!prop)
1623 return -ENOMEM;
1624 dev->mode_config.prop_crtc_w = prop;
1625
1626 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1627 "CRTC_H", 0, INT_MAX);
1628 if (!prop)
1629 return -ENOMEM;
1630 dev->mode_config.prop_crtc_h = prop;
1631
1632 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1633 "FB_ID", DRM_MODE_OBJECT_FB);
1634 if (!prop)
1635 return -ENOMEM;
1636 dev->mode_config.prop_fb_id = prop;
1637
1638 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1639 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1640 if (!prop)
1641 return -ENOMEM;
1642 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001643
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001644 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1645 "ACTIVE");
1646 if (!prop)
1647 return -ENOMEM;
1648 dev->mode_config.prop_active = prop;
1649
Daniel Stone955f3c32015-05-25 19:11:52 +01001650 prop = drm_property_create(dev,
1651 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1652 "MODE_ID", 0);
1653 if (!prop)
1654 return -ENOMEM;
1655 dev->mode_config.prop_mode_id = prop;
1656
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001657 prop = drm_property_create(dev,
1658 DRM_MODE_PROP_BLOB,
1659 "DEGAMMA_LUT", 0);
1660 if (!prop)
1661 return -ENOMEM;
1662 dev->mode_config.degamma_lut_property = prop;
1663
1664 prop = drm_property_create_range(dev,
1665 DRM_MODE_PROP_IMMUTABLE,
1666 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1667 if (!prop)
1668 return -ENOMEM;
1669 dev->mode_config.degamma_lut_size_property = prop;
1670
1671 prop = drm_property_create(dev,
1672 DRM_MODE_PROP_BLOB,
1673 "CTM", 0);
1674 if (!prop)
1675 return -ENOMEM;
1676 dev->mode_config.ctm_property = prop;
1677
1678 prop = drm_property_create(dev,
1679 DRM_MODE_PROP_BLOB,
1680 "GAMMA_LUT", 0);
1681 if (!prop)
1682 return -ENOMEM;
1683 dev->mode_config.gamma_lut_property = prop;
1684
1685 prop = drm_property_create_range(dev,
1686 DRM_MODE_PROP_IMMUTABLE,
1687 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1688 if (!prop)
1689 return -ENOMEM;
1690 dev->mode_config.gamma_lut_size_property = prop;
1691
Rob Clark9922ab52014-04-01 20:16:57 -04001692 return 0;
1693}
1694
Dave Airlief453ba02008-11-07 14:05:41 -08001695/**
1696 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1697 * @dev: DRM device
1698 *
1699 * Called by a driver the first time a DVI-I connector is made.
1700 */
1701int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1702{
1703 struct drm_property *dvi_i_selector;
1704 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001705
1706 if (dev->mode_config.dvi_i_select_subconnector_property)
1707 return 0;
1708
1709 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001710 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001711 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001712 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001713 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001714 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1715
Sascha Hauer4a67d392012-02-06 10:58:17 +01001716 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001717 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001718 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001719 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001720 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1721
1722 return 0;
1723}
1724EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1725
1726/**
1727 * drm_create_tv_properties - create TV specific connector properties
1728 * @dev: DRM device
1729 * @num_modes: number of different TV formats (modes) supported
1730 * @modes: array of pointers to strings containing name of each format
1731 *
1732 * Called by a driver's TV initialization routine, this function creates
1733 * the TV specific connector properties for a given device. Caller is
1734 * responsible for allocating a list of format names and passing them to
1735 * this routine.
1736 */
Thierry Reding2f763312014-10-13 12:45:57 +02001737int drm_mode_create_tv_properties(struct drm_device *dev,
1738 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001739 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001740{
1741 struct drm_property *tv_selector;
1742 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001743 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001744
1745 if (dev->mode_config.tv_select_subconnector_property)
1746 return 0;
1747
1748 /*
1749 * Basic connector properties
1750 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001751 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001752 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001753 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001754 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001755 if (!tv_selector)
1756 goto nomem;
1757
Dave Airlief453ba02008-11-07 14:05:41 -08001758 dev->mode_config.tv_select_subconnector_property = tv_selector;
1759
1760 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001761 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1762 "subconnector",
1763 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001764 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001765 if (!tv_subconnector)
1766 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001767 dev->mode_config.tv_subconnector_property = tv_subconnector;
1768
1769 /*
1770 * Other, TV specific properties: margins & TV modes.
1771 */
1772 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001773 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001774 if (!dev->mode_config.tv_left_margin_property)
1775 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001776
1777 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001778 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001779 if (!dev->mode_config.tv_right_margin_property)
1780 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001781
1782 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001783 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001784 if (!dev->mode_config.tv_top_margin_property)
1785 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001786
1787 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001788 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001789 if (!dev->mode_config.tv_bottom_margin_property)
1790 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001791
1792 dev->mode_config.tv_mode_property =
1793 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1794 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001795 if (!dev->mode_config.tv_mode_property)
1796 goto nomem;
1797
Dave Airlief453ba02008-11-07 14:05:41 -08001798 for (i = 0; i < num_modes; i++)
1799 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1800 i, modes[i]);
1801
Francisco Jerezb6b79022009-08-02 04:19:20 +02001802 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001803 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001804 if (!dev->mode_config.tv_brightness_property)
1805 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001806
1807 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001808 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001809 if (!dev->mode_config.tv_contrast_property)
1810 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001811
1812 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001813 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001814 if (!dev->mode_config.tv_flicker_reduction_property)
1815 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001816
Francisco Jereza75f0232009-08-12 02:30:10 +02001817 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001818 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001819 if (!dev->mode_config.tv_overscan_property)
1820 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001821
1822 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001823 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001824 if (!dev->mode_config.tv_saturation_property)
1825 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001826
1827 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001828 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001829 if (!dev->mode_config.tv_hue_property)
1830 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001831
Dave Airlief453ba02008-11-07 14:05:41 -08001832 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001833nomem:
1834 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001835}
1836EXPORT_SYMBOL(drm_mode_create_tv_properties);
1837
1838/**
1839 * drm_mode_create_scaling_mode_property - create scaling mode property
1840 * @dev: DRM device
1841 *
1842 * Called by a driver the first time it's needed, must be attached to desired
1843 * connectors.
1844 */
1845int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1846{
1847 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001848
1849 if (dev->mode_config.scaling_mode_property)
1850 return 0;
1851
1852 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001853 drm_property_create_enum(dev, 0, "scaling mode",
1854 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001855 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001856
1857 dev->mode_config.scaling_mode_property = scaling_mode;
1858
1859 return 0;
1860}
1861EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1862
1863/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301864 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1865 * @dev: DRM device
1866 *
1867 * Called by a driver the first time it's needed, must be attached to desired
1868 * connectors.
1869 *
1870 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001871 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301872 */
1873int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1874{
1875 if (dev->mode_config.aspect_ratio_property)
1876 return 0;
1877
1878 dev->mode_config.aspect_ratio_property =
1879 drm_property_create_enum(dev, 0, "aspect ratio",
1880 drm_aspect_ratio_enum_list,
1881 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1882
1883 if (dev->mode_config.aspect_ratio_property == NULL)
1884 return -ENOMEM;
1885
1886 return 0;
1887}
1888EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1889
1890/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001891 * drm_mode_create_dirty_property - create dirty property
1892 * @dev: DRM device
1893 *
1894 * Called by a driver the first time it's needed, must be attached to desired
1895 * connectors.
1896 */
1897int drm_mode_create_dirty_info_property(struct drm_device *dev)
1898{
1899 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001900
1901 if (dev->mode_config.dirty_info_property)
1902 return 0;
1903
1904 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001905 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001906 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001907 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001908 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001909 dev->mode_config.dirty_info_property = dirty_info;
1910
1911 return 0;
1912}
1913EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1914
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001915/**
1916 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1917 * @dev: DRM device
1918 *
1919 * Create the the suggested x/y offset property for connectors.
1920 */
1921int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1922{
1923 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1924 return 0;
1925
1926 dev->mode_config.suggested_x_property =
1927 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1928
1929 dev->mode_config.suggested_y_property =
1930 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1931
1932 if (dev->mode_config.suggested_x_property == NULL ||
1933 dev->mode_config.suggested_y_property == NULL)
1934 return -ENOMEM;
1935 return 0;
1936}
1937EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1938
Dave Airlief453ba02008-11-07 14:05:41 -08001939/**
Dave Airlief453ba02008-11-07 14:05:41 -08001940 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001941 * @dev: drm device for the ioctl
1942 * @data: data pointer for the ioctl
1943 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001944 *
Dave Airlief453ba02008-11-07 14:05:41 -08001945 * Construct a set of configuration description structures and return
1946 * them to the user, including CRTC, connector and framebuffer configuration.
1947 *
1948 * Called by the user via ioctl.
1949 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001950 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001951 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001952 */
1953int drm_mode_getresources(struct drm_device *dev, void *data,
1954 struct drm_file *file_priv)
1955{
1956 struct drm_mode_card_res *card_res = data;
1957 struct list_head *lh;
1958 struct drm_framebuffer *fb;
1959 struct drm_connector *connector;
1960 struct drm_crtc *crtc;
1961 struct drm_encoder *encoder;
1962 int ret = 0;
1963 int connector_count = 0;
1964 int crtc_count = 0;
1965 int fb_count = 0;
1966 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001967 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001968 uint32_t __user *fb_id;
1969 uint32_t __user *crtc_id;
1970 uint32_t __user *connector_id;
1971 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001972
Dave Airliefb3b06c2011-02-08 13:55:21 +10001973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1974 return -EINVAL;
1975
Dave Airlief453ba02008-11-07 14:05:41 -08001976
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001977 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001978 /*
1979 * For the non-control nodes we need to limit the list of resources
1980 * by IDs in the group list for this node
1981 */
1982 list_for_each(lh, &file_priv->fbs)
1983 fb_count++;
1984
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001985 /* handle this in 4 parts */
1986 /* FBs */
1987 if (card_res->count_fbs >= fb_count) {
1988 copied = 0;
1989 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1990 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1991 if (put_user(fb->base.id, fb_id + copied)) {
1992 mutex_unlock(&file_priv->fbs_lock);
1993 return -EFAULT;
1994 }
1995 copied++;
1996 }
1997 }
1998 card_res->count_fbs = fb_count;
1999 mutex_unlock(&file_priv->fbs_lock);
2000
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002001 /* mode_config.mutex protects the connector list against e.g. DP MST
2002 * connector hot-adding. CRTC/Plane lists are invariant. */
2003 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002004 drm_for_each_crtc(crtc, dev)
2005 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002006
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002007 drm_for_each_connector(connector, dev)
2008 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002009
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002010 drm_for_each_encoder(encoder, dev)
2011 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002012
2013 card_res->max_height = dev->mode_config.max_height;
2014 card_res->min_height = dev->mode_config.min_height;
2015 card_res->max_width = dev->mode_config.max_width;
2016 card_res->min_width = dev->mode_config.min_width;
2017
Dave Airlief453ba02008-11-07 14:05:41 -08002018 /* CRTCs */
2019 if (card_res->count_crtcs >= crtc_count) {
2020 copied = 0;
2021 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002022 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002023 if (put_user(crtc->base.id, crtc_id + copied)) {
2024 ret = -EFAULT;
2025 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002026 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002027 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002028 }
2029 }
2030 card_res->count_crtcs = crtc_count;
2031
2032 /* Encoders */
2033 if (card_res->count_encoders >= encoder_count) {
2034 copied = 0;
2035 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002036 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002037 if (put_user(encoder->base.id, encoder_id +
2038 copied)) {
2039 ret = -EFAULT;
2040 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002041 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002042 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002043 }
2044 }
2045 card_res->count_encoders = encoder_count;
2046
2047 /* Connectors */
2048 if (card_res->count_connectors >= connector_count) {
2049 copied = 0;
2050 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002051 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002052 if (put_user(connector->base.id,
2053 connector_id + copied)) {
2054 ret = -EFAULT;
2055 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002056 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02002057 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002058 }
2059 }
2060 card_res->count_connectors = connector_count;
2061
Dave Airlief453ba02008-11-07 14:05:41 -08002062out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002063 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002064 return ret;
2065}
2066
2067/**
2068 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002069 * @dev: drm device for the ioctl
2070 * @data: data pointer for the ioctl
2071 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002072 *
Dave Airlief453ba02008-11-07 14:05:41 -08002073 * Construct a CRTC configuration structure to return to the user.
2074 *
2075 * Called by the user via ioctl.
2076 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002077 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002078 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002079 */
2080int drm_mode_getcrtc(struct drm_device *dev,
2081 void *data, struct drm_file *file_priv)
2082{
2083 struct drm_mode_crtc *crtc_resp = data;
2084 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002085
Dave Airliefb3b06c2011-02-08 13:55:21 +10002086 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2087 return -EINVAL;
2088
Rob Clarka2b34e22013-10-05 16:36:52 -04002089 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002090 if (!crtc)
2091 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002092
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002093 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002094 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002095 if (crtc->primary->fb)
2096 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002097 else
2098 crtc_resp->fb_id = 0;
2099
Daniel Vetter31c946e2015-02-22 12:24:17 +01002100 if (crtc->state) {
2101 crtc_resp->x = crtc->primary->state->src_x >> 16;
2102 crtc_resp->y = crtc->primary->state->src_y >> 16;
2103 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002104 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002105 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002106
Daniel Vetter31c946e2015-02-22 12:24:17 +01002107 } else {
2108 crtc_resp->mode_valid = 0;
2109 }
Dave Airlief453ba02008-11-07 14:05:41 -08002110 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002111 crtc_resp->x = crtc->x;
2112 crtc_resp->y = crtc->y;
2113 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002114 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002115 crtc_resp->mode_valid = 1;
2116
2117 } else {
2118 crtc_resp->mode_valid = 0;
2119 }
Dave Airlief453ba02008-11-07 14:05:41 -08002120 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002121 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002122
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002123 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002124}
2125
Damien Lespiau61d8e322013-09-25 16:45:22 +01002126static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2127 const struct drm_file *file_priv)
2128{
2129 /*
2130 * If user-space hasn't configured the driver to expose the stereo 3D
2131 * modes, don't expose them.
2132 */
2133 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2134 return false;
2135
2136 return true;
2137}
2138
Daniel Vetterabd69c52014-11-25 23:50:05 +01002139static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2140{
2141 /* For atomic drivers only state objects are synchronously updated and
2142 * protected by modeset locks, so check those first. */
2143 if (connector->state)
2144 return connector->state->best_encoder;
2145 return connector->encoder;
2146}
2147
Rob Clark95cbf112014-12-18 16:01:49 -05002148/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002149static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002150 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2151 uint32_t *arg_count_props)
2152{
Rob Clark88a48e22014-12-18 16:01:50 -05002153 int props_count;
2154 int i, ret, copied;
2155
2156 props_count = obj->properties->count;
2157 if (!atomic)
2158 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002159
2160 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002161 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002162 struct drm_property *prop = obj->properties->properties[i];
2163 uint64_t val;
2164
Rob Clark88a48e22014-12-18 16:01:50 -05002165 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2166 continue;
2167
Rob Clark95cbf112014-12-18 16:01:49 -05002168 ret = drm_object_property_get_value(obj, prop, &val);
2169 if (ret)
2170 return ret;
2171
2172 if (put_user(prop->base.id, prop_ptr + copied))
2173 return -EFAULT;
2174
2175 if (put_user(val, prop_values + copied))
2176 return -EFAULT;
2177
2178 copied++;
2179 }
2180 }
2181 *arg_count_props = props_count;
2182
2183 return 0;
2184}
2185
Dave Airlief453ba02008-11-07 14:05:41 -08002186/**
2187 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002188 * @dev: drm device for the ioctl
2189 * @data: data pointer for the ioctl
2190 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002191 *
Dave Airlief453ba02008-11-07 14:05:41 -08002192 * Construct a connector configuration structure to return to the user.
2193 *
2194 * Called by the user via ioctl.
2195 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002196 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002197 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002198 */
2199int drm_mode_getconnector(struct drm_device *dev, void *data,
2200 struct drm_file *file_priv)
2201{
2202 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002203 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002204 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002205 struct drm_display_mode *mode;
2206 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002207 int encoders_count = 0;
2208 int ret = 0;
2209 int copied = 0;
2210 int i;
2211 struct drm_mode_modeinfo u_mode;
2212 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002213 uint32_t __user *encoder_ptr;
2214
Dave Airliefb3b06c2011-02-08 13:55:21 +10002215 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2216 return -EINVAL;
2217
Dave Airlief453ba02008-11-07 14:05:41 -08002218 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2219
Daniel Vetter7b240562012-12-12 00:35:33 +01002220 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002221
Dave Airlieb164d312016-04-27 11:10:09 +10002222 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002223 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002224 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002225 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002226 }
Dave Airlief453ba02008-11-07 14:05:41 -08002227
Thierry Reding01073b02014-12-10 13:03:38 +01002228 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2229 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002230 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002231
2232 if (out_resp->count_modes == 0) {
2233 connector->funcs->fill_modes(connector,
2234 dev->mode_config.max_width,
2235 dev->mode_config.max_height);
2236 }
2237
2238 /* delayed so we get modes regardless of pre-fill_modes state */
2239 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002240 if (drm_mode_expose_to_userspace(mode, file_priv))
2241 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002242
2243 out_resp->connector_id = connector->base.id;
2244 out_resp->connector_type = connector->connector_type;
2245 out_resp->connector_type_id = connector->connector_type_id;
2246 out_resp->mm_width = connector->display_info.width_mm;
2247 out_resp->mm_height = connector->display_info.height_mm;
2248 out_resp->subpixel = connector->display_info.subpixel_order;
2249 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002250
2251 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002252 encoder = drm_connector_get_encoder(connector);
2253 if (encoder)
2254 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002255 else
2256 out_resp->encoder_id = 0;
2257
2258 /*
2259 * This ioctl is called twice, once to determine how much space is
2260 * needed, and the 2nd time to fill it.
2261 */
2262 if ((out_resp->count_modes >= mode_count) && mode_count) {
2263 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002264 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002265 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002266 if (!drm_mode_expose_to_userspace(mode, file_priv))
2267 continue;
2268
Daniel Stone934a8a82015-05-22 13:34:48 +01002269 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002270 if (copy_to_user(mode_ptr + copied,
2271 &u_mode, sizeof(u_mode))) {
2272 ret = -EFAULT;
2273 goto out;
2274 }
2275 copied++;
2276 }
2277 }
2278 out_resp->count_modes = mode_count;
2279
Rob Clark88a48e22014-12-18 16:01:50 -05002280 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002281 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2282 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2283 &out_resp->count_props);
2284 if (ret)
2285 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002286
2287 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2288 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002289 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002290 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2291 if (connector->encoder_ids[i] != 0) {
2292 if (put_user(connector->encoder_ids[i],
2293 encoder_ptr + copied)) {
2294 ret = -EFAULT;
2295 goto out;
2296 }
2297 copied++;
2298 }
2299 }
2300 }
2301 out_resp->count_encoders = encoders_count;
2302
2303out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002304 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002305
Dave Airlieb164d312016-04-27 11:10:09 +10002306 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002307out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002308 mutex_unlock(&dev->mode_config.mutex);
2309
Dave Airlief453ba02008-11-07 14:05:41 -08002310 return ret;
2311}
2312
Daniel Vetterabd69c52014-11-25 23:50:05 +01002313static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2314{
2315 struct drm_connector *connector;
2316 struct drm_device *dev = encoder->dev;
2317 bool uses_atomic = false;
2318
2319 /* For atomic drivers only state objects are synchronously updated and
2320 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002321 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002322 if (!connector->state)
2323 continue;
2324
2325 uses_atomic = true;
2326
2327 if (connector->state->best_encoder != encoder)
2328 continue;
2329
2330 return connector->state->crtc;
2331 }
2332
2333 /* Don't return stale data (e.g. pending async disable). */
2334 if (uses_atomic)
2335 return NULL;
2336
2337 return encoder->crtc;
2338}
2339
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002340/**
2341 * drm_mode_getencoder - get encoder configuration
2342 * @dev: drm device for the ioctl
2343 * @data: data pointer for the ioctl
2344 * @file_priv: drm file for the ioctl call
2345 *
2346 * Construct a encoder configuration structure to return to the user.
2347 *
2348 * Called by the user via ioctl.
2349 *
2350 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002351 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002352 */
Dave Airlief453ba02008-11-07 14:05:41 -08002353int drm_mode_getencoder(struct drm_device *dev, void *data,
2354 struct drm_file *file_priv)
2355{
2356 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002357 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002358 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002359
Dave Airliefb3b06c2011-02-08 13:55:21 +10002360 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2361 return -EINVAL;
2362
Rob Clarka2b34e22013-10-05 16:36:52 -04002363 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002364 if (!encoder)
2365 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002366
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002367 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002368 crtc = drm_encoder_get_crtc(encoder);
2369 if (crtc)
2370 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002371 else
2372 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002373 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2374
Dave Airlief453ba02008-11-07 14:05:41 -08002375 enc_resp->encoder_type = encoder->encoder_type;
2376 enc_resp->encoder_id = encoder->base.id;
2377 enc_resp->possible_crtcs = encoder->possible_crtcs;
2378 enc_resp->possible_clones = encoder->possible_clones;
2379
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002380 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002381}
2382
2383/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002384 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002385 * @dev: DRM device
2386 * @data: ioctl data
2387 * @file_priv: DRM file info
2388 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002389 * Construct a list of plane ids to return to the user.
2390 *
2391 * Called by the user via ioctl.
2392 *
2393 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002394 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002395 */
2396int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002397 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002398{
2399 struct drm_mode_get_plane_res *plane_resp = data;
2400 struct drm_mode_config *config;
2401 struct drm_plane *plane;
2402 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002403 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002404 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002405
2406 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2407 return -EINVAL;
2408
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002409 config = &dev->mode_config;
2410
Matt Roper681e7ec2014-04-01 15:22:42 -07002411 if (file_priv->universal_planes)
2412 num_planes = config->num_total_plane;
2413 else
2414 num_planes = config->num_overlay_plane;
2415
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002416 /*
2417 * This ioctl is called twice, once to determine how much space is
2418 * needed, and the 2nd time to fill it.
2419 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002420 if (num_planes &&
2421 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002422 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002423
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002424 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002425 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002426 /*
2427 * Unless userspace set the 'universal planes'
2428 * capability bit, only advertise overlays.
2429 */
2430 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2431 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002432 continue;
2433
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002434 if (put_user(plane->base.id, plane_ptr + copied))
2435 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002436 copied++;
2437 }
2438 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002439 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002440
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002441 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002442}
2443
2444/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002445 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002446 * @dev: DRM device
2447 * @data: ioctl data
2448 * @file_priv: DRM file info
2449 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002450 * Construct a plane configuration structure to return to the user.
2451 *
2452 * Called by the user via ioctl.
2453 *
2454 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002455 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002456 */
2457int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002458 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002459{
2460 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002461 struct drm_plane *plane;
2462 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002463
2464 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2465 return -EINVAL;
2466
Rob Clarka2b34e22013-10-05 16:36:52 -04002467 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002468 if (!plane)
2469 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002470
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002471 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002472 if (plane->crtc)
2473 plane_resp->crtc_id = plane->crtc->base.id;
2474 else
2475 plane_resp->crtc_id = 0;
2476
2477 if (plane->fb)
2478 plane_resp->fb_id = plane->fb->base.id;
2479 else
2480 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002481 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002482
2483 plane_resp->plane_id = plane->base.id;
2484 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002485 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002486
2487 /*
2488 * This ioctl is called twice, once to determine how much space is
2489 * needed, and the 2nd time to fill it.
2490 */
2491 if (plane->format_count &&
2492 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002493 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002494 if (copy_to_user(format_ptr,
2495 plane->format_types,
2496 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002497 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002498 }
2499 }
2500 plane_resp->count_format_types = plane->format_count;
2501
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002502 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002503}
2504
Laurent Pinchartead86102015-03-05 02:25:43 +02002505/**
2506 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2507 * @plane: plane to check for format support
2508 * @format: the pixel format
2509 *
2510 * Returns:
2511 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2512 * otherwise.
2513 */
2514int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2515{
2516 unsigned int i;
2517
2518 for (i = 0; i < plane->format_count; i++) {
2519 if (format == plane->format_types[i])
2520 return 0;
2521 }
2522
2523 return -EINVAL;
2524}
2525
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002526static int check_src_coords(uint32_t src_x, uint32_t src_y,
2527 uint32_t src_w, uint32_t src_h,
2528 const struct drm_framebuffer *fb)
2529{
2530 unsigned int fb_width, fb_height;
2531
2532 fb_width = fb->width << 16;
2533 fb_height = fb->height << 16;
2534
2535 /* Make sure source coordinates are inside the fb. */
2536 if (src_w > fb_width ||
2537 src_x > fb_width - src_w ||
2538 src_h > fb_height ||
2539 src_y > fb_height - src_h) {
2540 DRM_DEBUG_KMS("Invalid source coordinates "
2541 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2542 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2543 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2544 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2545 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2546 return -ENOSPC;
2547 }
2548
2549 return 0;
2550}
2551
Matt Roperb36552b2014-06-10 08:28:09 -07002552/*
2553 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002554 *
Matt Roperb36552b2014-06-10 08:28:09 -07002555 * Note that we assume an extra reference has already been taken on fb. If the
2556 * update fails, this reference will be dropped before return; if it succeeds,
2557 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002558 *
Matt Roperb36552b2014-06-10 08:28:09 -07002559 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002560 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002561static int __setplane_internal(struct drm_plane *plane,
2562 struct drm_crtc *crtc,
2563 struct drm_framebuffer *fb,
2564 int32_t crtc_x, int32_t crtc_y,
2565 uint32_t crtc_w, uint32_t crtc_h,
2566 /* src_{x,y,w,h} values are 16.16 fixed point */
2567 uint32_t src_x, uint32_t src_y,
2568 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002569{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002570 int ret = 0;
2571
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002572 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002573 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002574 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002575 ret = plane->funcs->disable_plane(plane);
2576 if (!ret) {
2577 plane->crtc = NULL;
2578 plane->fb = NULL;
2579 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002580 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002581 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002582 goto out;
2583 }
2584
Matt Roper7f994f32014-05-29 08:06:51 -07002585 /* Check whether this plane is usable on this CRTC */
2586 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2587 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2588 ret = -EINVAL;
2589 goto out;
2590 }
2591
Ville Syrjälä62443be2011-12-20 00:06:47 +02002592 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002593 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2594 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01002595 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01002596 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
2597 kfree(format_name);
Ville Syrjälä62443be2011-12-20 00:06:47 +02002598 goto out;
2599 }
2600
Matt Roper3968be92015-04-13 11:06:13 -07002601 /* Give drivers some help against integer overflows */
2602 if (crtc_w > INT_MAX ||
2603 crtc_x > INT_MAX - (int32_t) crtc_w ||
2604 crtc_h > INT_MAX ||
2605 crtc_y > INT_MAX - (int32_t) crtc_h) {
2606 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2607 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002608 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002609 goto out;
2610 }
2611
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002612 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2613 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002614 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002615
Daniel Vetter3d30a592014-07-27 13:42:42 +02002616 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002617 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002618 crtc_x, crtc_y, crtc_w, crtc_h,
2619 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002620 if (!ret) {
2621 plane->crtc = crtc;
2622 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002623 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002624 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002625 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002626 }
2627
2628out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002629 if (fb)
2630 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002631 if (plane->old_fb)
2632 drm_framebuffer_unreference(plane->old_fb);
2633 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002634
2635 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002636}
Matt Roperb36552b2014-06-10 08:28:09 -07002637
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002638static int setplane_internal(struct drm_plane *plane,
2639 struct drm_crtc *crtc,
2640 struct drm_framebuffer *fb,
2641 int32_t crtc_x, int32_t crtc_y,
2642 uint32_t crtc_w, uint32_t crtc_h,
2643 /* src_{x,y,w,h} values are 16.16 fixed point */
2644 uint32_t src_x, uint32_t src_y,
2645 uint32_t src_w, uint32_t src_h)
2646{
2647 int ret;
2648
2649 drm_modeset_lock_all(plane->dev);
2650 ret = __setplane_internal(plane, crtc, fb,
2651 crtc_x, crtc_y, crtc_w, crtc_h,
2652 src_x, src_y, src_w, src_h);
2653 drm_modeset_unlock_all(plane->dev);
2654
2655 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002656}
2657
2658/**
2659 * drm_mode_setplane - configure a plane's configuration
2660 * @dev: DRM device
2661 * @data: ioctl data*
2662 * @file_priv: DRM file info
2663 *
2664 * Set plane configuration, including placement, fb, scaling, and other factors.
2665 * Or pass a NULL fb to disable (planes may be disabled without providing a
2666 * valid crtc).
2667 *
2668 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002669 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002670 */
2671int drm_mode_setplane(struct drm_device *dev, void *data,
2672 struct drm_file *file_priv)
2673{
2674 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002675 struct drm_plane *plane;
2676 struct drm_crtc *crtc = NULL;
2677 struct drm_framebuffer *fb = NULL;
2678
2679 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2680 return -EINVAL;
2681
Matt Roperb36552b2014-06-10 08:28:09 -07002682 /*
2683 * First, find the plane, crtc, and fb objects. If not available,
2684 * we don't bother to call the driver.
2685 */
Rob Clark933f6222014-11-25 20:33:11 -05002686 plane = drm_plane_find(dev, plane_req->plane_id);
2687 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002688 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2689 plane_req->plane_id);
2690 return -ENOENT;
2691 }
Matt Roperb36552b2014-06-10 08:28:09 -07002692
2693 if (plane_req->fb_id) {
2694 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2695 if (!fb) {
2696 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2697 plane_req->fb_id);
2698 return -ENOENT;
2699 }
2700
Rob Clark933f6222014-11-25 20:33:11 -05002701 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2702 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002703 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2704 plane_req->crtc_id);
2705 return -ENOENT;
2706 }
Matt Roperb36552b2014-06-10 08:28:09 -07002707 }
2708
Matt Roper161d0dc2014-06-10 08:28:10 -07002709 /*
2710 * setplane_internal will take care of deref'ing either the old or new
2711 * framebuffer depending on success.
2712 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002713 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002714 plane_req->crtc_x, plane_req->crtc_y,
2715 plane_req->crtc_w, plane_req->crtc_h,
2716 plane_req->src_x, plane_req->src_y,
2717 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002718}
2719
2720/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002721 * drm_mode_set_config_internal - helper to call ->set_config
2722 * @set: modeset config to set
2723 *
2724 * This is a little helper to wrap internal calls to the ->set_config driver
2725 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002726 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002727 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002728 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002729 */
2730int drm_mode_set_config_internal(struct drm_mode_set *set)
2731{
2732 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002733 struct drm_framebuffer *fb;
2734 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002735 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002736
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002737 /*
2738 * NOTE: ->set_config can also disable other crtcs (if we steal all
2739 * connectors from it), hence we need to refcount the fbs across all
2740 * crtcs. Atomic modeset will have saner semantics ...
2741 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002742 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002743 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002744
Daniel Vetterb0d12322012-12-11 01:07:12 +01002745 fb = set->fb;
2746
2747 ret = crtc->funcs->set_config(set);
2748 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002749 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002750 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002751 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002752
Daniel Vettere4f62542015-07-09 23:44:35 +02002753 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002754 if (tmp->primary->fb)
2755 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002756 if (tmp->primary->old_fb)
2757 drm_framebuffer_unreference(tmp->primary->old_fb);
2758 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002759 }
2760
2761 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002762}
2763EXPORT_SYMBOL(drm_mode_set_config_internal);
2764
Matt Roperaf936292014-04-01 15:22:34 -07002765/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002766 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2767 * @mode: mode to query
2768 * @hdisplay: hdisplay value to fill in
2769 * @vdisplay: vdisplay value to fill in
2770 *
2771 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2772 * the appropriate layout.
2773 */
2774void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2775 int *hdisplay, int *vdisplay)
2776{
2777 struct drm_display_mode adjusted;
2778
2779 drm_mode_copy(&adjusted, mode);
2780 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2781 *hdisplay = adjusted.crtc_hdisplay;
2782 *vdisplay = adjusted.crtc_vdisplay;
2783}
2784EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2785
2786/**
Matt Roperaf936292014-04-01 15:22:34 -07002787 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2788 * CRTC viewport
2789 * @crtc: CRTC that framebuffer will be displayed on
2790 * @x: x panning
2791 * @y: y panning
2792 * @mode: mode that framebuffer will be displayed under
2793 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002794 */
Matt Roperaf936292014-04-01 15:22:34 -07002795int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2796 int x, int y,
2797 const struct drm_display_mode *mode,
2798 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002799
2800{
2801 int hdisplay, vdisplay;
2802
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002803 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002804
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002805 if (crtc->state &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03002806 crtc->primary->state->rotation & (DRM_ROTATE_90 |
2807 DRM_ROTATE_270))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002808 swap(hdisplay, vdisplay);
2809
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002810 return check_src_coords(x << 16, y << 16,
2811 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002812}
Matt Roperaf936292014-04-01 15:22:34 -07002813EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002814
Daniel Vetter2d13b672012-12-11 13:47:23 +01002815/**
Dave Airlief453ba02008-11-07 14:05:41 -08002816 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002817 * @dev: drm device for the ioctl
2818 * @data: data pointer for the ioctl
2819 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002820 *
Dave Airlief453ba02008-11-07 14:05:41 -08002821 * Build a new CRTC configuration based on user request.
2822 *
2823 * Called by the user via ioctl.
2824 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002825 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002826 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002827 */
2828int drm_mode_setcrtc(struct drm_device *dev, void *data,
2829 struct drm_file *file_priv)
2830{
2831 struct drm_mode_config *config = &dev->mode_config;
2832 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002833 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002834 struct drm_connector **connector_set = NULL, *connector;
2835 struct drm_framebuffer *fb = NULL;
2836 struct drm_display_mode *mode = NULL;
2837 struct drm_mode_set set;
2838 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002839 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002840 int i;
2841
Dave Airliefb3b06c2011-02-08 13:55:21 +10002842 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2843 return -EINVAL;
2844
Zhao Junwang01447e92015-07-07 17:08:35 +08002845 /*
2846 * Universal plane src offsets are only 16.16, prevent havoc for
2847 * drivers using universal plane code internally.
2848 */
2849 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002850 return -ERANGE;
2851
Daniel Vetter84849902012-12-02 00:28:11 +01002852 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002853 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2854 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002855 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002856 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002857 goto out;
2858 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002859 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002860
2861 if (crtc_req->mode_valid) {
2862 /* If we have a mode we need a framebuffer. */
2863 /* If we pass -1, set the mode with the currently bound fb */
2864 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002865 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002866 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2867 ret = -EINVAL;
2868 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002869 }
Matt Roperf4510a22014-04-01 15:22:40 -07002870 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002871 /* Make refcounting symmetric with the lookup path. */
2872 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002873 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002874 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2875 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002876 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2877 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002878 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002879 goto out;
2880 }
Dave Airlief453ba02008-11-07 14:05:41 -08002881 }
2882
2883 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002884 if (!mode) {
2885 ret = -ENOMEM;
2886 goto out;
2887 }
2888
Daniel Stone934a8a82015-05-22 13:34:48 +01002889 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002890 if (ret) {
2891 DRM_DEBUG_KMS("Invalid mode\n");
2892 goto out;
2893 }
2894
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002895 /*
2896 * Check whether the primary plane supports the fb pixel format.
2897 * Drivers not implementing the universal planes API use a
2898 * default formats list provided by the DRM core which doesn't
2899 * match real hardware capabilities. Skip the check in that
2900 * case.
2901 */
2902 if (!crtc->primary->format_default) {
2903 ret = drm_plane_check_pixel_format(crtc->primary,
2904 fb->pixel_format);
2905 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01002906 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01002907 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
2908 kfree(format_name);
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002909 goto out;
2910 }
2911 }
2912
Damien Lespiauc11e9282013-09-25 16:45:30 +01002913 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2914 mode, fb);
2915 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002916 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002917
Dave Airlief453ba02008-11-07 14:05:41 -08002918 }
2919
2920 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002921 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002922 ret = -EINVAL;
2923 goto out;
2924 }
2925
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002926 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002927 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002928 crtc_req->count_connectors);
2929 ret = -EINVAL;
2930 goto out;
2931 }
2932
2933 if (crtc_req->count_connectors > 0) {
2934 u32 out_id;
2935
2936 /* Avoid unbounded kernel memory allocation */
2937 if (crtc_req->count_connectors > config->num_connector) {
2938 ret = -EINVAL;
2939 goto out;
2940 }
2941
Thierry Reding2f6c5382014-12-10 13:03:36 +01002942 connector_set = kmalloc_array(crtc_req->count_connectors,
2943 sizeof(struct drm_connector *),
2944 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002945 if (!connector_set) {
2946 ret = -ENOMEM;
2947 goto out;
2948 }
2949
2950 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002951 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002952 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002953 if (get_user(out_id, &set_connectors_ptr[i])) {
2954 ret = -EFAULT;
2955 goto out;
2956 }
2957
Dave Airlieb164d312016-04-27 11:10:09 +10002958 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002959 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002960 DRM_DEBUG_KMS("Connector id %d unknown\n",
2961 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002962 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002963 goto out;
2964 }
Jerome Glisse94401062010-07-15 15:43:25 -04002965 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2966 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002967 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002968
2969 connector_set[i] = connector;
2970 }
2971 }
2972
2973 set.crtc = crtc;
2974 set.x = crtc_req->x;
2975 set.y = crtc_req->y;
2976 set.mode = mode;
2977 set.connectors = connector_set;
2978 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002979 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002980 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002981
2982out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002983 if (fb)
2984 drm_framebuffer_unreference(fb);
2985
Dave Airlieb164d312016-04-27 11:10:09 +10002986 if (connector_set) {
2987 for (i = 0; i < crtc_req->count_connectors; i++) {
2988 if (connector_set[i])
2989 drm_connector_unreference(connector_set[i]);
2990 }
2991 }
Dave Airlief453ba02008-11-07 14:05:41 -08002992 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002993 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002994 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002995 return ret;
2996}
2997
Matt Roper161d0dc2014-06-10 08:28:10 -07002998/**
2999 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
3000 * universal plane handler call
3001 * @crtc: crtc to update cursor for
3002 * @req: data pointer for the ioctl
3003 * @file_priv: drm file for the ioctl call
3004 *
3005 * Legacy cursor ioctl's work directly with driver buffer handles. To
3006 * translate legacy ioctl calls into universal plane handler calls, we need to
3007 * wrap the native buffer handle in a drm_framebuffer.
3008 *
3009 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
3010 * buffer with a pitch of 4*width; the universal plane interface should be used
3011 * directly in cases where the hardware can support other buffer settings and
3012 * userspace wants to make use of these capabilities.
3013 *
3014 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003015 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07003016 */
3017static int drm_mode_cursor_universal(struct drm_crtc *crtc,
3018 struct drm_mode_cursor2 *req,
3019 struct drm_file *file_priv)
3020{
3021 struct drm_device *dev = crtc->dev;
3022 struct drm_framebuffer *fb = NULL;
3023 struct drm_mode_fb_cmd2 fbreq = {
3024 .width = req->width,
3025 .height = req->height,
3026 .pixel_format = DRM_FORMAT_ARGB8888,
3027 .pitches = { req->width * 4 },
3028 .handles = { req->handle },
3029 };
3030 int32_t crtc_x, crtc_y;
3031 uint32_t crtc_w = 0, crtc_h = 0;
3032 uint32_t src_w = 0, src_h = 0;
3033 int ret = 0;
3034
3035 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003036 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07003037
3038 /*
3039 * Obtain fb we'll be using (either new or existing) and take an extra
3040 * reference to it if fb != null. setplane will take care of dropping
3041 * the reference if the plane update fails.
3042 */
3043 if (req->flags & DRM_MODE_CURSOR_BO) {
3044 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00003045 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07003046 if (IS_ERR(fb)) {
3047 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
3048 return PTR_ERR(fb);
3049 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02003050 fb->hot_x = req->hot_x;
3051 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07003052 } else {
3053 fb = NULL;
3054 }
3055 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07003056 fb = crtc->cursor->fb;
3057 if (fb)
3058 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07003059 }
3060
3061 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3062 crtc_x = req->x;
3063 crtc_y = req->y;
3064 } else {
3065 crtc_x = crtc->cursor_x;
3066 crtc_y = crtc->cursor_y;
3067 }
3068
3069 if (fb) {
3070 crtc_w = fb->width;
3071 crtc_h = fb->height;
3072 src_w = fb->width << 16;
3073 src_h = fb->height << 16;
3074 }
3075
3076 /*
3077 * setplane_internal will take care of deref'ing either the old or new
3078 * framebuffer depending on success.
3079 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003080 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003081 crtc_x, crtc_y, crtc_w, crtc_h,
3082 0, 0, src_w, src_h);
3083
3084 /* Update successful; save new cursor position, if necessary */
3085 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3086 crtc->cursor_x = req->x;
3087 crtc->cursor_y = req->y;
3088 }
3089
3090 return ret;
3091}
3092
Dave Airlie4c813d42013-06-20 11:48:52 +10003093static int drm_mode_cursor_common(struct drm_device *dev,
3094 struct drm_mode_cursor2 *req,
3095 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003096{
Dave Airlief453ba02008-11-07 14:05:41 -08003097 struct drm_crtc *crtc;
3098 int ret = 0;
3099
Dave Airliefb3b06c2011-02-08 13:55:21 +10003100 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3101 return -EINVAL;
3102
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003103 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003104 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003105
Rob Clarka2b34e22013-10-05 16:36:52 -04003106 crtc = drm_crtc_find(dev, req->crtc_id);
3107 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003108 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003109 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003110 }
Dave Airlief453ba02008-11-07 14:05:41 -08003111
Matt Roper161d0dc2014-06-10 08:28:10 -07003112 /*
3113 * If this crtc has a universal cursor plane, call that plane's update
3114 * handler rather than using legacy cursor handlers.
3115 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003116 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003117 if (crtc->cursor) {
3118 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3119 goto out;
3120 }
3121
Dave Airlief453ba02008-11-07 14:05:41 -08003122 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003123 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003124 ret = -ENXIO;
3125 goto out;
3126 }
3127 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003128 if (crtc->funcs->cursor_set2)
3129 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3130 req->width, req->height, req->hot_x, req->hot_y);
3131 else
3132 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3133 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003134 }
3135
3136 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3137 if (crtc->funcs->cursor_move) {
3138 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3139 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003140 ret = -EFAULT;
3141 goto out;
3142 }
3143 }
3144out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003145 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003146
Dave Airlief453ba02008-11-07 14:05:41 -08003147 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003148
3149}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003150
3151
3152/**
3153 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3154 * @dev: drm device for the ioctl
3155 * @data: data pointer for the ioctl
3156 * @file_priv: drm file for the ioctl call
3157 *
3158 * Set the cursor configuration based on user request.
3159 *
3160 * Called by the user via ioctl.
3161 *
3162 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003163 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003164 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003165int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003166 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003167{
3168 struct drm_mode_cursor *req = data;
3169 struct drm_mode_cursor2 new_req;
3170
3171 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3172 new_req.hot_x = new_req.hot_y = 0;
3173
3174 return drm_mode_cursor_common(dev, &new_req, file_priv);
3175}
3176
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003177/**
3178 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3179 * @dev: drm device for the ioctl
3180 * @data: data pointer for the ioctl
3181 * @file_priv: drm file for the ioctl call
3182 *
3183 * Set the cursor configuration based on user request. This implements the 2nd
3184 * version of the cursor ioctl, which allows userspace to additionally specify
3185 * the hotspot of the pointer.
3186 *
3187 * Called by the user via ioctl.
3188 *
3189 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003190 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003191 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003192int drm_mode_cursor2_ioctl(struct drm_device *dev,
3193 void *data, struct drm_file *file_priv)
3194{
3195 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003196
Dave Airlie4c813d42013-06-20 11:48:52 +10003197 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003198}
3199
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003200/**
3201 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3202 * @bpp: bits per pixels
3203 * @depth: bit depth per pixel
3204 *
3205 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3206 * Useful in fbdev emulation code, since that deals in those values.
3207 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003208uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3209{
3210 uint32_t fmt;
3211
3212 switch (bpp) {
3213 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003214 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003215 break;
3216 case 16:
3217 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003218 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003219 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003220 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003221 break;
3222 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003223 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003224 break;
3225 case 32:
3226 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003227 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003228 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003229 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003230 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003231 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003232 break;
3233 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003234 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3235 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003236 break;
3237 }
3238
3239 return fmt;
3240}
3241EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3242
Dave Airlief453ba02008-11-07 14:05:41 -08003243/**
3244 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003245 * @dev: drm device for the ioctl
3246 * @data: data pointer for the ioctl
3247 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003248 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003249 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003250 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003251 *
3252 * Called by the user via ioctl.
3253 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003254 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003255 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003256 */
3257int drm_mode_addfb(struct drm_device *dev,
3258 void *data, struct drm_file *file_priv)
3259{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003260 struct drm_mode_fb_cmd *or = data;
3261 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003262 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003263
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003264 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003265 r.fb_id = or->fb_id;
3266 r.width = or->width;
3267 r.height = or->height;
3268 r.pitches[0] = or->pitch;
3269 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3270 r.handles[0] = or->handle;
3271
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003272 ret = drm_mode_addfb2(dev, &r, file_priv);
3273 if (ret)
3274 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003275
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003276 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003277
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003278 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003279}
3280
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003281static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003282{
3283 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
Eric Engestromd3828142016-08-15 16:29:55 +01003284 char *format_name;
Ville Syrjälä935b5972011-12-20 00:06:48 +02003285
3286 switch (format) {
3287 case DRM_FORMAT_C8:
3288 case DRM_FORMAT_RGB332:
3289 case DRM_FORMAT_BGR233:
3290 case DRM_FORMAT_XRGB4444:
3291 case DRM_FORMAT_XBGR4444:
3292 case DRM_FORMAT_RGBX4444:
3293 case DRM_FORMAT_BGRX4444:
3294 case DRM_FORMAT_ARGB4444:
3295 case DRM_FORMAT_ABGR4444:
3296 case DRM_FORMAT_RGBA4444:
3297 case DRM_FORMAT_BGRA4444:
3298 case DRM_FORMAT_XRGB1555:
3299 case DRM_FORMAT_XBGR1555:
3300 case DRM_FORMAT_RGBX5551:
3301 case DRM_FORMAT_BGRX5551:
3302 case DRM_FORMAT_ARGB1555:
3303 case DRM_FORMAT_ABGR1555:
3304 case DRM_FORMAT_RGBA5551:
3305 case DRM_FORMAT_BGRA5551:
3306 case DRM_FORMAT_RGB565:
3307 case DRM_FORMAT_BGR565:
3308 case DRM_FORMAT_RGB888:
3309 case DRM_FORMAT_BGR888:
3310 case DRM_FORMAT_XRGB8888:
3311 case DRM_FORMAT_XBGR8888:
3312 case DRM_FORMAT_RGBX8888:
3313 case DRM_FORMAT_BGRX8888:
3314 case DRM_FORMAT_ARGB8888:
3315 case DRM_FORMAT_ABGR8888:
3316 case DRM_FORMAT_RGBA8888:
3317 case DRM_FORMAT_BGRA8888:
3318 case DRM_FORMAT_XRGB2101010:
3319 case DRM_FORMAT_XBGR2101010:
3320 case DRM_FORMAT_RGBX1010102:
3321 case DRM_FORMAT_BGRX1010102:
3322 case DRM_FORMAT_ARGB2101010:
3323 case DRM_FORMAT_ABGR2101010:
3324 case DRM_FORMAT_RGBA1010102:
3325 case DRM_FORMAT_BGRA1010102:
3326 case DRM_FORMAT_YUYV:
3327 case DRM_FORMAT_YVYU:
3328 case DRM_FORMAT_UYVY:
3329 case DRM_FORMAT_VYUY:
3330 case DRM_FORMAT_AYUV:
3331 case DRM_FORMAT_NV12:
3332 case DRM_FORMAT_NV21:
3333 case DRM_FORMAT_NV16:
3334 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003335 case DRM_FORMAT_NV24:
3336 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003337 case DRM_FORMAT_YUV410:
3338 case DRM_FORMAT_YVU410:
3339 case DRM_FORMAT_YUV411:
3340 case DRM_FORMAT_YVU411:
3341 case DRM_FORMAT_YUV420:
3342 case DRM_FORMAT_YVU420:
3343 case DRM_FORMAT_YUV422:
3344 case DRM_FORMAT_YVU422:
3345 case DRM_FORMAT_YUV444:
3346 case DRM_FORMAT_YVU444:
3347 return 0;
3348 default:
Eric Engestrom90844f02016-08-15 01:02:38 +01003349 format_name = drm_get_format_name(r->pixel_format);
3350 DRM_DEBUG_KMS("invalid pixel format %s\n", format_name);
3351 kfree(format_name);
Ville Syrjälä935b5972011-12-20 00:06:48 +02003352 return -EINVAL;
3353 }
3354}
3355
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003356static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003357{
3358 int ret, hsub, vsub, num_planes, i;
3359
3360 ret = format_check(r);
3361 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01003362 char *format_name = drm_get_format_name(r->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01003363 DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name);
3364 kfree(format_name);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003365 return ret;
3366 }
3367
3368 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3369 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3370 num_planes = drm_format_num_planes(r->pixel_format);
3371
3372 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003373 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003374 return -EINVAL;
3375 }
3376
3377 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003378 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003379 return -EINVAL;
3380 }
3381
3382 for (i = 0; i < num_planes; i++) {
3383 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003384 unsigned int height = r->height / (i != 0 ? vsub : 1);
3385 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003386
3387 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003388 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003389 return -EINVAL;
3390 }
3391
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003392 if ((uint64_t) width * cpp > UINT_MAX)
3393 return -ERANGE;
3394
3395 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3396 return -ERANGE;
3397
3398 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003399 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003400 return -EINVAL;
3401 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003402
3403 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3404 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3405 r->modifier[i], i);
3406 return -EINVAL;
3407 }
Rob Clark570655b2015-01-30 20:18:11 +05303408
3409 /* modifier specific checks: */
3410 switch (r->modifier[i]) {
3411 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3412 /* NOTE: the pitch restriction may be lifted later if it turns
3413 * out that no hw has this restriction:
3414 */
3415 if (r->pixel_format != DRM_FORMAT_NV12 ||
3416 width % 128 || height % 32 ||
3417 r->pitches[i] % 128) {
3418 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3419 return -EINVAL;
3420 }
3421 break;
3422
3423 default:
3424 break;
3425 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003426 }
3427
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003428 for (i = num_planes; i < 4; i++) {
3429 if (r->modifier[i]) {
3430 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3431 return -EINVAL;
3432 }
3433
3434 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3435 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3436 continue;
3437
3438 if (r->handles[i]) {
3439 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3440 return -EINVAL;
3441 }
3442
3443 if (r->pitches[i]) {
3444 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3445 return -EINVAL;
3446 }
3447
3448 if (r->offsets[i]) {
3449 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3450 return -EINVAL;
3451 }
3452 }
3453
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003454 return 0;
3455}
3456
Chris Wilson9a6f5132015-02-25 13:45:26 +00003457static struct drm_framebuffer *
3458internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003459 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003460 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003461{
3462 struct drm_mode_config *config = &dev->mode_config;
3463 struct drm_framebuffer *fb;
3464 int ret;
3465
Rob Clarke3eb3252015-02-05 14:41:52 +00003466 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003467 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3468 return ERR_PTR(-EINVAL);
3469 }
3470
3471 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3472 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3473 r->width, config->min_width, config->max_width);
3474 return ERR_PTR(-EINVAL);
3475 }
3476 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3477 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3478 r->height, config->min_height, config->max_height);
3479 return ERR_PTR(-EINVAL);
3480 }
3481
Rob Clarke3eb3252015-02-05 14:41:52 +00003482 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3483 !dev->mode_config.allow_fb_modifiers) {
3484 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3485 return ERR_PTR(-EINVAL);
3486 }
3487
Matt Roperc394c2b2014-06-10 08:28:08 -07003488 ret = framebuffer_check(r);
3489 if (ret)
3490 return ERR_PTR(ret);
3491
3492 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3493 if (IS_ERR(fb)) {
3494 DRM_DEBUG_KMS("could not create framebuffer\n");
3495 return fb;
3496 }
3497
Matt Roperc394c2b2014-06-10 08:28:08 -07003498 return fb;
3499}
3500
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003501/**
3502 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003503 * @dev: drm device for the ioctl
3504 * @data: data pointer for the ioctl
3505 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003506 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003507 * Add a new FB to the specified CRTC, given a user request with format. This is
3508 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3509 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003510 *
3511 * Called by the user via ioctl.
3512 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003513 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003514 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003515 */
3516int drm_mode_addfb2(struct drm_device *dev,
3517 void *data, struct drm_file *file_priv)
3518{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003519 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003520 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003521
Dave Airliefb3b06c2011-02-08 13:55:21 +10003522 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3523 return -EINVAL;
3524
Chris Wilson9a6f5132015-02-25 13:45:26 +00003525 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003526 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003527 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003528
Chris Wilson9a6f5132015-02-25 13:45:26 +00003529 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003530 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003531
3532 /* Transfer ownership to the filp for reaping on close */
3533 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003534 list_add(&fb->filp_head, &file_priv->fbs);
3535 mutex_unlock(&file_priv->fbs_lock);
3536
Matt Roperc394c2b2014-06-10 08:28:08 -07003537 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003538}
3539
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003540struct drm_mode_rmfb_work {
3541 struct work_struct work;
3542 struct list_head fbs;
3543};
3544
3545static void drm_mode_rmfb_work_fn(struct work_struct *w)
3546{
3547 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3548
3549 while (!list_empty(&arg->fbs)) {
3550 struct drm_framebuffer *fb =
3551 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3552
3553 list_del_init(&fb->filp_head);
3554 drm_framebuffer_remove(fb);
3555 }
3556}
3557
Dave Airlief453ba02008-11-07 14:05:41 -08003558/**
3559 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003560 * @dev: drm device for the ioctl
3561 * @data: data pointer for the ioctl
3562 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003563 *
Dave Airlief453ba02008-11-07 14:05:41 -08003564 * Remove the FB specified by the user.
3565 *
3566 * Called by the user via ioctl.
3567 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003568 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003569 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003570 */
3571int drm_mode_rmfb(struct drm_device *dev,
3572 void *data, struct drm_file *file_priv)
3573{
Dave Airlief453ba02008-11-07 14:05:41 -08003574 struct drm_framebuffer *fb = NULL;
3575 struct drm_framebuffer *fbl = NULL;
3576 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003577 int found = 0;
3578
Dave Airliefb3b06c2011-02-08 13:55:21 +10003579 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3580 return -EINVAL;
3581
Dave Airlie72fe90b2016-04-15 15:10:40 +10003582 fb = drm_framebuffer_lookup(dev, *id);
3583 if (!fb)
3584 return -ENOENT;
3585
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003586 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003587 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3588 if (fb == fbl)
3589 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003590 if (!found) {
3591 mutex_unlock(&file_priv->fbs_lock);
3592 goto fail_unref;
3593 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003594
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003595 list_del_init(&fb->filp_head);
3596 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003597
Dave Airlie72fe90b2016-04-15 15:10:40 +10003598 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003599 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003600
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003601 /*
3602 * we now own the reference that was stored in the fbs list
3603 *
3604 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3605 * so run this in a separate stack as there's no way to correctly
3606 * handle this after the fb is already removed from the lookup table.
3607 */
3608 if (drm_framebuffer_read_refcount(fb) > 1) {
3609 struct drm_mode_rmfb_work arg;
3610
3611 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3612 INIT_LIST_HEAD(&arg.fbs);
3613 list_add_tail(&fb->filp_head, &arg.fbs);
3614
3615 schedule_work(&arg.work);
3616 flush_work(&arg.work);
3617 destroy_work_on_stack(&arg.work);
3618 } else
3619 drm_framebuffer_unreference(fb);
3620
Daniel Vetter2b677e82012-12-10 21:16:05 +01003621 return 0;
3622
Dave Airlie72fe90b2016-04-15 15:10:40 +10003623fail_unref:
3624 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003625 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003626}
3627
3628/**
3629 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003630 * @dev: drm device for the ioctl
3631 * @data: data pointer for the ioctl
3632 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003633 *
Dave Airlief453ba02008-11-07 14:05:41 -08003634 * Lookup the FB given its ID and return info about it.
3635 *
3636 * Called by the user via ioctl.
3637 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003638 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003639 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003640 */
3641int drm_mode_getfb(struct drm_device *dev,
3642 void *data, struct drm_file *file_priv)
3643{
3644 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003645 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003646 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003647
Dave Airliefb3b06c2011-02-08 13:55:21 +10003648 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3649 return -EINVAL;
3650
Daniel Vetter786b99e2012-12-02 21:53:40 +01003651 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003652 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003653 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003654
3655 r->height = fb->height;
3656 r->width = fb->width;
3657 r->depth = fb->depth;
3658 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003659 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003660 if (fb->funcs->create_handle) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02003661 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003662 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003663 ret = fb->funcs->create_handle(fb, file_priv,
3664 &r->handle);
3665 } else {
3666 /* GET_FB() is an unprivileged ioctl so we must not
3667 * return a buffer-handle to non-master processes! For
3668 * backwards-compatibility reasons, we cannot make
3669 * GET_FB() privileged, so just return an invalid handle
3670 * for non-masters. */
3671 r->handle = 0;
3672 ret = 0;
3673 }
3674 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003675 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003676 }
Dave Airlief453ba02008-11-07 14:05:41 -08003677
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003678 drm_framebuffer_unreference(fb);
3679
Dave Airlief453ba02008-11-07 14:05:41 -08003680 return ret;
3681}
3682
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003683/**
3684 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3685 * @dev: drm device for the ioctl
3686 * @data: data pointer for the ioctl
3687 * @file_priv: drm file for the ioctl call
3688 *
3689 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3690 * rectangle list. Generic userspace which does frontbuffer rendering must call
3691 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3692 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3693 *
3694 * Modesetting drivers which always update the frontbuffer do not need to
3695 * implement the corresponding ->dirty framebuffer callback.
3696 *
3697 * Called by the user via ioctl.
3698 *
3699 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003700 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003701 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003702int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3703 void *data, struct drm_file *file_priv)
3704{
3705 struct drm_clip_rect __user *clips_ptr;
3706 struct drm_clip_rect *clips = NULL;
3707 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003708 struct drm_framebuffer *fb;
3709 unsigned flags;
3710 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003711 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003712
Dave Airliefb3b06c2011-02-08 13:55:21 +10003713 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3714 return -EINVAL;
3715
Daniel Vetter786b99e2012-12-02 21:53:40 +01003716 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003717 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003718 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003719
3720 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003721 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003722
3723 if (!num_clips != !clips_ptr) {
3724 ret = -EINVAL;
3725 goto out_err1;
3726 }
3727
3728 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3729
3730 /* If userspace annotates copy, clips must come in pairs */
3731 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3732 ret = -EINVAL;
3733 goto out_err1;
3734 }
3735
3736 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003737 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3738 ret = -EINVAL;
3739 goto out_err1;
3740 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003741 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003742 if (!clips) {
3743 ret = -ENOMEM;
3744 goto out_err1;
3745 }
3746
3747 ret = copy_from_user(clips, clips_ptr,
3748 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003749 if (ret) {
3750 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003751 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003752 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003753 }
3754
3755 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003756 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3757 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003758 } else {
3759 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003760 }
3761
3762out_err2:
3763 kfree(clips);
3764out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003765 drm_framebuffer_unreference(fb);
3766
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003767 return ret;
3768}
3769
Dave Airlief453ba02008-11-07 14:05:41 -08003770/**
3771 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003772 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003773 *
Dave Airlief453ba02008-11-07 14:05:41 -08003774 * Destroy all the FBs associated with @filp.
3775 *
3776 * Called by the user via ioctl.
3777 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003778 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003779 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003780 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003781void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003782{
Dave Airlief453ba02008-11-07 14:05:41 -08003783 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003784 struct drm_mode_rmfb_work arg;
3785
3786 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003787
Daniel Vetter1b116292014-09-24 21:51:06 +02003788 /*
3789 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003790 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003791 * avoid upsetting lockdep since the universal cursor code adds a
3792 * framebuffer while holding mutex locks.
3793 *
3794 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3795 * locks is impossible here since no one else but this function can get
3796 * at it any more.
3797 */
Dave Airlief453ba02008-11-07 14:05:41 -08003798 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003799 if (drm_framebuffer_read_refcount(fb) > 1) {
3800 list_move_tail(&fb->filp_head, &arg.fbs);
3801 } else {
3802 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003803
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003804 /* This drops the fpriv->fbs reference. */
3805 drm_framebuffer_unreference(fb);
3806 }
3807 }
3808
3809 if (!list_empty(&arg.fbs)) {
3810 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3811
3812 schedule_work(&arg.work);
3813 flush_work(&arg.work);
3814 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003815 }
Dave Airlief453ba02008-11-07 14:05:41 -08003816}
3817
Daniel Vetter81065542016-06-21 10:54:13 +02003818static bool drm_property_type_valid(struct drm_property *property)
3819{
3820 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
3821 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3822 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3823}
3824
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003825/**
3826 * drm_property_create - create a new property type
3827 * @dev: drm device
3828 * @flags: flags specifying the property type
3829 * @name: name of the property
3830 * @num_values: number of pre-defined values
3831 *
3832 * This creates a new generic drm property which can then be attached to a drm
3833 * object with drm_object_attach_property. The returned property object must be
3834 * freed with drm_property_destroy.
3835 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003836 * Note that the DRM core keeps a per-device list of properties and that, if
3837 * drm_mode_config_cleanup() is called, it will destroy all properties created
3838 * by the driver.
3839 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003840 * Returns:
3841 * A pointer to the newly created property on success, NULL on failure.
3842 */
Dave Airlief453ba02008-11-07 14:05:41 -08003843struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3844 const char *name, int num_values)
3845{
3846 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003847 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003848
3849 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3850 if (!property)
3851 return NULL;
3852
Rob Clark98f75de2014-05-30 11:37:03 -04003853 property->dev = dev;
3854
Dave Airlief453ba02008-11-07 14:05:41 -08003855 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003856 property->values = kcalloc(num_values, sizeof(uint64_t),
3857 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003858 if (!property->values)
3859 goto fail;
3860 }
3861
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003862 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3863 if (ret)
3864 goto fail;
3865
Dave Airlief453ba02008-11-07 14:05:41 -08003866 property->flags = flags;
3867 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003868 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003869
Vinson Lee471dd2e2011-11-10 11:55:40 -08003870 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003871 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003872 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3873 }
Dave Airlief453ba02008-11-07 14:05:41 -08003874
3875 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003876
3877 WARN_ON(!drm_property_type_valid(property));
3878
Dave Airlief453ba02008-11-07 14:05:41 -08003879 return property;
3880fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003881 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003882 kfree(property);
3883 return NULL;
3884}
3885EXPORT_SYMBOL(drm_property_create);
3886
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003887/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003888 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003889 * @dev: drm device
3890 * @flags: flags specifying the property type
3891 * @name: name of the property
3892 * @props: enumeration lists with property values
3893 * @num_values: number of pre-defined values
3894 *
3895 * This creates a new generic drm property which can then be attached to a drm
3896 * object with drm_object_attach_property. The returned property object must be
3897 * freed with drm_property_destroy.
3898 *
3899 * Userspace is only allowed to set one of the predefined values for enumeration
3900 * properties.
3901 *
3902 * Returns:
3903 * A pointer to the newly created property on success, NULL on failure.
3904 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003905struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3906 const char *name,
3907 const struct drm_prop_enum_list *props,
3908 int num_values)
3909{
3910 struct drm_property *property;
3911 int i, ret;
3912
3913 flags |= DRM_MODE_PROP_ENUM;
3914
3915 property = drm_property_create(dev, flags, name, num_values);
3916 if (!property)
3917 return NULL;
3918
3919 for (i = 0; i < num_values; i++) {
3920 ret = drm_property_add_enum(property, i,
3921 props[i].type,
3922 props[i].name);
3923 if (ret) {
3924 drm_property_destroy(dev, property);
3925 return NULL;
3926 }
3927 }
3928
3929 return property;
3930}
3931EXPORT_SYMBOL(drm_property_create_enum);
3932
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003933/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003934 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003935 * @dev: drm device
3936 * @flags: flags specifying the property type
3937 * @name: name of the property
3938 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003939 * @num_props: size of the @props array
3940 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003941 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003942 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003943 * object with drm_object_attach_property. The returned property object must be
3944 * freed with drm_property_destroy.
3945 *
3946 * Compared to plain enumeration properties userspace is allowed to set any
3947 * or'ed together combination of the predefined property bitflag values
3948 *
3949 * Returns:
3950 * A pointer to the newly created property on success, NULL on failure.
3951 */
Rob Clark49e27542012-05-17 02:23:26 -06003952struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3953 int flags, const char *name,
3954 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303955 int num_props,
3956 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003957{
3958 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303959 int i, ret, index = 0;
3960 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003961
3962 flags |= DRM_MODE_PROP_BITMASK;
3963
3964 property = drm_property_create(dev, flags, name, num_values);
3965 if (!property)
3966 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303967 for (i = 0; i < num_props; i++) {
3968 if (!(supported_bits & (1ULL << props[i].type)))
3969 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003970
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303971 if (WARN_ON(index >= num_values)) {
3972 drm_property_destroy(dev, property);
3973 return NULL;
3974 }
3975
3976 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003977 props[i].type,
3978 props[i].name);
3979 if (ret) {
3980 drm_property_destroy(dev, property);
3981 return NULL;
3982 }
3983 }
3984
3985 return property;
3986}
3987EXPORT_SYMBOL(drm_property_create_bitmask);
3988
Rob Clarkebc44cf2012-09-12 22:22:31 -05003989static struct drm_property *property_create_range(struct drm_device *dev,
3990 int flags, const char *name,
3991 uint64_t min, uint64_t max)
3992{
3993 struct drm_property *property;
3994
3995 property = drm_property_create(dev, flags, name, 2);
3996 if (!property)
3997 return NULL;
3998
3999 property->values[0] = min;
4000 property->values[1] = max;
4001
4002 return property;
4003}
4004
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004005/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004006 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004007 * @dev: drm device
4008 * @flags: flags specifying the property type
4009 * @name: name of the property
4010 * @min: minimum value of the property
4011 * @max: maximum value of the property
4012 *
4013 * This creates a new generic drm property which can then be attached to a drm
4014 * object with drm_object_attach_property. The returned property object must be
4015 * freed with drm_property_destroy.
4016 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004017 * Userspace is allowed to set any unsigned integer value in the (min, max)
4018 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004019 *
4020 * Returns:
4021 * A pointer to the newly created property on success, NULL on failure.
4022 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01004023struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
4024 const char *name,
4025 uint64_t min, uint64_t max)
4026{
Rob Clarkebc44cf2012-09-12 22:22:31 -05004027 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
4028 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01004029}
4030EXPORT_SYMBOL(drm_property_create_range);
4031
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004032/**
4033 * drm_property_create_signed_range - create a new signed ranged property type
4034 * @dev: drm device
4035 * @flags: flags specifying the property type
4036 * @name: name of the property
4037 * @min: minimum value of the property
4038 * @max: maximum value of the property
4039 *
4040 * This creates a new generic drm property which can then be attached to a drm
4041 * object with drm_object_attach_property. The returned property object must be
4042 * freed with drm_property_destroy.
4043 *
4044 * Userspace is allowed to set any signed integer value in the (min, max)
4045 * range inclusive.
4046 *
4047 * Returns:
4048 * A pointer to the newly created property on success, NULL on failure.
4049 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05004050struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
4051 int flags, const char *name,
4052 int64_t min, int64_t max)
4053{
4054 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
4055 name, I642U64(min), I642U64(max));
4056}
4057EXPORT_SYMBOL(drm_property_create_signed_range);
4058
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004059/**
4060 * drm_property_create_object - create a new object property type
4061 * @dev: drm device
4062 * @flags: flags specifying the property type
4063 * @name: name of the property
4064 * @type: object type from DRM_MODE_OBJECT_* defines
4065 *
4066 * This creates a new generic drm property which can then be attached to a drm
4067 * object with drm_object_attach_property. The returned property object must be
4068 * freed with drm_property_destroy.
4069 *
4070 * Userspace is only allowed to set this to any property value of the given
4071 * @type. Only useful for atomic properties, which is enforced.
4072 *
4073 * Returns:
4074 * A pointer to the newly created property on success, NULL on failure.
4075 */
Rob Clark98f75de2014-05-30 11:37:03 -04004076struct drm_property *drm_property_create_object(struct drm_device *dev,
4077 int flags, const char *name, uint32_t type)
4078{
4079 struct drm_property *property;
4080
4081 flags |= DRM_MODE_PROP_OBJECT;
4082
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004083 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
4084 return NULL;
4085
Rob Clark98f75de2014-05-30 11:37:03 -04004086 property = drm_property_create(dev, flags, name, 1);
4087 if (!property)
4088 return NULL;
4089
4090 property->values[0] = type;
4091
4092 return property;
4093}
4094EXPORT_SYMBOL(drm_property_create_object);
4095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004096/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01004097 * drm_property_create_bool - create a new boolean property type
4098 * @dev: drm device
4099 * @flags: flags specifying the property type
4100 * @name: name of the property
4101 *
4102 * This creates a new generic drm property which can then be attached to a drm
4103 * object with drm_object_attach_property. The returned property object must be
4104 * freed with drm_property_destroy.
4105 *
4106 * This is implemented as a ranged property with only {0, 1} as valid values.
4107 *
4108 * Returns:
4109 * A pointer to the newly created property on success, NULL on failure.
4110 */
4111struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
4112 const char *name)
4113{
4114 return drm_property_create_range(dev, flags, name, 0, 1);
4115}
4116EXPORT_SYMBOL(drm_property_create_bool);
4117
4118/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004119 * drm_property_add_enum - add a possible value to an enumeration property
4120 * @property: enumeration property to change
4121 * @index: index of the new enumeration
4122 * @value: value of the new enumeration
4123 * @name: symbolic name of the new enumeration
4124 *
4125 * This functions adds enumerations to a property.
4126 *
4127 * It's use is deprecated, drivers should use one of the more specific helpers
4128 * to directly create the property with all enumerations already attached.
4129 *
4130 * Returns:
4131 * Zero on success, error code on failure.
4132 */
Dave Airlief453ba02008-11-07 14:05:41 -08004133int drm_property_add_enum(struct drm_property *property, int index,
4134 uint64_t value, const char *name)
4135{
4136 struct drm_property_enum *prop_enum;
4137
Rob Clark5ea22f22014-05-30 11:34:01 -04004138 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4139 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004140 return -EINVAL;
4141
4142 /*
4143 * Bitmask enum properties have the additional constraint of values
4144 * from 0 to 63
4145 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004146 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4147 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004148 return -EINVAL;
4149
Daniel Vetter3758b342014-11-19 18:38:10 +01004150 if (!list_empty(&property->enum_list)) {
4151 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004152 if (prop_enum->value == value) {
4153 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4154 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4155 return 0;
4156 }
4157 }
4158 }
4159
4160 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4161 if (!prop_enum)
4162 return -ENOMEM;
4163
4164 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4165 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4166 prop_enum->value = value;
4167
4168 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004169 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004170 return 0;
4171}
4172EXPORT_SYMBOL(drm_property_add_enum);
4173
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004174/**
4175 * drm_property_destroy - destroy a drm property
4176 * @dev: drm device
4177 * @property: property to destry
4178 *
4179 * This function frees a property including any attached resources like
4180 * enumeration values.
4181 */
Dave Airlief453ba02008-11-07 14:05:41 -08004182void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4183{
4184 struct drm_property_enum *prop_enum, *pt;
4185
Daniel Vetter3758b342014-11-19 18:38:10 +01004186 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004187 list_del(&prop_enum->head);
4188 kfree(prop_enum);
4189 }
4190
4191 if (property->num_values)
4192 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004193 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004194 list_del(&property->head);
4195 kfree(property);
4196}
4197EXPORT_SYMBOL(drm_property_destroy);
4198
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004199/**
4200 * drm_object_attach_property - attach a property to a modeset object
4201 * @obj: drm modeset object
4202 * @property: property to attach
4203 * @init_val: initial value of the property
4204 *
4205 * This attaches the given property to the modeset object with the given initial
4206 * value. Currently this function cannot fail since the properties are stored in
4207 * a statically sized array.
4208 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004209void drm_object_attach_property(struct drm_mode_object *obj,
4210 struct drm_property *property,
4211 uint64_t init_val)
4212{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004213 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004214
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004215 if (count == DRM_OBJECT_MAX_PROPERTY) {
4216 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4217 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4218 "you see this message on the same object type.\n",
4219 obj->type);
4220 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004221 }
4222
Rob Clarkb17cd752014-12-16 18:05:30 -05004223 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004224 obj->properties->values[count] = init_val;
4225 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004226 if (property->flags & DRM_MODE_PROP_ATOMIC)
4227 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004228}
4229EXPORT_SYMBOL(drm_object_attach_property);
4230
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004231/**
4232 * drm_object_property_set_value - set the value of a property
4233 * @obj: drm mode object to set property value for
4234 * @property: property to set
4235 * @val: value the property should be set to
4236 *
4237 * This functions sets a given property on a given object. This function only
4238 * changes the software state of the property, it does not call into the
4239 * driver's ->set_property callback.
4240 *
4241 * Returns:
4242 * Zero on success, error code on failure.
4243 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004244int drm_object_property_set_value(struct drm_mode_object *obj,
4245 struct drm_property *property, uint64_t val)
4246{
4247 int i;
4248
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004249 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004250 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004251 obj->properties->values[i] = val;
4252 return 0;
4253 }
4254 }
4255
4256 return -EINVAL;
4257}
4258EXPORT_SYMBOL(drm_object_property_set_value);
4259
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004260/**
4261 * drm_object_property_get_value - retrieve the value of a property
4262 * @obj: drm mode object to get property value from
4263 * @property: property to retrieve
4264 * @val: storage for the property value
4265 *
4266 * This function retrieves the softare state of the given property for the given
4267 * property. Since there is no driver callback to retrieve the current property
4268 * value this might be out of sync with the hardware, depending upon the driver
4269 * and property.
4270 *
4271 * Returns:
4272 * Zero on success, error code on failure.
4273 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004274int drm_object_property_get_value(struct drm_mode_object *obj,
4275 struct drm_property *property, uint64_t *val)
4276{
4277 int i;
4278
Rob Clark88a48e22014-12-18 16:01:50 -05004279 /* read-only properties bypass atomic mechanism and still store
4280 * their value in obj->properties->values[].. mostly to avoid
4281 * having to deal w/ EDID and similar props in atomic paths:
4282 */
4283 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4284 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4285 return drm_atomic_get_property(obj, property, val);
4286
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004287 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004288 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004289 *val = obj->properties->values[i];
4290 return 0;
4291 }
4292 }
4293
4294 return -EINVAL;
4295}
4296EXPORT_SYMBOL(drm_object_property_get_value);
4297
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004298/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004299 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004300 * @dev: DRM device
4301 * @data: ioctl data
4302 * @file_priv: DRM file info
4303 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004304 * This function retrieves the metadata for a given property, like the different
4305 * possible values for an enum property or the limits for a range property.
4306 *
4307 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004308 *
4309 * Called by the user via ioctl.
4310 *
4311 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004312 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004313 */
Dave Airlief453ba02008-11-07 14:05:41 -08004314int drm_mode_getproperty_ioctl(struct drm_device *dev,
4315 void *data, struct drm_file *file_priv)
4316{
Dave Airlief453ba02008-11-07 14:05:41 -08004317 struct drm_mode_get_property *out_resp = data;
4318 struct drm_property *property;
4319 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004320 int value_count = 0;
4321 int ret = 0, i;
4322 int copied;
4323 struct drm_property_enum *prop_enum;
4324 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004325 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004326
Dave Airliefb3b06c2011-02-08 13:55:21 +10004327 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4328 return -EINVAL;
4329
Daniel Vetter84849902012-12-02 00:28:11 +01004330 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004331 property = drm_property_find(dev, out_resp->prop_id);
4332 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004333 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004334 goto done;
4335 }
Dave Airlief453ba02008-11-07 14:05:41 -08004336
Rob Clark5ea22f22014-05-30 11:34:01 -04004337 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4338 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004339 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004340 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004341 }
4342
4343 value_count = property->num_values;
4344
4345 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4346 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4347 out_resp->flags = property->flags;
4348
4349 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004350 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004351 for (i = 0; i < value_count; i++) {
4352 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4353 ret = -EFAULT;
4354 goto done;
4355 }
4356 }
4357 }
4358 out_resp->count_values = value_count;
4359
Rob Clark5ea22f22014-05-30 11:34:01 -04004360 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4361 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004362 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4363 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004364 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004365 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004366
4367 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4368 ret = -EFAULT;
4369 goto done;
4370 }
4371
4372 if (copy_to_user(&enum_ptr[copied].name,
4373 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4374 ret = -EFAULT;
4375 goto done;
4376 }
4377 copied++;
4378 }
4379 }
4380 out_resp->count_enum_blobs = enum_count;
4381 }
4382
Daniel Vetter3758b342014-11-19 18:38:10 +01004383 /*
4384 * NOTE: The idea seems to have been to use this to read all the blob
4385 * property values. But nothing ever added them to the corresponding
4386 * list, userspace always used the special-purpose get_blob ioctl to
4387 * read the value for a blob property. It also doesn't make a lot of
4388 * sense to return values here when everything else is just metadata for
4389 * the property itself.
4390 */
4391 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4392 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004393done:
Daniel Vetter84849902012-12-02 00:28:11 +01004394 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004395 return ret;
4396}
4397
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004398static void drm_property_free_blob(struct kref *kref)
4399{
4400 struct drm_property_blob *blob =
4401 container_of(kref, struct drm_property_blob, base.refcount);
4402
4403 mutex_lock(&blob->dev->mode_config.blob_lock);
4404 list_del(&blob->head_global);
4405 mutex_unlock(&blob->dev->mode_config.blob_lock);
4406
4407 drm_mode_object_unregister(blob->dev, &blob->base);
4408
4409 kfree(blob);
4410}
4411
Daniel Stone99531d92015-05-22 13:34:49 +01004412/**
4413 * drm_property_create_blob - Create new blob property
4414 *
4415 * Creates a new blob property for a specified DRM device, optionally
4416 * copying data.
4417 *
4418 * @dev: DRM device to create property for
4419 * @length: Length to allocate for blob data
4420 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004421 *
4422 * Returns:
4423 * New blob property with a single reference on success, or an ERR_PTR
4424 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004425 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004426struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004427drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004428 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004429{
4430 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004431 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004432
Dan Carpenter9ac09342015-10-29 16:37:54 +03004433 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004434 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004435
4436 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4437 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004438 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004439
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004440 /* This must be explicitly initialised, so we can safely call list_del
4441 * on it in the removal handler, even if it isn't in a file list. */
4442 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004443 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004444 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004445
Daniel Stone99531d92015-05-22 13:34:49 +01004446 if (data)
4447 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004448
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004449 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4450 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004451 if (ret) {
4452 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004453 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004454 }
4455
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004456 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004457 list_add_tail(&blob->head_global,
4458 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004459 mutex_unlock(&dev->mode_config.blob_lock);
4460
Dave Airlief453ba02008-11-07 14:05:41 -08004461 return blob;
4462}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004463EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004464
Daniel Stone6bcacf52015-04-20 19:22:55 +01004465/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004466 * drm_property_unreference_blob - Unreference a blob property
4467 *
4468 * Drop a reference on a blob property. May free the object.
4469 *
Daniel Stonef102c162015-05-22 13:34:44 +01004470 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004471 */
4472void drm_property_unreference_blob(struct drm_property_blob *blob)
4473{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004474 if (!blob)
4475 return;
4476
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004477 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004478}
4479EXPORT_SYMBOL(drm_property_unreference_blob);
4480
4481/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004482 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4483 * @dev: DRM device
4484 * @file_priv: destroy all blobs owned by this file handle
4485 */
4486void drm_property_destroy_user_blobs(struct drm_device *dev,
4487 struct drm_file *file_priv)
4488{
4489 struct drm_property_blob *blob, *bt;
4490
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004491 /*
4492 * When the file gets released that means no one else can access the
4493 * blob list any more, so no need to grab dev->blob_lock.
4494 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004495 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4496 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004497 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004498 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004499}
4500
4501/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004502 * drm_property_reference_blob - Take a reference on an existing property
4503 *
4504 * Take a new reference on an existing blob property.
4505 *
Daniel Stonef102c162015-05-22 13:34:44 +01004506 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004507 */
4508struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4509{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004510 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004511 return blob;
4512}
4513EXPORT_SYMBOL(drm_property_reference_blob);
4514
Daniel Stone6bcacf52015-04-20 19:22:55 +01004515/**
4516 * drm_property_lookup_blob - look up a blob property and take a reference
4517 * @dev: drm device
4518 * @id: id of the blob property
4519 *
4520 * If successful, this takes an additional reference to the blob property.
4521 * callers need to make sure to eventually unreference the returned property
4522 * again, using @drm_property_unreference_blob.
4523 */
4524struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4525 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004526{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004527 struct drm_mode_object *obj;
4528 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004529
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004530 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4531 if (obj)
4532 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004533 return blob;
4534}
4535EXPORT_SYMBOL(drm_property_lookup_blob);
4536
4537/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004538 * drm_property_replace_global_blob - atomically replace existing blob property
4539 * @dev: drm device
4540 * @replace: location of blob property pointer to be replaced
4541 * @length: length of data for new blob, or 0 for no data
4542 * @data: content for new blob, or NULL for no data
4543 * @obj_holds_id: optional object for property holding blob ID
4544 * @prop_holds_id: optional property holding blob ID
4545 * @return 0 on success or error on failure
4546 *
4547 * This function will atomically replace a global property in the blob list,
4548 * optionally updating a property which holds the ID of that property. It is
4549 * guaranteed to be atomic: no caller will be allowed to see intermediate
4550 * results, and either the entire operation will succeed and clean up the
4551 * previous property, or it will fail and the state will be unchanged.
4552 *
4553 * If length is 0 or data is NULL, no new blob will be created, and the holding
4554 * property, if specified, will be set to 0.
4555 *
4556 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4557 * by holding the relevant modesetting object lock for its parent.
4558 *
4559 * For example, a drm_connector has a 'PATH' property, which contains the ID
4560 * of a blob property with the value of the MST path information. Calling this
4561 * function with replace pointing to the connector's path_blob_ptr, length and
4562 * data set for the new path information, obj_holds_id set to the connector's
4563 * base object, and prop_holds_id set to the path property name, will perform
4564 * a completely atomic update. The access to path_blob_ptr is protected by the
4565 * caller holding a lock on the connector.
4566 */
4567static int drm_property_replace_global_blob(struct drm_device *dev,
4568 struct drm_property_blob **replace,
4569 size_t length,
4570 const void *data,
4571 struct drm_mode_object *obj_holds_id,
4572 struct drm_property *prop_holds_id)
4573{
4574 struct drm_property_blob *new_blob = NULL;
4575 struct drm_property_blob *old_blob = NULL;
4576 int ret;
4577
4578 WARN_ON(replace == NULL);
4579
4580 old_blob = *replace;
4581
4582 if (length && data) {
4583 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004584 if (IS_ERR(new_blob))
4585 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004586 }
4587
4588 /* This does not need to be synchronised with blob_lock, as the
4589 * get_properties ioctl locks all modesetting objects, and
4590 * obj_holds_id must be locked before calling here, so we cannot
4591 * have its value out of sync with the list membership modified
4592 * below under blob_lock. */
4593 if (obj_holds_id) {
4594 ret = drm_object_property_set_value(obj_holds_id,
4595 prop_holds_id,
4596 new_blob ?
4597 new_blob->base.id : 0);
4598 if (ret != 0)
4599 goto err_created;
4600 }
4601
Markus Elfringec530822015-07-05 21:55:10 +02004602 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004603 *replace = new_blob;
4604
4605 return 0;
4606
4607err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004608 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004609 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004610}
4611
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004612/**
4613 * drm_mode_getblob_ioctl - get the contents of a blob property value
4614 * @dev: DRM device
4615 * @data: ioctl data
4616 * @file_priv: DRM file info
4617 *
4618 * This function retrieves the contents of a blob property. The value stored in
4619 * an object's blob property is just a normal modeset object id.
4620 *
4621 * Called by the user via ioctl.
4622 *
4623 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004624 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004625 */
Dave Airlief453ba02008-11-07 14:05:41 -08004626int drm_mode_getblob_ioctl(struct drm_device *dev,
4627 void *data, struct drm_file *file_priv)
4628{
Dave Airlief453ba02008-11-07 14:05:41 -08004629 struct drm_mode_get_blob *out_resp = data;
4630 struct drm_property_blob *blob;
4631 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004632 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004633
Dave Airliefb3b06c2011-02-08 13:55:21 +10004634 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4635 return -EINVAL;
4636
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004637 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4638 if (!blob)
4639 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004640
4641 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004642 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004643 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004644 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004645 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004646 }
4647 }
4648 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004649unref:
4650 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004651
Dave Airlief453ba02008-11-07 14:05:41 -08004652 return ret;
4653}
4654
Dave Airliecc7096f2014-10-22 12:03:04 +10004655/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004656 * drm_mode_createblob_ioctl - create a new blob property
4657 * @dev: DRM device
4658 * @data: ioctl data
4659 * @file_priv: DRM file info
4660 *
4661 * This function creates a new blob property with user-defined values. In order
4662 * to give us sensible validation and checking when creating, rather than at
4663 * every potential use, we also require a type to be provided upfront.
4664 *
4665 * Called by the user via ioctl.
4666 *
4667 * Returns:
4668 * Zero on success, negative errno on failure.
4669 */
4670int drm_mode_createblob_ioctl(struct drm_device *dev,
4671 void *data, struct drm_file *file_priv)
4672{
4673 struct drm_mode_create_blob *out_resp = data;
4674 struct drm_property_blob *blob;
4675 void __user *blob_ptr;
4676 int ret = 0;
4677
4678 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4679 return -EINVAL;
4680
4681 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4682 if (IS_ERR(blob))
4683 return PTR_ERR(blob);
4684
4685 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4686 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4687 ret = -EFAULT;
4688 goto out_blob;
4689 }
4690
4691 /* Dropping the lock between create_blob and our access here is safe
4692 * as only the same file_priv can remove the blob; at this point, it is
4693 * not associated with any file_priv. */
4694 mutex_lock(&dev->mode_config.blob_lock);
4695 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004696 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004697 mutex_unlock(&dev->mode_config.blob_lock);
4698
4699 return 0;
4700
4701out_blob:
4702 drm_property_unreference_blob(blob);
4703 return ret;
4704}
4705
4706/**
4707 * drm_mode_destroyblob_ioctl - destroy a user blob property
4708 * @dev: DRM device
4709 * @data: ioctl data
4710 * @file_priv: DRM file info
4711 *
4712 * Destroy an existing user-defined blob property.
4713 *
4714 * Called by the user via ioctl.
4715 *
4716 * Returns:
4717 * Zero on success, negative errno on failure.
4718 */
4719int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4720 void *data, struct drm_file *file_priv)
4721{
4722 struct drm_mode_destroy_blob *out_resp = data;
4723 struct drm_property_blob *blob = NULL, *bt;
4724 bool found = false;
4725 int ret = 0;
4726
4727 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4728 return -EINVAL;
4729
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004730 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4731 if (!blob)
4732 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004733
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004734 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004735 /* Ensure the property was actually created by this user. */
4736 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4737 if (bt == blob) {
4738 found = true;
4739 break;
4740 }
4741 }
4742
4743 if (!found) {
4744 ret = -EPERM;
4745 goto err;
4746 }
4747
4748 /* We must drop head_file here, because we may not be the last
4749 * reference on the blob. */
4750 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004751 mutex_unlock(&dev->mode_config.blob_lock);
4752
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004753 /* One reference from lookup, and one from the filp. */
4754 drm_property_unreference_blob(blob);
4755 drm_property_unreference_blob(blob);
4756
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004757 return 0;
4758
4759err:
4760 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004761 drm_property_unreference_blob(blob);
4762
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004763 return ret;
4764}
4765
4766/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004767 * drm_mode_connector_set_path_property - set tile property on connector
4768 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004769 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004770 *
4771 * This creates a property to expose to userspace to specify a
4772 * connector path. This is mainly used for DisplayPort MST where
4773 * connectors have a topology and we want to allow userspace to give
4774 * them more meaningful names.
4775 *
4776 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004777 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004778 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004779int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004780 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004781{
4782 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004783 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004784
Daniel Stoned2ed3432015-04-20 19:22:53 +01004785 ret = drm_property_replace_global_blob(dev,
4786 &connector->path_blob_ptr,
4787 strlen(path) + 1,
4788 path,
4789 &connector->base,
4790 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004791 return ret;
4792}
4793EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4794
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004795/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004796 * drm_mode_connector_set_tile_property - set tile property on connector
4797 * @connector: connector to set property on.
4798 *
4799 * This looks up the tile information for a connector, and creates a
4800 * property for userspace to parse if it exists. The property is of
4801 * the form of 8 integers using ':' as a separator.
4802 *
4803 * Returns:
4804 * Zero on success, errno on failure.
4805 */
4806int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4807{
4808 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004809 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004810 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004811
4812 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004813 ret = drm_property_replace_global_blob(dev,
4814 &connector->tile_blob_ptr,
4815 0,
4816 NULL,
4817 &connector->base,
4818 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004819 return ret;
4820 }
4821
4822 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4823 connector->tile_group->id, connector->tile_is_single_monitor,
4824 connector->num_h_tile, connector->num_v_tile,
4825 connector->tile_h_loc, connector->tile_v_loc,
4826 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004827
Daniel Stoned2ed3432015-04-20 19:22:53 +01004828 ret = drm_property_replace_global_blob(dev,
4829 &connector->tile_blob_ptr,
4830 strlen(tile) + 1,
4831 tile,
4832 &connector->base,
4833 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004834 return ret;
4835}
4836EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4837
4838/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004839 * drm_mode_connector_update_edid_property - update the edid property of a connector
4840 * @connector: drm connector
4841 * @edid: new value of the edid property
4842 *
4843 * This function creates a new blob modeset object and assigns its id to the
4844 * connector's edid property.
4845 *
4846 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004847 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004848 */
Dave Airlief453ba02008-11-07 14:05:41 -08004849int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004850 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004851{
4852 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004853 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004854 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004855
Thomas Wood4cf2b282014-06-18 17:52:33 +01004856 /* ignore requests to set edid when overridden */
4857 if (connector->override_edid)
4858 return 0;
4859
Daniel Stoned2ed3432015-04-20 19:22:53 +01004860 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004861 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004862
Daniel Stoned2ed3432015-04-20 19:22:53 +01004863 ret = drm_property_replace_global_blob(dev,
4864 &connector->edid_blob_ptr,
4865 size,
4866 edid,
4867 &connector->base,
4868 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004869 return ret;
4870}
4871EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4872
Rob Clark3843e712014-12-16 18:05:29 -05004873/* Some properties could refer to dynamic refcnt'd objects, or things that
4874 * need special locking to handle lifetime issues (ie. to ensure the prop
4875 * value doesn't become invalid part way through the property update due to
4876 * race). The value returned by reference via 'obj' should be passed back
4877 * to drm_property_change_valid_put() after the property is set (and the
4878 * object to which the property is attached has a chance to take it's own
4879 * reference).
4880 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004881bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004882 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004883{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004884 int i;
4885
Paulo Zanoni26a34812012-05-15 18:08:59 -03004886 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4887 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004888
Rob Clark3843e712014-12-16 18:05:29 -05004889 *ref = NULL;
4890
Rob Clark5ea22f22014-05-30 11:34:01 -04004891 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004892 if (value < property->values[0] || value > property->values[1])
4893 return false;
4894 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004895 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4896 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004897
Rob Clarkebc44cf2012-09-12 22:22:31 -05004898 if (svalue < U642I64(property->values[0]) ||
4899 svalue > U642I64(property->values[1]))
4900 return false;
4901 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004902 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004903 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004904
Rob Clark49e27542012-05-17 02:23:26 -06004905 for (i = 0; i < property->num_values; i++)
4906 valid_mask |= (1ULL << property->values[i]);
4907 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004908 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004909 struct drm_property_blob *blob;
4910
4911 if (value == 0)
4912 return true;
4913
4914 blob = drm_property_lookup_blob(property->dev, value);
4915 if (blob) {
4916 *ref = &blob->base;
4917 return true;
4918 } else {
4919 return false;
4920 }
Rob Clark98f75de2014-05-30 11:37:03 -04004921 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004922 /* a zero value for an object property translates to null: */
4923 if (value == 0)
4924 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004925
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004926 *ref = _object_find(property->dev, value, property->values[0]);
4927 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004928 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004929
4930 for (i = 0; i < property->num_values; i++)
4931 if (property->values[i] == value)
4932 return true;
4933 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004934}
4935
Rob Clarkd34f20d2014-12-18 16:01:56 -05004936void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004937 struct drm_mode_object *ref)
4938{
4939 if (!ref)
4940 return;
4941
4942 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004943 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004944 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4945 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004946}
4947
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004948/**
4949 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4950 * @dev: DRM device
4951 * @data: ioctl data
4952 * @file_priv: DRM file info
4953 *
4954 * This function sets the current value for a connectors's property. It also
4955 * calls into a driver's ->set_property callback to update the hardware state
4956 *
4957 * Called by the user via ioctl.
4958 *
4959 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004960 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004961 */
Dave Airlief453ba02008-11-07 14:05:41 -08004962int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4963 void *data, struct drm_file *file_priv)
4964{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004965 struct drm_mode_connector_set_property *conn_set_prop = data;
4966 struct drm_mode_obj_set_property obj_set_prop = {
4967 .value = conn_set_prop->value,
4968 .prop_id = conn_set_prop->prop_id,
4969 .obj_id = conn_set_prop->connector_id,
4970 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4971 };
Dave Airlief453ba02008-11-07 14:05:41 -08004972
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004973 /* It does all the locking and checking we need */
4974 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004975}
4976
Paulo Zanonic5431882012-05-15 18:09:02 -03004977static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4978 struct drm_property *property,
4979 uint64_t value)
4980{
4981 int ret = -EINVAL;
4982 struct drm_connector *connector = obj_to_connector(obj);
4983
4984 /* Do DPMS ourselves */
4985 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004986 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004987 } else if (connector->funcs->set_property)
4988 ret = connector->funcs->set_property(connector, property, value);
4989
4990 /* store the property value if successful */
4991 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004992 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004993 return ret;
4994}
4995
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004996static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4997 struct drm_property *property,
4998 uint64_t value)
4999{
5000 int ret = -EINVAL;
5001 struct drm_crtc *crtc = obj_to_crtc(obj);
5002
5003 if (crtc->funcs->set_property)
5004 ret = crtc->funcs->set_property(crtc, property, value);
5005 if (!ret)
5006 drm_object_property_set_value(obj, property, value);
5007
5008 return ret;
5009}
5010
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005011/**
5012 * drm_mode_plane_set_obj_prop - set the value of a property
5013 * @plane: drm plane object to set property value for
5014 * @property: property to set
5015 * @value: value the property should be set to
5016 *
5017 * This functions sets a given property on a given plane object. This function
5018 * calls the driver's ->set_property callback and changes the software state of
5019 * the property if the callback succeeds.
5020 *
5021 * Returns:
5022 * Zero on success, error code on failure.
5023 */
5024int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
5025 struct drm_property *property,
5026 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06005027{
5028 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005029 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06005030
5031 if (plane->funcs->set_property)
5032 ret = plane->funcs->set_property(plane, property, value);
5033 if (!ret)
5034 drm_object_property_set_value(obj, property, value);
5035
5036 return ret;
5037}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005038EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06005039
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005040/**
Daniel Vetter1a498632014-11-19 18:38:09 +01005041 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005042 * @dev: DRM device
5043 * @data: ioctl data
5044 * @file_priv: DRM file info
5045 *
5046 * This function retrieves the current value for an object's property. Compared
5047 * to the connector specific ioctl this one is extended to also work on crtc and
5048 * plane objects.
5049 *
5050 * Called by the user via ioctl.
5051 *
5052 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005053 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005054 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005055int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5056 struct drm_file *file_priv)
5057{
5058 struct drm_mode_obj_get_properties *arg = data;
5059 struct drm_mode_object *obj;
5060 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03005061
5062 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5063 return -EINVAL;
5064
Daniel Vetter84849902012-12-02 00:28:11 +01005065 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005066
5067 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5068 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005069 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005070 goto out;
5071 }
5072 if (!obj->properties) {
5073 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02005074 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005075 }
5076
Rob Clark88a48e22014-12-18 16:01:50 -05005077 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005078 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5079 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5080 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005081
Daniel Vetter1649c332016-04-22 22:10:28 +02005082out_unref:
5083 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005084out:
Daniel Vetter84849902012-12-02 00:28:11 +01005085 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005086 return ret;
5087}
5088
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005089/**
5090 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5091 * @dev: DRM device
5092 * @data: ioctl data
5093 * @file_priv: DRM file info
5094 *
5095 * This function sets the current value for an object's property. It also calls
5096 * into a driver's ->set_property callback to update the hardware state.
5097 * Compared to the connector specific ioctl this one is extended to also work on
5098 * crtc and plane objects.
5099 *
5100 * Called by the user via ioctl.
5101 *
5102 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005103 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005104 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005105int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5106 struct drm_file *file_priv)
5107{
5108 struct drm_mode_obj_set_property *arg = data;
5109 struct drm_mode_object *arg_obj;
5110 struct drm_mode_object *prop_obj;
5111 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005112 int i, ret = -EINVAL;
5113 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005114
5115 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5116 return -EINVAL;
5117
Daniel Vetter84849902012-12-02 00:28:11 +01005118 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005119
5120 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005121 if (!arg_obj) {
5122 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005123 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005124 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005125 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02005126 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005127
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005128 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005129 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005130 break;
5131
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005132 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02005133 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005134
5135 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5136 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005137 if (!prop_obj) {
5138 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02005139 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005140 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005141 property = obj_to_property(prop_obj);
5142
Rob Clark3843e712014-12-16 18:05:29 -05005143 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10005144 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005145
5146 switch (arg_obj->type) {
5147 case DRM_MODE_OBJECT_CONNECTOR:
5148 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5149 arg->value);
5150 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005151 case DRM_MODE_OBJECT_CRTC:
5152 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5153 break;
Rob Clark4d939142012-05-17 02:23:27 -06005154 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005155 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5156 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005157 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005158 }
5159
Rob Clark3843e712014-12-16 18:05:29 -05005160 drm_property_change_valid_put(property, ref);
5161
Daniel Vetter1649c332016-04-22 22:10:28 +02005162out_unref:
5163 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005164out:
Daniel Vetter84849902012-12-02 00:28:11 +01005165 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005166 return ret;
5167}
5168
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005169/**
5170 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5171 * @connector: connector to attach
5172 * @encoder: encoder to attach @connector to
5173 *
5174 * This function links up a connector to an encoder. Note that the routing
5175 * restrictions between encoders and crtcs are exposed to userspace through the
5176 * possible_clones and possible_crtcs bitmasks.
5177 *
5178 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005179 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005180 */
Dave Airlief453ba02008-11-07 14:05:41 -08005181int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5182 struct drm_encoder *encoder)
5183{
5184 int i;
5185
Thierry Redingeb47fe82015-11-16 18:19:53 +01005186 /*
5187 * In the past, drivers have attempted to model the static association
5188 * of connector to encoder in simple connector/encoder devices using a
5189 * direct assignment of connector->encoder = encoder. This connection
5190 * is a logical one and the responsibility of the core, so drivers are
5191 * expected not to mess with this.
5192 *
5193 * Note that the error return should've been enough here, but a large
5194 * majority of drivers ignores the return value, so add in a big WARN
5195 * to get people's attention.
5196 */
5197 if (WARN_ON(connector->encoder))
5198 return -EINVAL;
5199
Dave Airlief453ba02008-11-07 14:05:41 -08005200 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5201 if (connector->encoder_ids[i] == 0) {
5202 connector->encoder_ids[i] = encoder->base.id;
5203 return 0;
5204 }
5205 }
5206 return -ENOMEM;
5207}
5208EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5209
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005210/**
5211 * drm_mode_crtc_set_gamma_size - set the gamma table size
5212 * @crtc: CRTC to set the gamma table size for
5213 * @gamma_size: size of the gamma table
5214 *
5215 * Drivers which support gamma tables should set this to the supported gamma
5216 * table size when initializing the CRTC. Currently the drm core only supports a
5217 * fixed gamma table size.
5218 *
5219 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005220 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005221 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005222int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005223 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005224{
Daniel Vettercf48e292016-03-30 11:51:16 +02005225 uint16_t *r_base, *g_base, *b_base;
5226 int i;
5227
Dave Airlief453ba02008-11-07 14:05:41 -08005228 crtc->gamma_size = gamma_size;
5229
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005230 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5231 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005232 if (!crtc->gamma_store) {
5233 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005234 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005235 }
5236
Daniel Vettercf48e292016-03-30 11:51:16 +02005237 r_base = crtc->gamma_store;
5238 g_base = r_base + gamma_size;
5239 b_base = g_base + gamma_size;
5240 for (i = 0; i < gamma_size; i++) {
5241 r_base[i] = i << 8;
5242 g_base[i] = i << 8;
5243 b_base[i] = i << 8;
5244 }
5245
5246
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005247 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005248}
5249EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5250
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005251/**
5252 * drm_mode_gamma_set_ioctl - set the gamma table
5253 * @dev: DRM device
5254 * @data: ioctl data
5255 * @file_priv: DRM file info
5256 *
5257 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5258 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5259 *
5260 * Called by the user via ioctl.
5261 *
5262 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005263 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005264 */
Dave Airlief453ba02008-11-07 14:05:41 -08005265int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5266 void *data, struct drm_file *file_priv)
5267{
5268 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005269 struct drm_crtc *crtc;
5270 void *r_base, *g_base, *b_base;
5271 int size;
5272 int ret = 0;
5273
Dave Airliefb3b06c2011-02-08 13:55:21 +10005274 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5275 return -EINVAL;
5276
Daniel Vetter84849902012-12-02 00:28:11 +01005277 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005278 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5279 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005280 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005281 goto out;
5282 }
Dave Airlief453ba02008-11-07 14:05:41 -08005283
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005284 if (crtc->funcs->gamma_set == NULL) {
5285 ret = -ENOSYS;
5286 goto out;
5287 }
5288
Dave Airlief453ba02008-11-07 14:05:41 -08005289 /* memcpy into gamma store */
5290 if (crtc_lut->gamma_size != crtc->gamma_size) {
5291 ret = -EINVAL;
5292 goto out;
5293 }
5294
5295 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5296 r_base = crtc->gamma_store;
5297 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5298 ret = -EFAULT;
5299 goto out;
5300 }
5301
5302 g_base = r_base + size;
5303 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5304 ret = -EFAULT;
5305 goto out;
5306 }
5307
5308 b_base = g_base + size;
5309 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5310 ret = -EFAULT;
5311 goto out;
5312 }
5313
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02005314 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005315
5316out:
Daniel Vetter84849902012-12-02 00:28:11 +01005317 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005318 return ret;
5319
5320}
5321
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005322/**
5323 * drm_mode_gamma_get_ioctl - get the gamma table
5324 * @dev: DRM device
5325 * @data: ioctl data
5326 * @file_priv: DRM file info
5327 *
5328 * Copy the current gamma table into the storage provided. This also provides
5329 * the gamma table size the driver expects, which can be used to size the
5330 * allocated storage.
5331 *
5332 * Called by the user via ioctl.
5333 *
5334 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005335 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005336 */
Dave Airlief453ba02008-11-07 14:05:41 -08005337int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5338 void *data, struct drm_file *file_priv)
5339{
5340 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005341 struct drm_crtc *crtc;
5342 void *r_base, *g_base, *b_base;
5343 int size;
5344 int ret = 0;
5345
Dave Airliefb3b06c2011-02-08 13:55:21 +10005346 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5347 return -EINVAL;
5348
Daniel Vetter84849902012-12-02 00:28:11 +01005349 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005350 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5351 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005352 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005353 goto out;
5354 }
Dave Airlief453ba02008-11-07 14:05:41 -08005355
5356 /* memcpy into gamma store */
5357 if (crtc_lut->gamma_size != crtc->gamma_size) {
5358 ret = -EINVAL;
5359 goto out;
5360 }
5361
5362 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5363 r_base = crtc->gamma_store;
5364 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5365 ret = -EFAULT;
5366 goto out;
5367 }
5368
5369 g_base = r_base + size;
5370 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5371 ret = -EFAULT;
5372 goto out;
5373 }
5374
5375 b_base = g_base + size;
5376 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5377 ret = -EFAULT;
5378 goto out;
5379 }
5380out:
Daniel Vetter84849902012-12-02 00:28:11 +01005381 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005382 return ret;
5383}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005384
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005385/**
5386 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5387 * @dev: DRM device
5388 * @data: ioctl data
5389 * @file_priv: DRM file info
5390 *
5391 * This schedules an asynchronous update on a given CRTC, called page flip.
5392 * Optionally a drm event is generated to signal the completion of the event.
5393 * Generic drivers cannot assume that a pageflip with changed framebuffer
5394 * properties (including driver specific metadata like tiling layout) will work,
5395 * but some drivers support e.g. pixel format changes through the pageflip
5396 * ioctl.
5397 *
5398 * Called by the user via ioctl.
5399 *
5400 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005401 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005402 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005403int drm_mode_page_flip_ioctl(struct drm_device *dev,
5404 void *data, struct drm_file *file_priv)
5405{
5406 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005407 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005408 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005409 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005410 int ret = -EINVAL;
5411
5412 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5413 page_flip->reserved != 0)
5414 return -EINVAL;
5415
Keith Packard62f21042013-07-22 18:50:00 -07005416 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5417 return -EINVAL;
5418
Rob Clarka2b34e22013-10-05 16:36:52 -04005419 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5420 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005421 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005422
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005423 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005424 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005425 /* The framebuffer is currently unbound, presumably
5426 * due to a hotplug event, that userspace has not
5427 * yet discovered.
5428 */
5429 ret = -EBUSY;
5430 goto out;
5431 }
5432
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005433 if (crtc->funcs->page_flip == NULL)
5434 goto out;
5435
Daniel Vetter786b99e2012-12-02 21:53:40 +01005436 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005437 if (!fb) {
5438 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005439 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005440 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005441
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005442 if (crtc->state) {
5443 const struct drm_plane_state *state = crtc->primary->state;
5444
5445 ret = check_src_coords(state->src_x, state->src_y,
5446 state->src_w, state->src_h, fb);
5447 } else {
5448 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5449 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005450 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005451 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005452
Matt Roperf4510a22014-04-01 15:22:40 -07005453 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005454 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5455 ret = -EINVAL;
5456 goto out;
5457 }
5458
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005459 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005460 e = kzalloc(sizeof *e, GFP_KERNEL);
5461 if (!e) {
5462 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005463 goto out;
5464 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005465 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005466 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005467 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005468 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5469 if (ret) {
5470 kfree(e);
5471 goto out;
5472 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005473 }
5474
Daniel Vetter3d30a592014-07-27 13:42:42 +02005475 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005476 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005477 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005478 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5479 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005480 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005481 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005482 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005483 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005484 /* Unref only the old framebuffer. */
5485 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005486 }
5487
5488out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005489 if (fb)
5490 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005491 if (crtc->primary->old_fb)
5492 drm_framebuffer_unreference(crtc->primary->old_fb);
5493 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005494 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005495
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005496 return ret;
5497}
Chris Wilsoneb033552011-01-24 15:11:08 +00005498
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005499/**
5500 * drm_mode_config_reset - call ->reset callbacks
5501 * @dev: drm device
5502 *
5503 * This functions calls all the crtc's, encoder's and connector's ->reset
5504 * callback. Drivers can use this in e.g. their driver load or resume code to
5505 * reset hardware and software state.
5506 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005507void drm_mode_config_reset(struct drm_device *dev)
5508{
5509 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005510 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005511 struct drm_encoder *encoder;
5512 struct drm_connector *connector;
5513
Daniel Vettere4f62542015-07-09 23:44:35 +02005514 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005515 if (plane->funcs->reset)
5516 plane->funcs->reset(plane);
5517
Daniel Vettere4f62542015-07-09 23:44:35 +02005518 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005519 if (crtc->funcs->reset)
5520 crtc->funcs->reset(crtc);
5521
Daniel Vettere4f62542015-07-09 23:44:35 +02005522 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005523 if (encoder->funcs->reset)
5524 encoder->funcs->reset(encoder);
5525
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005526 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005527 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005528 if (connector->funcs->reset)
5529 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005530 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005531}
5532EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005533
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005534/**
5535 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5536 * @dev: DRM device
5537 * @data: ioctl data
5538 * @file_priv: DRM file info
5539 *
5540 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5541 * TTM or something else entirely) and returns the resulting buffer handle. This
5542 * handle can then be wrapped up into a framebuffer modeset object.
5543 *
5544 * Note that userspace is not allowed to use such objects for render
5545 * acceleration - drivers must create their own private ioctls for such a use
5546 * case.
5547 *
5548 * Called by the user via ioctl.
5549 *
5550 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005551 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005552 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005553int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5554 void *data, struct drm_file *file_priv)
5555{
5556 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005557 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005558
5559 if (!dev->driver->dumb_create)
5560 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005561 if (!args->width || !args->height || !args->bpp)
5562 return -EINVAL;
5563
5564 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005565 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005566 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005567 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005568 return -EINVAL;
5569 stride = cpp * args->width;
5570 if (args->height > 0xffffffffU / stride)
5571 return -EINVAL;
5572
5573 /* test for wrap-around */
5574 size = args->height * stride;
5575 if (PAGE_ALIGN(size) == 0)
5576 return -EINVAL;
5577
Thierry Redingf6085952014-11-03 11:14:14 +01005578 /*
5579 * handle, pitch and size are output parameters. Zero them out to
5580 * prevent drivers from accidentally using uninitialized data. Since
5581 * not all existing userspace is clearing these fields properly we
5582 * cannot reject IOCTL with garbage in them.
5583 */
5584 args->handle = 0;
5585 args->pitch = 0;
5586 args->size = 0;
5587
Dave Airlieff72145b2011-02-07 12:16:14 +10005588 return dev->driver->dumb_create(file_priv, dev, args);
5589}
5590
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005591/**
5592 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5593 * @dev: DRM device
5594 * @data: ioctl data
5595 * @file_priv: DRM file info
5596 *
5597 * Allocate an offset in the drm device node's address space to be able to
5598 * memory map a dumb buffer.
5599 *
5600 * Called by the user via ioctl.
5601 *
5602 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005603 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005604 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005605int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5606 void *data, struct drm_file *file_priv)
5607{
5608 struct drm_mode_map_dumb *args = data;
5609
5610 /* call driver ioctl to get mmap offset */
5611 if (!dev->driver->dumb_map_offset)
5612 return -ENOSYS;
5613
5614 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5615}
5616
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005617/**
5618 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5619 * @dev: DRM device
5620 * @data: ioctl data
5621 * @file_priv: DRM file info
5622 *
5623 * This destroys the userspace handle for the given dumb backing storage buffer.
5624 * Since buffer objects must be reference counted in the kernel a buffer object
5625 * won't be immediately freed if a framebuffer modeset object still uses it.
5626 *
5627 * Called by the user via ioctl.
5628 *
5629 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005630 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005631 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005632int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5633 void *data, struct drm_file *file_priv)
5634{
5635 struct drm_mode_destroy_dumb *args = data;
5636
5637 if (!dev->driver->dumb_destroy)
5638 return -ENOSYS;
5639
5640 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5641}
Dave Airlie248dbc22011-11-29 20:02:54 +00005642
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005643/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305644 * drm_rotation_simplify() - Try to simplify the rotation
5645 * @rotation: Rotation to be simplified
5646 * @supported_rotations: Supported rotations
5647 *
5648 * Attempt to simplify the rotation to a form that is supported.
5649 * Eg. if the hardware supports everything except DRM_REFLECT_X
5650 * one could call this function like this:
5651 *
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03005652 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
5653 * DRM_ROTATE_90 | DRM_ROTATE_180 |
5654 * DRM_ROTATE_270 | DRM_REFLECT_Y);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305655 *
5656 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5657 * transforms the hardware supports, this function may not
5658 * be able to produce a supported transform, so the caller should
5659 * check the result afterwards.
5660 */
5661unsigned int drm_rotation_simplify(unsigned int rotation,
5662 unsigned int supported_rotations)
5663{
5664 if (rotation & ~supported_rotations) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03005665 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005666 rotation = (rotation & DRM_REFLECT_MASK) |
5667 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305668 }
5669
5670 return rotation;
5671}
5672EXPORT_SYMBOL(drm_rotation_simplify);
5673
5674/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005675 * drm_mode_config_init - initialize DRM mode_configuration structure
5676 * @dev: DRM device
5677 *
5678 * Initialize @dev's mode_config structure, used for tracking the graphics
5679 * configuration of @dev.
5680 *
5681 * Since this initializes the modeset locks, no locking is possible. Which is no
5682 * problem, since this should happen single threaded at init time. It is the
5683 * driver's problem to ensure this guarantee.
5684 *
5685 */
5686void drm_mode_config_init(struct drm_device *dev)
5687{
5688 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005689 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005690 mutex_init(&dev->mode_config.idr_mutex);
5691 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005692 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005693 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5694 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5695 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5696 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5697 INIT_LIST_HEAD(&dev->mode_config.property_list);
5698 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5699 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5700 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005701 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005702 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005703
5704 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005705 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005706 drm_modeset_unlock_all(dev);
5707
5708 /* Just to be sure */
5709 dev->mode_config.num_fb = 0;
5710 dev->mode_config.num_connector = 0;
5711 dev->mode_config.num_crtc = 0;
5712 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005713 dev->mode_config.num_overlay_plane = 0;
5714 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005715}
5716EXPORT_SYMBOL(drm_mode_config_init);
5717
5718/**
5719 * drm_mode_config_cleanup - free up DRM mode_config info
5720 * @dev: DRM device
5721 *
5722 * Free up all the connectors and CRTCs associated with this DRM device, then
5723 * free up the framebuffers and associated buffer objects.
5724 *
5725 * Note that since this /should/ happen single-threaded at driver/device
5726 * teardown time, no locking is required. It's the driver's job to ensure that
5727 * this guarantee actually holds true.
5728 *
5729 * FIXME: cleanup any dangling user buffer objects too
5730 */
5731void drm_mode_config_cleanup(struct drm_device *dev)
5732{
5733 struct drm_connector *connector, *ot;
5734 struct drm_crtc *crtc, *ct;
5735 struct drm_encoder *encoder, *enct;
5736 struct drm_framebuffer *fb, *fbt;
5737 struct drm_property *property, *pt;
5738 struct drm_property_blob *blob, *bt;
5739 struct drm_plane *plane, *plt;
5740
5741 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5742 head) {
5743 encoder->funcs->destroy(encoder);
5744 }
5745
5746 list_for_each_entry_safe(connector, ot,
5747 &dev->mode_config.connector_list, head) {
5748 connector->funcs->destroy(connector);
5749 }
5750
5751 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5752 head) {
5753 drm_property_destroy(dev, property);
5754 }
5755
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005756 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5757 head) {
5758 plane->funcs->destroy(plane);
5759 }
5760
5761 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5762 crtc->funcs->destroy(crtc);
5763 }
5764
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005765 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005766 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005767 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005768 }
5769
5770 /*
5771 * Single-threaded teardown context, so it's not required to grab the
5772 * fb_lock to protect against concurrent fb_list access. Contrary, it
5773 * would actually deadlock with the drm_framebuffer_cleanup function.
5774 *
5775 * Also, if there are any framebuffers left, that's a driver leak now,
5776 * so politely WARN about this.
5777 */
5778 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5779 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf62016-04-15 15:10:36 +10005780 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005781 }
5782
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005783 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005784 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005785 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005786 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005787}
5788EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305789
5790struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5791 unsigned int supported_rotations)
5792{
5793 static const struct drm_prop_enum_list props[] = {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03005794 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
5795 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
5796 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
5797 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
5798 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
5799 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305800 };
5801
5802 return drm_property_create_bitmask(dev, 0, "rotation",
5803 props, ARRAY_SIZE(props),
5804 supported_rotations);
5805}
5806EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005807
5808/**
5809 * DOC: Tile group
5810 *
5811 * Tile groups are used to represent tiled monitors with a unique
5812 * integer identifier. Tiled monitors using DisplayID v1.3 have
5813 * a unique 8-byte handle, we store this in a tile group, so we
5814 * have a common identifier for all tiles in a monitor group.
5815 */
5816static void drm_tile_group_free(struct kref *kref)
5817{
5818 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5819 struct drm_device *dev = tg->dev;
5820 mutex_lock(&dev->mode_config.idr_mutex);
5821 idr_remove(&dev->mode_config.tile_idr, tg->id);
5822 mutex_unlock(&dev->mode_config.idr_mutex);
5823 kfree(tg);
5824}
5825
5826/**
5827 * drm_mode_put_tile_group - drop a reference to a tile group.
5828 * @dev: DRM device
5829 * @tg: tile group to drop reference to.
5830 *
5831 * drop reference to tile group and free if 0.
5832 */
5833void drm_mode_put_tile_group(struct drm_device *dev,
5834 struct drm_tile_group *tg)
5835{
5836 kref_put(&tg->refcount, drm_tile_group_free);
5837}
5838
5839/**
5840 * drm_mode_get_tile_group - get a reference to an existing tile group
5841 * @dev: DRM device
5842 * @topology: 8-bytes unique per monitor.
5843 *
5844 * Use the unique bytes to get a reference to an existing tile group.
5845 *
5846 * RETURNS:
5847 * tile group or NULL if not found.
5848 */
5849struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5850 char topology[8])
5851{
5852 struct drm_tile_group *tg;
5853 int id;
5854 mutex_lock(&dev->mode_config.idr_mutex);
5855 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5856 if (!memcmp(tg->group_data, topology, 8)) {
5857 if (!kref_get_unless_zero(&tg->refcount))
5858 tg = NULL;
5859 mutex_unlock(&dev->mode_config.idr_mutex);
5860 return tg;
5861 }
5862 }
5863 mutex_unlock(&dev->mode_config.idr_mutex);
5864 return NULL;
5865}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005866EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005867
5868/**
5869 * drm_mode_create_tile_group - create a tile group from a displayid description
5870 * @dev: DRM device
5871 * @topology: 8-bytes unique per monitor.
5872 *
5873 * Create a tile group for the unique monitor, and get a unique
5874 * identifier for the tile group.
5875 *
5876 * RETURNS:
5877 * new tile group or error.
5878 */
5879struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5880 char topology[8])
5881{
5882 struct drm_tile_group *tg;
5883 int ret;
5884
5885 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5886 if (!tg)
5887 return ERR_PTR(-ENOMEM);
5888
5889 kref_init(&tg->refcount);
5890 memcpy(tg->group_data, topology, 8);
5891 tg->dev = dev;
5892
5893 mutex_lock(&dev->mode_config.idr_mutex);
5894 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5895 if (ret >= 0) {
5896 tg->id = ret;
5897 } else {
5898 kfree(tg);
5899 tg = ERR_PTR(ret);
5900 }
5901
5902 mutex_unlock(&dev->mode_config.idr_mutex);
5903 return tg;
5904}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005905EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005906
5907/**
5908 * drm_crtc_enable_color_mgmt - enable color management properties
5909 * @crtc: DRM CRTC
5910 * @degamma_lut_size: the size of the degamma lut (before CSC)
5911 * @has_ctm: whether to attach ctm_property for CSC matrix
5912 * @gamma_lut_size: the size of the gamma lut (after CSC)
5913 *
5914 * This function lets the driver enable the color correction
5915 * properties on a CRTC. This includes 3 degamma, csc and gamma
5916 * properties that userspace can set and 2 size properties to inform
5917 * the userspace of the lut sizes. Each of the properties are
5918 * optional. The gamma and degamma properties are only attached if
5919 * their size is not 0 and ctm_property is only attached if has_ctm is
5920 * true.
5921 */
5922void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5923 uint degamma_lut_size,
5924 bool has_ctm,
5925 uint gamma_lut_size)
5926{
5927 struct drm_device *dev = crtc->dev;
5928 struct drm_mode_config *config = &dev->mode_config;
5929
5930 if (degamma_lut_size) {
5931 drm_object_attach_property(&crtc->base,
5932 config->degamma_lut_property, 0);
5933 drm_object_attach_property(&crtc->base,
5934 config->degamma_lut_size_property,
5935 degamma_lut_size);
5936 }
5937
5938 if (has_ctm)
5939 drm_object_attach_property(&crtc->base,
5940 config->ctm_property, 0);
5941
5942 if (gamma_lut_size) {
5943 drm_object_attach_property(&crtc->base,
5944 config->gamma_lut_property, 0);
5945 drm_object_attach_property(&crtc->base,
5946 config->gamma_lut_size_property,
5947 gamma_lut_size);
5948 }
5949}
5950EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);