blob: 91d03e3acefba4129e576683ee450c99f191545d [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;
1047
Rob Clark0a7eb242011-12-13 20:19:36 -06001048 /* private planes are not exposed to userspace, but depending on
1049 * display hardware, might be convenient to allow sharing programming
1050 * for the scanout engine with the crtc implementation.
1051 */
1052 if (!priv) {
1053 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1054 dev->mode_config.num_plane++;
1055 } else {
1056 INIT_LIST_HEAD(&plane->head);
1057 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001058
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001059 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001060 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001061
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001062 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001063}
1064EXPORT_SYMBOL(drm_plane_init);
1065
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001066/**
1067 * drm_plane_cleanup - Clean up the core plane usage
1068 * @plane: plane to cleanup
1069 *
1070 * This function cleans up @plane and removes it from the DRM mode setting
1071 * core. Note that the function does *not* free the plane structure itself,
1072 * this is the responsibility of the caller.
1073 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001074void drm_plane_cleanup(struct drm_plane *plane)
1075{
1076 struct drm_device *dev = plane->dev;
1077
Daniel Vetter84849902012-12-02 00:28:11 +01001078 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001079 kfree(plane->format_types);
1080 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -06001081 /* if not added to a list, it must be a private plane */
1082 if (!list_empty(&plane->head)) {
1083 list_del(&plane->head);
1084 dev->mode_config.num_plane--;
1085 }
Daniel Vetter84849902012-12-02 00:28:11 +01001086 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001087}
1088EXPORT_SYMBOL(drm_plane_cleanup);
1089
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001090/**
1091 * drm_plane_force_disable - Forcibly disable a plane
1092 * @plane: plane to disable
1093 *
1094 * Forces the plane to be disabled.
1095 *
1096 * Used when the plane's current framebuffer is destroyed,
1097 * and when restoring fbdev mode.
1098 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001099void drm_plane_force_disable(struct drm_plane *plane)
1100{
1101 int ret;
1102
1103 if (!plane->fb)
1104 return;
1105
1106 ret = plane->funcs->disable_plane(plane);
1107 if (ret)
1108 DRM_ERROR("failed to disable plane with busy fb\n");
1109 /* disconnect the plane from the fb and crtc: */
1110 __drm_framebuffer_unreference(plane->fb);
1111 plane->fb = NULL;
1112 plane->crtc = NULL;
1113}
1114EXPORT_SYMBOL(drm_plane_force_disable);
1115
Dave Airlief453ba02008-11-07 14:05:41 -08001116static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1117{
1118 struct drm_property *edid;
1119 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001120
1121 /*
1122 * Standard properties (apply to all connectors)
1123 */
1124 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1125 DRM_MODE_PROP_IMMUTABLE,
1126 "EDID", 0);
1127 dev->mode_config.edid_property = edid;
1128
Sascha Hauer4a67d392012-02-06 10:58:17 +01001129 dpms = drm_property_create_enum(dev, 0,
1130 "DPMS", drm_dpms_enum_list,
1131 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001132 dev->mode_config.dpms_property = dpms;
1133
1134 return 0;
1135}
1136
1137/**
1138 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1139 * @dev: DRM device
1140 *
1141 * Called by a driver the first time a DVI-I connector is made.
1142 */
1143int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1144{
1145 struct drm_property *dvi_i_selector;
1146 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001147
1148 if (dev->mode_config.dvi_i_select_subconnector_property)
1149 return 0;
1150
1151 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001152 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001153 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001154 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001155 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001156 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1157
Sascha Hauer4a67d392012-02-06 10:58:17 +01001158 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001159 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001160 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001161 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001162 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1163
1164 return 0;
1165}
1166EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1167
1168/**
1169 * drm_create_tv_properties - create TV specific connector properties
1170 * @dev: DRM device
1171 * @num_modes: number of different TV formats (modes) supported
1172 * @modes: array of pointers to strings containing name of each format
1173 *
1174 * Called by a driver's TV initialization routine, this function creates
1175 * the TV specific connector properties for a given device. Caller is
1176 * responsible for allocating a list of format names and passing them to
1177 * this routine.
1178 */
1179int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1180 char *modes[])
1181{
1182 struct drm_property *tv_selector;
1183 struct drm_property *tv_subconnector;
1184 int i;
1185
1186 if (dev->mode_config.tv_select_subconnector_property)
1187 return 0;
1188
1189 /*
1190 * Basic connector properties
1191 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001192 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001193 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001194 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001195 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001196 dev->mode_config.tv_select_subconnector_property = tv_selector;
1197
1198 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001199 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1200 "subconnector",
1201 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001202 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001203 dev->mode_config.tv_subconnector_property = tv_subconnector;
1204
1205 /*
1206 * Other, TV specific properties: margins & TV modes.
1207 */
1208 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001209 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001210
1211 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001212 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001213
1214 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001215 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001216
1217 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001218 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001219
1220 dev->mode_config.tv_mode_property =
1221 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1222 "mode", num_modes);
1223 for (i = 0; i < num_modes; i++)
1224 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1225 i, modes[i]);
1226
Francisco Jerezb6b79022009-08-02 04:19:20 +02001227 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001228 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001229
1230 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001231 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001232
1233 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001234 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001235
Francisco Jereza75f0232009-08-12 02:30:10 +02001236 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001237 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001238
1239 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001240 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001241
1242 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001243 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001244
Dave Airlief453ba02008-11-07 14:05:41 -08001245 return 0;
1246}
1247EXPORT_SYMBOL(drm_mode_create_tv_properties);
1248
1249/**
1250 * drm_mode_create_scaling_mode_property - create scaling mode property
1251 * @dev: DRM device
1252 *
1253 * Called by a driver the first time it's needed, must be attached to desired
1254 * connectors.
1255 */
1256int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1257{
1258 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001259
1260 if (dev->mode_config.scaling_mode_property)
1261 return 0;
1262
1263 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001264 drm_property_create_enum(dev, 0, "scaling mode",
1265 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001266 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001267
1268 dev->mode_config.scaling_mode_property = scaling_mode;
1269
1270 return 0;
1271}
1272EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1273
1274/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001275 * drm_mode_create_dirty_property - create dirty property
1276 * @dev: DRM device
1277 *
1278 * Called by a driver the first time it's needed, must be attached to desired
1279 * connectors.
1280 */
1281int drm_mode_create_dirty_info_property(struct drm_device *dev)
1282{
1283 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001284
1285 if (dev->mode_config.dirty_info_property)
1286 return 0;
1287
1288 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001289 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001290 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001291 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001292 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001293 dev->mode_config.dirty_info_property = dirty_info;
1294
1295 return 0;
1296}
1297EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1298
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001299static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001300{
1301 uint32_t total_objects = 0;
1302
1303 total_objects += dev->mode_config.num_crtc;
1304 total_objects += dev->mode_config.num_connector;
1305 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001306 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001307
Dave Airlief453ba02008-11-07 14:05:41 -08001308 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1309 if (!group->id_list)
1310 return -ENOMEM;
1311
1312 group->num_crtcs = 0;
1313 group->num_connectors = 0;
1314 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001315 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001316 return 0;
1317}
1318
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001319/*
1320 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1321 * the drm core's responsibility to set up mode control groups.
1322 */
Dave Airlief453ba02008-11-07 14:05:41 -08001323int drm_mode_group_init_legacy_group(struct drm_device *dev,
1324 struct drm_mode_group *group)
1325{
1326 struct drm_crtc *crtc;
1327 struct drm_encoder *encoder;
1328 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001329 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001330 int ret;
1331
1332 if ((ret = drm_mode_group_init(dev, group)))
1333 return ret;
1334
1335 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1336 group->id_list[group->num_crtcs++] = crtc->base.id;
1337
1338 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1339 group->id_list[group->num_crtcs + group->num_encoders++] =
1340 encoder->base.id;
1341
1342 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1343 group->id_list[group->num_crtcs + group->num_encoders +
1344 group->num_connectors++] = connector->base.id;
1345
Sean Paul3b336ec2013-08-14 16:47:37 -04001346 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1347 group->id_list[group->num_crtcs + group->num_encoders +
1348 group->num_connectors + group->num_bridges++] =
1349 bridge->base.id;
1350
Dave Airlief453ba02008-11-07 14:05:41 -08001351 return 0;
1352}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001353EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001354
1355/**
Dave Airlief453ba02008-11-07 14:05:41 -08001356 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1357 * @out: drm_mode_modeinfo struct to return to the user
1358 * @in: drm_display_mode to use
1359 *
Dave Airlief453ba02008-11-07 14:05:41 -08001360 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1361 * the user.
1362 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001363static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1364 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001365{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001366 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1367 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1368 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1369 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1370 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1371 "timing values too large for mode info\n");
1372
Dave Airlief453ba02008-11-07 14:05:41 -08001373 out->clock = in->clock;
1374 out->hdisplay = in->hdisplay;
1375 out->hsync_start = in->hsync_start;
1376 out->hsync_end = in->hsync_end;
1377 out->htotal = in->htotal;
1378 out->hskew = in->hskew;
1379 out->vdisplay = in->vdisplay;
1380 out->vsync_start = in->vsync_start;
1381 out->vsync_end = in->vsync_end;
1382 out->vtotal = in->vtotal;
1383 out->vscan = in->vscan;
1384 out->vrefresh = in->vrefresh;
1385 out->flags = in->flags;
1386 out->type = in->type;
1387 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1388 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1389}
1390
1391/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001392 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001393 * @out: drm_display_mode to return to the user
1394 * @in: drm_mode_modeinfo to use
1395 *
Dave Airlief453ba02008-11-07 14:05:41 -08001396 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1397 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001398 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001399 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001400 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001401 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001402static int drm_crtc_convert_umode(struct drm_display_mode *out,
1403 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001404{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001405 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1406 return -ERANGE;
1407
Damien Lespiau5848ad42013-09-27 12:11:50 +01001408 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1409 return -EINVAL;
1410
Dave Airlief453ba02008-11-07 14:05:41 -08001411 out->clock = in->clock;
1412 out->hdisplay = in->hdisplay;
1413 out->hsync_start = in->hsync_start;
1414 out->hsync_end = in->hsync_end;
1415 out->htotal = in->htotal;
1416 out->hskew = in->hskew;
1417 out->vdisplay = in->vdisplay;
1418 out->vsync_start = in->vsync_start;
1419 out->vsync_end = in->vsync_end;
1420 out->vtotal = in->vtotal;
1421 out->vscan = in->vscan;
1422 out->vrefresh = in->vrefresh;
1423 out->flags = in->flags;
1424 out->type = in->type;
1425 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1426 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001427
1428 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001429}
1430
1431/**
1432 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001433 * @dev: drm device for the ioctl
1434 * @data: data pointer for the ioctl
1435 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001436 *
Dave Airlief453ba02008-11-07 14:05:41 -08001437 * Construct a set of configuration description structures and return
1438 * them to the user, including CRTC, connector and framebuffer configuration.
1439 *
1440 * Called by the user via ioctl.
1441 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001442 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001443 * Zero on success, errno on failure.
1444 */
1445int drm_mode_getresources(struct drm_device *dev, void *data,
1446 struct drm_file *file_priv)
1447{
1448 struct drm_mode_card_res *card_res = data;
1449 struct list_head *lh;
1450 struct drm_framebuffer *fb;
1451 struct drm_connector *connector;
1452 struct drm_crtc *crtc;
1453 struct drm_encoder *encoder;
1454 int ret = 0;
1455 int connector_count = 0;
1456 int crtc_count = 0;
1457 int fb_count = 0;
1458 int encoder_count = 0;
1459 int copied = 0, i;
1460 uint32_t __user *fb_id;
1461 uint32_t __user *crtc_id;
1462 uint32_t __user *connector_id;
1463 uint32_t __user *encoder_id;
1464 struct drm_mode_group *mode_group;
1465
Dave Airliefb3b06c2011-02-08 13:55:21 +10001466 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1467 return -EINVAL;
1468
Dave Airlief453ba02008-11-07 14:05:41 -08001469
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001470 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001471 /*
1472 * For the non-control nodes we need to limit the list of resources
1473 * by IDs in the group list for this node
1474 */
1475 list_for_each(lh, &file_priv->fbs)
1476 fb_count++;
1477
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001478 /* handle this in 4 parts */
1479 /* FBs */
1480 if (card_res->count_fbs >= fb_count) {
1481 copied = 0;
1482 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1483 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1484 if (put_user(fb->base.id, fb_id + copied)) {
1485 mutex_unlock(&file_priv->fbs_lock);
1486 return -EFAULT;
1487 }
1488 copied++;
1489 }
1490 }
1491 card_res->count_fbs = fb_count;
1492 mutex_unlock(&file_priv->fbs_lock);
1493
1494 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001495 mode_group = &file_priv->master->minor->mode_group;
1496 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1497
1498 list_for_each(lh, &dev->mode_config.crtc_list)
1499 crtc_count++;
1500
1501 list_for_each(lh, &dev->mode_config.connector_list)
1502 connector_count++;
1503
1504 list_for_each(lh, &dev->mode_config.encoder_list)
1505 encoder_count++;
1506 } else {
1507
1508 crtc_count = mode_group->num_crtcs;
1509 connector_count = mode_group->num_connectors;
1510 encoder_count = mode_group->num_encoders;
1511 }
1512
1513 card_res->max_height = dev->mode_config.max_height;
1514 card_res->min_height = dev->mode_config.min_height;
1515 card_res->max_width = dev->mode_config.max_width;
1516 card_res->min_width = dev->mode_config.min_width;
1517
Dave Airlief453ba02008-11-07 14:05:41 -08001518 /* CRTCs */
1519 if (card_res->count_crtcs >= crtc_count) {
1520 copied = 0;
1521 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1522 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1523 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1524 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001525 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001526 if (put_user(crtc->base.id, crtc_id + copied)) {
1527 ret = -EFAULT;
1528 goto out;
1529 }
1530 copied++;
1531 }
1532 } else {
1533 for (i = 0; i < mode_group->num_crtcs; i++) {
1534 if (put_user(mode_group->id_list[i],
1535 crtc_id + copied)) {
1536 ret = -EFAULT;
1537 goto out;
1538 }
1539 copied++;
1540 }
1541 }
1542 }
1543 card_res->count_crtcs = crtc_count;
1544
1545 /* Encoders */
1546 if (card_res->count_encoders >= encoder_count) {
1547 copied = 0;
1548 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1549 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1550 list_for_each_entry(encoder,
1551 &dev->mode_config.encoder_list,
1552 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001553 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1554 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001555 if (put_user(encoder->base.id, encoder_id +
1556 copied)) {
1557 ret = -EFAULT;
1558 goto out;
1559 }
1560 copied++;
1561 }
1562 } else {
1563 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1564 if (put_user(mode_group->id_list[i],
1565 encoder_id + copied)) {
1566 ret = -EFAULT;
1567 goto out;
1568 }
1569 copied++;
1570 }
1571
1572 }
1573 }
1574 card_res->count_encoders = encoder_count;
1575
1576 /* Connectors */
1577 if (card_res->count_connectors >= connector_count) {
1578 copied = 0;
1579 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1580 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1581 list_for_each_entry(connector,
1582 &dev->mode_config.connector_list,
1583 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001584 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1585 connector->base.id,
1586 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001587 if (put_user(connector->base.id,
1588 connector_id + copied)) {
1589 ret = -EFAULT;
1590 goto out;
1591 }
1592 copied++;
1593 }
1594 } else {
1595 int start = mode_group->num_crtcs +
1596 mode_group->num_encoders;
1597 for (i = start; i < start + mode_group->num_connectors; i++) {
1598 if (put_user(mode_group->id_list[i],
1599 connector_id + copied)) {
1600 ret = -EFAULT;
1601 goto out;
1602 }
1603 copied++;
1604 }
1605 }
1606 }
1607 card_res->count_connectors = connector_count;
1608
Jerome Glisse94401062010-07-15 15:43:25 -04001609 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001610 card_res->count_connectors, card_res->count_encoders);
1611
1612out:
Daniel Vetter84849902012-12-02 00:28:11 +01001613 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001614 return ret;
1615}
1616
1617/**
1618 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001619 * @dev: drm device for the ioctl
1620 * @data: data pointer for the ioctl
1621 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001622 *
Dave Airlief453ba02008-11-07 14:05:41 -08001623 * Construct a CRTC configuration structure to return to the user.
1624 *
1625 * Called by the user via ioctl.
1626 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001627 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001628 * Zero on success, errno on failure.
1629 */
1630int drm_mode_getcrtc(struct drm_device *dev,
1631 void *data, struct drm_file *file_priv)
1632{
1633 struct drm_mode_crtc *crtc_resp = data;
1634 struct drm_crtc *crtc;
1635 struct drm_mode_object *obj;
1636 int ret = 0;
1637
Dave Airliefb3b06c2011-02-08 13:55:21 +10001638 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1639 return -EINVAL;
1640
Daniel Vetter84849902012-12-02 00:28:11 +01001641 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001642
1643 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1644 DRM_MODE_OBJECT_CRTC);
1645 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001646 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001647 goto out;
1648 }
1649 crtc = obj_to_crtc(obj);
1650
1651 crtc_resp->x = crtc->x;
1652 crtc_resp->y = crtc->y;
1653 crtc_resp->gamma_size = crtc->gamma_size;
1654 if (crtc->fb)
1655 crtc_resp->fb_id = crtc->fb->base.id;
1656 else
1657 crtc_resp->fb_id = 0;
1658
1659 if (crtc->enabled) {
1660
1661 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1662 crtc_resp->mode_valid = 1;
1663
1664 } else {
1665 crtc_resp->mode_valid = 0;
1666 }
1667
1668out:
Daniel Vetter84849902012-12-02 00:28:11 +01001669 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001670 return ret;
1671}
1672
Damien Lespiau61d8e322013-09-25 16:45:22 +01001673static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1674 const struct drm_file *file_priv)
1675{
1676 /*
1677 * If user-space hasn't configured the driver to expose the stereo 3D
1678 * modes, don't expose them.
1679 */
1680 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1681 return false;
1682
1683 return true;
1684}
1685
Dave Airlief453ba02008-11-07 14:05:41 -08001686/**
1687 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001688 * @dev: drm device for the ioctl
1689 * @data: data pointer for the ioctl
1690 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001691 *
Dave Airlief453ba02008-11-07 14:05:41 -08001692 * Construct a connector configuration structure to return to the user.
1693 *
1694 * Called by the user via ioctl.
1695 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001696 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001697 * Zero on success, errno on failure.
1698 */
1699int drm_mode_getconnector(struct drm_device *dev, void *data,
1700 struct drm_file *file_priv)
1701{
1702 struct drm_mode_get_connector *out_resp = data;
1703 struct drm_mode_object *obj;
1704 struct drm_connector *connector;
1705 struct drm_display_mode *mode;
1706 int mode_count = 0;
1707 int props_count = 0;
1708 int encoders_count = 0;
1709 int ret = 0;
1710 int copied = 0;
1711 int i;
1712 struct drm_mode_modeinfo u_mode;
1713 struct drm_mode_modeinfo __user *mode_ptr;
1714 uint32_t __user *prop_ptr;
1715 uint64_t __user *prop_values;
1716 uint32_t __user *encoder_ptr;
1717
Dave Airliefb3b06c2011-02-08 13:55:21 +10001718 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1719 return -EINVAL;
1720
Dave Airlief453ba02008-11-07 14:05:41 -08001721 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1722
Jerome Glisse94401062010-07-15 15:43:25 -04001723 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001724
Daniel Vetter7b240562012-12-12 00:35:33 +01001725 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001726
1727 obj = drm_mode_object_find(dev, out_resp->connector_id,
1728 DRM_MODE_OBJECT_CONNECTOR);
1729 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001730 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001731 goto out;
1732 }
1733 connector = obj_to_connector(obj);
1734
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001735 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001736
1737 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1738 if (connector->encoder_ids[i] != 0) {
1739 encoders_count++;
1740 }
1741 }
1742
1743 if (out_resp->count_modes == 0) {
1744 connector->funcs->fill_modes(connector,
1745 dev->mode_config.max_width,
1746 dev->mode_config.max_height);
1747 }
1748
1749 /* delayed so we get modes regardless of pre-fill_modes state */
1750 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001751 if (drm_mode_expose_to_userspace(mode, file_priv))
1752 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001753
1754 out_resp->connector_id = connector->base.id;
1755 out_resp->connector_type = connector->connector_type;
1756 out_resp->connector_type_id = connector->connector_type_id;
1757 out_resp->mm_width = connector->display_info.width_mm;
1758 out_resp->mm_height = connector->display_info.height_mm;
1759 out_resp->subpixel = connector->display_info.subpixel_order;
1760 out_resp->connection = connector->status;
1761 if (connector->encoder)
1762 out_resp->encoder_id = connector->encoder->base.id;
1763 else
1764 out_resp->encoder_id = 0;
1765
1766 /*
1767 * This ioctl is called twice, once to determine how much space is
1768 * needed, and the 2nd time to fill it.
1769 */
1770 if ((out_resp->count_modes >= mode_count) && mode_count) {
1771 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001772 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001773 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001774 if (!drm_mode_expose_to_userspace(mode, file_priv))
1775 continue;
1776
Dave Airlief453ba02008-11-07 14:05:41 -08001777 drm_crtc_convert_to_umode(&u_mode, mode);
1778 if (copy_to_user(mode_ptr + copied,
1779 &u_mode, sizeof(u_mode))) {
1780 ret = -EFAULT;
1781 goto out;
1782 }
1783 copied++;
1784 }
1785 }
1786 out_resp->count_modes = mode_count;
1787
1788 if ((out_resp->count_props >= props_count) && props_count) {
1789 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001790 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1791 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001792 for (i = 0; i < connector->properties.count; i++) {
1793 if (put_user(connector->properties.ids[i],
1794 prop_ptr + copied)) {
1795 ret = -EFAULT;
1796 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001797 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001798
1799 if (put_user(connector->properties.values[i],
1800 prop_values + copied)) {
1801 ret = -EFAULT;
1802 goto out;
1803 }
1804 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001805 }
1806 }
1807 out_resp->count_props = props_count;
1808
1809 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1810 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001811 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001812 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1813 if (connector->encoder_ids[i] != 0) {
1814 if (put_user(connector->encoder_ids[i],
1815 encoder_ptr + copied)) {
1816 ret = -EFAULT;
1817 goto out;
1818 }
1819 copied++;
1820 }
1821 }
1822 }
1823 out_resp->count_encoders = encoders_count;
1824
1825out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001826 mutex_unlock(&dev->mode_config.mutex);
1827
Dave Airlief453ba02008-11-07 14:05:41 -08001828 return ret;
1829}
1830
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001831/**
1832 * drm_mode_getencoder - get encoder configuration
1833 * @dev: drm device for the ioctl
1834 * @data: data pointer for the ioctl
1835 * @file_priv: drm file for the ioctl call
1836 *
1837 * Construct a encoder configuration structure to return to the user.
1838 *
1839 * Called by the user via ioctl.
1840 *
1841 * Returns:
1842 * Zero on success, errno on failure.
1843 */
Dave Airlief453ba02008-11-07 14:05:41 -08001844int drm_mode_getencoder(struct drm_device *dev, void *data,
1845 struct drm_file *file_priv)
1846{
1847 struct drm_mode_get_encoder *enc_resp = data;
1848 struct drm_mode_object *obj;
1849 struct drm_encoder *encoder;
1850 int ret = 0;
1851
Dave Airliefb3b06c2011-02-08 13:55:21 +10001852 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1853 return -EINVAL;
1854
Daniel Vetter84849902012-12-02 00:28:11 +01001855 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001856 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1857 DRM_MODE_OBJECT_ENCODER);
1858 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001859 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001860 goto out;
1861 }
1862 encoder = obj_to_encoder(obj);
1863
1864 if (encoder->crtc)
1865 enc_resp->crtc_id = encoder->crtc->base.id;
1866 else
1867 enc_resp->crtc_id = 0;
1868 enc_resp->encoder_type = encoder->encoder_type;
1869 enc_resp->encoder_id = encoder->base.id;
1870 enc_resp->possible_crtcs = encoder->possible_crtcs;
1871 enc_resp->possible_clones = encoder->possible_clones;
1872
1873out:
Daniel Vetter84849902012-12-02 00:28:11 +01001874 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001875 return ret;
1876}
1877
1878/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001879 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001880 * @dev: DRM device
1881 * @data: ioctl data
1882 * @file_priv: DRM file info
1883 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001884 * Construct a list of plane ids to return to the user.
1885 *
1886 * Called by the user via ioctl.
1887 *
1888 * Returns:
1889 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001890 */
1891int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001892 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001893{
1894 struct drm_mode_get_plane_res *plane_resp = data;
1895 struct drm_mode_config *config;
1896 struct drm_plane *plane;
1897 uint32_t __user *plane_ptr;
1898 int copied = 0, ret = 0;
1899
1900 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1901 return -EINVAL;
1902
Daniel Vetter84849902012-12-02 00:28:11 +01001903 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904 config = &dev->mode_config;
1905
1906 /*
1907 * This ioctl is called twice, once to determine how much space is
1908 * needed, and the 2nd time to fill it.
1909 */
1910 if (config->num_plane &&
1911 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001912 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001913
1914 list_for_each_entry(plane, &config->plane_list, head) {
1915 if (put_user(plane->base.id, plane_ptr + copied)) {
1916 ret = -EFAULT;
1917 goto out;
1918 }
1919 copied++;
1920 }
1921 }
1922 plane_resp->count_planes = config->num_plane;
1923
1924out:
Daniel Vetter84849902012-12-02 00:28:11 +01001925 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001926 return ret;
1927}
1928
1929/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001930 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001931 * @dev: DRM device
1932 * @data: ioctl data
1933 * @file_priv: DRM file info
1934 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001935 * Construct a plane configuration structure to return to the user.
1936 *
1937 * Called by the user via ioctl.
1938 *
1939 * Returns:
1940 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001941 */
1942int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001943 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001944{
1945 struct drm_mode_get_plane *plane_resp = data;
1946 struct drm_mode_object *obj;
1947 struct drm_plane *plane;
1948 uint32_t __user *format_ptr;
1949 int ret = 0;
1950
1951 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1952 return -EINVAL;
1953
Daniel Vetter84849902012-12-02 00:28:11 +01001954 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1956 DRM_MODE_OBJECT_PLANE);
1957 if (!obj) {
1958 ret = -ENOENT;
1959 goto out;
1960 }
1961 plane = obj_to_plane(obj);
1962
1963 if (plane->crtc)
1964 plane_resp->crtc_id = plane->crtc->base.id;
1965 else
1966 plane_resp->crtc_id = 0;
1967
1968 if (plane->fb)
1969 plane_resp->fb_id = plane->fb->base.id;
1970 else
1971 plane_resp->fb_id = 0;
1972
1973 plane_resp->plane_id = plane->base.id;
1974 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001975 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001976
1977 /*
1978 * This ioctl is called twice, once to determine how much space is
1979 * needed, and the 2nd time to fill it.
1980 */
1981 if (plane->format_count &&
1982 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001983 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001984 if (copy_to_user(format_ptr,
1985 plane->format_types,
1986 sizeof(uint32_t) * plane->format_count)) {
1987 ret = -EFAULT;
1988 goto out;
1989 }
1990 }
1991 plane_resp->count_format_types = plane->format_count;
1992
1993out:
Daniel Vetter84849902012-12-02 00:28:11 +01001994 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001995 return ret;
1996}
1997
1998/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001999 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002000 * @dev: DRM device
2001 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002002 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002003 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002004 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002005 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002006 *
2007 * Returns:
2008 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002009 */
2010int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002011 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002012{
2013 struct drm_mode_set_plane *plane_req = data;
2014 struct drm_mode_object *obj;
2015 struct drm_plane *plane;
2016 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002017 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002018 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002019 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002020 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002021
2022 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2023 return -EINVAL;
2024
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002025 /*
2026 * First, find the plane, crtc, and fb objects. If not available,
2027 * we don't bother to call the driver.
2028 */
2029 obj = drm_mode_object_find(dev, plane_req->plane_id,
2030 DRM_MODE_OBJECT_PLANE);
2031 if (!obj) {
2032 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2033 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002034 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035 }
2036 plane = obj_to_plane(obj);
2037
2038 /* No fb means shut it down */
2039 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002040 drm_modeset_lock_all(dev);
2041 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002042 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02002043 plane->crtc = NULL;
2044 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002045 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002046 goto out;
2047 }
2048
2049 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2050 DRM_MODE_OBJECT_CRTC);
2051 if (!obj) {
2052 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2053 plane_req->crtc_id);
2054 ret = -ENOENT;
2055 goto out;
2056 }
2057 crtc = obj_to_crtc(obj);
2058
Daniel Vetter786b99e2012-12-02 21:53:40 +01002059 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2060 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002061 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2062 plane_req->fb_id);
2063 ret = -ENOENT;
2064 goto out;
2065 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002066
Ville Syrjälä62443be2011-12-20 00:06:47 +02002067 /* Check whether this plane supports the fb pixel format. */
2068 for (i = 0; i < plane->format_count; i++)
2069 if (fb->pixel_format == plane->format_types[i])
2070 break;
2071 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002072 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2073 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002074 ret = -EINVAL;
2075 goto out;
2076 }
2077
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002078 fb_width = fb->width << 16;
2079 fb_height = fb->height << 16;
2080
2081 /* Make sure source coordinates are inside the fb. */
2082 if (plane_req->src_w > fb_width ||
2083 plane_req->src_x > fb_width - plane_req->src_w ||
2084 plane_req->src_h > fb_height ||
2085 plane_req->src_y > fb_height - plane_req->src_h) {
2086 DRM_DEBUG_KMS("Invalid source coordinates "
2087 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2088 plane_req->src_w >> 16,
2089 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2090 plane_req->src_h >> 16,
2091 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2092 plane_req->src_x >> 16,
2093 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2094 plane_req->src_y >> 16,
2095 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2096 ret = -ENOSPC;
2097 goto out;
2098 }
2099
Ville Syrjälä687a0402011-12-20 00:06:45 +02002100 /* Give drivers some help against integer overflows */
2101 if (plane_req->crtc_w > INT_MAX ||
2102 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2103 plane_req->crtc_h > INT_MAX ||
2104 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2105 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2106 plane_req->crtc_w, plane_req->crtc_h,
2107 plane_req->crtc_x, plane_req->crtc_y);
2108 ret = -ERANGE;
2109 goto out;
2110 }
2111
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002112 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002113 ret = plane->funcs->update_plane(plane, crtc, fb,
2114 plane_req->crtc_x, plane_req->crtc_y,
2115 plane_req->crtc_w, plane_req->crtc_h,
2116 plane_req->src_x, plane_req->src_y,
2117 plane_req->src_w, plane_req->src_h);
2118 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002119 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002120 plane->crtc = crtc;
2121 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002122 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002123 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002124 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002125
2126out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002127 if (fb)
2128 drm_framebuffer_unreference(fb);
2129 if (old_fb)
2130 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002131
2132 return ret;
2133}
2134
2135/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002136 * drm_mode_set_config_internal - helper to call ->set_config
2137 * @set: modeset config to set
2138 *
2139 * This is a little helper to wrap internal calls to the ->set_config driver
2140 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002141 *
2142 * Returns:
2143 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002144 */
2145int drm_mode_set_config_internal(struct drm_mode_set *set)
2146{
2147 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002148 struct drm_framebuffer *fb;
2149 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002150 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002151
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002152 /*
2153 * NOTE: ->set_config can also disable other crtcs (if we steal all
2154 * connectors from it), hence we need to refcount the fbs across all
2155 * crtcs. Atomic modeset will have saner semantics ...
2156 */
2157 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2158 tmp->old_fb = tmp->fb;
2159
Daniel Vetterb0d12322012-12-11 01:07:12 +01002160 fb = set->fb;
2161
2162 ret = crtc->funcs->set_config(set);
2163 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002164 /* crtc->fb must be updated by ->set_config, enforces this. */
2165 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002166 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002167
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002168 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2169 if (tmp->fb)
2170 drm_framebuffer_reference(tmp->fb);
2171 if (tmp->old_fb)
2172 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002173 }
2174
2175 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002176}
2177EXPORT_SYMBOL(drm_mode_set_config_internal);
2178
Damien Lespiauc11e9282013-09-25 16:45:30 +01002179/*
2180 * Checks that the framebuffer is big enough for the CRTC viewport
2181 * (x, y, hdisplay, vdisplay)
2182 */
2183static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2184 int x, int y,
2185 const struct drm_display_mode *mode,
2186 const struct drm_framebuffer *fb)
2187
2188{
2189 int hdisplay, vdisplay;
2190
2191 hdisplay = mode->hdisplay;
2192 vdisplay = mode->vdisplay;
2193
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002194 if (drm_mode_is_stereo(mode)) {
2195 struct drm_display_mode adjusted = *mode;
2196
2197 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2198 hdisplay = adjusted.crtc_hdisplay;
2199 vdisplay = adjusted.crtc_vdisplay;
2200 }
2201
Damien Lespiauc11e9282013-09-25 16:45:30 +01002202 if (crtc->invert_dimensions)
2203 swap(hdisplay, vdisplay);
2204
2205 if (hdisplay > fb->width ||
2206 vdisplay > fb->height ||
2207 x > fb->width - hdisplay ||
2208 y > fb->height - vdisplay) {
2209 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2210 fb->width, fb->height, hdisplay, vdisplay, x, y,
2211 crtc->invert_dimensions ? " (inverted)" : "");
2212 return -ENOSPC;
2213 }
2214
2215 return 0;
2216}
2217
Daniel Vetter2d13b672012-12-11 13:47:23 +01002218/**
Dave Airlief453ba02008-11-07 14:05:41 -08002219 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002220 * @dev: drm device for the ioctl
2221 * @data: data pointer for the ioctl
2222 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002223 *
Dave Airlief453ba02008-11-07 14:05:41 -08002224 * Build a new CRTC configuration based on user request.
2225 *
2226 * Called by the user via ioctl.
2227 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002228 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002229 * Zero on success, errno on failure.
2230 */
2231int drm_mode_setcrtc(struct drm_device *dev, void *data,
2232 struct drm_file *file_priv)
2233{
2234 struct drm_mode_config *config = &dev->mode_config;
2235 struct drm_mode_crtc *crtc_req = data;
2236 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002237 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002238 struct drm_connector **connector_set = NULL, *connector;
2239 struct drm_framebuffer *fb = NULL;
2240 struct drm_display_mode *mode = NULL;
2241 struct drm_mode_set set;
2242 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002243 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002244 int i;
2245
Dave Airliefb3b06c2011-02-08 13:55:21 +10002246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2247 return -EINVAL;
2248
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002249 /* For some reason crtc x/y offsets are signed internally. */
2250 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2251 return -ERANGE;
2252
Daniel Vetter84849902012-12-02 00:28:11 +01002253 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002254 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2255 DRM_MODE_OBJECT_CRTC);
2256 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002257 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002258 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002259 goto out;
2260 }
2261 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002262 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002263
2264 if (crtc_req->mode_valid) {
2265 /* If we have a mode we need a framebuffer. */
2266 /* If we pass -1, set the mode with the currently bound fb */
2267 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002268 if (!crtc->fb) {
2269 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2270 ret = -EINVAL;
2271 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002272 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002273 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002274 /* Make refcounting symmetric with the lookup path. */
2275 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002276 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002277 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2278 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002279 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2280 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002281 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002282 goto out;
2283 }
Dave Airlief453ba02008-11-07 14:05:41 -08002284 }
2285
2286 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002287 if (!mode) {
2288 ret = -ENOMEM;
2289 goto out;
2290 }
2291
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002292 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2293 if (ret) {
2294 DRM_DEBUG_KMS("Invalid mode\n");
2295 goto out;
2296 }
2297
Dave Airlief453ba02008-11-07 14:05:41 -08002298 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002299
Damien Lespiauc11e9282013-09-25 16:45:30 +01002300 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2301 mode, fb);
2302 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002303 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002304
Dave Airlief453ba02008-11-07 14:05:41 -08002305 }
2306
2307 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002308 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002309 ret = -EINVAL;
2310 goto out;
2311 }
2312
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002313 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002314 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002315 crtc_req->count_connectors);
2316 ret = -EINVAL;
2317 goto out;
2318 }
2319
2320 if (crtc_req->count_connectors > 0) {
2321 u32 out_id;
2322
2323 /* Avoid unbounded kernel memory allocation */
2324 if (crtc_req->count_connectors > config->num_connector) {
2325 ret = -EINVAL;
2326 goto out;
2327 }
2328
2329 connector_set = kmalloc(crtc_req->count_connectors *
2330 sizeof(struct drm_connector *),
2331 GFP_KERNEL);
2332 if (!connector_set) {
2333 ret = -ENOMEM;
2334 goto out;
2335 }
2336
2337 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002338 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002339 if (get_user(out_id, &set_connectors_ptr[i])) {
2340 ret = -EFAULT;
2341 goto out;
2342 }
2343
2344 obj = drm_mode_object_find(dev, out_id,
2345 DRM_MODE_OBJECT_CONNECTOR);
2346 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002347 DRM_DEBUG_KMS("Connector id %d unknown\n",
2348 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002349 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002350 goto out;
2351 }
2352 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002353 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2354 connector->base.id,
2355 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002356
2357 connector_set[i] = connector;
2358 }
2359 }
2360
2361 set.crtc = crtc;
2362 set.x = crtc_req->x;
2363 set.y = crtc_req->y;
2364 set.mode = mode;
2365 set.connectors = connector_set;
2366 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002367 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002368 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002369
2370out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002371 if (fb)
2372 drm_framebuffer_unreference(fb);
2373
Dave Airlief453ba02008-11-07 14:05:41 -08002374 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002375 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002376 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002377 return ret;
2378}
2379
Dave Airlie4c813d42013-06-20 11:48:52 +10002380static int drm_mode_cursor_common(struct drm_device *dev,
2381 struct drm_mode_cursor2 *req,
2382 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002383{
Dave Airlief453ba02008-11-07 14:05:41 -08002384 struct drm_mode_object *obj;
2385 struct drm_crtc *crtc;
2386 int ret = 0;
2387
Dave Airliefb3b06c2011-02-08 13:55:21 +10002388 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2389 return -EINVAL;
2390
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002391 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002392 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002393
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002394 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002395 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002396 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002397 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002398 }
2399 crtc = obj_to_crtc(obj);
2400
Daniel Vetterdac35662012-12-02 15:24:10 +01002401 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002402 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002403 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002404 ret = -ENXIO;
2405 goto out;
2406 }
2407 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002408 if (crtc->funcs->cursor_set2)
2409 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2410 req->width, req->height, req->hot_x, req->hot_y);
2411 else
2412 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2413 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002414 }
2415
2416 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2417 if (crtc->funcs->cursor_move) {
2418 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2419 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002420 ret = -EFAULT;
2421 goto out;
2422 }
2423 }
2424out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002425 mutex_unlock(&crtc->mutex);
2426
Dave Airlief453ba02008-11-07 14:05:41 -08002427 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002428
2429}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002430
2431
2432/**
2433 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2434 * @dev: drm device for the ioctl
2435 * @data: data pointer for the ioctl
2436 * @file_priv: drm file for the ioctl call
2437 *
2438 * Set the cursor configuration based on user request.
2439 *
2440 * Called by the user via ioctl.
2441 *
2442 * Returns:
2443 * Zero on success, errno on failure.
2444 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002445int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002446 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002447{
2448 struct drm_mode_cursor *req = data;
2449 struct drm_mode_cursor2 new_req;
2450
2451 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2452 new_req.hot_x = new_req.hot_y = 0;
2453
2454 return drm_mode_cursor_common(dev, &new_req, file_priv);
2455}
2456
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002457/**
2458 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2459 * @dev: drm device for the ioctl
2460 * @data: data pointer for the ioctl
2461 * @file_priv: drm file for the ioctl call
2462 *
2463 * Set the cursor configuration based on user request. This implements the 2nd
2464 * version of the cursor ioctl, which allows userspace to additionally specify
2465 * the hotspot of the pointer.
2466 *
2467 * Called by the user via ioctl.
2468 *
2469 * Returns:
2470 * Zero on success, errno on failure.
2471 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002472int drm_mode_cursor2_ioctl(struct drm_device *dev,
2473 void *data, struct drm_file *file_priv)
2474{
2475 struct drm_mode_cursor2 *req = data;
2476 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002477}
2478
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002479/**
2480 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2481 * @bpp: bits per pixels
2482 * @depth: bit depth per pixel
2483 *
2484 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2485 * Useful in fbdev emulation code, since that deals in those values.
2486 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002487uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2488{
2489 uint32_t fmt;
2490
2491 switch (bpp) {
2492 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002493 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002494 break;
2495 case 16:
2496 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002497 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002498 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002499 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002500 break;
2501 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002502 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002503 break;
2504 case 32:
2505 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002506 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002507 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002508 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002509 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002510 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002511 break;
2512 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002513 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2514 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002515 break;
2516 }
2517
2518 return fmt;
2519}
2520EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2521
Dave Airlief453ba02008-11-07 14:05:41 -08002522/**
2523 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002524 * @dev: drm device for the ioctl
2525 * @data: data pointer for the ioctl
2526 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002527 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002528 * Add a new FB to the specified CRTC, given a user request. This is the
2529 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002530 *
2531 * Called by the user via ioctl.
2532 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002533 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002534 * Zero on success, errno on failure.
2535 */
2536int drm_mode_addfb(struct drm_device *dev,
2537 void *data, struct drm_file *file_priv)
2538{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002539 struct drm_mode_fb_cmd *or = data;
2540 struct drm_mode_fb_cmd2 r = {};
2541 struct drm_mode_config *config = &dev->mode_config;
2542 struct drm_framebuffer *fb;
2543 int ret = 0;
2544
2545 /* Use new struct with format internally */
2546 r.fb_id = or->fb_id;
2547 r.width = or->width;
2548 r.height = or->height;
2549 r.pitches[0] = or->pitch;
2550 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2551 r.handles[0] = or->handle;
2552
2553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2554 return -EINVAL;
2555
Jesse Barnesacb4b992011-11-07 12:03:23 -08002556 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002557 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002558
2559 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002560 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002561
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002562 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2563 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002564 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002565 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002566 }
2567
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002568 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002569 or->fb_id = fb->base.id;
2570 list_add(&fb->filp_head, &file_priv->fbs);
2571 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002572 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002573
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002574 return ret;
2575}
2576
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002577static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002578{
2579 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2580
2581 switch (format) {
2582 case DRM_FORMAT_C8:
2583 case DRM_FORMAT_RGB332:
2584 case DRM_FORMAT_BGR233:
2585 case DRM_FORMAT_XRGB4444:
2586 case DRM_FORMAT_XBGR4444:
2587 case DRM_FORMAT_RGBX4444:
2588 case DRM_FORMAT_BGRX4444:
2589 case DRM_FORMAT_ARGB4444:
2590 case DRM_FORMAT_ABGR4444:
2591 case DRM_FORMAT_RGBA4444:
2592 case DRM_FORMAT_BGRA4444:
2593 case DRM_FORMAT_XRGB1555:
2594 case DRM_FORMAT_XBGR1555:
2595 case DRM_FORMAT_RGBX5551:
2596 case DRM_FORMAT_BGRX5551:
2597 case DRM_FORMAT_ARGB1555:
2598 case DRM_FORMAT_ABGR1555:
2599 case DRM_FORMAT_RGBA5551:
2600 case DRM_FORMAT_BGRA5551:
2601 case DRM_FORMAT_RGB565:
2602 case DRM_FORMAT_BGR565:
2603 case DRM_FORMAT_RGB888:
2604 case DRM_FORMAT_BGR888:
2605 case DRM_FORMAT_XRGB8888:
2606 case DRM_FORMAT_XBGR8888:
2607 case DRM_FORMAT_RGBX8888:
2608 case DRM_FORMAT_BGRX8888:
2609 case DRM_FORMAT_ARGB8888:
2610 case DRM_FORMAT_ABGR8888:
2611 case DRM_FORMAT_RGBA8888:
2612 case DRM_FORMAT_BGRA8888:
2613 case DRM_FORMAT_XRGB2101010:
2614 case DRM_FORMAT_XBGR2101010:
2615 case DRM_FORMAT_RGBX1010102:
2616 case DRM_FORMAT_BGRX1010102:
2617 case DRM_FORMAT_ARGB2101010:
2618 case DRM_FORMAT_ABGR2101010:
2619 case DRM_FORMAT_RGBA1010102:
2620 case DRM_FORMAT_BGRA1010102:
2621 case DRM_FORMAT_YUYV:
2622 case DRM_FORMAT_YVYU:
2623 case DRM_FORMAT_UYVY:
2624 case DRM_FORMAT_VYUY:
2625 case DRM_FORMAT_AYUV:
2626 case DRM_FORMAT_NV12:
2627 case DRM_FORMAT_NV21:
2628 case DRM_FORMAT_NV16:
2629 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002630 case DRM_FORMAT_NV24:
2631 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002632 case DRM_FORMAT_YUV410:
2633 case DRM_FORMAT_YVU410:
2634 case DRM_FORMAT_YUV411:
2635 case DRM_FORMAT_YVU411:
2636 case DRM_FORMAT_YUV420:
2637 case DRM_FORMAT_YVU420:
2638 case DRM_FORMAT_YUV422:
2639 case DRM_FORMAT_YVU422:
2640 case DRM_FORMAT_YUV444:
2641 case DRM_FORMAT_YVU444:
2642 return 0;
2643 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002644 DRM_DEBUG_KMS("invalid pixel format %s\n",
2645 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002646 return -EINVAL;
2647 }
2648}
2649
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002650static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002651{
2652 int ret, hsub, vsub, num_planes, i;
2653
2654 ret = format_check(r);
2655 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002656 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2657 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002658 return ret;
2659 }
2660
2661 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2662 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2663 num_planes = drm_format_num_planes(r->pixel_format);
2664
2665 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002666 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002667 return -EINVAL;
2668 }
2669
2670 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002671 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002672 return -EINVAL;
2673 }
2674
2675 for (i = 0; i < num_planes; i++) {
2676 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002677 unsigned int height = r->height / (i != 0 ? vsub : 1);
2678 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002679
2680 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002681 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002682 return -EINVAL;
2683 }
2684
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002685 if ((uint64_t) width * cpp > UINT_MAX)
2686 return -ERANGE;
2687
2688 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2689 return -ERANGE;
2690
2691 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002692 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002693 return -EINVAL;
2694 }
2695 }
2696
2697 return 0;
2698}
2699
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002700/**
2701 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002702 * @dev: drm device for the ioctl
2703 * @data: data pointer for the ioctl
2704 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002705 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002706 * Add a new FB to the specified CRTC, given a user request with format. This is
2707 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2708 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002709 *
2710 * Called by the user via ioctl.
2711 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002712 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002713 * Zero on success, errno on failure.
2714 */
2715int drm_mode_addfb2(struct drm_device *dev,
2716 void *data, struct drm_file *file_priv)
2717{
2718 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002719 struct drm_mode_config *config = &dev->mode_config;
2720 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002721 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002722
Dave Airliefb3b06c2011-02-08 13:55:21 +10002723 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2724 return -EINVAL;
2725
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002726 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2727 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2728 return -EINVAL;
2729 }
2730
Dave Airlief453ba02008-11-07 14:05:41 -08002731 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002732 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002733 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002734 return -EINVAL;
2735 }
2736 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002737 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002738 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002739 return -EINVAL;
2740 }
2741
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002742 ret = framebuffer_check(r);
2743 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002744 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002745
Dave Airlief453ba02008-11-07 14:05:41 -08002746 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002747 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002748 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002749 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002750 }
2751
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002752 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002753 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002754 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002755 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002756 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002757
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002758
Dave Airlief453ba02008-11-07 14:05:41 -08002759 return ret;
2760}
2761
2762/**
2763 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002764 * @dev: drm device for the ioctl
2765 * @data: data pointer for the ioctl
2766 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002767 *
Dave Airlief453ba02008-11-07 14:05:41 -08002768 * Remove the FB specified by the user.
2769 *
2770 * Called by the user via ioctl.
2771 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002772 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002773 * Zero on success, errno on failure.
2774 */
2775int drm_mode_rmfb(struct drm_device *dev,
2776 void *data, struct drm_file *file_priv)
2777{
Dave Airlief453ba02008-11-07 14:05:41 -08002778 struct drm_framebuffer *fb = NULL;
2779 struct drm_framebuffer *fbl = NULL;
2780 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002781 int found = 0;
2782
Dave Airliefb3b06c2011-02-08 13:55:21 +10002783 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2784 return -EINVAL;
2785
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002786 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002787 mutex_lock(&dev->mode_config.fb_lock);
2788 fb = __drm_framebuffer_lookup(dev, *id);
2789 if (!fb)
2790 goto fail_lookup;
2791
Dave Airlief453ba02008-11-07 14:05:41 -08002792 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2793 if (fb == fbl)
2794 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002795 if (!found)
2796 goto fail_lookup;
2797
2798 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2799 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002800
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002801 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002802 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002803 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002804
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002805 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002806
Daniel Vetter2b677e82012-12-10 21:16:05 +01002807 return 0;
2808
2809fail_lookup:
2810 mutex_unlock(&dev->mode_config.fb_lock);
2811 mutex_unlock(&file_priv->fbs_lock);
2812
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002813 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002814}
2815
2816/**
2817 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002818 * @dev: drm device for the ioctl
2819 * @data: data pointer for the ioctl
2820 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002821 *
Dave Airlief453ba02008-11-07 14:05:41 -08002822 * Lookup the FB given its ID and return info about it.
2823 *
2824 * Called by the user via ioctl.
2825 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002826 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002827 * Zero on success, errno on failure.
2828 */
2829int drm_mode_getfb(struct drm_device *dev,
2830 void *data, struct drm_file *file_priv)
2831{
2832 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002833 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002834 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002835
Dave Airliefb3b06c2011-02-08 13:55:21 +10002836 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2837 return -EINVAL;
2838
Daniel Vetter786b99e2012-12-02 21:53:40 +01002839 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002840 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002841 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002842
2843 r->height = fb->height;
2844 r->width = fb->width;
2845 r->depth = fb->depth;
2846 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002847 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002848 if (fb->funcs->create_handle) {
2849 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2850 ret = fb->funcs->create_handle(fb, file_priv,
2851 &r->handle);
2852 } else {
2853 /* GET_FB() is an unprivileged ioctl so we must not
2854 * return a buffer-handle to non-master processes! For
2855 * backwards-compatibility reasons, we cannot make
2856 * GET_FB() privileged, so just return an invalid handle
2857 * for non-masters. */
2858 r->handle = 0;
2859 ret = 0;
2860 }
2861 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002862 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002863 }
Dave Airlief453ba02008-11-07 14:05:41 -08002864
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002865 drm_framebuffer_unreference(fb);
2866
Dave Airlief453ba02008-11-07 14:05:41 -08002867 return ret;
2868}
2869
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002870/**
2871 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2872 * @dev: drm device for the ioctl
2873 * @data: data pointer for the ioctl
2874 * @file_priv: drm file for the ioctl call
2875 *
2876 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2877 * rectangle list. Generic userspace which does frontbuffer rendering must call
2878 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2879 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2880 *
2881 * Modesetting drivers which always update the frontbuffer do not need to
2882 * implement the corresponding ->dirty framebuffer callback.
2883 *
2884 * Called by the user via ioctl.
2885 *
2886 * Returns:
2887 * Zero on success, errno on failure.
2888 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002889int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2890 void *data, struct drm_file *file_priv)
2891{
2892 struct drm_clip_rect __user *clips_ptr;
2893 struct drm_clip_rect *clips = NULL;
2894 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002895 struct drm_framebuffer *fb;
2896 unsigned flags;
2897 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002898 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002899
Dave Airliefb3b06c2011-02-08 13:55:21 +10002900 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2901 return -EINVAL;
2902
Daniel Vetter786b99e2012-12-02 21:53:40 +01002903 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002904 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002905 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002906
2907 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002908 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002909
2910 if (!num_clips != !clips_ptr) {
2911 ret = -EINVAL;
2912 goto out_err1;
2913 }
2914
2915 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2916
2917 /* If userspace annotates copy, clips must come in pairs */
2918 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2919 ret = -EINVAL;
2920 goto out_err1;
2921 }
2922
2923 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002924 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2925 ret = -EINVAL;
2926 goto out_err1;
2927 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002928 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2929 if (!clips) {
2930 ret = -ENOMEM;
2931 goto out_err1;
2932 }
2933
2934 ret = copy_from_user(clips, clips_ptr,
2935 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002936 if (ret) {
2937 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002938 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002939 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002940 }
2941
2942 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002943 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2944 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002945 } else {
2946 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002947 }
2948
2949out_err2:
2950 kfree(clips);
2951out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002952 drm_framebuffer_unreference(fb);
2953
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002954 return ret;
2955}
2956
2957
Dave Airlief453ba02008-11-07 14:05:41 -08002958/**
2959 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002960 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002961 *
Dave Airlief453ba02008-11-07 14:05:41 -08002962 * Destroy all the FBs associated with @filp.
2963 *
2964 * Called by the user via ioctl.
2965 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002966 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002967 * Zero on success, errno on failure.
2968 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002969void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002970{
Dave Airlief453ba02008-11-07 14:05:41 -08002971 struct drm_device *dev = priv->minor->dev;
2972 struct drm_framebuffer *fb, *tfb;
2973
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002974 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002975 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002976
2977 mutex_lock(&dev->mode_config.fb_lock);
2978 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2979 __drm_framebuffer_unregister(dev, fb);
2980 mutex_unlock(&dev->mode_config.fb_lock);
2981
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002982 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002983
2984 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002985 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002986 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002987 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002988}
2989
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002990/**
2991 * drm_property_create - create a new property type
2992 * @dev: drm device
2993 * @flags: flags specifying the property type
2994 * @name: name of the property
2995 * @num_values: number of pre-defined values
2996 *
2997 * This creates a new generic drm property which can then be attached to a drm
2998 * object with drm_object_attach_property. The returned property object must be
2999 * freed with drm_property_destroy.
3000 *
3001 * Returns:
3002 * A pointer to the newly created property on success, NULL on failure.
3003 */
Dave Airlief453ba02008-11-07 14:05:41 -08003004struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3005 const char *name, int num_values)
3006{
3007 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003008 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003009
3010 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3011 if (!property)
3012 return NULL;
3013
3014 if (num_values) {
3015 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3016 if (!property->values)
3017 goto fail;
3018 }
3019
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003020 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3021 if (ret)
3022 goto fail;
3023
Dave Airlief453ba02008-11-07 14:05:41 -08003024 property->flags = flags;
3025 property->num_values = num_values;
3026 INIT_LIST_HEAD(&property->enum_blob_list);
3027
Vinson Lee471dd2e2011-11-10 11:55:40 -08003028 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003029 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003030 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3031 }
Dave Airlief453ba02008-11-07 14:05:41 -08003032
3033 list_add_tail(&property->head, &dev->mode_config.property_list);
3034 return property;
3035fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003036 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003037 kfree(property);
3038 return NULL;
3039}
3040EXPORT_SYMBOL(drm_property_create);
3041
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003042/**
3043 * drm_property_create - create a new enumeration property type
3044 * @dev: drm device
3045 * @flags: flags specifying the property type
3046 * @name: name of the property
3047 * @props: enumeration lists with property values
3048 * @num_values: number of pre-defined values
3049 *
3050 * This creates a new generic drm property which can then be attached to a drm
3051 * object with drm_object_attach_property. The returned property object must be
3052 * freed with drm_property_destroy.
3053 *
3054 * Userspace is only allowed to set one of the predefined values for enumeration
3055 * properties.
3056 *
3057 * Returns:
3058 * A pointer to the newly created property on success, NULL on failure.
3059 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003060struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3061 const char *name,
3062 const struct drm_prop_enum_list *props,
3063 int num_values)
3064{
3065 struct drm_property *property;
3066 int i, ret;
3067
3068 flags |= DRM_MODE_PROP_ENUM;
3069
3070 property = drm_property_create(dev, flags, name, num_values);
3071 if (!property)
3072 return NULL;
3073
3074 for (i = 0; i < num_values; i++) {
3075 ret = drm_property_add_enum(property, i,
3076 props[i].type,
3077 props[i].name);
3078 if (ret) {
3079 drm_property_destroy(dev, property);
3080 return NULL;
3081 }
3082 }
3083
3084 return property;
3085}
3086EXPORT_SYMBOL(drm_property_create_enum);
3087
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003088/**
3089 * drm_property_create - create a new bitmask property type
3090 * @dev: drm device
3091 * @flags: flags specifying the property type
3092 * @name: name of the property
3093 * @props: enumeration lists with property bitflags
3094 * @num_values: number of pre-defined values
3095 *
3096 * This creates a new generic drm property which can then be attached to a drm
3097 * object with drm_object_attach_property. The returned property object must be
3098 * freed with drm_property_destroy.
3099 *
3100 * Compared to plain enumeration properties userspace is allowed to set any
3101 * or'ed together combination of the predefined property bitflag values
3102 *
3103 * Returns:
3104 * A pointer to the newly created property on success, NULL on failure.
3105 */
Rob Clark49e27542012-05-17 02:23:26 -06003106struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3107 int flags, const char *name,
3108 const struct drm_prop_enum_list *props,
3109 int num_values)
3110{
3111 struct drm_property *property;
3112 int i, ret;
3113
3114 flags |= DRM_MODE_PROP_BITMASK;
3115
3116 property = drm_property_create(dev, flags, name, num_values);
3117 if (!property)
3118 return NULL;
3119
3120 for (i = 0; i < num_values; i++) {
3121 ret = drm_property_add_enum(property, i,
3122 props[i].type,
3123 props[i].name);
3124 if (ret) {
3125 drm_property_destroy(dev, property);
3126 return NULL;
3127 }
3128 }
3129
3130 return property;
3131}
3132EXPORT_SYMBOL(drm_property_create_bitmask);
3133
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003134/**
3135 * drm_property_create - create a new ranged property type
3136 * @dev: drm device
3137 * @flags: flags specifying the property type
3138 * @name: name of the property
3139 * @min: minimum value of the property
3140 * @max: maximum value of the property
3141 *
3142 * This creates a new generic drm property which can then be attached to a drm
3143 * object with drm_object_attach_property. The returned property object must be
3144 * freed with drm_property_destroy.
3145 *
3146 * Userspace is allowed to set any interger value in the (min, max) range
3147 * inclusive.
3148 *
3149 * Returns:
3150 * A pointer to the newly created property on success, NULL on failure.
3151 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003152struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3153 const char *name,
3154 uint64_t min, uint64_t max)
3155{
3156 struct drm_property *property;
3157
3158 flags |= DRM_MODE_PROP_RANGE;
3159
3160 property = drm_property_create(dev, flags, name, 2);
3161 if (!property)
3162 return NULL;
3163
3164 property->values[0] = min;
3165 property->values[1] = max;
3166
3167 return property;
3168}
3169EXPORT_SYMBOL(drm_property_create_range);
3170
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003171/**
3172 * drm_property_add_enum - add a possible value to an enumeration property
3173 * @property: enumeration property to change
3174 * @index: index of the new enumeration
3175 * @value: value of the new enumeration
3176 * @name: symbolic name of the new enumeration
3177 *
3178 * This functions adds enumerations to a property.
3179 *
3180 * It's use is deprecated, drivers should use one of the more specific helpers
3181 * to directly create the property with all enumerations already attached.
3182 *
3183 * Returns:
3184 * Zero on success, error code on failure.
3185 */
Dave Airlief453ba02008-11-07 14:05:41 -08003186int drm_property_add_enum(struct drm_property *property, int index,
3187 uint64_t value, const char *name)
3188{
3189 struct drm_property_enum *prop_enum;
3190
Rob Clark49e27542012-05-17 02:23:26 -06003191 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3192 return -EINVAL;
3193
3194 /*
3195 * Bitmask enum properties have the additional constraint of values
3196 * from 0 to 63
3197 */
3198 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003199 return -EINVAL;
3200
3201 if (!list_empty(&property->enum_blob_list)) {
3202 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3203 if (prop_enum->value == value) {
3204 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3205 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3206 return 0;
3207 }
3208 }
3209 }
3210
3211 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3212 if (!prop_enum)
3213 return -ENOMEM;
3214
3215 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3216 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3217 prop_enum->value = value;
3218
3219 property->values[index] = value;
3220 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3221 return 0;
3222}
3223EXPORT_SYMBOL(drm_property_add_enum);
3224
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003225/**
3226 * drm_property_destroy - destroy a drm property
3227 * @dev: drm device
3228 * @property: property to destry
3229 *
3230 * This function frees a property including any attached resources like
3231 * enumeration values.
3232 */
Dave Airlief453ba02008-11-07 14:05:41 -08003233void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3234{
3235 struct drm_property_enum *prop_enum, *pt;
3236
3237 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3238 list_del(&prop_enum->head);
3239 kfree(prop_enum);
3240 }
3241
3242 if (property->num_values)
3243 kfree(property->values);
3244 drm_mode_object_put(dev, &property->base);
3245 list_del(&property->head);
3246 kfree(property);
3247}
3248EXPORT_SYMBOL(drm_property_destroy);
3249
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003250/**
3251 * drm_object_attach_property - attach a property to a modeset object
3252 * @obj: drm modeset object
3253 * @property: property to attach
3254 * @init_val: initial value of the property
3255 *
3256 * This attaches the given property to the modeset object with the given initial
3257 * value. Currently this function cannot fail since the properties are stored in
3258 * a statically sized array.
3259 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003260void drm_object_attach_property(struct drm_mode_object *obj,
3261 struct drm_property *property,
3262 uint64_t init_val)
3263{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003264 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003265
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003266 if (count == DRM_OBJECT_MAX_PROPERTY) {
3267 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3268 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3269 "you see this message on the same object type.\n",
3270 obj->type);
3271 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003272 }
3273
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003274 obj->properties->ids[count] = property->base.id;
3275 obj->properties->values[count] = init_val;
3276 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003277}
3278EXPORT_SYMBOL(drm_object_attach_property);
3279
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003280/**
3281 * drm_object_property_set_value - set the value of a property
3282 * @obj: drm mode object to set property value for
3283 * @property: property to set
3284 * @val: value the property should be set to
3285 *
3286 * This functions sets a given property on a given object. This function only
3287 * changes the software state of the property, it does not call into the
3288 * driver's ->set_property callback.
3289 *
3290 * Returns:
3291 * Zero on success, error code on failure.
3292 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003293int drm_object_property_set_value(struct drm_mode_object *obj,
3294 struct drm_property *property, uint64_t val)
3295{
3296 int i;
3297
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003298 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003299 if (obj->properties->ids[i] == property->base.id) {
3300 obj->properties->values[i] = val;
3301 return 0;
3302 }
3303 }
3304
3305 return -EINVAL;
3306}
3307EXPORT_SYMBOL(drm_object_property_set_value);
3308
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003309/**
3310 * drm_object_property_get_value - retrieve the value of a property
3311 * @obj: drm mode object to get property value from
3312 * @property: property to retrieve
3313 * @val: storage for the property value
3314 *
3315 * This function retrieves the softare state of the given property for the given
3316 * property. Since there is no driver callback to retrieve the current property
3317 * value this might be out of sync with the hardware, depending upon the driver
3318 * and property.
3319 *
3320 * Returns:
3321 * Zero on success, error code on failure.
3322 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003323int drm_object_property_get_value(struct drm_mode_object *obj,
3324 struct drm_property *property, uint64_t *val)
3325{
3326 int i;
3327
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003328 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003329 if (obj->properties->ids[i] == property->base.id) {
3330 *val = obj->properties->values[i];
3331 return 0;
3332 }
3333 }
3334
3335 return -EINVAL;
3336}
3337EXPORT_SYMBOL(drm_object_property_get_value);
3338
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003339/**
3340 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3341 * @dev: DRM device
3342 * @data: ioctl data
3343 * @file_priv: DRM file info
3344 *
3345 * This function retrieves the current value for an connectors's property.
3346 *
3347 * Called by the user via ioctl.
3348 *
3349 * Returns:
3350 * Zero on success, errno on failure.
3351 */
Dave Airlief453ba02008-11-07 14:05:41 -08003352int drm_mode_getproperty_ioctl(struct drm_device *dev,
3353 void *data, struct drm_file *file_priv)
3354{
3355 struct drm_mode_object *obj;
3356 struct drm_mode_get_property *out_resp = data;
3357 struct drm_property *property;
3358 int enum_count = 0;
3359 int blob_count = 0;
3360 int value_count = 0;
3361 int ret = 0, i;
3362 int copied;
3363 struct drm_property_enum *prop_enum;
3364 struct drm_mode_property_enum __user *enum_ptr;
3365 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003366 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003367 uint64_t __user *values_ptr;
3368 uint32_t __user *blob_length_ptr;
3369
Dave Airliefb3b06c2011-02-08 13:55:21 +10003370 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3371 return -EINVAL;
3372
Daniel Vetter84849902012-12-02 00:28:11 +01003373 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003374 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3375 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003376 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003377 goto done;
3378 }
3379 property = obj_to_property(obj);
3380
Rob Clark49e27542012-05-17 02:23:26 -06003381 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003382 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3383 enum_count++;
3384 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3385 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3386 blob_count++;
3387 }
3388
3389 value_count = property->num_values;
3390
3391 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3392 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3393 out_resp->flags = property->flags;
3394
3395 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003396 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003397 for (i = 0; i < value_count; i++) {
3398 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3399 ret = -EFAULT;
3400 goto done;
3401 }
3402 }
3403 }
3404 out_resp->count_values = value_count;
3405
Rob Clark49e27542012-05-17 02:23:26 -06003406 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003407 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3408 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003409 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003410 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3411
3412 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3413 ret = -EFAULT;
3414 goto done;
3415 }
3416
3417 if (copy_to_user(&enum_ptr[copied].name,
3418 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3419 ret = -EFAULT;
3420 goto done;
3421 }
3422 copied++;
3423 }
3424 }
3425 out_resp->count_enum_blobs = enum_count;
3426 }
3427
3428 if (property->flags & DRM_MODE_PROP_BLOB) {
3429 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3430 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003431 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3432 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003433
3434 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3435 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3436 ret = -EFAULT;
3437 goto done;
3438 }
3439
3440 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3441 ret = -EFAULT;
3442 goto done;
3443 }
3444
3445 copied++;
3446 }
3447 }
3448 out_resp->count_enum_blobs = blob_count;
3449 }
3450done:
Daniel Vetter84849902012-12-02 00:28:11 +01003451 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003452 return ret;
3453}
3454
3455static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3456 void *data)
3457{
3458 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003459 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003460
3461 if (!length || !data)
3462 return NULL;
3463
3464 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3465 if (!blob)
3466 return NULL;
3467
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003468 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3469 if (ret) {
3470 kfree(blob);
3471 return NULL;
3472 }
3473
Dave Airlief453ba02008-11-07 14:05:41 -08003474 blob->length = length;
3475
3476 memcpy(blob->data, data, length);
3477
Dave Airlief453ba02008-11-07 14:05:41 -08003478 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3479 return blob;
3480}
3481
3482static void drm_property_destroy_blob(struct drm_device *dev,
3483 struct drm_property_blob *blob)
3484{
3485 drm_mode_object_put(dev, &blob->base);
3486 list_del(&blob->head);
3487 kfree(blob);
3488}
3489
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003490/**
3491 * drm_mode_getblob_ioctl - get the contents of a blob property value
3492 * @dev: DRM device
3493 * @data: ioctl data
3494 * @file_priv: DRM file info
3495 *
3496 * This function retrieves the contents of a blob property. The value stored in
3497 * an object's blob property is just a normal modeset object id.
3498 *
3499 * Called by the user via ioctl.
3500 *
3501 * Returns:
3502 * Zero on success, errno on failure.
3503 */
Dave Airlief453ba02008-11-07 14:05:41 -08003504int drm_mode_getblob_ioctl(struct drm_device *dev,
3505 void *data, struct drm_file *file_priv)
3506{
3507 struct drm_mode_object *obj;
3508 struct drm_mode_get_blob *out_resp = data;
3509 struct drm_property_blob *blob;
3510 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003511 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003512
Dave Airliefb3b06c2011-02-08 13:55:21 +10003513 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3514 return -EINVAL;
3515
Daniel Vetter84849902012-12-02 00:28:11 +01003516 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003517 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3518 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003519 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003520 goto done;
3521 }
3522 blob = obj_to_blob(obj);
3523
3524 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003525 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003526 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3527 ret = -EFAULT;
3528 goto done;
3529 }
3530 }
3531 out_resp->length = blob->length;
3532
3533done:
Daniel Vetter84849902012-12-02 00:28:11 +01003534 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003535 return ret;
3536}
3537
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003538/**
3539 * drm_mode_connector_update_edid_property - update the edid property of a connector
3540 * @connector: drm connector
3541 * @edid: new value of the edid property
3542 *
3543 * This function creates a new blob modeset object and assigns its id to the
3544 * connector's edid property.
3545 *
3546 * Returns:
3547 * Zero on success, errno on failure.
3548 */
Dave Airlief453ba02008-11-07 14:05:41 -08003549int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3550 struct edid *edid)
3551{
3552 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003553 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003554
3555 if (connector->edid_blob_ptr)
3556 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3557
3558 /* Delete edid, when there is none. */
3559 if (!edid) {
3560 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003561 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003562 return ret;
3563 }
3564
Adam Jackson7466f4c2010-03-29 21:43:23 +00003565 size = EDID_LENGTH * (1 + edid->extensions);
3566 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3567 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003568 if (!connector->edid_blob_ptr)
3569 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003570
Rob Clark58495562012-10-11 20:50:56 -05003571 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003572 dev->mode_config.edid_property,
3573 connector->edid_blob_ptr->base.id);
3574
3575 return ret;
3576}
3577EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3578
Paulo Zanoni26a34812012-05-15 18:08:59 -03003579static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003580 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003581{
3582 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3583 return false;
3584 if (property->flags & DRM_MODE_PROP_RANGE) {
3585 if (value < property->values[0] || value > property->values[1])
3586 return false;
3587 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003588 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3589 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003590 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003591 for (i = 0; i < property->num_values; i++)
3592 valid_mask |= (1ULL << property->values[i]);
3593 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003594 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3595 /* Only the driver knows */
3596 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003597 } else {
3598 int i;
3599 for (i = 0; i < property->num_values; i++)
3600 if (property->values[i] == value)
3601 return true;
3602 return false;
3603 }
3604}
3605
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003606/**
3607 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3608 * @dev: DRM device
3609 * @data: ioctl data
3610 * @file_priv: DRM file info
3611 *
3612 * This function sets the current value for a connectors's property. It also
3613 * calls into a driver's ->set_property callback to update the hardware state
3614 *
3615 * Called by the user via ioctl.
3616 *
3617 * Returns:
3618 * Zero on success, errno on failure.
3619 */
Dave Airlief453ba02008-11-07 14:05:41 -08003620int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3621 void *data, struct drm_file *file_priv)
3622{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003623 struct drm_mode_connector_set_property *conn_set_prop = data;
3624 struct drm_mode_obj_set_property obj_set_prop = {
3625 .value = conn_set_prop->value,
3626 .prop_id = conn_set_prop->prop_id,
3627 .obj_id = conn_set_prop->connector_id,
3628 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3629 };
Dave Airlief453ba02008-11-07 14:05:41 -08003630
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003631 /* It does all the locking and checking we need */
3632 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003633}
3634
Paulo Zanonic5431882012-05-15 18:09:02 -03003635static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3636 struct drm_property *property,
3637 uint64_t value)
3638{
3639 int ret = -EINVAL;
3640 struct drm_connector *connector = obj_to_connector(obj);
3641
3642 /* Do DPMS ourselves */
3643 if (property == connector->dev->mode_config.dpms_property) {
3644 if (connector->funcs->dpms)
3645 (*connector->funcs->dpms)(connector, (int)value);
3646 ret = 0;
3647 } else if (connector->funcs->set_property)
3648 ret = connector->funcs->set_property(connector, property, value);
3649
3650 /* store the property value if successful */
3651 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003652 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003653 return ret;
3654}
3655
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003656static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3657 struct drm_property *property,
3658 uint64_t value)
3659{
3660 int ret = -EINVAL;
3661 struct drm_crtc *crtc = obj_to_crtc(obj);
3662
3663 if (crtc->funcs->set_property)
3664 ret = crtc->funcs->set_property(crtc, property, value);
3665 if (!ret)
3666 drm_object_property_set_value(obj, property, value);
3667
3668 return ret;
3669}
3670
Rob Clark4d939142012-05-17 02:23:27 -06003671static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3672 struct drm_property *property,
3673 uint64_t value)
3674{
3675 int ret = -EINVAL;
3676 struct drm_plane *plane = obj_to_plane(obj);
3677
3678 if (plane->funcs->set_property)
3679 ret = plane->funcs->set_property(plane, property, value);
3680 if (!ret)
3681 drm_object_property_set_value(obj, property, value);
3682
3683 return ret;
3684}
3685
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003686/**
3687 * drm_mode_getproperty_ioctl - get the current value of a object's property
3688 * @dev: DRM device
3689 * @data: ioctl data
3690 * @file_priv: DRM file info
3691 *
3692 * This function retrieves the current value for an object's property. Compared
3693 * to the connector specific ioctl this one is extended to also work on crtc and
3694 * plane objects.
3695 *
3696 * Called by the user via ioctl.
3697 *
3698 * Returns:
3699 * Zero on success, errno on failure.
3700 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003701int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3702 struct drm_file *file_priv)
3703{
3704 struct drm_mode_obj_get_properties *arg = data;
3705 struct drm_mode_object *obj;
3706 int ret = 0;
3707 int i;
3708 int copied = 0;
3709 int props_count = 0;
3710 uint32_t __user *props_ptr;
3711 uint64_t __user *prop_values_ptr;
3712
3713 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3714 return -EINVAL;
3715
Daniel Vetter84849902012-12-02 00:28:11 +01003716 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003717
3718 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3719 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003720 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003721 goto out;
3722 }
3723 if (!obj->properties) {
3724 ret = -EINVAL;
3725 goto out;
3726 }
3727
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003728 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003729
3730 /* This ioctl is called twice, once to determine how much space is
3731 * needed, and the 2nd time to fill it. */
3732 if ((arg->count_props >= props_count) && props_count) {
3733 copied = 0;
3734 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3735 prop_values_ptr = (uint64_t __user *)(unsigned long)
3736 (arg->prop_values_ptr);
3737 for (i = 0; i < props_count; i++) {
3738 if (put_user(obj->properties->ids[i],
3739 props_ptr + copied)) {
3740 ret = -EFAULT;
3741 goto out;
3742 }
3743 if (put_user(obj->properties->values[i],
3744 prop_values_ptr + copied)) {
3745 ret = -EFAULT;
3746 goto out;
3747 }
3748 copied++;
3749 }
3750 }
3751 arg->count_props = props_count;
3752out:
Daniel Vetter84849902012-12-02 00:28:11 +01003753 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003754 return ret;
3755}
3756
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003757/**
3758 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3759 * @dev: DRM device
3760 * @data: ioctl data
3761 * @file_priv: DRM file info
3762 *
3763 * This function sets the current value for an object's property. It also calls
3764 * into a driver's ->set_property callback to update the hardware state.
3765 * Compared to the connector specific ioctl this one is extended to also work on
3766 * crtc and plane objects.
3767 *
3768 * Called by the user via ioctl.
3769 *
3770 * Returns:
3771 * Zero on success, errno on failure.
3772 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003773int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3774 struct drm_file *file_priv)
3775{
3776 struct drm_mode_obj_set_property *arg = data;
3777 struct drm_mode_object *arg_obj;
3778 struct drm_mode_object *prop_obj;
3779 struct drm_property *property;
3780 int ret = -EINVAL;
3781 int i;
3782
3783 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3784 return -EINVAL;
3785
Daniel Vetter84849902012-12-02 00:28:11 +01003786 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003787
3788 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003789 if (!arg_obj) {
3790 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003791 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003792 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003793 if (!arg_obj->properties)
3794 goto out;
3795
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003796 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003797 if (arg_obj->properties->ids[i] == arg->prop_id)
3798 break;
3799
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003800 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003801 goto out;
3802
3803 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3804 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003805 if (!prop_obj) {
3806 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003807 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003808 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003809 property = obj_to_property(prop_obj);
3810
3811 if (!drm_property_change_is_valid(property, arg->value))
3812 goto out;
3813
3814 switch (arg_obj->type) {
3815 case DRM_MODE_OBJECT_CONNECTOR:
3816 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3817 arg->value);
3818 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003819 case DRM_MODE_OBJECT_CRTC:
3820 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3821 break;
Rob Clark4d939142012-05-17 02:23:27 -06003822 case DRM_MODE_OBJECT_PLANE:
3823 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3824 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003825 }
3826
3827out:
Daniel Vetter84849902012-12-02 00:28:11 +01003828 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003829 return ret;
3830}
3831
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003832/**
3833 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3834 * @connector: connector to attach
3835 * @encoder: encoder to attach @connector to
3836 *
3837 * This function links up a connector to an encoder. Note that the routing
3838 * restrictions between encoders and crtcs are exposed to userspace through the
3839 * possible_clones and possible_crtcs bitmasks.
3840 *
3841 * Returns:
3842 * Zero on success, errno on failure.
3843 */
Dave Airlief453ba02008-11-07 14:05:41 -08003844int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3845 struct drm_encoder *encoder)
3846{
3847 int i;
3848
3849 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3850 if (connector->encoder_ids[i] == 0) {
3851 connector->encoder_ids[i] = encoder->base.id;
3852 return 0;
3853 }
3854 }
3855 return -ENOMEM;
3856}
3857EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3858
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003859/**
3860 * drm_mode_crtc_set_gamma_size - set the gamma table size
3861 * @crtc: CRTC to set the gamma table size for
3862 * @gamma_size: size of the gamma table
3863 *
3864 * Drivers which support gamma tables should set this to the supported gamma
3865 * table size when initializing the CRTC. Currently the drm core only supports a
3866 * fixed gamma table size.
3867 *
3868 * Returns:
3869 * Zero on success, errno on failure.
3870 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003871int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003872 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08003873{
3874 crtc->gamma_size = gamma_size;
3875
3876 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3877 if (!crtc->gamma_store) {
3878 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003879 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003880 }
3881
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003882 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003883}
3884EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3885
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003886/**
3887 * drm_mode_gamma_set_ioctl - set the gamma table
3888 * @dev: DRM device
3889 * @data: ioctl data
3890 * @file_priv: DRM file info
3891 *
3892 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3893 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3894 *
3895 * Called by the user via ioctl.
3896 *
3897 * Returns:
3898 * Zero on success, errno on failure.
3899 */
Dave Airlief453ba02008-11-07 14:05:41 -08003900int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3901 void *data, struct drm_file *file_priv)
3902{
3903 struct drm_mode_crtc_lut *crtc_lut = data;
3904 struct drm_mode_object *obj;
3905 struct drm_crtc *crtc;
3906 void *r_base, *g_base, *b_base;
3907 int size;
3908 int ret = 0;
3909
Dave Airliefb3b06c2011-02-08 13:55:21 +10003910 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3911 return -EINVAL;
3912
Daniel Vetter84849902012-12-02 00:28:11 +01003913 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003914 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3915 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003916 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003917 goto out;
3918 }
3919 crtc = obj_to_crtc(obj);
3920
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003921 if (crtc->funcs->gamma_set == NULL) {
3922 ret = -ENOSYS;
3923 goto out;
3924 }
3925
Dave Airlief453ba02008-11-07 14:05:41 -08003926 /* memcpy into gamma store */
3927 if (crtc_lut->gamma_size != crtc->gamma_size) {
3928 ret = -EINVAL;
3929 goto out;
3930 }
3931
3932 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3933 r_base = crtc->gamma_store;
3934 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3935 ret = -EFAULT;
3936 goto out;
3937 }
3938
3939 g_base = r_base + size;
3940 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3941 ret = -EFAULT;
3942 goto out;
3943 }
3944
3945 b_base = g_base + size;
3946 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3947 ret = -EFAULT;
3948 goto out;
3949 }
3950
James Simmons72034252010-08-03 01:33:19 +01003951 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003952
3953out:
Daniel Vetter84849902012-12-02 00:28:11 +01003954 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003955 return ret;
3956
3957}
3958
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003959/**
3960 * drm_mode_gamma_get_ioctl - get the gamma table
3961 * @dev: DRM device
3962 * @data: ioctl data
3963 * @file_priv: DRM file info
3964 *
3965 * Copy the current gamma table into the storage provided. This also provides
3966 * the gamma table size the driver expects, which can be used to size the
3967 * allocated storage.
3968 *
3969 * Called by the user via ioctl.
3970 *
3971 * Returns:
3972 * Zero on success, errno on failure.
3973 */
Dave Airlief453ba02008-11-07 14:05:41 -08003974int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3975 void *data, struct drm_file *file_priv)
3976{
3977 struct drm_mode_crtc_lut *crtc_lut = data;
3978 struct drm_mode_object *obj;
3979 struct drm_crtc *crtc;
3980 void *r_base, *g_base, *b_base;
3981 int size;
3982 int ret = 0;
3983
Dave Airliefb3b06c2011-02-08 13:55:21 +10003984 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3985 return -EINVAL;
3986
Daniel Vetter84849902012-12-02 00:28:11 +01003987 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003988 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3989 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003990 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003991 goto out;
3992 }
3993 crtc = obj_to_crtc(obj);
3994
3995 /* memcpy into gamma store */
3996 if (crtc_lut->gamma_size != crtc->gamma_size) {
3997 ret = -EINVAL;
3998 goto out;
3999 }
4000
4001 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4002 r_base = crtc->gamma_store;
4003 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4004 ret = -EFAULT;
4005 goto out;
4006 }
4007
4008 g_base = r_base + size;
4009 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4010 ret = -EFAULT;
4011 goto out;
4012 }
4013
4014 b_base = g_base + size;
4015 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4016 ret = -EFAULT;
4017 goto out;
4018 }
4019out:
Daniel Vetter84849902012-12-02 00:28:11 +01004020 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004021 return ret;
4022}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004023
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004024/**
4025 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4026 * @dev: DRM device
4027 * @data: ioctl data
4028 * @file_priv: DRM file info
4029 *
4030 * This schedules an asynchronous update on a given CRTC, called page flip.
4031 * Optionally a drm event is generated to signal the completion of the event.
4032 * Generic drivers cannot assume that a pageflip with changed framebuffer
4033 * properties (including driver specific metadata like tiling layout) will work,
4034 * but some drivers support e.g. pixel format changes through the pageflip
4035 * ioctl.
4036 *
4037 * Called by the user via ioctl.
4038 *
4039 * Returns:
4040 * Zero on success, errno on failure.
4041 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004042int drm_mode_page_flip_ioctl(struct drm_device *dev,
4043 void *data, struct drm_file *file_priv)
4044{
4045 struct drm_mode_crtc_page_flip *page_flip = data;
4046 struct drm_mode_object *obj;
4047 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004048 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004049 struct drm_pending_vblank_event *e = NULL;
4050 unsigned long flags;
4051 int ret = -EINVAL;
4052
4053 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4054 page_flip->reserved != 0)
4055 return -EINVAL;
4056
Keith Packard62f21042013-07-22 18:50:00 -07004057 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4058 return -EINVAL;
4059
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004060 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4061 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004062 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004063 crtc = obj_to_crtc(obj);
4064
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004065 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01004066 if (crtc->fb == NULL) {
4067 /* The framebuffer is currently unbound, presumably
4068 * due to a hotplug event, that userspace has not
4069 * yet discovered.
4070 */
4071 ret = -EBUSY;
4072 goto out;
4073 }
4074
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004075 if (crtc->funcs->page_flip == NULL)
4076 goto out;
4077
Daniel Vetter786b99e2012-12-02 21:53:40 +01004078 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004079 if (!fb) {
4080 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004081 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004082 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004083
Damien Lespiauc11e9282013-09-25 16:45:30 +01004084 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4085 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004086 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004087
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004088 if (crtc->fb->pixel_format != fb->pixel_format) {
4089 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4090 ret = -EINVAL;
4091 goto out;
4092 }
4093
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004094 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4095 ret = -ENOMEM;
4096 spin_lock_irqsave(&dev->event_lock, flags);
4097 if (file_priv->event_space < sizeof e->event) {
4098 spin_unlock_irqrestore(&dev->event_lock, flags);
4099 goto out;
4100 }
4101 file_priv->event_space -= sizeof e->event;
4102 spin_unlock_irqrestore(&dev->event_lock, flags);
4103
4104 e = kzalloc(sizeof *e, GFP_KERNEL);
4105 if (e == NULL) {
4106 spin_lock_irqsave(&dev->event_lock, flags);
4107 file_priv->event_space += sizeof e->event;
4108 spin_unlock_irqrestore(&dev->event_lock, flags);
4109 goto out;
4110 }
4111
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004112 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004113 e->event.base.length = sizeof e->event;
4114 e->event.user_data = page_flip->user_data;
4115 e->base.event = &e->event.base;
4116 e->base.file_priv = file_priv;
4117 e->base.destroy =
4118 (void (*) (struct drm_pending_event *)) kfree;
4119 }
4120
Daniel Vetterb0d12322012-12-11 01:07:12 +01004121 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004122 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004123 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004124 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4125 spin_lock_irqsave(&dev->event_lock, flags);
4126 file_priv->event_space += sizeof e->event;
4127 spin_unlock_irqrestore(&dev->event_lock, flags);
4128 kfree(e);
4129 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004130 /* Keep the old fb, don't unref it. */
4131 old_fb = NULL;
4132 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004133 /*
4134 * Warn if the driver hasn't properly updated the crtc->fb
4135 * field to reflect that the new framebuffer is now used.
4136 * Failing to do so will screw with the reference counting
4137 * on framebuffers.
4138 */
4139 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004140 /* Unref only the old framebuffer. */
4141 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004142 }
4143
4144out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004145 if (fb)
4146 drm_framebuffer_unreference(fb);
4147 if (old_fb)
4148 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004149 mutex_unlock(&crtc->mutex);
4150
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004151 return ret;
4152}
Chris Wilsoneb033552011-01-24 15:11:08 +00004153
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004154/**
4155 * drm_mode_config_reset - call ->reset callbacks
4156 * @dev: drm device
4157 *
4158 * This functions calls all the crtc's, encoder's and connector's ->reset
4159 * callback. Drivers can use this in e.g. their driver load or resume code to
4160 * reset hardware and software state.
4161 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004162void drm_mode_config_reset(struct drm_device *dev)
4163{
4164 struct drm_crtc *crtc;
4165 struct drm_encoder *encoder;
4166 struct drm_connector *connector;
4167
4168 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4169 if (crtc->funcs->reset)
4170 crtc->funcs->reset(crtc);
4171
4172 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4173 if (encoder->funcs->reset)
4174 encoder->funcs->reset(encoder);
4175
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004176 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4177 connector->status = connector_status_unknown;
4178
Chris Wilsoneb033552011-01-24 15:11:08 +00004179 if (connector->funcs->reset)
4180 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004181 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004182}
4183EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004184
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004185/**
4186 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4187 * @dev: DRM device
4188 * @data: ioctl data
4189 * @file_priv: DRM file info
4190 *
4191 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4192 * TTM or something else entirely) and returns the resulting buffer handle. This
4193 * handle can then be wrapped up into a framebuffer modeset object.
4194 *
4195 * Note that userspace is not allowed to use such objects for render
4196 * acceleration - drivers must create their own private ioctls for such a use
4197 * case.
4198 *
4199 * Called by the user via ioctl.
4200 *
4201 * Returns:
4202 * Zero on success, errno on failure.
4203 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004204int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4205 void *data, struct drm_file *file_priv)
4206{
4207 struct drm_mode_create_dumb *args = data;
4208
4209 if (!dev->driver->dumb_create)
4210 return -ENOSYS;
4211 return dev->driver->dumb_create(file_priv, dev, args);
4212}
4213
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004214/**
4215 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4216 * @dev: DRM device
4217 * @data: ioctl data
4218 * @file_priv: DRM file info
4219 *
4220 * Allocate an offset in the drm device node's address space to be able to
4221 * memory map a dumb buffer.
4222 *
4223 * Called by the user via ioctl.
4224 *
4225 * Returns:
4226 * Zero on success, errno on failure.
4227 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004228int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4229 void *data, struct drm_file *file_priv)
4230{
4231 struct drm_mode_map_dumb *args = data;
4232
4233 /* call driver ioctl to get mmap offset */
4234 if (!dev->driver->dumb_map_offset)
4235 return -ENOSYS;
4236
4237 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4238}
4239
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004240/**
4241 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4242 * @dev: DRM device
4243 * @data: ioctl data
4244 * @file_priv: DRM file info
4245 *
4246 * This destroys the userspace handle for the given dumb backing storage buffer.
4247 * Since buffer objects must be reference counted in the kernel a buffer object
4248 * won't be immediately freed if a framebuffer modeset object still uses it.
4249 *
4250 * Called by the user via ioctl.
4251 *
4252 * Returns:
4253 * Zero on success, errno on failure.
4254 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004255int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4256 void *data, struct drm_file *file_priv)
4257{
4258 struct drm_mode_destroy_dumb *args = data;
4259
4260 if (!dev->driver->dumb_destroy)
4261 return -ENOSYS;
4262
4263 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4264}
Dave Airlie248dbc22011-11-29 20:02:54 +00004265
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004266/**
4267 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4268 * @format: pixel format (DRM_FORMAT_*)
4269 * @depth: storage for the depth value
4270 * @bpp: storage for the bpp value
4271 *
4272 * This only supports RGB formats here for compat with code that doesn't use
4273 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004274 */
4275void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4276 int *bpp)
4277{
4278 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004279 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004280 case DRM_FORMAT_RGB332:
4281 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004282 *depth = 8;
4283 *bpp = 8;
4284 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004285 case DRM_FORMAT_XRGB1555:
4286 case DRM_FORMAT_XBGR1555:
4287 case DRM_FORMAT_RGBX5551:
4288 case DRM_FORMAT_BGRX5551:
4289 case DRM_FORMAT_ARGB1555:
4290 case DRM_FORMAT_ABGR1555:
4291 case DRM_FORMAT_RGBA5551:
4292 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004293 *depth = 15;
4294 *bpp = 16;
4295 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004296 case DRM_FORMAT_RGB565:
4297 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004298 *depth = 16;
4299 *bpp = 16;
4300 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004301 case DRM_FORMAT_RGB888:
4302 case DRM_FORMAT_BGR888:
4303 *depth = 24;
4304 *bpp = 24;
4305 break;
4306 case DRM_FORMAT_XRGB8888:
4307 case DRM_FORMAT_XBGR8888:
4308 case DRM_FORMAT_RGBX8888:
4309 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004310 *depth = 24;
4311 *bpp = 32;
4312 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004313 case DRM_FORMAT_XRGB2101010:
4314 case DRM_FORMAT_XBGR2101010:
4315 case DRM_FORMAT_RGBX1010102:
4316 case DRM_FORMAT_BGRX1010102:
4317 case DRM_FORMAT_ARGB2101010:
4318 case DRM_FORMAT_ABGR2101010:
4319 case DRM_FORMAT_RGBA1010102:
4320 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004321 *depth = 30;
4322 *bpp = 32;
4323 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004324 case DRM_FORMAT_ARGB8888:
4325 case DRM_FORMAT_ABGR8888:
4326 case DRM_FORMAT_RGBA8888:
4327 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004328 *depth = 32;
4329 *bpp = 32;
4330 break;
4331 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004332 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4333 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004334 *depth = 0;
4335 *bpp = 0;
4336 break;
4337 }
4338}
4339EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004340
4341/**
4342 * drm_format_num_planes - get the number of planes for format
4343 * @format: pixel format (DRM_FORMAT_*)
4344 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004345 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004346 * The number of planes used by the specified pixel format.
4347 */
4348int drm_format_num_planes(uint32_t format)
4349{
4350 switch (format) {
4351 case DRM_FORMAT_YUV410:
4352 case DRM_FORMAT_YVU410:
4353 case DRM_FORMAT_YUV411:
4354 case DRM_FORMAT_YVU411:
4355 case DRM_FORMAT_YUV420:
4356 case DRM_FORMAT_YVU420:
4357 case DRM_FORMAT_YUV422:
4358 case DRM_FORMAT_YVU422:
4359 case DRM_FORMAT_YUV444:
4360 case DRM_FORMAT_YVU444:
4361 return 3;
4362 case DRM_FORMAT_NV12:
4363 case DRM_FORMAT_NV21:
4364 case DRM_FORMAT_NV16:
4365 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004366 case DRM_FORMAT_NV24:
4367 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004368 return 2;
4369 default:
4370 return 1;
4371 }
4372}
4373EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004374
4375/**
4376 * drm_format_plane_cpp - determine the bytes per pixel value
4377 * @format: pixel format (DRM_FORMAT_*)
4378 * @plane: plane index
4379 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004380 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004381 * The bytes per pixel value for the specified plane.
4382 */
4383int drm_format_plane_cpp(uint32_t format, int plane)
4384{
4385 unsigned int depth;
4386 int bpp;
4387
4388 if (plane >= drm_format_num_planes(format))
4389 return 0;
4390
4391 switch (format) {
4392 case DRM_FORMAT_YUYV:
4393 case DRM_FORMAT_YVYU:
4394 case DRM_FORMAT_UYVY:
4395 case DRM_FORMAT_VYUY:
4396 return 2;
4397 case DRM_FORMAT_NV12:
4398 case DRM_FORMAT_NV21:
4399 case DRM_FORMAT_NV16:
4400 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004401 case DRM_FORMAT_NV24:
4402 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004403 return plane ? 2 : 1;
4404 case DRM_FORMAT_YUV410:
4405 case DRM_FORMAT_YVU410:
4406 case DRM_FORMAT_YUV411:
4407 case DRM_FORMAT_YVU411:
4408 case DRM_FORMAT_YUV420:
4409 case DRM_FORMAT_YVU420:
4410 case DRM_FORMAT_YUV422:
4411 case DRM_FORMAT_YVU422:
4412 case DRM_FORMAT_YUV444:
4413 case DRM_FORMAT_YVU444:
4414 return 1;
4415 default:
4416 drm_fb_get_bpp_depth(format, &depth, &bpp);
4417 return bpp >> 3;
4418 }
4419}
4420EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004421
4422/**
4423 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4424 * @format: pixel format (DRM_FORMAT_*)
4425 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004426 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004427 * The horizontal chroma subsampling factor for the
4428 * specified pixel format.
4429 */
4430int drm_format_horz_chroma_subsampling(uint32_t format)
4431{
4432 switch (format) {
4433 case DRM_FORMAT_YUV411:
4434 case DRM_FORMAT_YVU411:
4435 case DRM_FORMAT_YUV410:
4436 case DRM_FORMAT_YVU410:
4437 return 4;
4438 case DRM_FORMAT_YUYV:
4439 case DRM_FORMAT_YVYU:
4440 case DRM_FORMAT_UYVY:
4441 case DRM_FORMAT_VYUY:
4442 case DRM_FORMAT_NV12:
4443 case DRM_FORMAT_NV21:
4444 case DRM_FORMAT_NV16:
4445 case DRM_FORMAT_NV61:
4446 case DRM_FORMAT_YUV422:
4447 case DRM_FORMAT_YVU422:
4448 case DRM_FORMAT_YUV420:
4449 case DRM_FORMAT_YVU420:
4450 return 2;
4451 default:
4452 return 1;
4453 }
4454}
4455EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4456
4457/**
4458 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4459 * @format: pixel format (DRM_FORMAT_*)
4460 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004461 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004462 * The vertical chroma subsampling factor for the
4463 * specified pixel format.
4464 */
4465int drm_format_vert_chroma_subsampling(uint32_t format)
4466{
4467 switch (format) {
4468 case DRM_FORMAT_YUV410:
4469 case DRM_FORMAT_YVU410:
4470 return 4;
4471 case DRM_FORMAT_YUV420:
4472 case DRM_FORMAT_YVU420:
4473 case DRM_FORMAT_NV12:
4474 case DRM_FORMAT_NV21:
4475 return 2;
4476 default:
4477 return 1;
4478 }
4479}
4480EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004481
4482/**
4483 * drm_mode_config_init - initialize DRM mode_configuration structure
4484 * @dev: DRM device
4485 *
4486 * Initialize @dev's mode_config structure, used for tracking the graphics
4487 * configuration of @dev.
4488 *
4489 * Since this initializes the modeset locks, no locking is possible. Which is no
4490 * problem, since this should happen single threaded at init time. It is the
4491 * driver's problem to ensure this guarantee.
4492 *
4493 */
4494void drm_mode_config_init(struct drm_device *dev)
4495{
4496 mutex_init(&dev->mode_config.mutex);
4497 mutex_init(&dev->mode_config.idr_mutex);
4498 mutex_init(&dev->mode_config.fb_lock);
4499 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4500 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4501 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004502 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004503 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4504 INIT_LIST_HEAD(&dev->mode_config.property_list);
4505 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4506 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4507 idr_init(&dev->mode_config.crtc_idr);
4508
4509 drm_modeset_lock_all(dev);
4510 drm_mode_create_standard_connector_properties(dev);
4511 drm_modeset_unlock_all(dev);
4512
4513 /* Just to be sure */
4514 dev->mode_config.num_fb = 0;
4515 dev->mode_config.num_connector = 0;
4516 dev->mode_config.num_crtc = 0;
4517 dev->mode_config.num_encoder = 0;
4518}
4519EXPORT_SYMBOL(drm_mode_config_init);
4520
4521/**
4522 * drm_mode_config_cleanup - free up DRM mode_config info
4523 * @dev: DRM device
4524 *
4525 * Free up all the connectors and CRTCs associated with this DRM device, then
4526 * free up the framebuffers and associated buffer objects.
4527 *
4528 * Note that since this /should/ happen single-threaded at driver/device
4529 * teardown time, no locking is required. It's the driver's job to ensure that
4530 * this guarantee actually holds true.
4531 *
4532 * FIXME: cleanup any dangling user buffer objects too
4533 */
4534void drm_mode_config_cleanup(struct drm_device *dev)
4535{
4536 struct drm_connector *connector, *ot;
4537 struct drm_crtc *crtc, *ct;
4538 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004539 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004540 struct drm_framebuffer *fb, *fbt;
4541 struct drm_property *property, *pt;
4542 struct drm_property_blob *blob, *bt;
4543 struct drm_plane *plane, *plt;
4544
4545 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4546 head) {
4547 encoder->funcs->destroy(encoder);
4548 }
4549
Sean Paul3b336ec2013-08-14 16:47:37 -04004550 list_for_each_entry_safe(bridge, brt,
4551 &dev->mode_config.bridge_list, head) {
4552 bridge->funcs->destroy(bridge);
4553 }
4554
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004555 list_for_each_entry_safe(connector, ot,
4556 &dev->mode_config.connector_list, head) {
4557 connector->funcs->destroy(connector);
4558 }
4559
4560 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4561 head) {
4562 drm_property_destroy(dev, property);
4563 }
4564
4565 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4566 head) {
4567 drm_property_destroy_blob(dev, blob);
4568 }
4569
4570 /*
4571 * Single-threaded teardown context, so it's not required to grab the
4572 * fb_lock to protect against concurrent fb_list access. Contrary, it
4573 * would actually deadlock with the drm_framebuffer_cleanup function.
4574 *
4575 * Also, if there are any framebuffers left, that's a driver leak now,
4576 * so politely WARN about this.
4577 */
4578 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4579 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4580 drm_framebuffer_remove(fb);
4581 }
4582
4583 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4584 head) {
4585 plane->funcs->destroy(plane);
4586 }
4587
4588 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4589 crtc->funcs->destroy(crtc);
4590 }
4591
4592 idr_destroy(&dev->mode_config.crtc_idr);
4593}
4594EXPORT_SYMBOL(drm_mode_config_cleanup);