blob: 99e6b229bb382aaf894ca4db2b7eed41318d9980 [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>
Dave Airlief453ba02008-11-07 14:05:41 -080040
Daniel Vetter8bd441b2014-01-23 15:35:24 +010041#include "drm_crtc_internal.h"
42
Daniel Vetter84849902012-12-02 00:28:11 +010043/**
44 * drm_modeset_lock_all - take all modeset locks
45 * @dev: drm device
46 *
47 * This function takes all modeset locks, suitable where a more fine-grained
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010048 * scheme isn't (yet) implemented. Locks must be dropped with
49 * drm_modeset_unlock_all.
Daniel Vetter84849902012-12-02 00:28:11 +010050 */
51void drm_modeset_lock_all(struct drm_device *dev)
52{
Daniel Vetter29494c12012-12-02 02:18:25 +010053 struct drm_crtc *crtc;
54
Daniel Vetter84849902012-12-02 00:28:11 +010055 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter29494c12012-12-02 02:18:25 +010056
57 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
58 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Daniel Vetter84849902012-12-02 00:28:11 +010059}
60EXPORT_SYMBOL(drm_modeset_lock_all);
61
62/**
63 * drm_modeset_unlock_all - drop all modeset locks
64 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010065 *
66 * This function drop all modeset locks taken by drm_modeset_lock_all.
Daniel Vetter84849902012-12-02 00:28:11 +010067 */
68void drm_modeset_unlock_all(struct drm_device *dev)
69{
Daniel Vetter29494c12012-12-02 02:18:25 +010070 struct drm_crtc *crtc;
71
72 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
73 mutex_unlock(&crtc->mutex);
74
Daniel Vetter84849902012-12-02 00:28:11 +010075 mutex_unlock(&dev->mode_config.mutex);
76}
77EXPORT_SYMBOL(drm_modeset_unlock_all);
78
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010079/**
80 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
81 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010082 *
83 * Useful as a debug assert.
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010084 */
85void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
86{
87 struct drm_crtc *crtc;
88
Daniel Vettera9b054e2013-05-02 09:43:05 +020089 /* Locking is currently fubar in the panic handler. */
90 if (oops_in_progress)
91 return;
92
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010093 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
94 WARN_ON(!mutex_is_locked(&crtc->mutex));
95
96 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
97}
98EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
99
Dave Airlief453ba02008-11-07 14:05:41 -0800100/* Avoid boilerplate. I'm tired of typing. */
101#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000102 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -0800103 { \
104 int i; \
105 for (i = 0; i < ARRAY_SIZE(list); i++) { \
106 if (list[i].type == val) \
107 return list[i].name; \
108 } \
109 return "(unknown)"; \
110 }
111
112/*
113 * Global properties
114 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000115static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800116{ { DRM_MODE_DPMS_ON, "On" },
117 { DRM_MODE_DPMS_STANDBY, "Standby" },
118 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
119 { DRM_MODE_DPMS_OFF, "Off" }
120};
121
122DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
123
124/*
125 * Optional properties
126 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000127static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800128{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700129 { DRM_MODE_SCALE_NONE, "None" },
130 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
131 { DRM_MODE_SCALE_CENTER, "Center" },
132 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800133};
134
Dave Airlief453ba02008-11-07 14:05:41 -0800135/*
136 * Non-global properties, but "required" for certain connectors.
137 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000138static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800139{
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
142 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
143};
144
145DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
146
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000147static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800148{
149 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
150 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
151 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
152};
153
154DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
155 drm_dvi_i_subconnector_enum_list)
156
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000157static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800158{
159 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
160 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
162 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200163 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800164};
165
166DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
167
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000168static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800169{
170 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
171 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
173 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200174 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800175};
176
177DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
178 drm_tv_subconnector_enum_list)
179
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000180static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000181 { DRM_MODE_DIRTY_OFF, "Off" },
182 { DRM_MODE_DIRTY_ON, "On" },
183 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
184};
185
Dave Airlief453ba02008-11-07 14:05:41 -0800186struct drm_conn_prop_enum_list {
187 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000188 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400189 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800190};
191
192/*
193 * Connector and encoder types.
194 */
195static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400196{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
197 { DRM_MODE_CONNECTOR_VGA, "VGA" },
198 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
199 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
200 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
201 { DRM_MODE_CONNECTOR_Composite, "Composite" },
202 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
203 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
204 { DRM_MODE_CONNECTOR_Component, "Component" },
205 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
206 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
207 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
208 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
209 { DRM_MODE_CONNECTOR_TV, "TV" },
210 { DRM_MODE_CONNECTOR_eDP, "eDP" },
211 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300212 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800213};
214
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000215static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800216{ { DRM_MODE_ENCODER_NONE, "None" },
217 { DRM_MODE_ENCODER_DAC, "DAC" },
218 { DRM_MODE_ENCODER_TMDS, "TMDS" },
219 { DRM_MODE_ENCODER_LVDS, "LVDS" },
220 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200221 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300222 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800223};
224
Jesse Barnesac1bb362014-02-10 15:32:44 -0800225static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
226{
227 { SubPixelUnknown, "Unknown" },
228 { SubPixelHorizontalRGB, "Horizontal RGB" },
229 { SubPixelHorizontalBGR, "Horizontal BGR" },
230 { SubPixelVerticalRGB, "Vertical RGB" },
231 { SubPixelVerticalBGR, "Vertical BGR" },
232 { SubPixelNone, "None" },
233};
234
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400235void drm_connector_ida_init(void)
236{
237 int i;
238
239 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
240 ida_init(&drm_connector_enum_list[i].ida);
241}
242
243void drm_connector_ida_destroy(void)
244{
245 int i;
246
247 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
248 ida_destroy(&drm_connector_enum_list[i].ida);
249}
250
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100251/**
252 * drm_get_encoder_name - return a string for encoder
253 * @encoder: encoder to compute name of
254 *
255 * Note that the buffer used by this function is globally shared and owned by
256 * the function itself.
257 *
258 * FIXME: This isn't really multithreading safe.
259 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000260const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800261{
262 static char buf[32];
263
264 snprintf(buf, 32, "%s-%d",
265 drm_encoder_enum_list[encoder->encoder_type].name,
266 encoder->base.id);
267 return buf;
268}
Dave Airlie13a81952009-09-07 15:45:33 +1000269EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800270
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100271/**
272 * drm_get_connector_name - return a string for connector
273 * @connector: connector to compute name of
274 *
275 * Note that the buffer used by this function is globally shared and owned by
276 * the function itself.
277 *
278 * FIXME: This isn't really multithreading safe.
279 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000280const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800281{
282 static char buf[32];
283
284 snprintf(buf, 32, "%s-%d",
285 drm_connector_enum_list[connector->connector_type].name,
286 connector->connector_type_id);
287 return buf;
288}
289EXPORT_SYMBOL(drm_get_connector_name);
290
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100291/**
292 * drm_get_connector_status_name - return a string for connector status
293 * @status: connector status to compute name of
294 *
295 * In contrast to the other drm_get_*_name functions this one here returns a
296 * const pointer and hence is threadsafe.
297 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000298const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800299{
300 if (status == connector_status_connected)
301 return "connected";
302 else if (status == connector_status_disconnected)
303 return "disconnected";
304 else
305 return "unknown";
306}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000307EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800308
Jesse Barnesac1bb362014-02-10 15:32:44 -0800309/**
310 * drm_get_subpixel_order_name - return a string for a given subpixel enum
311 * @order: enum of subpixel_order
312 *
313 * Note you could abuse this and return something out of bounds, but that
314 * would be a caller error. No unscrubbed user data should make it here.
315 */
316const char *drm_get_subpixel_order_name(enum subpixel_order order)
317{
318 return drm_subpixel_enum_list[order].name;
319}
320EXPORT_SYMBOL(drm_get_subpixel_order_name);
321
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300322static char printable_char(int c)
323{
324 return isascii(c) && isprint(c) ? c : '?';
325}
326
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100327/**
328 * drm_get_format_name - return a string for drm fourcc format
329 * @format: format to compute name of
330 *
331 * Note that the buffer used by this function is globally shared and owned by
332 * the function itself.
333 *
334 * FIXME: This isn't really multithreading safe.
335 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000336const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300337{
338 static char buf[32];
339
340 snprintf(buf, sizeof(buf),
341 "%c%c%c%c %s-endian (0x%08x)",
342 printable_char(format & 0xff),
343 printable_char((format >> 8) & 0xff),
344 printable_char((format >> 16) & 0xff),
345 printable_char((format >> 24) & 0x7f),
346 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
347 format);
348
349 return buf;
350}
351EXPORT_SYMBOL(drm_get_format_name);
352
Dave Airlief453ba02008-11-07 14:05:41 -0800353/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100354 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800355 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100356 * @obj: object pointer, used to generate unique ID
357 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800358 *
Dave Airlief453ba02008-11-07 14:05:41 -0800359 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100360 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
361 * modeset identifiers are _not_ reference counted. Hence don't use this for
362 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800363 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100364 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800365 * New unique (relative to other objects in @dev) integer identifier for the
366 * object.
367 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100368int drm_mode_object_get(struct drm_device *dev,
369 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800370{
Dave Airlief453ba02008-11-07 14:05:41 -0800371 int ret;
372
Jesse Barnesad2563c2009-01-19 17:21:45 +1000373 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800374 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
375 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100376 /*
377 * Set up the object linking under the protection of the idr
378 * lock so that other users can't see inconsistent state.
379 */
Tejun Heo2e928812013-02-27 17:04:08 -0800380 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100381 obj->type = obj_type;
382 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000383 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100384
Tejun Heo2e928812013-02-27 17:04:08 -0800385 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800386}
387
388/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100389 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800390 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100391 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800392 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100393 * Free @id from @dev's unique identifier pool. Note that despite the _get
394 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
395 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800396 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100397void drm_mode_object_put(struct drm_device *dev,
398 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800399{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000400 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800401 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000402 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800403}
404
Daniel Vetter786b99e2012-12-02 21:53:40 +0100405/**
406 * drm_mode_object_find - look up a drm object with static lifetime
407 * @dev: drm device
408 * @id: id of the mode object
409 * @type: type of the mode object
410 *
411 * Note that framebuffers cannot be looked up with this functions - since those
412 * are reference counted, they need special treatment.
413 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200414struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
415 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800416{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000417 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800418
Daniel Vetter786b99e2012-12-02 21:53:40 +0100419 /* Framebuffers are reference counted and need their own lookup
420 * function.*/
421 WARN_ON(type == DRM_MODE_OBJECT_FB);
422
Jesse Barnesad2563c2009-01-19 17:21:45 +1000423 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800424 obj = idr_find(&dev->mode_config.crtc_idr, id);
425 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000426 obj = NULL;
427 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800428
429 return obj;
430}
431EXPORT_SYMBOL(drm_mode_object_find);
432
433/**
Dave Airlief453ba02008-11-07 14:05:41 -0800434 * drm_framebuffer_init - initialize a framebuffer
435 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100436 * @fb: framebuffer to be initialized
437 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800438 *
Dave Airlief453ba02008-11-07 14:05:41 -0800439 * Allocates an ID for the framebuffer's parent mode object, sets its mode
440 * functions & device file and adds it to the master fd list.
441 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100442 * IMPORTANT:
443 * This functions publishes the fb and makes it available for concurrent access
444 * by other users. Which means by this point the fb _must_ be fully set up -
445 * since all the fb attributes are invariant over its lifetime, no further
446 * locking but only correct reference counting is required.
447 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100448 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200449 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800450 */
451int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
452 const struct drm_framebuffer_funcs *funcs)
453{
454 int ret;
455
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100456 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000457 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100458 INIT_LIST_HEAD(&fb->filp_head);
459 fb->dev = dev;
460 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000461
Dave Airlief453ba02008-11-07 14:05:41 -0800462 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200463 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100464 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800465
Daniel Vetter2b677e82012-12-10 21:16:05 +0100466 /* Grab the idr reference. */
467 drm_framebuffer_reference(fb);
468
Dave Airlief453ba02008-11-07 14:05:41 -0800469 dev->mode_config.num_fb++;
470 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100471out:
472 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800473
474 return 0;
475}
476EXPORT_SYMBOL(drm_framebuffer_init);
477
Rob Clarkf7eff602012-09-05 21:48:38 +0000478static void drm_framebuffer_free(struct kref *kref)
479{
480 struct drm_framebuffer *fb =
481 container_of(kref, struct drm_framebuffer, refcount);
482 fb->funcs->destroy(fb);
483}
484
Daniel Vetter2b677e82012-12-10 21:16:05 +0100485static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
486 uint32_t id)
487{
488 struct drm_mode_object *obj = NULL;
489 struct drm_framebuffer *fb;
490
491 mutex_lock(&dev->mode_config.idr_mutex);
492 obj = idr_find(&dev->mode_config.crtc_idr, id);
493 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
494 fb = NULL;
495 else
496 fb = obj_to_fb(obj);
497 mutex_unlock(&dev->mode_config.idr_mutex);
498
499 return fb;
500}
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{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100514 struct drm_framebuffer *fb;
515
516 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100517 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100518 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000519 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100520 mutex_unlock(&dev->mode_config.fb_lock);
521
522 return fb;
523}
524EXPORT_SYMBOL(drm_framebuffer_lookup);
525
526/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000527 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100528 * @fb: framebuffer to unref
529 *
530 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000531 */
532void drm_framebuffer_unreference(struct drm_framebuffer *fb)
533{
Rob Clarkf7eff602012-09-05 21:48:38 +0000534 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000535 kref_put(&fb->refcount, drm_framebuffer_free);
536}
537EXPORT_SYMBOL(drm_framebuffer_unreference);
538
539/**
540 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100541 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100542 *
543 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000544 */
545void drm_framebuffer_reference(struct drm_framebuffer *fb)
546{
547 DRM_DEBUG("FB ID: %d\n", fb->base.id);
548 kref_get(&fb->refcount);
549}
550EXPORT_SYMBOL(drm_framebuffer_reference);
551
Daniel Vetter2b677e82012-12-10 21:16:05 +0100552static void drm_framebuffer_free_bug(struct kref *kref)
553{
554 BUG();
555}
556
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100557static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
558{
559 DRM_DEBUG("FB ID: %d\n", fb->base.id);
560 kref_put(&fb->refcount, drm_framebuffer_free_bug);
561}
562
Daniel Vetter2b677e82012-12-10 21:16:05 +0100563/* dev->mode_config.fb_lock must be held! */
564static void __drm_framebuffer_unregister(struct drm_device *dev,
565 struct drm_framebuffer *fb)
566{
567 mutex_lock(&dev->mode_config.idr_mutex);
568 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
569 mutex_unlock(&dev->mode_config.idr_mutex);
570
571 fb->base.id = 0;
572
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100573 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100574}
575
Dave Airlief453ba02008-11-07 14:05:41 -0800576/**
Daniel Vetter36206362012-12-10 20:42:17 +0100577 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
578 * @fb: fb to unregister
579 *
580 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
581 * those used for fbdev. Note that the caller must hold a reference of it's own,
582 * i.e. the object may not be destroyed through this call (since it'll lead to a
583 * locking inversion).
584 */
585void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
586{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100587 struct drm_device *dev = fb->dev;
588
589 mutex_lock(&dev->mode_config.fb_lock);
590 /* Mark fb as reaped and drop idr ref. */
591 __drm_framebuffer_unregister(dev, fb);
592 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100593}
594EXPORT_SYMBOL(drm_framebuffer_unregister_private);
595
596/**
Dave Airlief453ba02008-11-07 14:05:41 -0800597 * drm_framebuffer_cleanup - remove a framebuffer object
598 * @fb: framebuffer to remove
599 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100600 * Cleanup framebuffer. This function is intended to be used from the drivers
601 * ->destroy callback. It can also be used to clean up driver private
602 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100603 *
604 * Note that this function does not remove the fb from active usuage - if it is
605 * still used anywhere, hilarity can ensue since userspace could call getfb on
606 * the id and get back -EINVAL. Obviously no concern at driver unload time.
607 *
608 * Also, the framebuffer will not be removed from the lookup idr - for
609 * user-created framebuffers this will happen in in the rmfb ioctl. For
610 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
611 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800612 */
613void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
614{
615 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100616
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100617 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000618 list_del(&fb->head);
619 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100620 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000621}
622EXPORT_SYMBOL(drm_framebuffer_cleanup);
623
624/**
625 * drm_framebuffer_remove - remove and unreference a framebuffer object
626 * @fb: framebuffer to remove
627 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000628 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100629 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100630 * passed-in framebuffer. Might take the modeset locks.
631 *
632 * Note that this function optimizes the cleanup away if the caller holds the
633 * last reference to the framebuffer. It is also guaranteed to not take the
634 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000635 */
636void drm_framebuffer_remove(struct drm_framebuffer *fb)
637{
638 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800639 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800640 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000641 struct drm_mode_set set;
642 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800643
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100644 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100645
Daniel Vetterb62584e2012-12-11 16:51:35 +0100646 /*
647 * drm ABI mandates that we remove any deleted framebuffers from active
648 * useage. But since most sane clients only remove framebuffers they no
649 * longer need, try to optimize this away.
650 *
651 * Since we're holding a reference ourselves, observing a refcount of 1
652 * means that we're the last holder and can skip it. Also, the refcount
653 * can never increase from 1 again, so we don't need any barriers or
654 * locks.
655 *
656 * Note that userspace could try to race with use and instate a new
657 * usage _after_ we've cleared all current ones. End result will be an
658 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
659 * in this manner.
660 */
661 if (atomic_read(&fb->refcount.refcount) > 1) {
662 drm_modeset_lock_all(dev);
663 /* remove from any CRTC */
664 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
665 if (crtc->fb == fb) {
666 /* should turn off the crtc */
667 memset(&set, 0, sizeof(struct drm_mode_set));
668 set.crtc = crtc;
669 set.fb = NULL;
670 ret = drm_mode_set_config_internal(&set);
671 if (ret)
672 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
673 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000674 }
Dave Airlief453ba02008-11-07 14:05:41 -0800675
Daniel Vetterb62584e2012-12-11 16:51:35 +0100676 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300677 if (plane->fb == fb)
678 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800679 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100680 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800681 }
682
Rob Clarkf7eff602012-09-05 21:48:38 +0000683 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800684}
Rob Clarkf7eff602012-09-05 21:48:38 +0000685EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800686
687/**
688 * drm_crtc_init - Initialise a new CRTC object
689 * @dev: DRM device
690 * @crtc: CRTC object to init
691 * @funcs: callbacks for the new CRTC
692 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000693 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200694 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100695 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200696 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800697 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200698int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800699 const struct drm_crtc_funcs *funcs)
700{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200701 int ret;
702
Dave Airlief453ba02008-11-07 14:05:41 -0800703 crtc->dev = dev;
704 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000705 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800706
Daniel Vetter84849902012-12-02 00:28:11 +0100707 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100708 mutex_init(&crtc->mutex);
709 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200710
711 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
712 if (ret)
713 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800714
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300715 crtc->base.properties = &crtc->properties;
716
Dave Airlief453ba02008-11-07 14:05:41 -0800717 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
718 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200719
720 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100721 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200722
723 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800724}
725EXPORT_SYMBOL(drm_crtc_init);
726
727/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000728 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800729 * @crtc: CRTC to cleanup
730 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000731 * This function cleans up @crtc and removes it from the DRM mode setting
732 * core. Note that the function does *not* free the crtc structure itself,
733 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800734 */
735void drm_crtc_cleanup(struct drm_crtc *crtc)
736{
737 struct drm_device *dev = crtc->dev;
738
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000739 kfree(crtc->gamma_store);
740 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800741
742 drm_mode_object_put(dev, &crtc->base);
743 list_del(&crtc->head);
744 dev->mode_config.num_crtc--;
745}
746EXPORT_SYMBOL(drm_crtc_cleanup);
747
748/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000749 * drm_crtc_index - find the index of a registered CRTC
750 * @crtc: CRTC to find index for
751 *
752 * Given a registered CRTC, return the index of that CRTC within a DRM
753 * device's list of CRTCs.
754 */
755unsigned int drm_crtc_index(struct drm_crtc *crtc)
756{
757 unsigned int index = 0;
758 struct drm_crtc *tmp;
759
760 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
761 if (tmp == crtc)
762 return index;
763
764 index++;
765 }
766
767 BUG();
768}
769EXPORT_SYMBOL(drm_crtc_index);
770
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100771/*
Dave Airlief453ba02008-11-07 14:05:41 -0800772 * drm_mode_remove - remove and free a mode
773 * @connector: connector list to modify
774 * @mode: mode to remove
775 *
Dave Airlief453ba02008-11-07 14:05:41 -0800776 * Remove @mode from @connector's mode list, then free it.
777 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100778static void drm_mode_remove(struct drm_connector *connector,
779 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800780{
781 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100782 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800783}
Dave Airlief453ba02008-11-07 14:05:41 -0800784
785/**
786 * drm_connector_init - Init a preallocated connector
787 * @dev: DRM device
788 * @connector: the connector to init
789 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100790 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800791 *
Dave Airlief453ba02008-11-07 14:05:41 -0800792 * Initialises a preallocated connector. Connectors should be
793 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100795 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200796 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800797 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200798int drm_connector_init(struct drm_device *dev,
799 struct drm_connector *connector,
800 const struct drm_connector_funcs *funcs,
801 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800802{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200803 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400804 struct ida *connector_ida =
805 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200806
Daniel Vetter84849902012-12-02 00:28:11 +0100807 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800808
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200809 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
810 if (ret)
811 goto out;
812
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300813 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800814 connector->dev = dev;
815 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800816 connector->connector_type = connector_type;
817 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400818 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
819 if (connector->connector_type_id < 0) {
820 ret = connector->connector_type_id;
821 drm_mode_object_put(dev, &connector->base);
822 goto out;
823 }
Dave Airlief453ba02008-11-07 14:05:41 -0800824 INIT_LIST_HEAD(&connector->probed_modes);
825 INIT_LIST_HEAD(&connector->modes);
826 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000827 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800828
829 list_add_tail(&connector->head, &dev->mode_config.connector_list);
830 dev->mode_config.num_connector++;
831
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200832 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500833 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200834 dev->mode_config.edid_property,
835 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800836
Rob Clark58495562012-10-11 20:50:56 -0500837 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800838 dev->mode_config.dpms_property, 0);
839
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200840 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100841 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842
843 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800844}
845EXPORT_SYMBOL(drm_connector_init);
846
847/**
848 * drm_connector_cleanup - cleans up an initialised connector
849 * @connector: connector to cleanup
850 *
Dave Airlief453ba02008-11-07 14:05:41 -0800851 * Cleans up the connector but doesn't free the object.
852 */
853void drm_connector_cleanup(struct drm_connector *connector)
854{
855 struct drm_device *dev = connector->dev;
856 struct drm_display_mode *mode, *t;
857
858 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
859 drm_mode_remove(connector, mode);
860
861 list_for_each_entry_safe(mode, t, &connector->modes, head)
862 drm_mode_remove(connector, mode);
863
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400864 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
865 connector->connector_type_id);
866
Dave Airlief453ba02008-11-07 14:05:41 -0800867 drm_mode_object_put(dev, &connector->base);
868 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000869 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800870}
871EXPORT_SYMBOL(drm_connector_cleanup);
872
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100873/**
874 * drm_connector_unplug_all - unregister connector userspace interfaces
875 * @dev: drm device
876 *
877 * This function unregisters all connector userspace interfaces in sysfs. Should
878 * be call when the device is disconnected, e.g. from an usb driver's
879 * ->disconnect callback.
880 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000881void drm_connector_unplug_all(struct drm_device *dev)
882{
883 struct drm_connector *connector;
884
885 /* taking the mode config mutex ends up in a clash with sysfs */
886 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
887 drm_sysfs_connector_remove(connector);
888
889}
890EXPORT_SYMBOL(drm_connector_unplug_all);
891
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100892/**
893 * drm_bridge_init - initialize a drm transcoder/bridge
894 * @dev: drm device
895 * @bridge: transcoder/bridge to set up
896 * @funcs: bridge function table
897 *
898 * Initialises a preallocated bridge. Bridges should be
899 * subclassed as part of driver connector objects.
900 *
901 * Returns:
902 * Zero on success, error code on failure.
903 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400904int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
905 const struct drm_bridge_funcs *funcs)
906{
907 int ret;
908
909 drm_modeset_lock_all(dev);
910
911 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
912 if (ret)
913 goto out;
914
915 bridge->dev = dev;
916 bridge->funcs = funcs;
917
918 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
919 dev->mode_config.num_bridge++;
920
921 out:
922 drm_modeset_unlock_all(dev);
923 return ret;
924}
925EXPORT_SYMBOL(drm_bridge_init);
926
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100927/**
928 * drm_bridge_cleanup - cleans up an initialised bridge
929 * @bridge: bridge to cleanup
930 *
931 * Cleans up the bridge but doesn't free the object.
932 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400933void drm_bridge_cleanup(struct drm_bridge *bridge)
934{
935 struct drm_device *dev = bridge->dev;
936
937 drm_modeset_lock_all(dev);
938 drm_mode_object_put(dev, &bridge->base);
939 list_del(&bridge->head);
940 dev->mode_config.num_bridge--;
941 drm_modeset_unlock_all(dev);
942}
943EXPORT_SYMBOL(drm_bridge_cleanup);
944
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100945/**
946 * drm_encoder_init - Init a preallocated encoder
947 * @dev: drm device
948 * @encoder: the encoder to init
949 * @funcs: callbacks for this encoder
950 * @encoder_type: user visible type of the encoder
951 *
952 * Initialises a preallocated encoder. Encoder should be
953 * subclassed as part of driver encoder objects.
954 *
955 * Returns:
956 * Zero on success, error code on failure.
957 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200958int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000959 struct drm_encoder *encoder,
960 const struct drm_encoder_funcs *funcs,
961 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800962{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200963 int ret;
964
Daniel Vetter84849902012-12-02 00:28:11 +0100965 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800966
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200967 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
968 if (ret)
969 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800970
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200971 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800972 encoder->encoder_type = encoder_type;
973 encoder->funcs = funcs;
974
975 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
976 dev->mode_config.num_encoder++;
977
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200978 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100979 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200980
981 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800982}
983EXPORT_SYMBOL(drm_encoder_init);
984
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100985/**
986 * drm_encoder_cleanup - cleans up an initialised encoder
987 * @encoder: encoder to cleanup
988 *
989 * Cleans up the encoder but doesn't free the object.
990 */
Dave Airlief453ba02008-11-07 14:05:41 -0800991void drm_encoder_cleanup(struct drm_encoder *encoder)
992{
993 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100994 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800995 drm_mode_object_put(dev, &encoder->base);
996 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000997 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100998 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800999}
1000EXPORT_SYMBOL(drm_encoder_cleanup);
1001
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001002/**
1003 * drm_plane_init - Initialise a new plane object
1004 * @dev: DRM device
1005 * @plane: plane object to init
1006 * @possible_crtcs: bitmask of possible CRTCs
1007 * @funcs: callbacks for the new plane
1008 * @formats: array of supported formats (%DRM_FORMAT_*)
1009 * @format_count: number of elements in @formats
1010 * @priv: plane is private (hidden from userspace)?
1011 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001012 * Inits a preallocate plane object created as base part of a driver plane
1013 * object.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001014 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001015 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001016 * Zero on success, error code on failure.
1017 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001018int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1019 unsigned long possible_crtcs,
1020 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -06001021 const uint32_t *formats, uint32_t format_count,
1022 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001023{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001024 int ret;
1025
Daniel Vetter84849902012-12-02 00:28:11 +01001026 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001027
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001028 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1029 if (ret)
1030 goto out;
1031
Rob Clark4d939142012-05-17 02:23:27 -06001032 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001033 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001034 plane->funcs = funcs;
1035 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1036 GFP_KERNEL);
1037 if (!plane->format_types) {
1038 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1039 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001040 ret = -ENOMEM;
1041 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001042 }
1043
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001044 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001045 plane->format_count = format_count;
1046 plane->possible_crtcs = possible_crtcs;
Matt Ropere27dde32014-04-01 15:22:30 -07001047 plane->type = DRM_PLANE_TYPE_OVERLAY;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001048
Rob Clark0a7eb242011-12-13 20:19:36 -06001049 /* private planes are not exposed to userspace, but depending on
1050 * display hardware, might be convenient to allow sharing programming
1051 * for the scanout engine with the crtc implementation.
1052 */
1053 if (!priv) {
1054 list_add_tail(&plane->head, &dev->mode_config.plane_list);
Matt Ropere27dde32014-04-01 15:22:30 -07001055 dev->mode_config.num_total_plane++;
1056 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1057 dev->mode_config.num_overlay_plane++;
Rob Clark0a7eb242011-12-13 20:19:36 -06001058 } else {
1059 INIT_LIST_HEAD(&plane->head);
1060 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001061
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001062 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001063 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001064
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001065 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001066}
1067EXPORT_SYMBOL(drm_plane_init);
1068
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001069/**
1070 * drm_plane_cleanup - Clean up the core plane usage
1071 * @plane: plane to cleanup
1072 *
1073 * This function cleans up @plane and removes it from the DRM mode setting
1074 * core. Note that the function does *not* free the plane structure itself,
1075 * this is the responsibility of the caller.
1076 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001077void drm_plane_cleanup(struct drm_plane *plane)
1078{
1079 struct drm_device *dev = plane->dev;
1080
Daniel Vetter84849902012-12-02 00:28:11 +01001081 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001082 kfree(plane->format_types);
1083 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -06001084 /* if not added to a list, it must be a private plane */
1085 if (!list_empty(&plane->head)) {
1086 list_del(&plane->head);
Matt Ropere27dde32014-04-01 15:22:30 -07001087 dev->mode_config.num_total_plane--;
1088 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1089 dev->mode_config.num_overlay_plane--;
Rob Clark0a7eb242011-12-13 20:19:36 -06001090 }
Daniel Vetter84849902012-12-02 00:28:11 +01001091 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001092}
1093EXPORT_SYMBOL(drm_plane_cleanup);
1094
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001095/**
1096 * drm_plane_force_disable - Forcibly disable a plane
1097 * @plane: plane to disable
1098 *
1099 * Forces the plane to be disabled.
1100 *
1101 * Used when the plane's current framebuffer is destroyed,
1102 * and when restoring fbdev mode.
1103 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001104void drm_plane_force_disable(struct drm_plane *plane)
1105{
1106 int ret;
1107
1108 if (!plane->fb)
1109 return;
1110
1111 ret = plane->funcs->disable_plane(plane);
1112 if (ret)
1113 DRM_ERROR("failed to disable plane with busy fb\n");
1114 /* disconnect the plane from the fb and crtc: */
1115 __drm_framebuffer_unreference(plane->fb);
1116 plane->fb = NULL;
1117 plane->crtc = NULL;
1118}
1119EXPORT_SYMBOL(drm_plane_force_disable);
1120
Dave Airlief453ba02008-11-07 14:05:41 -08001121static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1122{
1123 struct drm_property *edid;
1124 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001125
1126 /*
1127 * Standard properties (apply to all connectors)
1128 */
1129 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1130 DRM_MODE_PROP_IMMUTABLE,
1131 "EDID", 0);
1132 dev->mode_config.edid_property = edid;
1133
Sascha Hauer4a67d392012-02-06 10:58:17 +01001134 dpms = drm_property_create_enum(dev, 0,
1135 "DPMS", drm_dpms_enum_list,
1136 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001137 dev->mode_config.dpms_property = dpms;
1138
1139 return 0;
1140}
1141
1142/**
1143 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1144 * @dev: DRM device
1145 *
1146 * Called by a driver the first time a DVI-I connector is made.
1147 */
1148int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1149{
1150 struct drm_property *dvi_i_selector;
1151 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001152
1153 if (dev->mode_config.dvi_i_select_subconnector_property)
1154 return 0;
1155
1156 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001157 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001158 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001159 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001160 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001161 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1162
Sascha Hauer4a67d392012-02-06 10:58:17 +01001163 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001164 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001165 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001166 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001167 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1168
1169 return 0;
1170}
1171EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1172
1173/**
1174 * drm_create_tv_properties - create TV specific connector properties
1175 * @dev: DRM device
1176 * @num_modes: number of different TV formats (modes) supported
1177 * @modes: array of pointers to strings containing name of each format
1178 *
1179 * Called by a driver's TV initialization routine, this function creates
1180 * the TV specific connector properties for a given device. Caller is
1181 * responsible for allocating a list of format names and passing them to
1182 * this routine.
1183 */
1184int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1185 char *modes[])
1186{
1187 struct drm_property *tv_selector;
1188 struct drm_property *tv_subconnector;
1189 int i;
1190
1191 if (dev->mode_config.tv_select_subconnector_property)
1192 return 0;
1193
1194 /*
1195 * Basic connector properties
1196 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001197 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001198 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001199 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001200 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001201 dev->mode_config.tv_select_subconnector_property = tv_selector;
1202
1203 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001204 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1205 "subconnector",
1206 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001207 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001208 dev->mode_config.tv_subconnector_property = tv_subconnector;
1209
1210 /*
1211 * Other, TV specific properties: margins & TV modes.
1212 */
1213 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001214 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001215
1216 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001217 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001218
1219 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001220 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001221
1222 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001223 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001224
1225 dev->mode_config.tv_mode_property =
1226 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1227 "mode", num_modes);
1228 for (i = 0; i < num_modes; i++)
1229 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1230 i, modes[i]);
1231
Francisco Jerezb6b79022009-08-02 04:19:20 +02001232 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001233 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001234
1235 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001236 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001237
1238 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001239 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001240
Francisco Jereza75f0232009-08-12 02:30:10 +02001241 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001242 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001243
1244 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001245 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001246
1247 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001248 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001249
Dave Airlief453ba02008-11-07 14:05:41 -08001250 return 0;
1251}
1252EXPORT_SYMBOL(drm_mode_create_tv_properties);
1253
1254/**
1255 * drm_mode_create_scaling_mode_property - create scaling mode property
1256 * @dev: DRM device
1257 *
1258 * Called by a driver the first time it's needed, must be attached to desired
1259 * connectors.
1260 */
1261int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1262{
1263 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001264
1265 if (dev->mode_config.scaling_mode_property)
1266 return 0;
1267
1268 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001269 drm_property_create_enum(dev, 0, "scaling mode",
1270 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001271 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001272
1273 dev->mode_config.scaling_mode_property = scaling_mode;
1274
1275 return 0;
1276}
1277EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1278
1279/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001280 * drm_mode_create_dirty_property - create dirty property
1281 * @dev: DRM device
1282 *
1283 * Called by a driver the first time it's needed, must be attached to desired
1284 * connectors.
1285 */
1286int drm_mode_create_dirty_info_property(struct drm_device *dev)
1287{
1288 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001289
1290 if (dev->mode_config.dirty_info_property)
1291 return 0;
1292
1293 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001294 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001295 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001296 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001297 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001298 dev->mode_config.dirty_info_property = dirty_info;
1299
1300 return 0;
1301}
1302EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1303
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001304static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001305{
1306 uint32_t total_objects = 0;
1307
1308 total_objects += dev->mode_config.num_crtc;
1309 total_objects += dev->mode_config.num_connector;
1310 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001311 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001312
Dave Airlief453ba02008-11-07 14:05:41 -08001313 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1314 if (!group->id_list)
1315 return -ENOMEM;
1316
1317 group->num_crtcs = 0;
1318 group->num_connectors = 0;
1319 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001320 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001321 return 0;
1322}
1323
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001324/*
1325 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1326 * the drm core's responsibility to set up mode control groups.
1327 */
Dave Airlief453ba02008-11-07 14:05:41 -08001328int drm_mode_group_init_legacy_group(struct drm_device *dev,
1329 struct drm_mode_group *group)
1330{
1331 struct drm_crtc *crtc;
1332 struct drm_encoder *encoder;
1333 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001334 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001335 int ret;
1336
1337 if ((ret = drm_mode_group_init(dev, group)))
1338 return ret;
1339
1340 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1341 group->id_list[group->num_crtcs++] = crtc->base.id;
1342
1343 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1344 group->id_list[group->num_crtcs + group->num_encoders++] =
1345 encoder->base.id;
1346
1347 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1348 group->id_list[group->num_crtcs + group->num_encoders +
1349 group->num_connectors++] = connector->base.id;
1350
Sean Paul3b336ec2013-08-14 16:47:37 -04001351 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1352 group->id_list[group->num_crtcs + group->num_encoders +
1353 group->num_connectors + group->num_bridges++] =
1354 bridge->base.id;
1355
Dave Airlief453ba02008-11-07 14:05:41 -08001356 return 0;
1357}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001358EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001359
1360/**
Dave Airlief453ba02008-11-07 14:05:41 -08001361 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1362 * @out: drm_mode_modeinfo struct to return to the user
1363 * @in: drm_display_mode to use
1364 *
Dave Airlief453ba02008-11-07 14:05:41 -08001365 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1366 * the user.
1367 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001368static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1369 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001370{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001371 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1372 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1373 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1374 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1375 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1376 "timing values too large for mode info\n");
1377
Dave Airlief453ba02008-11-07 14:05:41 -08001378 out->clock = in->clock;
1379 out->hdisplay = in->hdisplay;
1380 out->hsync_start = in->hsync_start;
1381 out->hsync_end = in->hsync_end;
1382 out->htotal = in->htotal;
1383 out->hskew = in->hskew;
1384 out->vdisplay = in->vdisplay;
1385 out->vsync_start = in->vsync_start;
1386 out->vsync_end = in->vsync_end;
1387 out->vtotal = in->vtotal;
1388 out->vscan = in->vscan;
1389 out->vrefresh = in->vrefresh;
1390 out->flags = in->flags;
1391 out->type = in->type;
1392 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1393 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1394}
1395
1396/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001397 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001398 * @out: drm_display_mode to return to the user
1399 * @in: drm_mode_modeinfo to use
1400 *
Dave Airlief453ba02008-11-07 14:05:41 -08001401 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1402 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001403 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001404 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001405 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001406 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001407static int drm_crtc_convert_umode(struct drm_display_mode *out,
1408 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001409{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001410 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1411 return -ERANGE;
1412
Damien Lespiau5848ad42013-09-27 12:11:50 +01001413 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1414 return -EINVAL;
1415
Dave Airlief453ba02008-11-07 14:05:41 -08001416 out->clock = in->clock;
1417 out->hdisplay = in->hdisplay;
1418 out->hsync_start = in->hsync_start;
1419 out->hsync_end = in->hsync_end;
1420 out->htotal = in->htotal;
1421 out->hskew = in->hskew;
1422 out->vdisplay = in->vdisplay;
1423 out->vsync_start = in->vsync_start;
1424 out->vsync_end = in->vsync_end;
1425 out->vtotal = in->vtotal;
1426 out->vscan = in->vscan;
1427 out->vrefresh = in->vrefresh;
1428 out->flags = in->flags;
1429 out->type = in->type;
1430 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1431 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001432
1433 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001434}
1435
1436/**
1437 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001438 * @dev: drm device for the ioctl
1439 * @data: data pointer for the ioctl
1440 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001441 *
Dave Airlief453ba02008-11-07 14:05:41 -08001442 * Construct a set of configuration description structures and return
1443 * them to the user, including CRTC, connector and framebuffer configuration.
1444 *
1445 * Called by the user via ioctl.
1446 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001447 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001448 * Zero on success, errno on failure.
1449 */
1450int drm_mode_getresources(struct drm_device *dev, void *data,
1451 struct drm_file *file_priv)
1452{
1453 struct drm_mode_card_res *card_res = data;
1454 struct list_head *lh;
1455 struct drm_framebuffer *fb;
1456 struct drm_connector *connector;
1457 struct drm_crtc *crtc;
1458 struct drm_encoder *encoder;
1459 int ret = 0;
1460 int connector_count = 0;
1461 int crtc_count = 0;
1462 int fb_count = 0;
1463 int encoder_count = 0;
1464 int copied = 0, i;
1465 uint32_t __user *fb_id;
1466 uint32_t __user *crtc_id;
1467 uint32_t __user *connector_id;
1468 uint32_t __user *encoder_id;
1469 struct drm_mode_group *mode_group;
1470
Dave Airliefb3b06c2011-02-08 13:55:21 +10001471 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1472 return -EINVAL;
1473
Dave Airlief453ba02008-11-07 14:05:41 -08001474
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001475 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001476 /*
1477 * For the non-control nodes we need to limit the list of resources
1478 * by IDs in the group list for this node
1479 */
1480 list_for_each(lh, &file_priv->fbs)
1481 fb_count++;
1482
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001483 /* handle this in 4 parts */
1484 /* FBs */
1485 if (card_res->count_fbs >= fb_count) {
1486 copied = 0;
1487 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1488 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1489 if (put_user(fb->base.id, fb_id + copied)) {
1490 mutex_unlock(&file_priv->fbs_lock);
1491 return -EFAULT;
1492 }
1493 copied++;
1494 }
1495 }
1496 card_res->count_fbs = fb_count;
1497 mutex_unlock(&file_priv->fbs_lock);
1498
1499 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001500 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001501
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001502 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001503 list_for_each(lh, &dev->mode_config.crtc_list)
1504 crtc_count++;
1505
1506 list_for_each(lh, &dev->mode_config.connector_list)
1507 connector_count++;
1508
1509 list_for_each(lh, &dev->mode_config.encoder_list)
1510 encoder_count++;
1511 } else {
1512
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001513 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001514 crtc_count = mode_group->num_crtcs;
1515 connector_count = mode_group->num_connectors;
1516 encoder_count = mode_group->num_encoders;
1517 }
1518
1519 card_res->max_height = dev->mode_config.max_height;
1520 card_res->min_height = dev->mode_config.min_height;
1521 card_res->max_width = dev->mode_config.max_width;
1522 card_res->min_width = dev->mode_config.min_width;
1523
Dave Airlief453ba02008-11-07 14:05:41 -08001524 /* CRTCs */
1525 if (card_res->count_crtcs >= crtc_count) {
1526 copied = 0;
1527 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001528 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001529 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1530 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001531 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001532 if (put_user(crtc->base.id, crtc_id + copied)) {
1533 ret = -EFAULT;
1534 goto out;
1535 }
1536 copied++;
1537 }
1538 } else {
1539 for (i = 0; i < mode_group->num_crtcs; i++) {
1540 if (put_user(mode_group->id_list[i],
1541 crtc_id + copied)) {
1542 ret = -EFAULT;
1543 goto out;
1544 }
1545 copied++;
1546 }
1547 }
1548 }
1549 card_res->count_crtcs = crtc_count;
1550
1551 /* Encoders */
1552 if (card_res->count_encoders >= encoder_count) {
1553 copied = 0;
1554 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001555 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001556 list_for_each_entry(encoder,
1557 &dev->mode_config.encoder_list,
1558 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001559 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1560 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001561 if (put_user(encoder->base.id, encoder_id +
1562 copied)) {
1563 ret = -EFAULT;
1564 goto out;
1565 }
1566 copied++;
1567 }
1568 } else {
1569 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1570 if (put_user(mode_group->id_list[i],
1571 encoder_id + copied)) {
1572 ret = -EFAULT;
1573 goto out;
1574 }
1575 copied++;
1576 }
1577
1578 }
1579 }
1580 card_res->count_encoders = encoder_count;
1581
1582 /* Connectors */
1583 if (card_res->count_connectors >= connector_count) {
1584 copied = 0;
1585 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001586 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001587 list_for_each_entry(connector,
1588 &dev->mode_config.connector_list,
1589 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001590 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1591 connector->base.id,
1592 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001593 if (put_user(connector->base.id,
1594 connector_id + copied)) {
1595 ret = -EFAULT;
1596 goto out;
1597 }
1598 copied++;
1599 }
1600 } else {
1601 int start = mode_group->num_crtcs +
1602 mode_group->num_encoders;
1603 for (i = start; i < start + mode_group->num_connectors; i++) {
1604 if (put_user(mode_group->id_list[i],
1605 connector_id + copied)) {
1606 ret = -EFAULT;
1607 goto out;
1608 }
1609 copied++;
1610 }
1611 }
1612 }
1613 card_res->count_connectors = connector_count;
1614
Jerome Glisse94401062010-07-15 15:43:25 -04001615 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001616 card_res->count_connectors, card_res->count_encoders);
1617
1618out:
Daniel Vetter84849902012-12-02 00:28:11 +01001619 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001620 return ret;
1621}
1622
1623/**
1624 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001625 * @dev: drm device for the ioctl
1626 * @data: data pointer for the ioctl
1627 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001628 *
Dave Airlief453ba02008-11-07 14:05:41 -08001629 * Construct a CRTC configuration structure to return to the user.
1630 *
1631 * Called by the user via ioctl.
1632 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001633 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001634 * Zero on success, errno on failure.
1635 */
1636int drm_mode_getcrtc(struct drm_device *dev,
1637 void *data, struct drm_file *file_priv)
1638{
1639 struct drm_mode_crtc *crtc_resp = data;
1640 struct drm_crtc *crtc;
1641 struct drm_mode_object *obj;
1642 int ret = 0;
1643
Dave Airliefb3b06c2011-02-08 13:55:21 +10001644 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1645 return -EINVAL;
1646
Daniel Vetter84849902012-12-02 00:28:11 +01001647 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001648
1649 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1650 DRM_MODE_OBJECT_CRTC);
1651 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001652 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001653 goto out;
1654 }
1655 crtc = obj_to_crtc(obj);
1656
1657 crtc_resp->x = crtc->x;
1658 crtc_resp->y = crtc->y;
1659 crtc_resp->gamma_size = crtc->gamma_size;
1660 if (crtc->fb)
1661 crtc_resp->fb_id = crtc->fb->base.id;
1662 else
1663 crtc_resp->fb_id = 0;
1664
1665 if (crtc->enabled) {
1666
1667 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1668 crtc_resp->mode_valid = 1;
1669
1670 } else {
1671 crtc_resp->mode_valid = 0;
1672 }
1673
1674out:
Daniel Vetter84849902012-12-02 00:28:11 +01001675 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001676 return ret;
1677}
1678
Damien Lespiau61d8e322013-09-25 16:45:22 +01001679static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1680 const struct drm_file *file_priv)
1681{
1682 /*
1683 * If user-space hasn't configured the driver to expose the stereo 3D
1684 * modes, don't expose them.
1685 */
1686 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1687 return false;
1688
1689 return true;
1690}
1691
Dave Airlief453ba02008-11-07 14:05:41 -08001692/**
1693 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001694 * @dev: drm device for the ioctl
1695 * @data: data pointer for the ioctl
1696 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001697 *
Dave Airlief453ba02008-11-07 14:05:41 -08001698 * Construct a connector configuration structure to return to the user.
1699 *
1700 * Called by the user via ioctl.
1701 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001702 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001703 * Zero on success, errno on failure.
1704 */
1705int drm_mode_getconnector(struct drm_device *dev, void *data,
1706 struct drm_file *file_priv)
1707{
1708 struct drm_mode_get_connector *out_resp = data;
1709 struct drm_mode_object *obj;
1710 struct drm_connector *connector;
1711 struct drm_display_mode *mode;
1712 int mode_count = 0;
1713 int props_count = 0;
1714 int encoders_count = 0;
1715 int ret = 0;
1716 int copied = 0;
1717 int i;
1718 struct drm_mode_modeinfo u_mode;
1719 struct drm_mode_modeinfo __user *mode_ptr;
1720 uint32_t __user *prop_ptr;
1721 uint64_t __user *prop_values;
1722 uint32_t __user *encoder_ptr;
1723
Dave Airliefb3b06c2011-02-08 13:55:21 +10001724 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1725 return -EINVAL;
1726
Dave Airlief453ba02008-11-07 14:05:41 -08001727 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1728
Jerome Glisse94401062010-07-15 15:43:25 -04001729 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001730
Daniel Vetter7b240562012-12-12 00:35:33 +01001731 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001732
1733 obj = drm_mode_object_find(dev, out_resp->connector_id,
1734 DRM_MODE_OBJECT_CONNECTOR);
1735 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001736 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001737 goto out;
1738 }
1739 connector = obj_to_connector(obj);
1740
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001741 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001742
1743 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1744 if (connector->encoder_ids[i] != 0) {
1745 encoders_count++;
1746 }
1747 }
1748
1749 if (out_resp->count_modes == 0) {
1750 connector->funcs->fill_modes(connector,
1751 dev->mode_config.max_width,
1752 dev->mode_config.max_height);
1753 }
1754
1755 /* delayed so we get modes regardless of pre-fill_modes state */
1756 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001757 if (drm_mode_expose_to_userspace(mode, file_priv))
1758 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001759
1760 out_resp->connector_id = connector->base.id;
1761 out_resp->connector_type = connector->connector_type;
1762 out_resp->connector_type_id = connector->connector_type_id;
1763 out_resp->mm_width = connector->display_info.width_mm;
1764 out_resp->mm_height = connector->display_info.height_mm;
1765 out_resp->subpixel = connector->display_info.subpixel_order;
1766 out_resp->connection = connector->status;
1767 if (connector->encoder)
1768 out_resp->encoder_id = connector->encoder->base.id;
1769 else
1770 out_resp->encoder_id = 0;
1771
1772 /*
1773 * This ioctl is called twice, once to determine how much space is
1774 * needed, and the 2nd time to fill it.
1775 */
1776 if ((out_resp->count_modes >= mode_count) && mode_count) {
1777 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001778 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001779 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001780 if (!drm_mode_expose_to_userspace(mode, file_priv))
1781 continue;
1782
Dave Airlief453ba02008-11-07 14:05:41 -08001783 drm_crtc_convert_to_umode(&u_mode, mode);
1784 if (copy_to_user(mode_ptr + copied,
1785 &u_mode, sizeof(u_mode))) {
1786 ret = -EFAULT;
1787 goto out;
1788 }
1789 copied++;
1790 }
1791 }
1792 out_resp->count_modes = mode_count;
1793
1794 if ((out_resp->count_props >= props_count) && props_count) {
1795 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001796 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1797 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001798 for (i = 0; i < connector->properties.count; i++) {
1799 if (put_user(connector->properties.ids[i],
1800 prop_ptr + copied)) {
1801 ret = -EFAULT;
1802 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001803 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001804
1805 if (put_user(connector->properties.values[i],
1806 prop_values + copied)) {
1807 ret = -EFAULT;
1808 goto out;
1809 }
1810 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001811 }
1812 }
1813 out_resp->count_props = props_count;
1814
1815 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1816 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001817 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001818 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1819 if (connector->encoder_ids[i] != 0) {
1820 if (put_user(connector->encoder_ids[i],
1821 encoder_ptr + copied)) {
1822 ret = -EFAULT;
1823 goto out;
1824 }
1825 copied++;
1826 }
1827 }
1828 }
1829 out_resp->count_encoders = encoders_count;
1830
1831out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001832 mutex_unlock(&dev->mode_config.mutex);
1833
Dave Airlief453ba02008-11-07 14:05:41 -08001834 return ret;
1835}
1836
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001837/**
1838 * drm_mode_getencoder - get encoder configuration
1839 * @dev: drm device for the ioctl
1840 * @data: data pointer for the ioctl
1841 * @file_priv: drm file for the ioctl call
1842 *
1843 * Construct a encoder configuration structure to return to the user.
1844 *
1845 * Called by the user via ioctl.
1846 *
1847 * Returns:
1848 * Zero on success, errno on failure.
1849 */
Dave Airlief453ba02008-11-07 14:05:41 -08001850int drm_mode_getencoder(struct drm_device *dev, void *data,
1851 struct drm_file *file_priv)
1852{
1853 struct drm_mode_get_encoder *enc_resp = data;
1854 struct drm_mode_object *obj;
1855 struct drm_encoder *encoder;
1856 int ret = 0;
1857
Dave Airliefb3b06c2011-02-08 13:55:21 +10001858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1859 return -EINVAL;
1860
Daniel Vetter84849902012-12-02 00:28:11 +01001861 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001862 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1863 DRM_MODE_OBJECT_ENCODER);
1864 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001865 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001866 goto out;
1867 }
1868 encoder = obj_to_encoder(obj);
1869
1870 if (encoder->crtc)
1871 enc_resp->crtc_id = encoder->crtc->base.id;
1872 else
1873 enc_resp->crtc_id = 0;
1874 enc_resp->encoder_type = encoder->encoder_type;
1875 enc_resp->encoder_id = encoder->base.id;
1876 enc_resp->possible_crtcs = encoder->possible_crtcs;
1877 enc_resp->possible_clones = encoder->possible_clones;
1878
1879out:
Daniel Vetter84849902012-12-02 00:28:11 +01001880 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001881 return ret;
1882}
1883
1884/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001885 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001886 * @dev: DRM device
1887 * @data: ioctl data
1888 * @file_priv: DRM file info
1889 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001890 * Construct a list of plane ids to return to the user.
1891 *
1892 * Called by the user via ioctl.
1893 *
1894 * Returns:
1895 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001896 */
1897int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001898 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001899{
1900 struct drm_mode_get_plane_res *plane_resp = data;
1901 struct drm_mode_config *config;
1902 struct drm_plane *plane;
1903 uint32_t __user *plane_ptr;
1904 int copied = 0, ret = 0;
1905
1906 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1907 return -EINVAL;
1908
Daniel Vetter84849902012-12-02 00:28:11 +01001909 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001910 config = &dev->mode_config;
1911
1912 /*
1913 * This ioctl is called twice, once to determine how much space is
1914 * needed, and the 2nd time to fill it.
1915 */
Matt Ropere27dde32014-04-01 15:22:30 -07001916 if (config->num_overlay_plane &&
1917 (plane_resp->count_planes >= config->num_overlay_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001918 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001919
1920 list_for_each_entry(plane, &config->plane_list, head) {
Matt Ropere27dde32014-04-01 15:22:30 -07001921 /* Only advertise overlays to userspace for now. */
1922 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
1923 continue;
1924
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001925 if (put_user(plane->base.id, plane_ptr + copied)) {
1926 ret = -EFAULT;
1927 goto out;
1928 }
1929 copied++;
1930 }
1931 }
Matt Ropere27dde32014-04-01 15:22:30 -07001932 plane_resp->count_planes = config->num_overlay_plane;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001933
1934out:
Daniel Vetter84849902012-12-02 00:28:11 +01001935 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001936 return ret;
1937}
1938
1939/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001940 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001941 * @dev: DRM device
1942 * @data: ioctl data
1943 * @file_priv: DRM file info
1944 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001945 * Construct a plane configuration structure to return to the user.
1946 *
1947 * Called by the user via ioctl.
1948 *
1949 * Returns:
1950 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001951 */
1952int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001953 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001954{
1955 struct drm_mode_get_plane *plane_resp = data;
1956 struct drm_mode_object *obj;
1957 struct drm_plane *plane;
1958 uint32_t __user *format_ptr;
1959 int ret = 0;
1960
1961 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1962 return -EINVAL;
1963
Daniel Vetter84849902012-12-02 00:28:11 +01001964 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001965 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1966 DRM_MODE_OBJECT_PLANE);
1967 if (!obj) {
1968 ret = -ENOENT;
1969 goto out;
1970 }
1971 plane = obj_to_plane(obj);
1972
1973 if (plane->crtc)
1974 plane_resp->crtc_id = plane->crtc->base.id;
1975 else
1976 plane_resp->crtc_id = 0;
1977
1978 if (plane->fb)
1979 plane_resp->fb_id = plane->fb->base.id;
1980 else
1981 plane_resp->fb_id = 0;
1982
1983 plane_resp->plane_id = plane->base.id;
1984 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001985 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001986
1987 /*
1988 * This ioctl is called twice, once to determine how much space is
1989 * needed, and the 2nd time to fill it.
1990 */
1991 if (plane->format_count &&
1992 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001993 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001994 if (copy_to_user(format_ptr,
1995 plane->format_types,
1996 sizeof(uint32_t) * plane->format_count)) {
1997 ret = -EFAULT;
1998 goto out;
1999 }
2000 }
2001 plane_resp->count_format_types = plane->format_count;
2002
2003out:
Daniel Vetter84849902012-12-02 00:28:11 +01002004 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002005 return ret;
2006}
2007
2008/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002009 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002010 * @dev: DRM device
2011 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002012 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002013 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002014 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002015 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002016 *
2017 * Returns:
2018 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002019 */
2020int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002021 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002022{
2023 struct drm_mode_set_plane *plane_req = data;
2024 struct drm_mode_object *obj;
2025 struct drm_plane *plane;
2026 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002027 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002028 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002029 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002030 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002031
2032 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2033 return -EINVAL;
2034
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035 /*
2036 * First, find the plane, crtc, and fb objects. If not available,
2037 * we don't bother to call the driver.
2038 */
2039 obj = drm_mode_object_find(dev, plane_req->plane_id,
2040 DRM_MODE_OBJECT_PLANE);
2041 if (!obj) {
2042 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2043 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002044 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002045 }
2046 plane = obj_to_plane(obj);
2047
2048 /* No fb means shut it down */
2049 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002050 drm_modeset_lock_all(dev);
2051 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002052 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02002053 plane->crtc = NULL;
2054 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002055 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002056 goto out;
2057 }
2058
2059 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2060 DRM_MODE_OBJECT_CRTC);
2061 if (!obj) {
2062 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2063 plane_req->crtc_id);
2064 ret = -ENOENT;
2065 goto out;
2066 }
2067 crtc = obj_to_crtc(obj);
2068
Daniel Vetter786b99e2012-12-02 21:53:40 +01002069 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2070 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002071 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2072 plane_req->fb_id);
2073 ret = -ENOENT;
2074 goto out;
2075 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002076
Ville Syrjälä62443be2011-12-20 00:06:47 +02002077 /* Check whether this plane supports the fb pixel format. */
2078 for (i = 0; i < plane->format_count; i++)
2079 if (fb->pixel_format == plane->format_types[i])
2080 break;
2081 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002082 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2083 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002084 ret = -EINVAL;
2085 goto out;
2086 }
2087
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002088 fb_width = fb->width << 16;
2089 fb_height = fb->height << 16;
2090
2091 /* Make sure source coordinates are inside the fb. */
2092 if (plane_req->src_w > fb_width ||
2093 plane_req->src_x > fb_width - plane_req->src_w ||
2094 plane_req->src_h > fb_height ||
2095 plane_req->src_y > fb_height - plane_req->src_h) {
2096 DRM_DEBUG_KMS("Invalid source coordinates "
2097 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2098 plane_req->src_w >> 16,
2099 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2100 plane_req->src_h >> 16,
2101 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2102 plane_req->src_x >> 16,
2103 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2104 plane_req->src_y >> 16,
2105 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2106 ret = -ENOSPC;
2107 goto out;
2108 }
2109
Ville Syrjälä687a0402011-12-20 00:06:45 +02002110 /* Give drivers some help against integer overflows */
2111 if (plane_req->crtc_w > INT_MAX ||
2112 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2113 plane_req->crtc_h > INT_MAX ||
2114 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2115 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2116 plane_req->crtc_w, plane_req->crtc_h,
2117 plane_req->crtc_x, plane_req->crtc_y);
2118 ret = -ERANGE;
2119 goto out;
2120 }
2121
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002122 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002123 ret = plane->funcs->update_plane(plane, crtc, fb,
2124 plane_req->crtc_x, plane_req->crtc_y,
2125 plane_req->crtc_w, plane_req->crtc_h,
2126 plane_req->src_x, plane_req->src_y,
2127 plane_req->src_w, plane_req->src_h);
2128 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002129 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002130 plane->crtc = crtc;
2131 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002132 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002133 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002134 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002135
2136out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002137 if (fb)
2138 drm_framebuffer_unreference(fb);
2139 if (old_fb)
2140 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002141
2142 return ret;
2143}
2144
2145/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002146 * drm_mode_set_config_internal - helper to call ->set_config
2147 * @set: modeset config to set
2148 *
2149 * This is a little helper to wrap internal calls to the ->set_config driver
2150 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002151 *
2152 * Returns:
2153 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002154 */
2155int drm_mode_set_config_internal(struct drm_mode_set *set)
2156{
2157 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002158 struct drm_framebuffer *fb;
2159 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002160 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002161
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002162 /*
2163 * NOTE: ->set_config can also disable other crtcs (if we steal all
2164 * connectors from it), hence we need to refcount the fbs across all
2165 * crtcs. Atomic modeset will have saner semantics ...
2166 */
2167 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2168 tmp->old_fb = tmp->fb;
2169
Daniel Vetterb0d12322012-12-11 01:07:12 +01002170 fb = set->fb;
2171
2172 ret = crtc->funcs->set_config(set);
2173 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002174 /* crtc->fb must be updated by ->set_config, enforces this. */
2175 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002176 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002177
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002178 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2179 if (tmp->fb)
2180 drm_framebuffer_reference(tmp->fb);
2181 if (tmp->old_fb)
2182 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002183 }
2184
2185 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002186}
2187EXPORT_SYMBOL(drm_mode_set_config_internal);
2188
Matt Roperaf936292014-04-01 15:22:34 -07002189/**
2190 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2191 * CRTC viewport
2192 * @crtc: CRTC that framebuffer will be displayed on
2193 * @x: x panning
2194 * @y: y panning
2195 * @mode: mode that framebuffer will be displayed under
2196 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002197 */
Matt Roperaf936292014-04-01 15:22:34 -07002198int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2199 int x, int y,
2200 const struct drm_display_mode *mode,
2201 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002202
2203{
2204 int hdisplay, vdisplay;
2205
2206 hdisplay = mode->hdisplay;
2207 vdisplay = mode->vdisplay;
2208
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002209 if (drm_mode_is_stereo(mode)) {
2210 struct drm_display_mode adjusted = *mode;
2211
2212 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2213 hdisplay = adjusted.crtc_hdisplay;
2214 vdisplay = adjusted.crtc_vdisplay;
2215 }
2216
Damien Lespiauc11e9282013-09-25 16:45:30 +01002217 if (crtc->invert_dimensions)
2218 swap(hdisplay, vdisplay);
2219
2220 if (hdisplay > fb->width ||
2221 vdisplay > fb->height ||
2222 x > fb->width - hdisplay ||
2223 y > fb->height - vdisplay) {
2224 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2225 fb->width, fb->height, hdisplay, vdisplay, x, y,
2226 crtc->invert_dimensions ? " (inverted)" : "");
2227 return -ENOSPC;
2228 }
2229
2230 return 0;
2231}
Matt Roperaf936292014-04-01 15:22:34 -07002232EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002233
Daniel Vetter2d13b672012-12-11 13:47:23 +01002234/**
Dave Airlief453ba02008-11-07 14:05:41 -08002235 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002236 * @dev: drm device for the ioctl
2237 * @data: data pointer for the ioctl
2238 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002239 *
Dave Airlief453ba02008-11-07 14:05:41 -08002240 * Build a new CRTC configuration based on user request.
2241 *
2242 * Called by the user via ioctl.
2243 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002244 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002245 * Zero on success, errno on failure.
2246 */
2247int drm_mode_setcrtc(struct drm_device *dev, void *data,
2248 struct drm_file *file_priv)
2249{
2250 struct drm_mode_config *config = &dev->mode_config;
2251 struct drm_mode_crtc *crtc_req = data;
2252 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002253 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002254 struct drm_connector **connector_set = NULL, *connector;
2255 struct drm_framebuffer *fb = NULL;
2256 struct drm_display_mode *mode = NULL;
2257 struct drm_mode_set set;
2258 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002259 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002260 int i;
2261
Dave Airliefb3b06c2011-02-08 13:55:21 +10002262 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2263 return -EINVAL;
2264
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002265 /* For some reason crtc x/y offsets are signed internally. */
2266 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2267 return -ERANGE;
2268
Daniel Vetter84849902012-12-02 00:28:11 +01002269 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002270 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2271 DRM_MODE_OBJECT_CRTC);
2272 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002273 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002274 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002275 goto out;
2276 }
2277 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002278 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002279
2280 if (crtc_req->mode_valid) {
2281 /* If we have a mode we need a framebuffer. */
2282 /* If we pass -1, set the mode with the currently bound fb */
2283 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002284 if (!crtc->fb) {
2285 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2286 ret = -EINVAL;
2287 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002288 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002289 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002290 /* Make refcounting symmetric with the lookup path. */
2291 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002292 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002293 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2294 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002295 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2296 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002297 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002298 goto out;
2299 }
Dave Airlief453ba02008-11-07 14:05:41 -08002300 }
2301
2302 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002303 if (!mode) {
2304 ret = -ENOMEM;
2305 goto out;
2306 }
2307
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002308 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2309 if (ret) {
2310 DRM_DEBUG_KMS("Invalid mode\n");
2311 goto out;
2312 }
2313
Dave Airlief453ba02008-11-07 14:05:41 -08002314 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002315
Damien Lespiauc11e9282013-09-25 16:45:30 +01002316 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2317 mode, fb);
2318 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002319 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002320
Dave Airlief453ba02008-11-07 14:05:41 -08002321 }
2322
2323 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002324 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002325 ret = -EINVAL;
2326 goto out;
2327 }
2328
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002329 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002330 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002331 crtc_req->count_connectors);
2332 ret = -EINVAL;
2333 goto out;
2334 }
2335
2336 if (crtc_req->count_connectors > 0) {
2337 u32 out_id;
2338
2339 /* Avoid unbounded kernel memory allocation */
2340 if (crtc_req->count_connectors > config->num_connector) {
2341 ret = -EINVAL;
2342 goto out;
2343 }
2344
2345 connector_set = kmalloc(crtc_req->count_connectors *
2346 sizeof(struct drm_connector *),
2347 GFP_KERNEL);
2348 if (!connector_set) {
2349 ret = -ENOMEM;
2350 goto out;
2351 }
2352
2353 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002354 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002355 if (get_user(out_id, &set_connectors_ptr[i])) {
2356 ret = -EFAULT;
2357 goto out;
2358 }
2359
2360 obj = drm_mode_object_find(dev, out_id,
2361 DRM_MODE_OBJECT_CONNECTOR);
2362 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002363 DRM_DEBUG_KMS("Connector id %d unknown\n",
2364 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002365 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002366 goto out;
2367 }
2368 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002369 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2370 connector->base.id,
2371 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002372
2373 connector_set[i] = connector;
2374 }
2375 }
2376
2377 set.crtc = crtc;
2378 set.x = crtc_req->x;
2379 set.y = crtc_req->y;
2380 set.mode = mode;
2381 set.connectors = connector_set;
2382 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002383 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002384 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002385
2386out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002387 if (fb)
2388 drm_framebuffer_unreference(fb);
2389
Dave Airlief453ba02008-11-07 14:05:41 -08002390 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002391 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002392 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002393 return ret;
2394}
2395
Dave Airlie4c813d42013-06-20 11:48:52 +10002396static int drm_mode_cursor_common(struct drm_device *dev,
2397 struct drm_mode_cursor2 *req,
2398 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002399{
Dave Airlief453ba02008-11-07 14:05:41 -08002400 struct drm_mode_object *obj;
2401 struct drm_crtc *crtc;
2402 int ret = 0;
2403
Dave Airliefb3b06c2011-02-08 13:55:21 +10002404 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2405 return -EINVAL;
2406
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002407 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002408 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002409
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002410 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002411 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002412 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002413 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002414 }
2415 crtc = obj_to_crtc(obj);
2416
Daniel Vetterdac35662012-12-02 15:24:10 +01002417 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002418 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002419 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002420 ret = -ENXIO;
2421 goto out;
2422 }
2423 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002424 if (crtc->funcs->cursor_set2)
2425 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2426 req->width, req->height, req->hot_x, req->hot_y);
2427 else
2428 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2429 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002430 }
2431
2432 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2433 if (crtc->funcs->cursor_move) {
2434 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2435 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002436 ret = -EFAULT;
2437 goto out;
2438 }
2439 }
2440out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002441 mutex_unlock(&crtc->mutex);
2442
Dave Airlief453ba02008-11-07 14:05:41 -08002443 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002444
2445}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002446
2447
2448/**
2449 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2450 * @dev: drm device for the ioctl
2451 * @data: data pointer for the ioctl
2452 * @file_priv: drm file for the ioctl call
2453 *
2454 * Set the cursor configuration based on user request.
2455 *
2456 * Called by the user via ioctl.
2457 *
2458 * Returns:
2459 * Zero on success, errno on failure.
2460 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002461int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002462 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002463{
2464 struct drm_mode_cursor *req = data;
2465 struct drm_mode_cursor2 new_req;
2466
2467 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2468 new_req.hot_x = new_req.hot_y = 0;
2469
2470 return drm_mode_cursor_common(dev, &new_req, file_priv);
2471}
2472
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002473/**
2474 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2475 * @dev: drm device for the ioctl
2476 * @data: data pointer for the ioctl
2477 * @file_priv: drm file for the ioctl call
2478 *
2479 * Set the cursor configuration based on user request. This implements the 2nd
2480 * version of the cursor ioctl, which allows userspace to additionally specify
2481 * the hotspot of the pointer.
2482 *
2483 * Called by the user via ioctl.
2484 *
2485 * Returns:
2486 * Zero on success, errno on failure.
2487 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002488int drm_mode_cursor2_ioctl(struct drm_device *dev,
2489 void *data, struct drm_file *file_priv)
2490{
2491 struct drm_mode_cursor2 *req = data;
2492 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002493}
2494
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002495/**
2496 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2497 * @bpp: bits per pixels
2498 * @depth: bit depth per pixel
2499 *
2500 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2501 * Useful in fbdev emulation code, since that deals in those values.
2502 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002503uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2504{
2505 uint32_t fmt;
2506
2507 switch (bpp) {
2508 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002509 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002510 break;
2511 case 16:
2512 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002513 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002514 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002515 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002516 break;
2517 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002518 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002519 break;
2520 case 32:
2521 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002522 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002523 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002524 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002525 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002526 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002527 break;
2528 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002529 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2530 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002531 break;
2532 }
2533
2534 return fmt;
2535}
2536EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2537
Dave Airlief453ba02008-11-07 14:05:41 -08002538/**
2539 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002540 * @dev: drm device for the ioctl
2541 * @data: data pointer for the ioctl
2542 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002543 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002544 * Add a new FB to the specified CRTC, given a user request. This is the
2545 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002546 *
2547 * Called by the user via ioctl.
2548 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002549 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002550 * Zero on success, errno on failure.
2551 */
2552int drm_mode_addfb(struct drm_device *dev,
2553 void *data, struct drm_file *file_priv)
2554{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002555 struct drm_mode_fb_cmd *or = data;
2556 struct drm_mode_fb_cmd2 r = {};
2557 struct drm_mode_config *config = &dev->mode_config;
2558 struct drm_framebuffer *fb;
2559 int ret = 0;
2560
2561 /* Use new struct with format internally */
2562 r.fb_id = or->fb_id;
2563 r.width = or->width;
2564 r.height = or->height;
2565 r.pitches[0] = or->pitch;
2566 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2567 r.handles[0] = or->handle;
2568
2569 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2570 return -EINVAL;
2571
Jesse Barnesacb4b992011-11-07 12:03:23 -08002572 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002573 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002574
2575 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002576 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002577
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002578 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2579 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002580 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002581 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002582 }
2583
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002584 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002585 or->fb_id = fb->base.id;
2586 list_add(&fb->filp_head, &file_priv->fbs);
2587 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002588 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002589
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002590 return ret;
2591}
2592
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002593static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002594{
2595 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2596
2597 switch (format) {
2598 case DRM_FORMAT_C8:
2599 case DRM_FORMAT_RGB332:
2600 case DRM_FORMAT_BGR233:
2601 case DRM_FORMAT_XRGB4444:
2602 case DRM_FORMAT_XBGR4444:
2603 case DRM_FORMAT_RGBX4444:
2604 case DRM_FORMAT_BGRX4444:
2605 case DRM_FORMAT_ARGB4444:
2606 case DRM_FORMAT_ABGR4444:
2607 case DRM_FORMAT_RGBA4444:
2608 case DRM_FORMAT_BGRA4444:
2609 case DRM_FORMAT_XRGB1555:
2610 case DRM_FORMAT_XBGR1555:
2611 case DRM_FORMAT_RGBX5551:
2612 case DRM_FORMAT_BGRX5551:
2613 case DRM_FORMAT_ARGB1555:
2614 case DRM_FORMAT_ABGR1555:
2615 case DRM_FORMAT_RGBA5551:
2616 case DRM_FORMAT_BGRA5551:
2617 case DRM_FORMAT_RGB565:
2618 case DRM_FORMAT_BGR565:
2619 case DRM_FORMAT_RGB888:
2620 case DRM_FORMAT_BGR888:
2621 case DRM_FORMAT_XRGB8888:
2622 case DRM_FORMAT_XBGR8888:
2623 case DRM_FORMAT_RGBX8888:
2624 case DRM_FORMAT_BGRX8888:
2625 case DRM_FORMAT_ARGB8888:
2626 case DRM_FORMAT_ABGR8888:
2627 case DRM_FORMAT_RGBA8888:
2628 case DRM_FORMAT_BGRA8888:
2629 case DRM_FORMAT_XRGB2101010:
2630 case DRM_FORMAT_XBGR2101010:
2631 case DRM_FORMAT_RGBX1010102:
2632 case DRM_FORMAT_BGRX1010102:
2633 case DRM_FORMAT_ARGB2101010:
2634 case DRM_FORMAT_ABGR2101010:
2635 case DRM_FORMAT_RGBA1010102:
2636 case DRM_FORMAT_BGRA1010102:
2637 case DRM_FORMAT_YUYV:
2638 case DRM_FORMAT_YVYU:
2639 case DRM_FORMAT_UYVY:
2640 case DRM_FORMAT_VYUY:
2641 case DRM_FORMAT_AYUV:
2642 case DRM_FORMAT_NV12:
2643 case DRM_FORMAT_NV21:
2644 case DRM_FORMAT_NV16:
2645 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002646 case DRM_FORMAT_NV24:
2647 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002648 case DRM_FORMAT_YUV410:
2649 case DRM_FORMAT_YVU410:
2650 case DRM_FORMAT_YUV411:
2651 case DRM_FORMAT_YVU411:
2652 case DRM_FORMAT_YUV420:
2653 case DRM_FORMAT_YVU420:
2654 case DRM_FORMAT_YUV422:
2655 case DRM_FORMAT_YVU422:
2656 case DRM_FORMAT_YUV444:
2657 case DRM_FORMAT_YVU444:
2658 return 0;
2659 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002660 DRM_DEBUG_KMS("invalid pixel format %s\n",
2661 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002662 return -EINVAL;
2663 }
2664}
2665
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002666static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002667{
2668 int ret, hsub, vsub, num_planes, i;
2669
2670 ret = format_check(r);
2671 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002672 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2673 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002674 return ret;
2675 }
2676
2677 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2678 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2679 num_planes = drm_format_num_planes(r->pixel_format);
2680
2681 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002682 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002683 return -EINVAL;
2684 }
2685
2686 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002687 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002688 return -EINVAL;
2689 }
2690
2691 for (i = 0; i < num_planes; i++) {
2692 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002693 unsigned int height = r->height / (i != 0 ? vsub : 1);
2694 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002695
2696 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002697 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002698 return -EINVAL;
2699 }
2700
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002701 if ((uint64_t) width * cpp > UINT_MAX)
2702 return -ERANGE;
2703
2704 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2705 return -ERANGE;
2706
2707 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002708 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002709 return -EINVAL;
2710 }
2711 }
2712
2713 return 0;
2714}
2715
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002716/**
2717 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002718 * @dev: drm device for the ioctl
2719 * @data: data pointer for the ioctl
2720 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002721 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002722 * Add a new FB to the specified CRTC, given a user request with format. This is
2723 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2724 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002725 *
2726 * Called by the user via ioctl.
2727 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002728 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002729 * Zero on success, errno on failure.
2730 */
2731int drm_mode_addfb2(struct drm_device *dev,
2732 void *data, struct drm_file *file_priv)
2733{
2734 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002735 struct drm_mode_config *config = &dev->mode_config;
2736 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002737 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002738
Dave Airliefb3b06c2011-02-08 13:55:21 +10002739 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2740 return -EINVAL;
2741
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002742 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2743 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2744 return -EINVAL;
2745 }
2746
Dave Airlief453ba02008-11-07 14:05:41 -08002747 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002748 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002749 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002750 return -EINVAL;
2751 }
2752 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002753 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002754 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002755 return -EINVAL;
2756 }
2757
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002758 ret = framebuffer_check(r);
2759 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002760 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002761
Dave Airlief453ba02008-11-07 14:05:41 -08002762 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002763 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002764 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002765 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002766 }
2767
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002768 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002769 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002770 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002771 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002772 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002773
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002774
Dave Airlief453ba02008-11-07 14:05:41 -08002775 return ret;
2776}
2777
2778/**
2779 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002780 * @dev: drm device for the ioctl
2781 * @data: data pointer for the ioctl
2782 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002783 *
Dave Airlief453ba02008-11-07 14:05:41 -08002784 * Remove the FB specified by the user.
2785 *
2786 * Called by the user via ioctl.
2787 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002788 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002789 * Zero on success, errno on failure.
2790 */
2791int drm_mode_rmfb(struct drm_device *dev,
2792 void *data, struct drm_file *file_priv)
2793{
Dave Airlief453ba02008-11-07 14:05:41 -08002794 struct drm_framebuffer *fb = NULL;
2795 struct drm_framebuffer *fbl = NULL;
2796 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002797 int found = 0;
2798
Dave Airliefb3b06c2011-02-08 13:55:21 +10002799 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2800 return -EINVAL;
2801
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002802 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002803 mutex_lock(&dev->mode_config.fb_lock);
2804 fb = __drm_framebuffer_lookup(dev, *id);
2805 if (!fb)
2806 goto fail_lookup;
2807
Dave Airlief453ba02008-11-07 14:05:41 -08002808 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2809 if (fb == fbl)
2810 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002811 if (!found)
2812 goto fail_lookup;
2813
2814 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2815 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002816
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002817 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002818 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002819 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002820
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002821 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002822
Daniel Vetter2b677e82012-12-10 21:16:05 +01002823 return 0;
2824
2825fail_lookup:
2826 mutex_unlock(&dev->mode_config.fb_lock);
2827 mutex_unlock(&file_priv->fbs_lock);
2828
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002829 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002830}
2831
2832/**
2833 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002834 * @dev: drm device for the ioctl
2835 * @data: data pointer for the ioctl
2836 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002837 *
Dave Airlief453ba02008-11-07 14:05:41 -08002838 * Lookup the FB given its ID and return info about it.
2839 *
2840 * Called by the user via ioctl.
2841 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002842 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002843 * Zero on success, errno on failure.
2844 */
2845int drm_mode_getfb(struct drm_device *dev,
2846 void *data, struct drm_file *file_priv)
2847{
2848 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002849 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002850 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002851
Dave Airliefb3b06c2011-02-08 13:55:21 +10002852 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2853 return -EINVAL;
2854
Daniel Vetter786b99e2012-12-02 21:53:40 +01002855 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002856 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002857 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002858
2859 r->height = fb->height;
2860 r->width = fb->width;
2861 r->depth = fb->depth;
2862 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002863 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002864 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01002865 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01002866 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02002867 ret = fb->funcs->create_handle(fb, file_priv,
2868 &r->handle);
2869 } else {
2870 /* GET_FB() is an unprivileged ioctl so we must not
2871 * return a buffer-handle to non-master processes! For
2872 * backwards-compatibility reasons, we cannot make
2873 * GET_FB() privileged, so just return an invalid handle
2874 * for non-masters. */
2875 r->handle = 0;
2876 ret = 0;
2877 }
2878 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002879 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002880 }
Dave Airlief453ba02008-11-07 14:05:41 -08002881
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002882 drm_framebuffer_unreference(fb);
2883
Dave Airlief453ba02008-11-07 14:05:41 -08002884 return ret;
2885}
2886
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002887/**
2888 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2889 * @dev: drm device for the ioctl
2890 * @data: data pointer for the ioctl
2891 * @file_priv: drm file for the ioctl call
2892 *
2893 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2894 * rectangle list. Generic userspace which does frontbuffer rendering must call
2895 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2896 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2897 *
2898 * Modesetting drivers which always update the frontbuffer do not need to
2899 * implement the corresponding ->dirty framebuffer callback.
2900 *
2901 * Called by the user via ioctl.
2902 *
2903 * Returns:
2904 * Zero on success, errno on failure.
2905 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002906int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2907 void *data, struct drm_file *file_priv)
2908{
2909 struct drm_clip_rect __user *clips_ptr;
2910 struct drm_clip_rect *clips = NULL;
2911 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002912 struct drm_framebuffer *fb;
2913 unsigned flags;
2914 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002915 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002916
Dave Airliefb3b06c2011-02-08 13:55:21 +10002917 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2918 return -EINVAL;
2919
Daniel Vetter786b99e2012-12-02 21:53:40 +01002920 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002921 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002922 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002923
2924 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002925 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002926
2927 if (!num_clips != !clips_ptr) {
2928 ret = -EINVAL;
2929 goto out_err1;
2930 }
2931
2932 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2933
2934 /* If userspace annotates copy, clips must come in pairs */
2935 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2936 ret = -EINVAL;
2937 goto out_err1;
2938 }
2939
2940 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002941 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2942 ret = -EINVAL;
2943 goto out_err1;
2944 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002945 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2946 if (!clips) {
2947 ret = -ENOMEM;
2948 goto out_err1;
2949 }
2950
2951 ret = copy_from_user(clips, clips_ptr,
2952 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002953 if (ret) {
2954 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002955 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002956 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002957 }
2958
2959 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002960 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2961 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002962 } else {
2963 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002964 }
2965
2966out_err2:
2967 kfree(clips);
2968out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002969 drm_framebuffer_unreference(fb);
2970
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002971 return ret;
2972}
2973
2974
Dave Airlief453ba02008-11-07 14:05:41 -08002975/**
2976 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002977 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002978 *
Dave Airlief453ba02008-11-07 14:05:41 -08002979 * Destroy all the FBs associated with @filp.
2980 *
2981 * Called by the user via ioctl.
2982 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002983 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002984 * Zero on success, errno on failure.
2985 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002986void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002987{
Dave Airlief453ba02008-11-07 14:05:41 -08002988 struct drm_device *dev = priv->minor->dev;
2989 struct drm_framebuffer *fb, *tfb;
2990
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002991 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002992 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002993
2994 mutex_lock(&dev->mode_config.fb_lock);
2995 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2996 __drm_framebuffer_unregister(dev, fb);
2997 mutex_unlock(&dev->mode_config.fb_lock);
2998
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002999 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003000
3001 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003002 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003003 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003004 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003005}
3006
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003007/**
3008 * drm_property_create - create a new property type
3009 * @dev: drm device
3010 * @flags: flags specifying the property type
3011 * @name: name of the property
3012 * @num_values: number of pre-defined values
3013 *
3014 * This creates a new generic drm property which can then be attached to a drm
3015 * object with drm_object_attach_property. The returned property object must be
3016 * freed with drm_property_destroy.
3017 *
3018 * Returns:
3019 * A pointer to the newly created property on success, NULL on failure.
3020 */
Dave Airlief453ba02008-11-07 14:05:41 -08003021struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3022 const char *name, int num_values)
3023{
3024 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003025 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003026
3027 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3028 if (!property)
3029 return NULL;
3030
3031 if (num_values) {
3032 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3033 if (!property->values)
3034 goto fail;
3035 }
3036
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003037 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3038 if (ret)
3039 goto fail;
3040
Dave Airlief453ba02008-11-07 14:05:41 -08003041 property->flags = flags;
3042 property->num_values = num_values;
3043 INIT_LIST_HEAD(&property->enum_blob_list);
3044
Vinson Lee471dd2e2011-11-10 11:55:40 -08003045 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003046 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003047 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3048 }
Dave Airlief453ba02008-11-07 14:05:41 -08003049
3050 list_add_tail(&property->head, &dev->mode_config.property_list);
3051 return property;
3052fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003053 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003054 kfree(property);
3055 return NULL;
3056}
3057EXPORT_SYMBOL(drm_property_create);
3058
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003059/**
3060 * drm_property_create - create a new enumeration property type
3061 * @dev: drm device
3062 * @flags: flags specifying the property type
3063 * @name: name of the property
3064 * @props: enumeration lists with property values
3065 * @num_values: number of pre-defined values
3066 *
3067 * This creates a new generic drm property which can then be attached to a drm
3068 * object with drm_object_attach_property. The returned property object must be
3069 * freed with drm_property_destroy.
3070 *
3071 * Userspace is only allowed to set one of the predefined values for enumeration
3072 * properties.
3073 *
3074 * Returns:
3075 * A pointer to the newly created property on success, NULL on failure.
3076 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003077struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3078 const char *name,
3079 const struct drm_prop_enum_list *props,
3080 int num_values)
3081{
3082 struct drm_property *property;
3083 int i, ret;
3084
3085 flags |= DRM_MODE_PROP_ENUM;
3086
3087 property = drm_property_create(dev, flags, name, num_values);
3088 if (!property)
3089 return NULL;
3090
3091 for (i = 0; i < num_values; i++) {
3092 ret = drm_property_add_enum(property, i,
3093 props[i].type,
3094 props[i].name);
3095 if (ret) {
3096 drm_property_destroy(dev, property);
3097 return NULL;
3098 }
3099 }
3100
3101 return property;
3102}
3103EXPORT_SYMBOL(drm_property_create_enum);
3104
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003105/**
3106 * drm_property_create - create a new bitmask property type
3107 * @dev: drm device
3108 * @flags: flags specifying the property type
3109 * @name: name of the property
3110 * @props: enumeration lists with property bitflags
3111 * @num_values: number of pre-defined values
3112 *
3113 * This creates a new generic drm property which can then be attached to a drm
3114 * object with drm_object_attach_property. The returned property object must be
3115 * freed with drm_property_destroy.
3116 *
3117 * Compared to plain enumeration properties userspace is allowed to set any
3118 * or'ed together combination of the predefined property bitflag values
3119 *
3120 * Returns:
3121 * A pointer to the newly created property on success, NULL on failure.
3122 */
Rob Clark49e27542012-05-17 02:23:26 -06003123struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3124 int flags, const char *name,
3125 const struct drm_prop_enum_list *props,
3126 int num_values)
3127{
3128 struct drm_property *property;
3129 int i, ret;
3130
3131 flags |= DRM_MODE_PROP_BITMASK;
3132
3133 property = drm_property_create(dev, flags, name, num_values);
3134 if (!property)
3135 return NULL;
3136
3137 for (i = 0; i < num_values; i++) {
3138 ret = drm_property_add_enum(property, i,
3139 props[i].type,
3140 props[i].name);
3141 if (ret) {
3142 drm_property_destroy(dev, property);
3143 return NULL;
3144 }
3145 }
3146
3147 return property;
3148}
3149EXPORT_SYMBOL(drm_property_create_bitmask);
3150
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003151/**
3152 * drm_property_create - create a new ranged property type
3153 * @dev: drm device
3154 * @flags: flags specifying the property type
3155 * @name: name of the property
3156 * @min: minimum value of the property
3157 * @max: maximum value of the property
3158 *
3159 * This creates a new generic drm property which can then be attached to a drm
3160 * object with drm_object_attach_property. The returned property object must be
3161 * freed with drm_property_destroy.
3162 *
3163 * Userspace is allowed to set any interger value in the (min, max) range
3164 * inclusive.
3165 *
3166 * Returns:
3167 * A pointer to the newly created property on success, NULL on failure.
3168 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003169struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3170 const char *name,
3171 uint64_t min, uint64_t max)
3172{
3173 struct drm_property *property;
3174
3175 flags |= DRM_MODE_PROP_RANGE;
3176
3177 property = drm_property_create(dev, flags, name, 2);
3178 if (!property)
3179 return NULL;
3180
3181 property->values[0] = min;
3182 property->values[1] = max;
3183
3184 return property;
3185}
3186EXPORT_SYMBOL(drm_property_create_range);
3187
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003188/**
3189 * drm_property_add_enum - add a possible value to an enumeration property
3190 * @property: enumeration property to change
3191 * @index: index of the new enumeration
3192 * @value: value of the new enumeration
3193 * @name: symbolic name of the new enumeration
3194 *
3195 * This functions adds enumerations to a property.
3196 *
3197 * It's use is deprecated, drivers should use one of the more specific helpers
3198 * to directly create the property with all enumerations already attached.
3199 *
3200 * Returns:
3201 * Zero on success, error code on failure.
3202 */
Dave Airlief453ba02008-11-07 14:05:41 -08003203int drm_property_add_enum(struct drm_property *property, int index,
3204 uint64_t value, const char *name)
3205{
3206 struct drm_property_enum *prop_enum;
3207
Rob Clark49e27542012-05-17 02:23:26 -06003208 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3209 return -EINVAL;
3210
3211 /*
3212 * Bitmask enum properties have the additional constraint of values
3213 * from 0 to 63
3214 */
3215 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003216 return -EINVAL;
3217
3218 if (!list_empty(&property->enum_blob_list)) {
3219 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3220 if (prop_enum->value == value) {
3221 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3222 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3223 return 0;
3224 }
3225 }
3226 }
3227
3228 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3229 if (!prop_enum)
3230 return -ENOMEM;
3231
3232 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3233 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3234 prop_enum->value = value;
3235
3236 property->values[index] = value;
3237 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3238 return 0;
3239}
3240EXPORT_SYMBOL(drm_property_add_enum);
3241
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003242/**
3243 * drm_property_destroy - destroy a drm property
3244 * @dev: drm device
3245 * @property: property to destry
3246 *
3247 * This function frees a property including any attached resources like
3248 * enumeration values.
3249 */
Dave Airlief453ba02008-11-07 14:05:41 -08003250void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3251{
3252 struct drm_property_enum *prop_enum, *pt;
3253
3254 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3255 list_del(&prop_enum->head);
3256 kfree(prop_enum);
3257 }
3258
3259 if (property->num_values)
3260 kfree(property->values);
3261 drm_mode_object_put(dev, &property->base);
3262 list_del(&property->head);
3263 kfree(property);
3264}
3265EXPORT_SYMBOL(drm_property_destroy);
3266
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003267/**
3268 * drm_object_attach_property - attach a property to a modeset object
3269 * @obj: drm modeset object
3270 * @property: property to attach
3271 * @init_val: initial value of the property
3272 *
3273 * This attaches the given property to the modeset object with the given initial
3274 * value. Currently this function cannot fail since the properties are stored in
3275 * a statically sized array.
3276 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003277void drm_object_attach_property(struct drm_mode_object *obj,
3278 struct drm_property *property,
3279 uint64_t init_val)
3280{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003281 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003282
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003283 if (count == DRM_OBJECT_MAX_PROPERTY) {
3284 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3285 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3286 "you see this message on the same object type.\n",
3287 obj->type);
3288 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003289 }
3290
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003291 obj->properties->ids[count] = property->base.id;
3292 obj->properties->values[count] = init_val;
3293 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003294}
3295EXPORT_SYMBOL(drm_object_attach_property);
3296
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003297/**
3298 * drm_object_property_set_value - set the value of a property
3299 * @obj: drm mode object to set property value for
3300 * @property: property to set
3301 * @val: value the property should be set to
3302 *
3303 * This functions sets a given property on a given object. This function only
3304 * changes the software state of the property, it does not call into the
3305 * driver's ->set_property callback.
3306 *
3307 * Returns:
3308 * Zero on success, error code on failure.
3309 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003310int drm_object_property_set_value(struct drm_mode_object *obj,
3311 struct drm_property *property, uint64_t val)
3312{
3313 int i;
3314
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003315 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003316 if (obj->properties->ids[i] == property->base.id) {
3317 obj->properties->values[i] = val;
3318 return 0;
3319 }
3320 }
3321
3322 return -EINVAL;
3323}
3324EXPORT_SYMBOL(drm_object_property_set_value);
3325
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003326/**
3327 * drm_object_property_get_value - retrieve the value of a property
3328 * @obj: drm mode object to get property value from
3329 * @property: property to retrieve
3330 * @val: storage for the property value
3331 *
3332 * This function retrieves the softare state of the given property for the given
3333 * property. Since there is no driver callback to retrieve the current property
3334 * value this might be out of sync with the hardware, depending upon the driver
3335 * and property.
3336 *
3337 * Returns:
3338 * Zero on success, error code on failure.
3339 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003340int drm_object_property_get_value(struct drm_mode_object *obj,
3341 struct drm_property *property, uint64_t *val)
3342{
3343 int i;
3344
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003345 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003346 if (obj->properties->ids[i] == property->base.id) {
3347 *val = obj->properties->values[i];
3348 return 0;
3349 }
3350 }
3351
3352 return -EINVAL;
3353}
3354EXPORT_SYMBOL(drm_object_property_get_value);
3355
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003356/**
3357 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3358 * @dev: DRM device
3359 * @data: ioctl data
3360 * @file_priv: DRM file info
3361 *
3362 * This function retrieves the current value for an connectors's property.
3363 *
3364 * Called by the user via ioctl.
3365 *
3366 * Returns:
3367 * Zero on success, errno on failure.
3368 */
Dave Airlief453ba02008-11-07 14:05:41 -08003369int drm_mode_getproperty_ioctl(struct drm_device *dev,
3370 void *data, struct drm_file *file_priv)
3371{
3372 struct drm_mode_object *obj;
3373 struct drm_mode_get_property *out_resp = data;
3374 struct drm_property *property;
3375 int enum_count = 0;
3376 int blob_count = 0;
3377 int value_count = 0;
3378 int ret = 0, i;
3379 int copied;
3380 struct drm_property_enum *prop_enum;
3381 struct drm_mode_property_enum __user *enum_ptr;
3382 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003383 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003384 uint64_t __user *values_ptr;
3385 uint32_t __user *blob_length_ptr;
3386
Dave Airliefb3b06c2011-02-08 13:55:21 +10003387 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3388 return -EINVAL;
3389
Daniel Vetter84849902012-12-02 00:28:11 +01003390 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003391 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3392 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003393 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003394 goto done;
3395 }
3396 property = obj_to_property(obj);
3397
Rob Clark49e27542012-05-17 02:23:26 -06003398 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003399 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3400 enum_count++;
3401 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3402 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3403 blob_count++;
3404 }
3405
3406 value_count = property->num_values;
3407
3408 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3409 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3410 out_resp->flags = property->flags;
3411
3412 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003413 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003414 for (i = 0; i < value_count; i++) {
3415 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3416 ret = -EFAULT;
3417 goto done;
3418 }
3419 }
3420 }
3421 out_resp->count_values = value_count;
3422
Rob Clark49e27542012-05-17 02:23:26 -06003423 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003424 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3425 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003426 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003427 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3428
3429 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3430 ret = -EFAULT;
3431 goto done;
3432 }
3433
3434 if (copy_to_user(&enum_ptr[copied].name,
3435 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3436 ret = -EFAULT;
3437 goto done;
3438 }
3439 copied++;
3440 }
3441 }
3442 out_resp->count_enum_blobs = enum_count;
3443 }
3444
3445 if (property->flags & DRM_MODE_PROP_BLOB) {
3446 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3447 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003448 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3449 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003450
3451 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3452 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3453 ret = -EFAULT;
3454 goto done;
3455 }
3456
3457 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3458 ret = -EFAULT;
3459 goto done;
3460 }
3461
3462 copied++;
3463 }
3464 }
3465 out_resp->count_enum_blobs = blob_count;
3466 }
3467done:
Daniel Vetter84849902012-12-02 00:28:11 +01003468 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003469 return ret;
3470}
3471
3472static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3473 void *data)
3474{
3475 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003476 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003477
3478 if (!length || !data)
3479 return NULL;
3480
3481 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3482 if (!blob)
3483 return NULL;
3484
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003485 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3486 if (ret) {
3487 kfree(blob);
3488 return NULL;
3489 }
3490
Dave Airlief453ba02008-11-07 14:05:41 -08003491 blob->length = length;
3492
3493 memcpy(blob->data, data, length);
3494
Dave Airlief453ba02008-11-07 14:05:41 -08003495 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3496 return blob;
3497}
3498
3499static void drm_property_destroy_blob(struct drm_device *dev,
3500 struct drm_property_blob *blob)
3501{
3502 drm_mode_object_put(dev, &blob->base);
3503 list_del(&blob->head);
3504 kfree(blob);
3505}
3506
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003507/**
3508 * drm_mode_getblob_ioctl - get the contents of a blob property value
3509 * @dev: DRM device
3510 * @data: ioctl data
3511 * @file_priv: DRM file info
3512 *
3513 * This function retrieves the contents of a blob property. The value stored in
3514 * an object's blob property is just a normal modeset object id.
3515 *
3516 * Called by the user via ioctl.
3517 *
3518 * Returns:
3519 * Zero on success, errno on failure.
3520 */
Dave Airlief453ba02008-11-07 14:05:41 -08003521int drm_mode_getblob_ioctl(struct drm_device *dev,
3522 void *data, struct drm_file *file_priv)
3523{
3524 struct drm_mode_object *obj;
3525 struct drm_mode_get_blob *out_resp = data;
3526 struct drm_property_blob *blob;
3527 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003528 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003529
Dave Airliefb3b06c2011-02-08 13:55:21 +10003530 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3531 return -EINVAL;
3532
Daniel Vetter84849902012-12-02 00:28:11 +01003533 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003534 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3535 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003536 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003537 goto done;
3538 }
3539 blob = obj_to_blob(obj);
3540
3541 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003542 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003543 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3544 ret = -EFAULT;
3545 goto done;
3546 }
3547 }
3548 out_resp->length = blob->length;
3549
3550done:
Daniel Vetter84849902012-12-02 00:28:11 +01003551 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003552 return ret;
3553}
3554
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003555/**
3556 * drm_mode_connector_update_edid_property - update the edid property of a connector
3557 * @connector: drm connector
3558 * @edid: new value of the edid property
3559 *
3560 * This function creates a new blob modeset object and assigns its id to the
3561 * connector's edid property.
3562 *
3563 * Returns:
3564 * Zero on success, errno on failure.
3565 */
Dave Airlief453ba02008-11-07 14:05:41 -08003566int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3567 struct edid *edid)
3568{
3569 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003570 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003571
3572 if (connector->edid_blob_ptr)
3573 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3574
3575 /* Delete edid, when there is none. */
3576 if (!edid) {
3577 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003578 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003579 return ret;
3580 }
3581
Adam Jackson7466f4c2010-03-29 21:43:23 +00003582 size = EDID_LENGTH * (1 + edid->extensions);
3583 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3584 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003585 if (!connector->edid_blob_ptr)
3586 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003587
Rob Clark58495562012-10-11 20:50:56 -05003588 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003589 dev->mode_config.edid_property,
3590 connector->edid_blob_ptr->base.id);
3591
3592 return ret;
3593}
3594EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3595
Paulo Zanoni26a34812012-05-15 18:08:59 -03003596static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003597 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003598{
3599 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3600 return false;
3601 if (property->flags & DRM_MODE_PROP_RANGE) {
3602 if (value < property->values[0] || value > property->values[1])
3603 return false;
3604 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003605 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3606 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003607 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003608 for (i = 0; i < property->num_values; i++)
3609 valid_mask |= (1ULL << property->values[i]);
3610 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003611 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3612 /* Only the driver knows */
3613 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003614 } else {
3615 int i;
3616 for (i = 0; i < property->num_values; i++)
3617 if (property->values[i] == value)
3618 return true;
3619 return false;
3620 }
3621}
3622
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003623/**
3624 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3625 * @dev: DRM device
3626 * @data: ioctl data
3627 * @file_priv: DRM file info
3628 *
3629 * This function sets the current value for a connectors's property. It also
3630 * calls into a driver's ->set_property callback to update the hardware state
3631 *
3632 * Called by the user via ioctl.
3633 *
3634 * Returns:
3635 * Zero on success, errno on failure.
3636 */
Dave Airlief453ba02008-11-07 14:05:41 -08003637int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3638 void *data, struct drm_file *file_priv)
3639{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003640 struct drm_mode_connector_set_property *conn_set_prop = data;
3641 struct drm_mode_obj_set_property obj_set_prop = {
3642 .value = conn_set_prop->value,
3643 .prop_id = conn_set_prop->prop_id,
3644 .obj_id = conn_set_prop->connector_id,
3645 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3646 };
Dave Airlief453ba02008-11-07 14:05:41 -08003647
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003648 /* It does all the locking and checking we need */
3649 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003650}
3651
Paulo Zanonic5431882012-05-15 18:09:02 -03003652static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3653 struct drm_property *property,
3654 uint64_t value)
3655{
3656 int ret = -EINVAL;
3657 struct drm_connector *connector = obj_to_connector(obj);
3658
3659 /* Do DPMS ourselves */
3660 if (property == connector->dev->mode_config.dpms_property) {
3661 if (connector->funcs->dpms)
3662 (*connector->funcs->dpms)(connector, (int)value);
3663 ret = 0;
3664 } else if (connector->funcs->set_property)
3665 ret = connector->funcs->set_property(connector, property, value);
3666
3667 /* store the property value if successful */
3668 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003669 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003670 return ret;
3671}
3672
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003673static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3674 struct drm_property *property,
3675 uint64_t value)
3676{
3677 int ret = -EINVAL;
3678 struct drm_crtc *crtc = obj_to_crtc(obj);
3679
3680 if (crtc->funcs->set_property)
3681 ret = crtc->funcs->set_property(crtc, property, value);
3682 if (!ret)
3683 drm_object_property_set_value(obj, property, value);
3684
3685 return ret;
3686}
3687
Rob Clark4d939142012-05-17 02:23:27 -06003688static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3689 struct drm_property *property,
3690 uint64_t value)
3691{
3692 int ret = -EINVAL;
3693 struct drm_plane *plane = obj_to_plane(obj);
3694
3695 if (plane->funcs->set_property)
3696 ret = plane->funcs->set_property(plane, property, value);
3697 if (!ret)
3698 drm_object_property_set_value(obj, property, value);
3699
3700 return ret;
3701}
3702
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003703/**
3704 * drm_mode_getproperty_ioctl - get the current value of a object's property
3705 * @dev: DRM device
3706 * @data: ioctl data
3707 * @file_priv: DRM file info
3708 *
3709 * This function retrieves the current value for an object's property. Compared
3710 * to the connector specific ioctl this one is extended to also work on crtc and
3711 * plane objects.
3712 *
3713 * Called by the user via ioctl.
3714 *
3715 * Returns:
3716 * Zero on success, errno on failure.
3717 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003718int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3719 struct drm_file *file_priv)
3720{
3721 struct drm_mode_obj_get_properties *arg = data;
3722 struct drm_mode_object *obj;
3723 int ret = 0;
3724 int i;
3725 int copied = 0;
3726 int props_count = 0;
3727 uint32_t __user *props_ptr;
3728 uint64_t __user *prop_values_ptr;
3729
3730 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3731 return -EINVAL;
3732
Daniel Vetter84849902012-12-02 00:28:11 +01003733 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003734
3735 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3736 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003737 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003738 goto out;
3739 }
3740 if (!obj->properties) {
3741 ret = -EINVAL;
3742 goto out;
3743 }
3744
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003745 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003746
3747 /* This ioctl is called twice, once to determine how much space is
3748 * needed, and the 2nd time to fill it. */
3749 if ((arg->count_props >= props_count) && props_count) {
3750 copied = 0;
3751 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3752 prop_values_ptr = (uint64_t __user *)(unsigned long)
3753 (arg->prop_values_ptr);
3754 for (i = 0; i < props_count; i++) {
3755 if (put_user(obj->properties->ids[i],
3756 props_ptr + copied)) {
3757 ret = -EFAULT;
3758 goto out;
3759 }
3760 if (put_user(obj->properties->values[i],
3761 prop_values_ptr + copied)) {
3762 ret = -EFAULT;
3763 goto out;
3764 }
3765 copied++;
3766 }
3767 }
3768 arg->count_props = props_count;
3769out:
Daniel Vetter84849902012-12-02 00:28:11 +01003770 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003771 return ret;
3772}
3773
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003774/**
3775 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3776 * @dev: DRM device
3777 * @data: ioctl data
3778 * @file_priv: DRM file info
3779 *
3780 * This function sets the current value for an object's property. It also calls
3781 * into a driver's ->set_property callback to update the hardware state.
3782 * Compared to the connector specific ioctl this one is extended to also work on
3783 * crtc and plane objects.
3784 *
3785 * Called by the user via ioctl.
3786 *
3787 * Returns:
3788 * Zero on success, errno on failure.
3789 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003790int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3791 struct drm_file *file_priv)
3792{
3793 struct drm_mode_obj_set_property *arg = data;
3794 struct drm_mode_object *arg_obj;
3795 struct drm_mode_object *prop_obj;
3796 struct drm_property *property;
3797 int ret = -EINVAL;
3798 int i;
3799
3800 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3801 return -EINVAL;
3802
Daniel Vetter84849902012-12-02 00:28:11 +01003803 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003804
3805 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003806 if (!arg_obj) {
3807 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003808 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003809 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003810 if (!arg_obj->properties)
3811 goto out;
3812
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003813 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003814 if (arg_obj->properties->ids[i] == arg->prop_id)
3815 break;
3816
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003817 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003818 goto out;
3819
3820 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3821 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003822 if (!prop_obj) {
3823 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003824 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003825 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003826 property = obj_to_property(prop_obj);
3827
3828 if (!drm_property_change_is_valid(property, arg->value))
3829 goto out;
3830
3831 switch (arg_obj->type) {
3832 case DRM_MODE_OBJECT_CONNECTOR:
3833 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3834 arg->value);
3835 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003836 case DRM_MODE_OBJECT_CRTC:
3837 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3838 break;
Rob Clark4d939142012-05-17 02:23:27 -06003839 case DRM_MODE_OBJECT_PLANE:
3840 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3841 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003842 }
3843
3844out:
Daniel Vetter84849902012-12-02 00:28:11 +01003845 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003846 return ret;
3847}
3848
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003849/**
3850 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3851 * @connector: connector to attach
3852 * @encoder: encoder to attach @connector to
3853 *
3854 * This function links up a connector to an encoder. Note that the routing
3855 * restrictions between encoders and crtcs are exposed to userspace through the
3856 * possible_clones and possible_crtcs bitmasks.
3857 *
3858 * Returns:
3859 * Zero on success, errno on failure.
3860 */
Dave Airlief453ba02008-11-07 14:05:41 -08003861int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3862 struct drm_encoder *encoder)
3863{
3864 int i;
3865
3866 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3867 if (connector->encoder_ids[i] == 0) {
3868 connector->encoder_ids[i] = encoder->base.id;
3869 return 0;
3870 }
3871 }
3872 return -ENOMEM;
3873}
3874EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3875
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003876/**
3877 * drm_mode_crtc_set_gamma_size - set the gamma table size
3878 * @crtc: CRTC to set the gamma table size for
3879 * @gamma_size: size of the gamma table
3880 *
3881 * Drivers which support gamma tables should set this to the supported gamma
3882 * table size when initializing the CRTC. Currently the drm core only supports a
3883 * fixed gamma table size.
3884 *
3885 * Returns:
3886 * Zero on success, errno on failure.
3887 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003888int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003889 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08003890{
3891 crtc->gamma_size = gamma_size;
3892
3893 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3894 if (!crtc->gamma_store) {
3895 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003896 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003897 }
3898
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003899 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003900}
3901EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3902
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003903/**
3904 * drm_mode_gamma_set_ioctl - set the gamma table
3905 * @dev: DRM device
3906 * @data: ioctl data
3907 * @file_priv: DRM file info
3908 *
3909 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3910 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3911 *
3912 * Called by the user via ioctl.
3913 *
3914 * Returns:
3915 * Zero on success, errno on failure.
3916 */
Dave Airlief453ba02008-11-07 14:05:41 -08003917int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3918 void *data, struct drm_file *file_priv)
3919{
3920 struct drm_mode_crtc_lut *crtc_lut = data;
3921 struct drm_mode_object *obj;
3922 struct drm_crtc *crtc;
3923 void *r_base, *g_base, *b_base;
3924 int size;
3925 int ret = 0;
3926
Dave Airliefb3b06c2011-02-08 13:55:21 +10003927 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3928 return -EINVAL;
3929
Daniel Vetter84849902012-12-02 00:28:11 +01003930 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003931 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3932 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003933 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003934 goto out;
3935 }
3936 crtc = obj_to_crtc(obj);
3937
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003938 if (crtc->funcs->gamma_set == NULL) {
3939 ret = -ENOSYS;
3940 goto out;
3941 }
3942
Dave Airlief453ba02008-11-07 14:05:41 -08003943 /* memcpy into gamma store */
3944 if (crtc_lut->gamma_size != crtc->gamma_size) {
3945 ret = -EINVAL;
3946 goto out;
3947 }
3948
3949 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3950 r_base = crtc->gamma_store;
3951 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3952 ret = -EFAULT;
3953 goto out;
3954 }
3955
3956 g_base = r_base + size;
3957 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3958 ret = -EFAULT;
3959 goto out;
3960 }
3961
3962 b_base = g_base + size;
3963 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3964 ret = -EFAULT;
3965 goto out;
3966 }
3967
James Simmons72034252010-08-03 01:33:19 +01003968 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003969
3970out:
Daniel Vetter84849902012-12-02 00:28:11 +01003971 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003972 return ret;
3973
3974}
3975
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003976/**
3977 * drm_mode_gamma_get_ioctl - get the gamma table
3978 * @dev: DRM device
3979 * @data: ioctl data
3980 * @file_priv: DRM file info
3981 *
3982 * Copy the current gamma table into the storage provided. This also provides
3983 * the gamma table size the driver expects, which can be used to size the
3984 * allocated storage.
3985 *
3986 * Called by the user via ioctl.
3987 *
3988 * Returns:
3989 * Zero on success, errno on failure.
3990 */
Dave Airlief453ba02008-11-07 14:05:41 -08003991int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3992 void *data, struct drm_file *file_priv)
3993{
3994 struct drm_mode_crtc_lut *crtc_lut = data;
3995 struct drm_mode_object *obj;
3996 struct drm_crtc *crtc;
3997 void *r_base, *g_base, *b_base;
3998 int size;
3999 int ret = 0;
4000
Dave Airliefb3b06c2011-02-08 13:55:21 +10004001 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4002 return -EINVAL;
4003
Daniel Vetter84849902012-12-02 00:28:11 +01004004 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004005 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4006 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004007 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004008 goto out;
4009 }
4010 crtc = obj_to_crtc(obj);
4011
4012 /* memcpy into gamma store */
4013 if (crtc_lut->gamma_size != crtc->gamma_size) {
4014 ret = -EINVAL;
4015 goto out;
4016 }
4017
4018 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4019 r_base = crtc->gamma_store;
4020 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4021 ret = -EFAULT;
4022 goto out;
4023 }
4024
4025 g_base = r_base + size;
4026 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4027 ret = -EFAULT;
4028 goto out;
4029 }
4030
4031 b_base = g_base + size;
4032 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4033 ret = -EFAULT;
4034 goto out;
4035 }
4036out:
Daniel Vetter84849902012-12-02 00:28:11 +01004037 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004038 return ret;
4039}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004040
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004041/**
4042 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4043 * @dev: DRM device
4044 * @data: ioctl data
4045 * @file_priv: DRM file info
4046 *
4047 * This schedules an asynchronous update on a given CRTC, called page flip.
4048 * Optionally a drm event is generated to signal the completion of the event.
4049 * Generic drivers cannot assume that a pageflip with changed framebuffer
4050 * properties (including driver specific metadata like tiling layout) will work,
4051 * but some drivers support e.g. pixel format changes through the pageflip
4052 * ioctl.
4053 *
4054 * Called by the user via ioctl.
4055 *
4056 * Returns:
4057 * Zero on success, errno on failure.
4058 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004059int drm_mode_page_flip_ioctl(struct drm_device *dev,
4060 void *data, struct drm_file *file_priv)
4061{
4062 struct drm_mode_crtc_page_flip *page_flip = data;
4063 struct drm_mode_object *obj;
4064 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004065 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004066 struct drm_pending_vblank_event *e = NULL;
4067 unsigned long flags;
4068 int ret = -EINVAL;
4069
4070 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4071 page_flip->reserved != 0)
4072 return -EINVAL;
4073
Keith Packard62f21042013-07-22 18:50:00 -07004074 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4075 return -EINVAL;
4076
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004077 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4078 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004079 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004080 crtc = obj_to_crtc(obj);
4081
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004082 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01004083 if (crtc->fb == NULL) {
4084 /* The framebuffer is currently unbound, presumably
4085 * due to a hotplug event, that userspace has not
4086 * yet discovered.
4087 */
4088 ret = -EBUSY;
4089 goto out;
4090 }
4091
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004092 if (crtc->funcs->page_flip == NULL)
4093 goto out;
4094
Daniel Vetter786b99e2012-12-02 21:53:40 +01004095 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004096 if (!fb) {
4097 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004098 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004099 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004100
Damien Lespiauc11e9282013-09-25 16:45:30 +01004101 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4102 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004103 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004104
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004105 if (crtc->fb->pixel_format != fb->pixel_format) {
4106 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4107 ret = -EINVAL;
4108 goto out;
4109 }
4110
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004111 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4112 ret = -ENOMEM;
4113 spin_lock_irqsave(&dev->event_lock, flags);
4114 if (file_priv->event_space < sizeof e->event) {
4115 spin_unlock_irqrestore(&dev->event_lock, flags);
4116 goto out;
4117 }
4118 file_priv->event_space -= sizeof e->event;
4119 spin_unlock_irqrestore(&dev->event_lock, flags);
4120
4121 e = kzalloc(sizeof *e, GFP_KERNEL);
4122 if (e == NULL) {
4123 spin_lock_irqsave(&dev->event_lock, flags);
4124 file_priv->event_space += sizeof e->event;
4125 spin_unlock_irqrestore(&dev->event_lock, flags);
4126 goto out;
4127 }
4128
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004129 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004130 e->event.base.length = sizeof e->event;
4131 e->event.user_data = page_flip->user_data;
4132 e->base.event = &e->event.base;
4133 e->base.file_priv = file_priv;
4134 e->base.destroy =
4135 (void (*) (struct drm_pending_event *)) kfree;
4136 }
4137
Daniel Vetterb0d12322012-12-11 01:07:12 +01004138 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004139 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004140 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004141 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4142 spin_lock_irqsave(&dev->event_lock, flags);
4143 file_priv->event_space += sizeof e->event;
4144 spin_unlock_irqrestore(&dev->event_lock, flags);
4145 kfree(e);
4146 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004147 /* Keep the old fb, don't unref it. */
4148 old_fb = NULL;
4149 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004150 /*
4151 * Warn if the driver hasn't properly updated the crtc->fb
4152 * field to reflect that the new framebuffer is now used.
4153 * Failing to do so will screw with the reference counting
4154 * on framebuffers.
4155 */
4156 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004157 /* Unref only the old framebuffer. */
4158 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004159 }
4160
4161out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004162 if (fb)
4163 drm_framebuffer_unreference(fb);
4164 if (old_fb)
4165 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004166 mutex_unlock(&crtc->mutex);
4167
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004168 return ret;
4169}
Chris Wilsoneb033552011-01-24 15:11:08 +00004170
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004171/**
4172 * drm_mode_config_reset - call ->reset callbacks
4173 * @dev: drm device
4174 *
4175 * This functions calls all the crtc's, encoder's and connector's ->reset
4176 * callback. Drivers can use this in e.g. their driver load or resume code to
4177 * reset hardware and software state.
4178 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004179void drm_mode_config_reset(struct drm_device *dev)
4180{
4181 struct drm_crtc *crtc;
4182 struct drm_encoder *encoder;
4183 struct drm_connector *connector;
4184
4185 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4186 if (crtc->funcs->reset)
4187 crtc->funcs->reset(crtc);
4188
4189 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4190 if (encoder->funcs->reset)
4191 encoder->funcs->reset(encoder);
4192
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004193 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4194 connector->status = connector_status_unknown;
4195
Chris Wilsoneb033552011-01-24 15:11:08 +00004196 if (connector->funcs->reset)
4197 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004198 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004199}
4200EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004201
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004202/**
4203 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4204 * @dev: DRM device
4205 * @data: ioctl data
4206 * @file_priv: DRM file info
4207 *
4208 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4209 * TTM or something else entirely) and returns the resulting buffer handle. This
4210 * handle can then be wrapped up into a framebuffer modeset object.
4211 *
4212 * Note that userspace is not allowed to use such objects for render
4213 * acceleration - drivers must create their own private ioctls for such a use
4214 * case.
4215 *
4216 * Called by the user via ioctl.
4217 *
4218 * Returns:
4219 * Zero on success, errno on failure.
4220 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004221int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4222 void *data, struct drm_file *file_priv)
4223{
4224 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004225 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004226
4227 if (!dev->driver->dumb_create)
4228 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004229 if (!args->width || !args->height || !args->bpp)
4230 return -EINVAL;
4231
4232 /* overflow checks for 32bit size calculations */
4233 cpp = DIV_ROUND_UP(args->bpp, 8);
4234 if (cpp > 0xffffffffU / args->width)
4235 return -EINVAL;
4236 stride = cpp * args->width;
4237 if (args->height > 0xffffffffU / stride)
4238 return -EINVAL;
4239
4240 /* test for wrap-around */
4241 size = args->height * stride;
4242 if (PAGE_ALIGN(size) == 0)
4243 return -EINVAL;
4244
Dave Airlieff72145b2011-02-07 12:16:14 +10004245 return dev->driver->dumb_create(file_priv, dev, args);
4246}
4247
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004248/**
4249 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4250 * @dev: DRM device
4251 * @data: ioctl data
4252 * @file_priv: DRM file info
4253 *
4254 * Allocate an offset in the drm device node's address space to be able to
4255 * memory map a dumb buffer.
4256 *
4257 * Called by the user via ioctl.
4258 *
4259 * Returns:
4260 * Zero on success, errno on failure.
4261 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004262int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4263 void *data, struct drm_file *file_priv)
4264{
4265 struct drm_mode_map_dumb *args = data;
4266
4267 /* call driver ioctl to get mmap offset */
4268 if (!dev->driver->dumb_map_offset)
4269 return -ENOSYS;
4270
4271 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4272}
4273
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004274/**
4275 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4276 * @dev: DRM device
4277 * @data: ioctl data
4278 * @file_priv: DRM file info
4279 *
4280 * This destroys the userspace handle for the given dumb backing storage buffer.
4281 * Since buffer objects must be reference counted in the kernel a buffer object
4282 * won't be immediately freed if a framebuffer modeset object still uses it.
4283 *
4284 * Called by the user via ioctl.
4285 *
4286 * Returns:
4287 * Zero on success, errno on failure.
4288 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004289int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4290 void *data, struct drm_file *file_priv)
4291{
4292 struct drm_mode_destroy_dumb *args = data;
4293
4294 if (!dev->driver->dumb_destroy)
4295 return -ENOSYS;
4296
4297 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4298}
Dave Airlie248dbc22011-11-29 20:02:54 +00004299
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004300/**
4301 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4302 * @format: pixel format (DRM_FORMAT_*)
4303 * @depth: storage for the depth value
4304 * @bpp: storage for the bpp value
4305 *
4306 * This only supports RGB formats here for compat with code that doesn't use
4307 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004308 */
4309void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4310 int *bpp)
4311{
4312 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004313 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004314 case DRM_FORMAT_RGB332:
4315 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004316 *depth = 8;
4317 *bpp = 8;
4318 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004319 case DRM_FORMAT_XRGB1555:
4320 case DRM_FORMAT_XBGR1555:
4321 case DRM_FORMAT_RGBX5551:
4322 case DRM_FORMAT_BGRX5551:
4323 case DRM_FORMAT_ARGB1555:
4324 case DRM_FORMAT_ABGR1555:
4325 case DRM_FORMAT_RGBA5551:
4326 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004327 *depth = 15;
4328 *bpp = 16;
4329 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004330 case DRM_FORMAT_RGB565:
4331 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004332 *depth = 16;
4333 *bpp = 16;
4334 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004335 case DRM_FORMAT_RGB888:
4336 case DRM_FORMAT_BGR888:
4337 *depth = 24;
4338 *bpp = 24;
4339 break;
4340 case DRM_FORMAT_XRGB8888:
4341 case DRM_FORMAT_XBGR8888:
4342 case DRM_FORMAT_RGBX8888:
4343 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004344 *depth = 24;
4345 *bpp = 32;
4346 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004347 case DRM_FORMAT_XRGB2101010:
4348 case DRM_FORMAT_XBGR2101010:
4349 case DRM_FORMAT_RGBX1010102:
4350 case DRM_FORMAT_BGRX1010102:
4351 case DRM_FORMAT_ARGB2101010:
4352 case DRM_FORMAT_ABGR2101010:
4353 case DRM_FORMAT_RGBA1010102:
4354 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004355 *depth = 30;
4356 *bpp = 32;
4357 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004358 case DRM_FORMAT_ARGB8888:
4359 case DRM_FORMAT_ABGR8888:
4360 case DRM_FORMAT_RGBA8888:
4361 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004362 *depth = 32;
4363 *bpp = 32;
4364 break;
4365 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004366 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4367 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004368 *depth = 0;
4369 *bpp = 0;
4370 break;
4371 }
4372}
4373EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004374
4375/**
4376 * drm_format_num_planes - get the number of planes for format
4377 * @format: pixel format (DRM_FORMAT_*)
4378 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004379 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004380 * The number of planes used by the specified pixel format.
4381 */
4382int drm_format_num_planes(uint32_t format)
4383{
4384 switch (format) {
4385 case DRM_FORMAT_YUV410:
4386 case DRM_FORMAT_YVU410:
4387 case DRM_FORMAT_YUV411:
4388 case DRM_FORMAT_YVU411:
4389 case DRM_FORMAT_YUV420:
4390 case DRM_FORMAT_YVU420:
4391 case DRM_FORMAT_YUV422:
4392 case DRM_FORMAT_YVU422:
4393 case DRM_FORMAT_YUV444:
4394 case DRM_FORMAT_YVU444:
4395 return 3;
4396 case DRM_FORMAT_NV12:
4397 case DRM_FORMAT_NV21:
4398 case DRM_FORMAT_NV16:
4399 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004400 case DRM_FORMAT_NV24:
4401 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004402 return 2;
4403 default:
4404 return 1;
4405 }
4406}
4407EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004408
4409/**
4410 * drm_format_plane_cpp - determine the bytes per pixel value
4411 * @format: pixel format (DRM_FORMAT_*)
4412 * @plane: plane index
4413 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004414 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004415 * The bytes per pixel value for the specified plane.
4416 */
4417int drm_format_plane_cpp(uint32_t format, int plane)
4418{
4419 unsigned int depth;
4420 int bpp;
4421
4422 if (plane >= drm_format_num_planes(format))
4423 return 0;
4424
4425 switch (format) {
4426 case DRM_FORMAT_YUYV:
4427 case DRM_FORMAT_YVYU:
4428 case DRM_FORMAT_UYVY:
4429 case DRM_FORMAT_VYUY:
4430 return 2;
4431 case DRM_FORMAT_NV12:
4432 case DRM_FORMAT_NV21:
4433 case DRM_FORMAT_NV16:
4434 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004435 case DRM_FORMAT_NV24:
4436 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004437 return plane ? 2 : 1;
4438 case DRM_FORMAT_YUV410:
4439 case DRM_FORMAT_YVU410:
4440 case DRM_FORMAT_YUV411:
4441 case DRM_FORMAT_YVU411:
4442 case DRM_FORMAT_YUV420:
4443 case DRM_FORMAT_YVU420:
4444 case DRM_FORMAT_YUV422:
4445 case DRM_FORMAT_YVU422:
4446 case DRM_FORMAT_YUV444:
4447 case DRM_FORMAT_YVU444:
4448 return 1;
4449 default:
4450 drm_fb_get_bpp_depth(format, &depth, &bpp);
4451 return bpp >> 3;
4452 }
4453}
4454EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004455
4456/**
4457 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4458 * @format: pixel format (DRM_FORMAT_*)
4459 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004460 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004461 * The horizontal chroma subsampling factor for the
4462 * specified pixel format.
4463 */
4464int drm_format_horz_chroma_subsampling(uint32_t format)
4465{
4466 switch (format) {
4467 case DRM_FORMAT_YUV411:
4468 case DRM_FORMAT_YVU411:
4469 case DRM_FORMAT_YUV410:
4470 case DRM_FORMAT_YVU410:
4471 return 4;
4472 case DRM_FORMAT_YUYV:
4473 case DRM_FORMAT_YVYU:
4474 case DRM_FORMAT_UYVY:
4475 case DRM_FORMAT_VYUY:
4476 case DRM_FORMAT_NV12:
4477 case DRM_FORMAT_NV21:
4478 case DRM_FORMAT_NV16:
4479 case DRM_FORMAT_NV61:
4480 case DRM_FORMAT_YUV422:
4481 case DRM_FORMAT_YVU422:
4482 case DRM_FORMAT_YUV420:
4483 case DRM_FORMAT_YVU420:
4484 return 2;
4485 default:
4486 return 1;
4487 }
4488}
4489EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4490
4491/**
4492 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4493 * @format: pixel format (DRM_FORMAT_*)
4494 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004495 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004496 * The vertical chroma subsampling factor for the
4497 * specified pixel format.
4498 */
4499int drm_format_vert_chroma_subsampling(uint32_t format)
4500{
4501 switch (format) {
4502 case DRM_FORMAT_YUV410:
4503 case DRM_FORMAT_YVU410:
4504 return 4;
4505 case DRM_FORMAT_YUV420:
4506 case DRM_FORMAT_YVU420:
4507 case DRM_FORMAT_NV12:
4508 case DRM_FORMAT_NV21:
4509 return 2;
4510 default:
4511 return 1;
4512 }
4513}
4514EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004515
4516/**
4517 * drm_mode_config_init - initialize DRM mode_configuration structure
4518 * @dev: DRM device
4519 *
4520 * Initialize @dev's mode_config structure, used for tracking the graphics
4521 * configuration of @dev.
4522 *
4523 * Since this initializes the modeset locks, no locking is possible. Which is no
4524 * problem, since this should happen single threaded at init time. It is the
4525 * driver's problem to ensure this guarantee.
4526 *
4527 */
4528void drm_mode_config_init(struct drm_device *dev)
4529{
4530 mutex_init(&dev->mode_config.mutex);
4531 mutex_init(&dev->mode_config.idr_mutex);
4532 mutex_init(&dev->mode_config.fb_lock);
4533 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4534 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4535 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004536 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004537 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4538 INIT_LIST_HEAD(&dev->mode_config.property_list);
4539 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4540 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4541 idr_init(&dev->mode_config.crtc_idr);
4542
4543 drm_modeset_lock_all(dev);
4544 drm_mode_create_standard_connector_properties(dev);
4545 drm_modeset_unlock_all(dev);
4546
4547 /* Just to be sure */
4548 dev->mode_config.num_fb = 0;
4549 dev->mode_config.num_connector = 0;
4550 dev->mode_config.num_crtc = 0;
4551 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004552 dev->mode_config.num_overlay_plane = 0;
4553 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004554}
4555EXPORT_SYMBOL(drm_mode_config_init);
4556
4557/**
4558 * drm_mode_config_cleanup - free up DRM mode_config info
4559 * @dev: DRM device
4560 *
4561 * Free up all the connectors and CRTCs associated with this DRM device, then
4562 * free up the framebuffers and associated buffer objects.
4563 *
4564 * Note that since this /should/ happen single-threaded at driver/device
4565 * teardown time, no locking is required. It's the driver's job to ensure that
4566 * this guarantee actually holds true.
4567 *
4568 * FIXME: cleanup any dangling user buffer objects too
4569 */
4570void drm_mode_config_cleanup(struct drm_device *dev)
4571{
4572 struct drm_connector *connector, *ot;
4573 struct drm_crtc *crtc, *ct;
4574 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004575 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004576 struct drm_framebuffer *fb, *fbt;
4577 struct drm_property *property, *pt;
4578 struct drm_property_blob *blob, *bt;
4579 struct drm_plane *plane, *plt;
4580
4581 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4582 head) {
4583 encoder->funcs->destroy(encoder);
4584 }
4585
Sean Paul3b336ec2013-08-14 16:47:37 -04004586 list_for_each_entry_safe(bridge, brt,
4587 &dev->mode_config.bridge_list, head) {
4588 bridge->funcs->destroy(bridge);
4589 }
4590
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004591 list_for_each_entry_safe(connector, ot,
4592 &dev->mode_config.connector_list, head) {
4593 connector->funcs->destroy(connector);
4594 }
4595
4596 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4597 head) {
4598 drm_property_destroy(dev, property);
4599 }
4600
4601 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4602 head) {
4603 drm_property_destroy_blob(dev, blob);
4604 }
4605
4606 /*
4607 * Single-threaded teardown context, so it's not required to grab the
4608 * fb_lock to protect against concurrent fb_list access. Contrary, it
4609 * would actually deadlock with the drm_framebuffer_cleanup function.
4610 *
4611 * Also, if there are any framebuffers left, that's a driver leak now,
4612 * so politely WARN about this.
4613 */
4614 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4615 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4616 drm_framebuffer_remove(fb);
4617 }
4618
4619 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4620 head) {
4621 plane->funcs->destroy(plane);
4622 }
4623
4624 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4625 crtc->funcs->destroy(crtc);
4626 }
4627
4628 idr_destroy(&dev->mode_config.crtc_idr);
4629}
4630EXPORT_SYMBOL(drm_mode_config_cleanup);