blob: 6127073407e0629ca83095bd9b9365c5dbb57fef [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
Rob Clark9922ab52014-04-01 20:16:57 -0400124static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
125{
126 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
127 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
128 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
129};
130
Dave Airlief453ba02008-11-07 14:05:41 -0800131/*
132 * Optional properties
133 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000134static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800135{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700136 { DRM_MODE_SCALE_NONE, "None" },
137 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
138 { DRM_MODE_SCALE_CENTER, "Center" },
139 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800140};
141
Dave Airlief453ba02008-11-07 14:05:41 -0800142/*
143 * Non-global properties, but "required" for certain connectors.
144 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000145static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800146{
147 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
148 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
149 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
150};
151
152DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
153
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000154static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800155{
156 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
157 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
158 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
159};
160
161DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
162 drm_dvi_i_subconnector_enum_list)
163
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000164static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800165{
166 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
167 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
169 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200170 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800171};
172
173DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
174
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000175static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800176{
177 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
178 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
179 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
180 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200181 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800182};
183
184DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
185 drm_tv_subconnector_enum_list)
186
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000187static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000188 { DRM_MODE_DIRTY_OFF, "Off" },
189 { DRM_MODE_DIRTY_ON, "On" },
190 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
191};
192
Dave Airlief453ba02008-11-07 14:05:41 -0800193struct drm_conn_prop_enum_list {
194 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000195 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400196 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800197};
198
199/*
200 * Connector and encoder types.
201 */
202static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400203{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
204 { DRM_MODE_CONNECTOR_VGA, "VGA" },
205 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
206 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
207 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
208 { DRM_MODE_CONNECTOR_Composite, "Composite" },
209 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
210 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
211 { DRM_MODE_CONNECTOR_Component, "Component" },
212 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
213 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
214 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
215 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
216 { DRM_MODE_CONNECTOR_TV, "TV" },
217 { DRM_MODE_CONNECTOR_eDP, "eDP" },
218 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300219 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800220};
221
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000222static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800223{ { DRM_MODE_ENCODER_NONE, "None" },
224 { DRM_MODE_ENCODER_DAC, "DAC" },
225 { DRM_MODE_ENCODER_TMDS, "TMDS" },
226 { DRM_MODE_ENCODER_LVDS, "LVDS" },
227 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200228 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300229 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000230 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800231};
232
Jesse Barnesac1bb362014-02-10 15:32:44 -0800233static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
234{
235 { SubPixelUnknown, "Unknown" },
236 { SubPixelHorizontalRGB, "Horizontal RGB" },
237 { SubPixelHorizontalBGR, "Horizontal BGR" },
238 { SubPixelVerticalRGB, "Vertical RGB" },
239 { SubPixelVerticalBGR, "Vertical BGR" },
240 { SubPixelNone, "None" },
241};
242
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400243void drm_connector_ida_init(void)
244{
245 int i;
246
247 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
248 ida_init(&drm_connector_enum_list[i].ida);
249}
250
251void drm_connector_ida_destroy(void)
252{
253 int i;
254
255 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
256 ida_destroy(&drm_connector_enum_list[i].ida);
257}
258
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100259/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100260 * drm_get_connector_status_name - return a string for connector status
261 * @status: connector status to compute name of
262 *
263 * In contrast to the other drm_get_*_name functions this one here returns a
264 * const pointer and hence is threadsafe.
265 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000266const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800267{
268 if (status == connector_status_connected)
269 return "connected";
270 else if (status == connector_status_disconnected)
271 return "disconnected";
272 else
273 return "unknown";
274}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000275EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800276
Jesse Barnesac1bb362014-02-10 15:32:44 -0800277/**
278 * drm_get_subpixel_order_name - return a string for a given subpixel enum
279 * @order: enum of subpixel_order
280 *
281 * Note you could abuse this and return something out of bounds, but that
282 * would be a caller error. No unscrubbed user data should make it here.
283 */
284const char *drm_get_subpixel_order_name(enum subpixel_order order)
285{
286 return drm_subpixel_enum_list[order].name;
287}
288EXPORT_SYMBOL(drm_get_subpixel_order_name);
289
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300290static char printable_char(int c)
291{
292 return isascii(c) && isprint(c) ? c : '?';
293}
294
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100295/**
296 * drm_get_format_name - return a string for drm fourcc format
297 * @format: format to compute name of
298 *
299 * Note that the buffer used by this function is globally shared and owned by
300 * the function itself.
301 *
302 * FIXME: This isn't really multithreading safe.
303 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000304const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300305{
306 static char buf[32];
307
308 snprintf(buf, sizeof(buf),
309 "%c%c%c%c %s-endian (0x%08x)",
310 printable_char(format & 0xff),
311 printable_char((format >> 8) & 0xff),
312 printable_char((format >> 16) & 0xff),
313 printable_char((format >> 24) & 0x7f),
314 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
315 format);
316
317 return buf;
318}
319EXPORT_SYMBOL(drm_get_format_name);
320
Dave Airlief453ba02008-11-07 14:05:41 -0800321/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100322 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800323 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100324 * @obj: object pointer, used to generate unique ID
325 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800326 *
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100328 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
329 * modeset identifiers are _not_ reference counted. Hence don't use this for
330 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800331 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100332 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800333 * New unique (relative to other objects in @dev) integer identifier for the
334 * object.
335 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100336int drm_mode_object_get(struct drm_device *dev,
337 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800338{
Dave Airlief453ba02008-11-07 14:05:41 -0800339 int ret;
340
Jesse Barnesad2563c2009-01-19 17:21:45 +1000341 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800342 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
343 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100344 /*
345 * Set up the object linking under the protection of the idr
346 * lock so that other users can't see inconsistent state.
347 */
Tejun Heo2e928812013-02-27 17:04:08 -0800348 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100349 obj->type = obj_type;
350 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000351 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100352
Tejun Heo2e928812013-02-27 17:04:08 -0800353 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800354}
355
356/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100357 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800358 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100359 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800360 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100361 * Free @id from @dev's unique identifier pool. Note that despite the _get
362 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
363 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800364 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100365void drm_mode_object_put(struct drm_device *dev,
366 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800367{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000368 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800369 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000370 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800371}
372
Rob Clark98f75de2014-05-30 11:37:03 -0400373static struct drm_mode_object *_object_find(struct drm_device *dev,
374 uint32_t id, uint32_t type)
375{
376 struct drm_mode_object *obj = NULL;
377
378 mutex_lock(&dev->mode_config.idr_mutex);
379 obj = idr_find(&dev->mode_config.crtc_idr, id);
380 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
381 (obj->id != id))
382 obj = NULL;
383 mutex_unlock(&dev->mode_config.idr_mutex);
384
385 return obj;
386}
387
Daniel Vetter786b99e2012-12-02 21:53:40 +0100388/**
389 * drm_mode_object_find - look up a drm object with static lifetime
390 * @dev: drm device
391 * @id: id of the mode object
392 * @type: type of the mode object
393 *
394 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400395 * are reference counted, they need special treatment. Even with
396 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
397 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100398 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200399struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
400 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800401{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000402 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800403
Daniel Vetter786b99e2012-12-02 21:53:40 +0100404 /* Framebuffers are reference counted and need their own lookup
405 * function.*/
406 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400407 obj = _object_find(dev, id, type);
408 /* don't leak out unref'd fb's */
409 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000410 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800411 return obj;
412}
413EXPORT_SYMBOL(drm_mode_object_find);
414
415/**
Dave Airlief453ba02008-11-07 14:05:41 -0800416 * drm_framebuffer_init - initialize a framebuffer
417 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100418 * @fb: framebuffer to be initialized
419 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800420 *
Dave Airlief453ba02008-11-07 14:05:41 -0800421 * Allocates an ID for the framebuffer's parent mode object, sets its mode
422 * functions & device file and adds it to the master fd list.
423 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100424 * IMPORTANT:
425 * This functions publishes the fb and makes it available for concurrent access
426 * by other users. Which means by this point the fb _must_ be fully set up -
427 * since all the fb attributes are invariant over its lifetime, no further
428 * locking but only correct reference counting is required.
429 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100430 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200431 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800432 */
433int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
434 const struct drm_framebuffer_funcs *funcs)
435{
436 int ret;
437
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100438 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000439 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100440 INIT_LIST_HEAD(&fb->filp_head);
441 fb->dev = dev;
442 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000443
Dave Airlief453ba02008-11-07 14:05:41 -0800444 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200445 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100446 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800447
Daniel Vetter2b677e82012-12-10 21:16:05 +0100448 /* Grab the idr reference. */
449 drm_framebuffer_reference(fb);
450
Dave Airlief453ba02008-11-07 14:05:41 -0800451 dev->mode_config.num_fb++;
452 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100453out:
454 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800455
456 return 0;
457}
458EXPORT_SYMBOL(drm_framebuffer_init);
459
Rob Clarkf7eff602012-09-05 21:48:38 +0000460static void drm_framebuffer_free(struct kref *kref)
461{
462 struct drm_framebuffer *fb =
463 container_of(kref, struct drm_framebuffer, refcount);
464 fb->funcs->destroy(fb);
465}
466
Daniel Vetter2b677e82012-12-10 21:16:05 +0100467static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
468 uint32_t id)
469{
470 struct drm_mode_object *obj = NULL;
471 struct drm_framebuffer *fb;
472
473 mutex_lock(&dev->mode_config.idr_mutex);
474 obj = idr_find(&dev->mode_config.crtc_idr, id);
475 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
476 fb = NULL;
477 else
478 fb = obj_to_fb(obj);
479 mutex_unlock(&dev->mode_config.idr_mutex);
480
481 return fb;
482}
483
Rob Clarkf7eff602012-09-05 21:48:38 +0000484/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100485 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
486 * @dev: drm device
487 * @id: id of the fb object
488 *
489 * If successful, this grabs an additional reference to the framebuffer -
490 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100491 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100492 */
493struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
494 uint32_t id)
495{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100496 struct drm_framebuffer *fb;
497
498 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100499 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100500 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000501 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100502 mutex_unlock(&dev->mode_config.fb_lock);
503
504 return fb;
505}
506EXPORT_SYMBOL(drm_framebuffer_lookup);
507
508/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000509 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100510 * @fb: framebuffer to unref
511 *
512 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000513 */
514void drm_framebuffer_unreference(struct drm_framebuffer *fb)
515{
Rob Clarkf7eff602012-09-05 21:48:38 +0000516 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000517 kref_put(&fb->refcount, drm_framebuffer_free);
518}
519EXPORT_SYMBOL(drm_framebuffer_unreference);
520
521/**
522 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100523 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100524 *
525 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000526 */
527void drm_framebuffer_reference(struct drm_framebuffer *fb)
528{
529 DRM_DEBUG("FB ID: %d\n", fb->base.id);
530 kref_get(&fb->refcount);
531}
532EXPORT_SYMBOL(drm_framebuffer_reference);
533
Daniel Vetter2b677e82012-12-10 21:16:05 +0100534static void drm_framebuffer_free_bug(struct kref *kref)
535{
536 BUG();
537}
538
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100539static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
540{
541 DRM_DEBUG("FB ID: %d\n", fb->base.id);
542 kref_put(&fb->refcount, drm_framebuffer_free_bug);
543}
544
Daniel Vetter2b677e82012-12-10 21:16:05 +0100545/* dev->mode_config.fb_lock must be held! */
546static void __drm_framebuffer_unregister(struct drm_device *dev,
547 struct drm_framebuffer *fb)
548{
549 mutex_lock(&dev->mode_config.idr_mutex);
550 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
551 mutex_unlock(&dev->mode_config.idr_mutex);
552
553 fb->base.id = 0;
554
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100555 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100556}
557
Dave Airlief453ba02008-11-07 14:05:41 -0800558/**
Daniel Vetter36206362012-12-10 20:42:17 +0100559 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
560 * @fb: fb to unregister
561 *
562 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
563 * those used for fbdev. Note that the caller must hold a reference of it's own,
564 * i.e. the object may not be destroyed through this call (since it'll lead to a
565 * locking inversion).
566 */
567void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
568{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100569 struct drm_device *dev = fb->dev;
570
571 mutex_lock(&dev->mode_config.fb_lock);
572 /* Mark fb as reaped and drop idr ref. */
573 __drm_framebuffer_unregister(dev, fb);
574 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100575}
576EXPORT_SYMBOL(drm_framebuffer_unregister_private);
577
578/**
Dave Airlief453ba02008-11-07 14:05:41 -0800579 * drm_framebuffer_cleanup - remove a framebuffer object
580 * @fb: framebuffer to remove
581 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100582 * Cleanup framebuffer. This function is intended to be used from the drivers
583 * ->destroy callback. It can also be used to clean up driver private
584 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100585 *
586 * Note that this function does not remove the fb from active usuage - if it is
587 * still used anywhere, hilarity can ensue since userspace could call getfb on
588 * the id and get back -EINVAL. Obviously no concern at driver unload time.
589 *
590 * Also, the framebuffer will not be removed from the lookup idr - for
591 * user-created framebuffers this will happen in in the rmfb ioctl. For
592 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
593 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800594 */
595void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
596{
597 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100598
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100599 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000600 list_del(&fb->head);
601 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100602 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000603}
604EXPORT_SYMBOL(drm_framebuffer_cleanup);
605
606/**
607 * drm_framebuffer_remove - remove and unreference a framebuffer object
608 * @fb: framebuffer to remove
609 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000610 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100611 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100612 * passed-in framebuffer. Might take the modeset locks.
613 *
614 * Note that this function optimizes the cleanup away if the caller holds the
615 * last reference to the framebuffer. It is also guaranteed to not take the
616 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000617 */
618void drm_framebuffer_remove(struct drm_framebuffer *fb)
619{
620 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800621 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800622 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000623 struct drm_mode_set set;
624 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800625
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100626 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100627
Daniel Vetterb62584e2012-12-11 16:51:35 +0100628 /*
629 * drm ABI mandates that we remove any deleted framebuffers from active
630 * useage. But since most sane clients only remove framebuffers they no
631 * longer need, try to optimize this away.
632 *
633 * Since we're holding a reference ourselves, observing a refcount of 1
634 * means that we're the last holder and can skip it. Also, the refcount
635 * can never increase from 1 again, so we don't need any barriers or
636 * locks.
637 *
638 * Note that userspace could try to race with use and instate a new
639 * usage _after_ we've cleared all current ones. End result will be an
640 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
641 * in this manner.
642 */
643 if (atomic_read(&fb->refcount.refcount) > 1) {
644 drm_modeset_lock_all(dev);
645 /* remove from any CRTC */
646 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700647 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100648 /* should turn off the crtc */
649 memset(&set, 0, sizeof(struct drm_mode_set));
650 set.crtc = crtc;
651 set.fb = NULL;
652 ret = drm_mode_set_config_internal(&set);
653 if (ret)
654 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
655 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000656 }
Dave Airlief453ba02008-11-07 14:05:41 -0800657
Daniel Vetterb62584e2012-12-11 16:51:35 +0100658 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300659 if (plane->fb == fb)
660 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800661 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100662 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800663 }
664
Rob Clarkf7eff602012-09-05 21:48:38 +0000665 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800666}
Rob Clarkf7eff602012-09-05 21:48:38 +0000667EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800668
669/**
Matt Ropere13161a2014-04-01 15:22:38 -0700670 * drm_crtc_init_with_planes - Initialise a new CRTC object with
671 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800672 * @dev: DRM device
673 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700674 * @primary: Primary plane for CRTC
675 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800676 * @funcs: callbacks for the new CRTC
677 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000678 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200679 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100680 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200681 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800682 */
Matt Ropere13161a2014-04-01 15:22:38 -0700683int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
684 struct drm_plane *primary,
685 void *cursor,
686 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800687{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200688 int ret;
689
Dave Airlief453ba02008-11-07 14:05:41 -0800690 crtc->dev = dev;
691 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000692 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800693
Daniel Vetter84849902012-12-02 00:28:11 +0100694 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100695 mutex_init(&crtc->mutex);
696 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200697
698 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
699 if (ret)
700 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800701
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300702 crtc->base.properties = &crtc->properties;
703
Dave Airlief453ba02008-11-07 14:05:41 -0800704 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
705 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200706
Matt Ropere13161a2014-04-01 15:22:38 -0700707 crtc->primary = primary;
708 if (primary)
709 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
710
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200711 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100712 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200713
714 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800715}
Matt Ropere13161a2014-04-01 15:22:38 -0700716EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800717
718/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000719 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800720 * @crtc: CRTC to cleanup
721 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000722 * This function cleans up @crtc and removes it from the DRM mode setting
723 * core. Note that the function does *not* free the crtc structure itself,
724 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800725 */
726void drm_crtc_cleanup(struct drm_crtc *crtc)
727{
728 struct drm_device *dev = crtc->dev;
729
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000730 kfree(crtc->gamma_store);
731 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800732
733 drm_mode_object_put(dev, &crtc->base);
734 list_del(&crtc->head);
735 dev->mode_config.num_crtc--;
736}
737EXPORT_SYMBOL(drm_crtc_cleanup);
738
739/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000740 * drm_crtc_index - find the index of a registered CRTC
741 * @crtc: CRTC to find index for
742 *
743 * Given a registered CRTC, return the index of that CRTC within a DRM
744 * device's list of CRTCs.
745 */
746unsigned int drm_crtc_index(struct drm_crtc *crtc)
747{
748 unsigned int index = 0;
749 struct drm_crtc *tmp;
750
751 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
752 if (tmp == crtc)
753 return index;
754
755 index++;
756 }
757
758 BUG();
759}
760EXPORT_SYMBOL(drm_crtc_index);
761
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100762/*
Dave Airlief453ba02008-11-07 14:05:41 -0800763 * drm_mode_remove - remove and free a mode
764 * @connector: connector list to modify
765 * @mode: mode to remove
766 *
Dave Airlief453ba02008-11-07 14:05:41 -0800767 * Remove @mode from @connector's mode list, then free it.
768 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100769static void drm_mode_remove(struct drm_connector *connector,
770 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800771{
772 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100773 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800774}
Dave Airlief453ba02008-11-07 14:05:41 -0800775
776/**
777 * drm_connector_init - Init a preallocated connector
778 * @dev: DRM device
779 * @connector: the connector to init
780 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100781 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800782 *
Dave Airlief453ba02008-11-07 14:05:41 -0800783 * Initialises a preallocated connector. Connectors should be
784 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200785 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100786 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200787 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800788 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789int drm_connector_init(struct drm_device *dev,
790 struct drm_connector *connector,
791 const struct drm_connector_funcs *funcs,
792 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800793{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400795 struct ida *connector_ida =
796 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200797
Daniel Vetter84849902012-12-02 00:28:11 +0100798 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800799
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200800 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
801 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300802 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200803
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300804 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800805 connector->dev = dev;
806 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800807 connector->connector_type = connector_type;
808 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400809 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
810 if (connector->connector_type_id < 0) {
811 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300812 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400813 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300814 connector->name =
815 kasprintf(GFP_KERNEL, "%s-%d",
816 drm_connector_enum_list[connector_type].name,
817 connector->connector_type_id);
818 if (!connector->name) {
819 ret = -ENOMEM;
820 goto out_put;
821 }
822
Dave Airlief453ba02008-11-07 14:05:41 -0800823 INIT_LIST_HEAD(&connector->probed_modes);
824 INIT_LIST_HEAD(&connector->modes);
825 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000826 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800827
828 list_add_tail(&connector->head, &dev->mode_config.connector_list);
829 dev->mode_config.num_connector++;
830
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200831 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500832 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200833 dev->mode_config.edid_property,
834 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800835
Rob Clark58495562012-10-11 20:50:56 -0500836 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800837 dev->mode_config.dpms_property, 0);
838
Jani Nikula2abdd312014-05-14 16:58:19 +0300839out_put:
840 if (ret)
841 drm_mode_object_put(dev, &connector->base);
842
843out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100844 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200845
846 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800847}
848EXPORT_SYMBOL(drm_connector_init);
849
850/**
851 * drm_connector_cleanup - cleans up an initialised connector
852 * @connector: connector to cleanup
853 *
Dave Airlief453ba02008-11-07 14:05:41 -0800854 * Cleans up the connector but doesn't free the object.
855 */
856void drm_connector_cleanup(struct drm_connector *connector)
857{
858 struct drm_device *dev = connector->dev;
859 struct drm_display_mode *mode, *t;
860
861 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
862 drm_mode_remove(connector, mode);
863
864 list_for_each_entry_safe(mode, t, &connector->modes, head)
865 drm_mode_remove(connector, mode);
866
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400867 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
868 connector->connector_type_id);
869
Dave Airlief453ba02008-11-07 14:05:41 -0800870 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300871 kfree(connector->name);
872 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800873 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000874 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800875}
876EXPORT_SYMBOL(drm_connector_cleanup);
877
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100878/**
879 * drm_connector_unplug_all - unregister connector userspace interfaces
880 * @dev: drm device
881 *
882 * This function unregisters all connector userspace interfaces in sysfs. Should
883 * be call when the device is disconnected, e.g. from an usb driver's
884 * ->disconnect callback.
885 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000886void drm_connector_unplug_all(struct drm_device *dev)
887{
888 struct drm_connector *connector;
889
890 /* taking the mode config mutex ends up in a clash with sysfs */
891 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
892 drm_sysfs_connector_remove(connector);
893
894}
895EXPORT_SYMBOL(drm_connector_unplug_all);
896
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100897/**
898 * drm_bridge_init - initialize a drm transcoder/bridge
899 * @dev: drm device
900 * @bridge: transcoder/bridge to set up
901 * @funcs: bridge function table
902 *
903 * Initialises a preallocated bridge. Bridges should be
904 * subclassed as part of driver connector objects.
905 *
906 * Returns:
907 * Zero on success, error code on failure.
908 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400909int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
910 const struct drm_bridge_funcs *funcs)
911{
912 int ret;
913
914 drm_modeset_lock_all(dev);
915
916 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
917 if (ret)
918 goto out;
919
920 bridge->dev = dev;
921 bridge->funcs = funcs;
922
923 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
924 dev->mode_config.num_bridge++;
925
926 out:
927 drm_modeset_unlock_all(dev);
928 return ret;
929}
930EXPORT_SYMBOL(drm_bridge_init);
931
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100932/**
933 * drm_bridge_cleanup - cleans up an initialised bridge
934 * @bridge: bridge to cleanup
935 *
936 * Cleans up the bridge but doesn't free the object.
937 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400938void drm_bridge_cleanup(struct drm_bridge *bridge)
939{
940 struct drm_device *dev = bridge->dev;
941
942 drm_modeset_lock_all(dev);
943 drm_mode_object_put(dev, &bridge->base);
944 list_del(&bridge->head);
945 dev->mode_config.num_bridge--;
946 drm_modeset_unlock_all(dev);
947}
948EXPORT_SYMBOL(drm_bridge_cleanup);
949
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100950/**
951 * drm_encoder_init - Init a preallocated encoder
952 * @dev: drm device
953 * @encoder: the encoder to init
954 * @funcs: callbacks for this encoder
955 * @encoder_type: user visible type of the encoder
956 *
957 * Initialises a preallocated encoder. Encoder should be
958 * subclassed as part of driver encoder objects.
959 *
960 * Returns:
961 * Zero on success, error code on failure.
962 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200963int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000964 struct drm_encoder *encoder,
965 const struct drm_encoder_funcs *funcs,
966 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800967{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200968 int ret;
969
Daniel Vetter84849902012-12-02 00:28:11 +0100970 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800971
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200972 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
973 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +0300974 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800975
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200976 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800977 encoder->encoder_type = encoder_type;
978 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +0300979 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
980 drm_encoder_enum_list[encoder_type].name,
981 encoder->base.id);
982 if (!encoder->name) {
983 ret = -ENOMEM;
984 goto out_put;
985 }
Dave Airlief453ba02008-11-07 14:05:41 -0800986
987 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
988 dev->mode_config.num_encoder++;
989
Jani Nikulae5748942014-05-14 16:58:20 +0300990out_put:
991 if (ret)
992 drm_mode_object_put(dev, &encoder->base);
993
994out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100995 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200996
997 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800998}
999EXPORT_SYMBOL(drm_encoder_init);
1000
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001001/**
1002 * drm_encoder_cleanup - cleans up an initialised encoder
1003 * @encoder: encoder to cleanup
1004 *
1005 * Cleans up the encoder but doesn't free the object.
1006 */
Dave Airlief453ba02008-11-07 14:05:41 -08001007void drm_encoder_cleanup(struct drm_encoder *encoder)
1008{
1009 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001010 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001011 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001012 kfree(encoder->name);
1013 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001014 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001015 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001016 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001017}
1018EXPORT_SYMBOL(drm_encoder_cleanup);
1019
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001020/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001021 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001022 * @dev: DRM device
1023 * @plane: plane object to init
1024 * @possible_crtcs: bitmask of possible CRTCs
1025 * @funcs: callbacks for the new plane
1026 * @formats: array of supported formats (%DRM_FORMAT_*)
1027 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001028 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001029 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001030 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001031 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001032 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001033 * Zero on success, error code on failure.
1034 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001035int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1036 unsigned long possible_crtcs,
1037 const struct drm_plane_funcs *funcs,
1038 const uint32_t *formats, uint32_t format_count,
1039 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001040{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001041 int ret;
1042
Daniel Vetter84849902012-12-02 00:28:11 +01001043 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001044
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001045 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1046 if (ret)
1047 goto out;
1048
Rob Clark4d939142012-05-17 02:23:27 -06001049 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001050 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001051 plane->funcs = funcs;
1052 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1053 GFP_KERNEL);
1054 if (!plane->format_types) {
1055 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1056 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001057 ret = -ENOMEM;
1058 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001059 }
1060
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001061 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001062 plane->format_count = format_count;
1063 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001064 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001065
Matt Roperdc415ff2014-04-01 15:22:36 -07001066 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1067 dev->mode_config.num_total_plane++;
1068 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1069 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001070
Rob Clark9922ab52014-04-01 20:16:57 -04001071 drm_object_attach_property(&plane->base,
1072 dev->mode_config.plane_type_property,
1073 plane->type);
1074
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001075 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001076 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001077
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001078 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001079}
Matt Roperdc415ff2014-04-01 15:22:36 -07001080EXPORT_SYMBOL(drm_universal_plane_init);
1081
1082/**
1083 * drm_plane_init - Initialize a legacy plane
1084 * @dev: DRM device
1085 * @plane: plane object to init
1086 * @possible_crtcs: bitmask of possible CRTCs
1087 * @funcs: callbacks for the new plane
1088 * @formats: array of supported formats (%DRM_FORMAT_*)
1089 * @format_count: number of elements in @formats
1090 * @is_primary: plane type (primary vs overlay)
1091 *
1092 * Legacy API to initialize a DRM plane.
1093 *
1094 * New drivers should call drm_universal_plane_init() instead.
1095 *
1096 * Returns:
1097 * Zero on success, error code on failure.
1098 */
1099int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1100 unsigned long possible_crtcs,
1101 const struct drm_plane_funcs *funcs,
1102 const uint32_t *formats, uint32_t format_count,
1103 bool is_primary)
1104{
1105 enum drm_plane_type type;
1106
1107 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1108 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1109 formats, format_count, type);
1110}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001111EXPORT_SYMBOL(drm_plane_init);
1112
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001113/**
1114 * drm_plane_cleanup - Clean up the core plane usage
1115 * @plane: plane to cleanup
1116 *
1117 * This function cleans up @plane and removes it from the DRM mode setting
1118 * core. Note that the function does *not* free the plane structure itself,
1119 * this is the responsibility of the caller.
1120 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001121void drm_plane_cleanup(struct drm_plane *plane)
1122{
1123 struct drm_device *dev = plane->dev;
1124
Daniel Vetter84849902012-12-02 00:28:11 +01001125 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001126 kfree(plane->format_types);
1127 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001128
1129 BUG_ON(list_empty(&plane->head));
1130
1131 list_del(&plane->head);
1132 dev->mode_config.num_total_plane--;
1133 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1134 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001135 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001136}
1137EXPORT_SYMBOL(drm_plane_cleanup);
1138
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001139/**
1140 * drm_plane_force_disable - Forcibly disable a plane
1141 * @plane: plane to disable
1142 *
1143 * Forces the plane to be disabled.
1144 *
1145 * Used when the plane's current framebuffer is destroyed,
1146 * and when restoring fbdev mode.
1147 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001148void drm_plane_force_disable(struct drm_plane *plane)
1149{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001150 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001151 int ret;
1152
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001153 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001154 return;
1155
1156 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001157 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001158 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001159 return;
1160 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001161 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001162 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001163 plane->fb = NULL;
1164 plane->crtc = NULL;
1165}
1166EXPORT_SYMBOL(drm_plane_force_disable);
1167
Dave Airlief453ba02008-11-07 14:05:41 -08001168static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1169{
1170 struct drm_property *edid;
1171 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001172
1173 /*
1174 * Standard properties (apply to all connectors)
1175 */
1176 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1177 DRM_MODE_PROP_IMMUTABLE,
1178 "EDID", 0);
1179 dev->mode_config.edid_property = edid;
1180
Sascha Hauer4a67d392012-02-06 10:58:17 +01001181 dpms = drm_property_create_enum(dev, 0,
1182 "DPMS", drm_dpms_enum_list,
1183 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001184 dev->mode_config.dpms_property = dpms;
1185
1186 return 0;
1187}
1188
Rob Clark9922ab52014-04-01 20:16:57 -04001189static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1190{
1191 struct drm_property *type;
1192
1193 /*
1194 * Standard properties (apply to all planes)
1195 */
1196 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1197 "type", drm_plane_type_enum_list,
1198 ARRAY_SIZE(drm_plane_type_enum_list));
1199 dev->mode_config.plane_type_property = type;
1200
1201 return 0;
1202}
1203
Dave Airlief453ba02008-11-07 14:05:41 -08001204/**
1205 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1206 * @dev: DRM device
1207 *
1208 * Called by a driver the first time a DVI-I connector is made.
1209 */
1210int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1211{
1212 struct drm_property *dvi_i_selector;
1213 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001214
1215 if (dev->mode_config.dvi_i_select_subconnector_property)
1216 return 0;
1217
1218 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001219 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001220 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001221 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001222 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001223 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1224
Sascha Hauer4a67d392012-02-06 10:58:17 +01001225 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001226 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001227 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001228 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001229 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1230
1231 return 0;
1232}
1233EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1234
1235/**
1236 * drm_create_tv_properties - create TV specific connector properties
1237 * @dev: DRM device
1238 * @num_modes: number of different TV formats (modes) supported
1239 * @modes: array of pointers to strings containing name of each format
1240 *
1241 * Called by a driver's TV initialization routine, this function creates
1242 * the TV specific connector properties for a given device. Caller is
1243 * responsible for allocating a list of format names and passing them to
1244 * this routine.
1245 */
1246int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1247 char *modes[])
1248{
1249 struct drm_property *tv_selector;
1250 struct drm_property *tv_subconnector;
1251 int i;
1252
1253 if (dev->mode_config.tv_select_subconnector_property)
1254 return 0;
1255
1256 /*
1257 * Basic connector properties
1258 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001259 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001260 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001261 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001262 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001263 dev->mode_config.tv_select_subconnector_property = tv_selector;
1264
1265 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001266 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1267 "subconnector",
1268 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001269 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001270 dev->mode_config.tv_subconnector_property = tv_subconnector;
1271
1272 /*
1273 * Other, TV specific properties: margins & TV modes.
1274 */
1275 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001276 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001277
1278 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001279 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001280
1281 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001282 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001283
1284 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001285 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001286
1287 dev->mode_config.tv_mode_property =
1288 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1289 "mode", num_modes);
1290 for (i = 0; i < num_modes; i++)
1291 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1292 i, modes[i]);
1293
Francisco Jerezb6b79022009-08-02 04:19:20 +02001294 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001295 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001296
1297 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001298 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001299
1300 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001301 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001302
Francisco Jereza75f0232009-08-12 02:30:10 +02001303 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001304 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001305
1306 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001307 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001308
1309 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001310 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001311
Dave Airlief453ba02008-11-07 14:05:41 -08001312 return 0;
1313}
1314EXPORT_SYMBOL(drm_mode_create_tv_properties);
1315
1316/**
1317 * drm_mode_create_scaling_mode_property - create scaling mode property
1318 * @dev: DRM device
1319 *
1320 * Called by a driver the first time it's needed, must be attached to desired
1321 * connectors.
1322 */
1323int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1324{
1325 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001326
1327 if (dev->mode_config.scaling_mode_property)
1328 return 0;
1329
1330 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001331 drm_property_create_enum(dev, 0, "scaling mode",
1332 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001333 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001334
1335 dev->mode_config.scaling_mode_property = scaling_mode;
1336
1337 return 0;
1338}
1339EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1340
1341/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001342 * drm_mode_create_dirty_property - create dirty property
1343 * @dev: DRM device
1344 *
1345 * Called by a driver the first time it's needed, must be attached to desired
1346 * connectors.
1347 */
1348int drm_mode_create_dirty_info_property(struct drm_device *dev)
1349{
1350 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001351
1352 if (dev->mode_config.dirty_info_property)
1353 return 0;
1354
1355 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001356 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001357 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001358 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001359 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001360 dev->mode_config.dirty_info_property = dirty_info;
1361
1362 return 0;
1363}
1364EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1365
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001366static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001367{
1368 uint32_t total_objects = 0;
1369
1370 total_objects += dev->mode_config.num_crtc;
1371 total_objects += dev->mode_config.num_connector;
1372 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001373 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001374
Dave Airlief453ba02008-11-07 14:05:41 -08001375 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1376 if (!group->id_list)
1377 return -ENOMEM;
1378
1379 group->num_crtcs = 0;
1380 group->num_connectors = 0;
1381 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001382 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001383 return 0;
1384}
1385
Dave Airliead222792014-05-02 13:22:19 +10001386void drm_mode_group_destroy(struct drm_mode_group *group)
1387{
1388 kfree(group->id_list);
1389 group->id_list = NULL;
1390}
1391
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001392/*
1393 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1394 * the drm core's responsibility to set up mode control groups.
1395 */
Dave Airlief453ba02008-11-07 14:05:41 -08001396int drm_mode_group_init_legacy_group(struct drm_device *dev,
1397 struct drm_mode_group *group)
1398{
1399 struct drm_crtc *crtc;
1400 struct drm_encoder *encoder;
1401 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001402 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001403 int ret;
1404
1405 if ((ret = drm_mode_group_init(dev, group)))
1406 return ret;
1407
1408 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1409 group->id_list[group->num_crtcs++] = crtc->base.id;
1410
1411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1412 group->id_list[group->num_crtcs + group->num_encoders++] =
1413 encoder->base.id;
1414
1415 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1416 group->id_list[group->num_crtcs + group->num_encoders +
1417 group->num_connectors++] = connector->base.id;
1418
Sean Paul3b336ec2013-08-14 16:47:37 -04001419 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1420 group->id_list[group->num_crtcs + group->num_encoders +
1421 group->num_connectors + group->num_bridges++] =
1422 bridge->base.id;
1423
Dave Airlief453ba02008-11-07 14:05:41 -08001424 return 0;
1425}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001426EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001427
1428/**
Dave Airlief453ba02008-11-07 14:05:41 -08001429 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1430 * @out: drm_mode_modeinfo struct to return to the user
1431 * @in: drm_display_mode to use
1432 *
Dave Airlief453ba02008-11-07 14:05:41 -08001433 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1434 * the user.
1435 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001436static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1437 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001438{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001439 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1440 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1441 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1442 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1443 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1444 "timing values too large for mode info\n");
1445
Dave Airlief453ba02008-11-07 14:05:41 -08001446 out->clock = in->clock;
1447 out->hdisplay = in->hdisplay;
1448 out->hsync_start = in->hsync_start;
1449 out->hsync_end = in->hsync_end;
1450 out->htotal = in->htotal;
1451 out->hskew = in->hskew;
1452 out->vdisplay = in->vdisplay;
1453 out->vsync_start = in->vsync_start;
1454 out->vsync_end = in->vsync_end;
1455 out->vtotal = in->vtotal;
1456 out->vscan = in->vscan;
1457 out->vrefresh = in->vrefresh;
1458 out->flags = in->flags;
1459 out->type = in->type;
1460 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1461 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1462}
1463
1464/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001465 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001466 * @out: drm_display_mode to return to the user
1467 * @in: drm_mode_modeinfo to use
1468 *
Dave Airlief453ba02008-11-07 14:05:41 -08001469 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1470 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001471 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001472 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001473 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001474 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001475static int drm_crtc_convert_umode(struct drm_display_mode *out,
1476 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001477{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001478 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1479 return -ERANGE;
1480
Damien Lespiau5848ad42013-09-27 12:11:50 +01001481 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1482 return -EINVAL;
1483
Dave Airlief453ba02008-11-07 14:05:41 -08001484 out->clock = in->clock;
1485 out->hdisplay = in->hdisplay;
1486 out->hsync_start = in->hsync_start;
1487 out->hsync_end = in->hsync_end;
1488 out->htotal = in->htotal;
1489 out->hskew = in->hskew;
1490 out->vdisplay = in->vdisplay;
1491 out->vsync_start = in->vsync_start;
1492 out->vsync_end = in->vsync_end;
1493 out->vtotal = in->vtotal;
1494 out->vscan = in->vscan;
1495 out->vrefresh = in->vrefresh;
1496 out->flags = in->flags;
1497 out->type = in->type;
1498 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1499 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001500
1501 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001502}
1503
1504/**
1505 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001506 * @dev: drm device for the ioctl
1507 * @data: data pointer for the ioctl
1508 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001509 *
Dave Airlief453ba02008-11-07 14:05:41 -08001510 * Construct a set of configuration description structures and return
1511 * them to the user, including CRTC, connector and framebuffer configuration.
1512 *
1513 * Called by the user via ioctl.
1514 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001515 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001516 * Zero on success, errno on failure.
1517 */
1518int drm_mode_getresources(struct drm_device *dev, void *data,
1519 struct drm_file *file_priv)
1520{
1521 struct drm_mode_card_res *card_res = data;
1522 struct list_head *lh;
1523 struct drm_framebuffer *fb;
1524 struct drm_connector *connector;
1525 struct drm_crtc *crtc;
1526 struct drm_encoder *encoder;
1527 int ret = 0;
1528 int connector_count = 0;
1529 int crtc_count = 0;
1530 int fb_count = 0;
1531 int encoder_count = 0;
1532 int copied = 0, i;
1533 uint32_t __user *fb_id;
1534 uint32_t __user *crtc_id;
1535 uint32_t __user *connector_id;
1536 uint32_t __user *encoder_id;
1537 struct drm_mode_group *mode_group;
1538
Dave Airliefb3b06c2011-02-08 13:55:21 +10001539 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1540 return -EINVAL;
1541
Dave Airlief453ba02008-11-07 14:05:41 -08001542
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001543 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001544 /*
1545 * For the non-control nodes we need to limit the list of resources
1546 * by IDs in the group list for this node
1547 */
1548 list_for_each(lh, &file_priv->fbs)
1549 fb_count++;
1550
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001551 /* handle this in 4 parts */
1552 /* FBs */
1553 if (card_res->count_fbs >= fb_count) {
1554 copied = 0;
1555 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1556 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1557 if (put_user(fb->base.id, fb_id + copied)) {
1558 mutex_unlock(&file_priv->fbs_lock);
1559 return -EFAULT;
1560 }
1561 copied++;
1562 }
1563 }
1564 card_res->count_fbs = fb_count;
1565 mutex_unlock(&file_priv->fbs_lock);
1566
1567 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001568 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001569
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001570 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001571 list_for_each(lh, &dev->mode_config.crtc_list)
1572 crtc_count++;
1573
1574 list_for_each(lh, &dev->mode_config.connector_list)
1575 connector_count++;
1576
1577 list_for_each(lh, &dev->mode_config.encoder_list)
1578 encoder_count++;
1579 } else {
1580
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001581 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001582 crtc_count = mode_group->num_crtcs;
1583 connector_count = mode_group->num_connectors;
1584 encoder_count = mode_group->num_encoders;
1585 }
1586
1587 card_res->max_height = dev->mode_config.max_height;
1588 card_res->min_height = dev->mode_config.min_height;
1589 card_res->max_width = dev->mode_config.max_width;
1590 card_res->min_width = dev->mode_config.min_width;
1591
Dave Airlief453ba02008-11-07 14:05:41 -08001592 /* CRTCs */
1593 if (card_res->count_crtcs >= crtc_count) {
1594 copied = 0;
1595 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001596 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001597 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1598 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001599 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001600 if (put_user(crtc->base.id, crtc_id + copied)) {
1601 ret = -EFAULT;
1602 goto out;
1603 }
1604 copied++;
1605 }
1606 } else {
1607 for (i = 0; i < mode_group->num_crtcs; i++) {
1608 if (put_user(mode_group->id_list[i],
1609 crtc_id + copied)) {
1610 ret = -EFAULT;
1611 goto out;
1612 }
1613 copied++;
1614 }
1615 }
1616 }
1617 card_res->count_crtcs = crtc_count;
1618
1619 /* Encoders */
1620 if (card_res->count_encoders >= encoder_count) {
1621 copied = 0;
1622 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001623 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001624 list_for_each_entry(encoder,
1625 &dev->mode_config.encoder_list,
1626 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001627 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001628 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001629 if (put_user(encoder->base.id, encoder_id +
1630 copied)) {
1631 ret = -EFAULT;
1632 goto out;
1633 }
1634 copied++;
1635 }
1636 } else {
1637 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1638 if (put_user(mode_group->id_list[i],
1639 encoder_id + copied)) {
1640 ret = -EFAULT;
1641 goto out;
1642 }
1643 copied++;
1644 }
1645
1646 }
1647 }
1648 card_res->count_encoders = encoder_count;
1649
1650 /* Connectors */
1651 if (card_res->count_connectors >= connector_count) {
1652 copied = 0;
1653 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001654 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001655 list_for_each_entry(connector,
1656 &dev->mode_config.connector_list,
1657 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001658 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1659 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001660 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001661 if (put_user(connector->base.id,
1662 connector_id + copied)) {
1663 ret = -EFAULT;
1664 goto out;
1665 }
1666 copied++;
1667 }
1668 } else {
1669 int start = mode_group->num_crtcs +
1670 mode_group->num_encoders;
1671 for (i = start; i < start + mode_group->num_connectors; i++) {
1672 if (put_user(mode_group->id_list[i],
1673 connector_id + copied)) {
1674 ret = -EFAULT;
1675 goto out;
1676 }
1677 copied++;
1678 }
1679 }
1680 }
1681 card_res->count_connectors = connector_count;
1682
Jerome Glisse94401062010-07-15 15:43:25 -04001683 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001684 card_res->count_connectors, card_res->count_encoders);
1685
1686out:
Daniel Vetter84849902012-12-02 00:28:11 +01001687 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001688 return ret;
1689}
1690
1691/**
1692 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001693 * @dev: drm device for the ioctl
1694 * @data: data pointer for the ioctl
1695 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001696 *
Dave Airlief453ba02008-11-07 14:05:41 -08001697 * Construct a CRTC configuration structure to return to the user.
1698 *
1699 * Called by the user via ioctl.
1700 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001701 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001702 * Zero on success, errno on failure.
1703 */
1704int drm_mode_getcrtc(struct drm_device *dev,
1705 void *data, struct drm_file *file_priv)
1706{
1707 struct drm_mode_crtc *crtc_resp = data;
1708 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001709 int ret = 0;
1710
Dave Airliefb3b06c2011-02-08 13:55:21 +10001711 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1712 return -EINVAL;
1713
Daniel Vetter84849902012-12-02 00:28:11 +01001714 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001715
Rob Clarka2b34e22013-10-05 16:36:52 -04001716 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1717 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001718 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001719 goto out;
1720 }
Dave Airlief453ba02008-11-07 14:05:41 -08001721
1722 crtc_resp->x = crtc->x;
1723 crtc_resp->y = crtc->y;
1724 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001725 if (crtc->primary->fb)
1726 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001727 else
1728 crtc_resp->fb_id = 0;
1729
1730 if (crtc->enabled) {
1731
1732 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1733 crtc_resp->mode_valid = 1;
1734
1735 } else {
1736 crtc_resp->mode_valid = 0;
1737 }
1738
1739out:
Daniel Vetter84849902012-12-02 00:28:11 +01001740 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001741 return ret;
1742}
1743
Damien Lespiau61d8e322013-09-25 16:45:22 +01001744static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1745 const struct drm_file *file_priv)
1746{
1747 /*
1748 * If user-space hasn't configured the driver to expose the stereo 3D
1749 * modes, don't expose them.
1750 */
1751 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1752 return false;
1753
1754 return true;
1755}
1756
Dave Airlief453ba02008-11-07 14:05:41 -08001757/**
1758 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001759 * @dev: drm device for the ioctl
1760 * @data: data pointer for the ioctl
1761 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001762 *
Dave Airlief453ba02008-11-07 14:05:41 -08001763 * Construct a connector configuration structure to return to the user.
1764 *
1765 * Called by the user via ioctl.
1766 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001767 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001768 * Zero on success, errno on failure.
1769 */
1770int drm_mode_getconnector(struct drm_device *dev, void *data,
1771 struct drm_file *file_priv)
1772{
1773 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001774 struct drm_connector *connector;
1775 struct drm_display_mode *mode;
1776 int mode_count = 0;
1777 int props_count = 0;
1778 int encoders_count = 0;
1779 int ret = 0;
1780 int copied = 0;
1781 int i;
1782 struct drm_mode_modeinfo u_mode;
1783 struct drm_mode_modeinfo __user *mode_ptr;
1784 uint32_t __user *prop_ptr;
1785 uint64_t __user *prop_values;
1786 uint32_t __user *encoder_ptr;
1787
Dave Airliefb3b06c2011-02-08 13:55:21 +10001788 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1789 return -EINVAL;
1790
Dave Airlief453ba02008-11-07 14:05:41 -08001791 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1792
Jerome Glisse94401062010-07-15 15:43:25 -04001793 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001794
Daniel Vetter7b240562012-12-12 00:35:33 +01001795 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001796
Rob Clarka2b34e22013-10-05 16:36:52 -04001797 connector = drm_connector_find(dev, out_resp->connector_id);
1798 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001799 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001800 goto out;
1801 }
Dave Airlief453ba02008-11-07 14:05:41 -08001802
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001803 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001804
1805 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1806 if (connector->encoder_ids[i] != 0) {
1807 encoders_count++;
1808 }
1809 }
1810
1811 if (out_resp->count_modes == 0) {
1812 connector->funcs->fill_modes(connector,
1813 dev->mode_config.max_width,
1814 dev->mode_config.max_height);
1815 }
1816
1817 /* delayed so we get modes regardless of pre-fill_modes state */
1818 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001819 if (drm_mode_expose_to_userspace(mode, file_priv))
1820 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001821
1822 out_resp->connector_id = connector->base.id;
1823 out_resp->connector_type = connector->connector_type;
1824 out_resp->connector_type_id = connector->connector_type_id;
1825 out_resp->mm_width = connector->display_info.width_mm;
1826 out_resp->mm_height = connector->display_info.height_mm;
1827 out_resp->subpixel = connector->display_info.subpixel_order;
1828 out_resp->connection = connector->status;
1829 if (connector->encoder)
1830 out_resp->encoder_id = connector->encoder->base.id;
1831 else
1832 out_resp->encoder_id = 0;
1833
1834 /*
1835 * This ioctl is called twice, once to determine how much space is
1836 * needed, and the 2nd time to fill it.
1837 */
1838 if ((out_resp->count_modes >= mode_count) && mode_count) {
1839 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001840 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001841 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001842 if (!drm_mode_expose_to_userspace(mode, file_priv))
1843 continue;
1844
Dave Airlief453ba02008-11-07 14:05:41 -08001845 drm_crtc_convert_to_umode(&u_mode, mode);
1846 if (copy_to_user(mode_ptr + copied,
1847 &u_mode, sizeof(u_mode))) {
1848 ret = -EFAULT;
1849 goto out;
1850 }
1851 copied++;
1852 }
1853 }
1854 out_resp->count_modes = mode_count;
1855
1856 if ((out_resp->count_props >= props_count) && props_count) {
1857 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001858 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1859 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001860 for (i = 0; i < connector->properties.count; i++) {
1861 if (put_user(connector->properties.ids[i],
1862 prop_ptr + copied)) {
1863 ret = -EFAULT;
1864 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001865 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001866
1867 if (put_user(connector->properties.values[i],
1868 prop_values + copied)) {
1869 ret = -EFAULT;
1870 goto out;
1871 }
1872 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001873 }
1874 }
1875 out_resp->count_props = props_count;
1876
1877 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1878 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001879 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001880 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1881 if (connector->encoder_ids[i] != 0) {
1882 if (put_user(connector->encoder_ids[i],
1883 encoder_ptr + copied)) {
1884 ret = -EFAULT;
1885 goto out;
1886 }
1887 copied++;
1888 }
1889 }
1890 }
1891 out_resp->count_encoders = encoders_count;
1892
1893out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001894 mutex_unlock(&dev->mode_config.mutex);
1895
Dave Airlief453ba02008-11-07 14:05:41 -08001896 return ret;
1897}
1898
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001899/**
1900 * drm_mode_getencoder - get encoder configuration
1901 * @dev: drm device for the ioctl
1902 * @data: data pointer for the ioctl
1903 * @file_priv: drm file for the ioctl call
1904 *
1905 * Construct a encoder configuration structure to return to the user.
1906 *
1907 * Called by the user via ioctl.
1908 *
1909 * Returns:
1910 * Zero on success, errno on failure.
1911 */
Dave Airlief453ba02008-11-07 14:05:41 -08001912int drm_mode_getencoder(struct drm_device *dev, void *data,
1913 struct drm_file *file_priv)
1914{
1915 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001916 struct drm_encoder *encoder;
1917 int ret = 0;
1918
Dave Airliefb3b06c2011-02-08 13:55:21 +10001919 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1920 return -EINVAL;
1921
Daniel Vetter84849902012-12-02 00:28:11 +01001922 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04001923 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1924 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001925 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001926 goto out;
1927 }
Dave Airlief453ba02008-11-07 14:05:41 -08001928
1929 if (encoder->crtc)
1930 enc_resp->crtc_id = encoder->crtc->base.id;
1931 else
1932 enc_resp->crtc_id = 0;
1933 enc_resp->encoder_type = encoder->encoder_type;
1934 enc_resp->encoder_id = encoder->base.id;
1935 enc_resp->possible_crtcs = encoder->possible_crtcs;
1936 enc_resp->possible_clones = encoder->possible_clones;
1937
1938out:
Daniel Vetter84849902012-12-02 00:28:11 +01001939 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001940 return ret;
1941}
1942
1943/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001944 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001945 * @dev: DRM device
1946 * @data: ioctl data
1947 * @file_priv: DRM file info
1948 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001949 * Construct a list of plane ids to return to the user.
1950 *
1951 * Called by the user via ioctl.
1952 *
1953 * Returns:
1954 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955 */
1956int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001957 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001958{
1959 struct drm_mode_get_plane_res *plane_resp = data;
1960 struct drm_mode_config *config;
1961 struct drm_plane *plane;
1962 uint32_t __user *plane_ptr;
1963 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07001964 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001965
1966 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1967 return -EINVAL;
1968
Daniel Vetter84849902012-12-02 00:28:11 +01001969 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001970 config = &dev->mode_config;
1971
Matt Roper681e7ec2014-04-01 15:22:42 -07001972 if (file_priv->universal_planes)
1973 num_planes = config->num_total_plane;
1974 else
1975 num_planes = config->num_overlay_plane;
1976
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001977 /*
1978 * This ioctl is called twice, once to determine how much space is
1979 * needed, and the 2nd time to fill it.
1980 */
Matt Roper681e7ec2014-04-01 15:22:42 -07001981 if (num_planes &&
1982 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001983 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001984
1985 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07001986 /*
1987 * Unless userspace set the 'universal planes'
1988 * capability bit, only advertise overlays.
1989 */
1990 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1991 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07001992 continue;
1993
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001994 if (put_user(plane->base.id, plane_ptr + copied)) {
1995 ret = -EFAULT;
1996 goto out;
1997 }
1998 copied++;
1999 }
2000 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002001 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002002
2003out:
Daniel Vetter84849902012-12-02 00:28:11 +01002004 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002005 return ret;
2006}
2007
2008/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002009 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002010 * @dev: DRM device
2011 * @data: ioctl data
2012 * @file_priv: DRM file info
2013 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002014 * Construct a plane configuration structure to return to the user.
2015 *
2016 * Called by the user via ioctl.
2017 *
2018 * Returns:
2019 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002020 */
2021int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002022 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002023{
2024 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002025 struct drm_plane *plane;
2026 uint32_t __user *format_ptr;
2027 int ret = 0;
2028
2029 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2030 return -EINVAL;
2031
Daniel Vetter84849902012-12-02 00:28:11 +01002032 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002033 plane = drm_plane_find(dev, plane_resp->plane_id);
2034 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035 ret = -ENOENT;
2036 goto out;
2037 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002038
2039 if (plane->crtc)
2040 plane_resp->crtc_id = plane->crtc->base.id;
2041 else
2042 plane_resp->crtc_id = 0;
2043
2044 if (plane->fb)
2045 plane_resp->fb_id = plane->fb->base.id;
2046 else
2047 plane_resp->fb_id = 0;
2048
2049 plane_resp->plane_id = plane->base.id;
2050 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002051 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002052
2053 /*
2054 * This ioctl is called twice, once to determine how much space is
2055 * needed, and the 2nd time to fill it.
2056 */
2057 if (plane->format_count &&
2058 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002059 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002060 if (copy_to_user(format_ptr,
2061 plane->format_types,
2062 sizeof(uint32_t) * plane->format_count)) {
2063 ret = -EFAULT;
2064 goto out;
2065 }
2066 }
2067 plane_resp->count_format_types = plane->format_count;
2068
2069out:
Daniel Vetter84849902012-12-02 00:28:11 +01002070 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002071 return ret;
2072}
2073
2074/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002075 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002076 * @dev: DRM device
2077 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002078 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002079 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002080 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002081 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002082 *
2083 * Returns:
2084 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002085 */
2086int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002087 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002088{
2089 struct drm_mode_set_plane *plane_req = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002090 struct drm_plane *plane;
2091 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002092 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002093 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002094 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002095 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002096
2097 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2098 return -EINVAL;
2099
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002100 /*
2101 * First, find the plane, crtc, and fb objects. If not available,
2102 * we don't bother to call the driver.
2103 */
Rob Clarka2b34e22013-10-05 16:36:52 -04002104 plane = drm_plane_find(dev, plane_req->plane_id);
2105 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002106 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2107 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002108 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002109 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002110
2111 /* No fb means shut it down */
2112 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002113 drm_modeset_lock_all(dev);
2114 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002115 ret = plane->funcs->disable_plane(plane);
2116 if (!ret) {
2117 plane->crtc = NULL;
2118 plane->fb = NULL;
2119 } else {
2120 old_fb = NULL;
2121 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002122 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002123 goto out;
2124 }
2125
Rob Clarka2b34e22013-10-05 16:36:52 -04002126 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2127 if (!crtc) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002128 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2129 plane_req->crtc_id);
2130 ret = -ENOENT;
2131 goto out;
2132 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002133
Daniel Vetter786b99e2012-12-02 21:53:40 +01002134 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2135 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002136 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2137 plane_req->fb_id);
2138 ret = -ENOENT;
2139 goto out;
2140 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002141
Ville Syrjälä62443be2011-12-20 00:06:47 +02002142 /* Check whether this plane supports the fb pixel format. */
2143 for (i = 0; i < plane->format_count; i++)
2144 if (fb->pixel_format == plane->format_types[i])
2145 break;
2146 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002147 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2148 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002149 ret = -EINVAL;
2150 goto out;
2151 }
2152
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002153 fb_width = fb->width << 16;
2154 fb_height = fb->height << 16;
2155
2156 /* Make sure source coordinates are inside the fb. */
2157 if (plane_req->src_w > fb_width ||
2158 plane_req->src_x > fb_width - plane_req->src_w ||
2159 plane_req->src_h > fb_height ||
2160 plane_req->src_y > fb_height - plane_req->src_h) {
2161 DRM_DEBUG_KMS("Invalid source coordinates "
2162 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2163 plane_req->src_w >> 16,
2164 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2165 plane_req->src_h >> 16,
2166 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2167 plane_req->src_x >> 16,
2168 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2169 plane_req->src_y >> 16,
2170 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2171 ret = -ENOSPC;
2172 goto out;
2173 }
2174
Ville Syrjälä687a0402011-12-20 00:06:45 +02002175 /* Give drivers some help against integer overflows */
2176 if (plane_req->crtc_w > INT_MAX ||
2177 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2178 plane_req->crtc_h > INT_MAX ||
2179 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2180 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2181 plane_req->crtc_w, plane_req->crtc_h,
2182 plane_req->crtc_x, plane_req->crtc_y);
2183 ret = -ERANGE;
2184 goto out;
2185 }
2186
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002187 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002188 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002189 ret = plane->funcs->update_plane(plane, crtc, fb,
2190 plane_req->crtc_x, plane_req->crtc_y,
2191 plane_req->crtc_w, plane_req->crtc_h,
2192 plane_req->src_x, plane_req->src_y,
2193 plane_req->src_w, plane_req->src_h);
2194 if (!ret) {
2195 plane->crtc = crtc;
2196 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002197 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002198 } else {
2199 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002200 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002201 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002202
2203out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002204 if (fb)
2205 drm_framebuffer_unreference(fb);
2206 if (old_fb)
2207 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002208
2209 return ret;
2210}
2211
2212/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002213 * drm_mode_set_config_internal - helper to call ->set_config
2214 * @set: modeset config to set
2215 *
2216 * This is a little helper to wrap internal calls to the ->set_config driver
2217 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002218 *
2219 * Returns:
2220 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002221 */
2222int drm_mode_set_config_internal(struct drm_mode_set *set)
2223{
2224 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002225 struct drm_framebuffer *fb;
2226 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002227 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002228
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002229 /*
2230 * NOTE: ->set_config can also disable other crtcs (if we steal all
2231 * connectors from it), hence we need to refcount the fbs across all
2232 * crtcs. Atomic modeset will have saner semantics ...
2233 */
2234 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002235 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002236
Daniel Vetterb0d12322012-12-11 01:07:12 +01002237 fb = set->fb;
2238
2239 ret = crtc->funcs->set_config(set);
2240 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002241 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002242 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002243 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002244
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002245 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002246 if (tmp->primary->fb)
2247 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002248 if (tmp->old_fb)
2249 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002250 }
2251
2252 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002253}
2254EXPORT_SYMBOL(drm_mode_set_config_internal);
2255
Matt Roperaf936292014-04-01 15:22:34 -07002256/**
2257 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2258 * CRTC viewport
2259 * @crtc: CRTC that framebuffer will be displayed on
2260 * @x: x panning
2261 * @y: y panning
2262 * @mode: mode that framebuffer will be displayed under
2263 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002264 */
Matt Roperaf936292014-04-01 15:22:34 -07002265int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2266 int x, int y,
2267 const struct drm_display_mode *mode,
2268 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002269
2270{
2271 int hdisplay, vdisplay;
2272
2273 hdisplay = mode->hdisplay;
2274 vdisplay = mode->vdisplay;
2275
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002276 if (drm_mode_is_stereo(mode)) {
2277 struct drm_display_mode adjusted = *mode;
2278
2279 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2280 hdisplay = adjusted.crtc_hdisplay;
2281 vdisplay = adjusted.crtc_vdisplay;
2282 }
2283
Damien Lespiauc11e9282013-09-25 16:45:30 +01002284 if (crtc->invert_dimensions)
2285 swap(hdisplay, vdisplay);
2286
2287 if (hdisplay > fb->width ||
2288 vdisplay > fb->height ||
2289 x > fb->width - hdisplay ||
2290 y > fb->height - vdisplay) {
2291 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2292 fb->width, fb->height, hdisplay, vdisplay, x, y,
2293 crtc->invert_dimensions ? " (inverted)" : "");
2294 return -ENOSPC;
2295 }
2296
2297 return 0;
2298}
Matt Roperaf936292014-04-01 15:22:34 -07002299EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002300
Daniel Vetter2d13b672012-12-11 13:47:23 +01002301/**
Dave Airlief453ba02008-11-07 14:05:41 -08002302 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002303 * @dev: drm device for the ioctl
2304 * @data: data pointer for the ioctl
2305 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002306 *
Dave Airlief453ba02008-11-07 14:05:41 -08002307 * Build a new CRTC configuration based on user request.
2308 *
2309 * Called by the user via ioctl.
2310 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002311 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002312 * Zero on success, errno on failure.
2313 */
2314int drm_mode_setcrtc(struct drm_device *dev, void *data,
2315 struct drm_file *file_priv)
2316{
2317 struct drm_mode_config *config = &dev->mode_config;
2318 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002319 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002320 struct drm_connector **connector_set = NULL, *connector;
2321 struct drm_framebuffer *fb = NULL;
2322 struct drm_display_mode *mode = NULL;
2323 struct drm_mode_set set;
2324 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002325 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002326 int i;
2327
Dave Airliefb3b06c2011-02-08 13:55:21 +10002328 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2329 return -EINVAL;
2330
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002331 /* For some reason crtc x/y offsets are signed internally. */
2332 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2333 return -ERANGE;
2334
Daniel Vetter84849902012-12-02 00:28:11 +01002335 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002336 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2337 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002338 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002339 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002340 goto out;
2341 }
Jerome Glisse94401062010-07-15 15:43:25 -04002342 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002343
2344 if (crtc_req->mode_valid) {
2345 /* If we have a mode we need a framebuffer. */
2346 /* If we pass -1, set the mode with the currently bound fb */
2347 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002348 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002349 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2350 ret = -EINVAL;
2351 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002352 }
Matt Roperf4510a22014-04-01 15:22:40 -07002353 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002354 /* Make refcounting symmetric with the lookup path. */
2355 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002356 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002357 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2358 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002359 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2360 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002361 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002362 goto out;
2363 }
Dave Airlief453ba02008-11-07 14:05:41 -08002364 }
2365
2366 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002367 if (!mode) {
2368 ret = -ENOMEM;
2369 goto out;
2370 }
2371
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002372 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2373 if (ret) {
2374 DRM_DEBUG_KMS("Invalid mode\n");
2375 goto out;
2376 }
2377
Dave Airlief453ba02008-11-07 14:05:41 -08002378 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002379
Damien Lespiauc11e9282013-09-25 16:45:30 +01002380 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2381 mode, fb);
2382 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002383 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002384
Dave Airlief453ba02008-11-07 14:05:41 -08002385 }
2386
2387 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002388 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002389 ret = -EINVAL;
2390 goto out;
2391 }
2392
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002393 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002394 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002395 crtc_req->count_connectors);
2396 ret = -EINVAL;
2397 goto out;
2398 }
2399
2400 if (crtc_req->count_connectors > 0) {
2401 u32 out_id;
2402
2403 /* Avoid unbounded kernel memory allocation */
2404 if (crtc_req->count_connectors > config->num_connector) {
2405 ret = -EINVAL;
2406 goto out;
2407 }
2408
2409 connector_set = kmalloc(crtc_req->count_connectors *
2410 sizeof(struct drm_connector *),
2411 GFP_KERNEL);
2412 if (!connector_set) {
2413 ret = -ENOMEM;
2414 goto out;
2415 }
2416
2417 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002418 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002419 if (get_user(out_id, &set_connectors_ptr[i])) {
2420 ret = -EFAULT;
2421 goto out;
2422 }
2423
Rob Clarka2b34e22013-10-05 16:36:52 -04002424 connector = drm_connector_find(dev, out_id);
2425 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002426 DRM_DEBUG_KMS("Connector id %d unknown\n",
2427 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002428 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002429 goto out;
2430 }
Jerome Glisse94401062010-07-15 15:43:25 -04002431 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2432 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002433 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002434
2435 connector_set[i] = connector;
2436 }
2437 }
2438
2439 set.crtc = crtc;
2440 set.x = crtc_req->x;
2441 set.y = crtc_req->y;
2442 set.mode = mode;
2443 set.connectors = connector_set;
2444 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002445 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002446 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002447
2448out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002449 if (fb)
2450 drm_framebuffer_unreference(fb);
2451
Dave Airlief453ba02008-11-07 14:05:41 -08002452 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002453 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002454 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002455 return ret;
2456}
2457
Dave Airlie4c813d42013-06-20 11:48:52 +10002458static int drm_mode_cursor_common(struct drm_device *dev,
2459 struct drm_mode_cursor2 *req,
2460 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002461{
Dave Airlief453ba02008-11-07 14:05:41 -08002462 struct drm_crtc *crtc;
2463 int ret = 0;
2464
Dave Airliefb3b06c2011-02-08 13:55:21 +10002465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2466 return -EINVAL;
2467
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002468 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002469 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002470
Rob Clarka2b34e22013-10-05 16:36:52 -04002471 crtc = drm_crtc_find(dev, req->crtc_id);
2472 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002473 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002474 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002475 }
Dave Airlief453ba02008-11-07 14:05:41 -08002476
Daniel Vetterdac35662012-12-02 15:24:10 +01002477 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002478 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002479 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002480 ret = -ENXIO;
2481 goto out;
2482 }
2483 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002484 if (crtc->funcs->cursor_set2)
2485 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2486 req->width, req->height, req->hot_x, req->hot_y);
2487 else
2488 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2489 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002490 }
2491
2492 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2493 if (crtc->funcs->cursor_move) {
2494 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2495 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002496 ret = -EFAULT;
2497 goto out;
2498 }
2499 }
2500out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002501 mutex_unlock(&crtc->mutex);
2502
Dave Airlief453ba02008-11-07 14:05:41 -08002503 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002504
2505}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002506
2507
2508/**
2509 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2510 * @dev: drm device for the ioctl
2511 * @data: data pointer for the ioctl
2512 * @file_priv: drm file for the ioctl call
2513 *
2514 * Set the cursor configuration based on user request.
2515 *
2516 * Called by the user via ioctl.
2517 *
2518 * Returns:
2519 * Zero on success, errno on failure.
2520 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002521int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002522 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002523{
2524 struct drm_mode_cursor *req = data;
2525 struct drm_mode_cursor2 new_req;
2526
2527 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2528 new_req.hot_x = new_req.hot_y = 0;
2529
2530 return drm_mode_cursor_common(dev, &new_req, file_priv);
2531}
2532
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002533/**
2534 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2535 * @dev: drm device for the ioctl
2536 * @data: data pointer for the ioctl
2537 * @file_priv: drm file for the ioctl call
2538 *
2539 * Set the cursor configuration based on user request. This implements the 2nd
2540 * version of the cursor ioctl, which allows userspace to additionally specify
2541 * the hotspot of the pointer.
2542 *
2543 * Called by the user via ioctl.
2544 *
2545 * Returns:
2546 * Zero on success, errno on failure.
2547 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002548int drm_mode_cursor2_ioctl(struct drm_device *dev,
2549 void *data, struct drm_file *file_priv)
2550{
2551 struct drm_mode_cursor2 *req = data;
2552 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002553}
2554
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002555/**
2556 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2557 * @bpp: bits per pixels
2558 * @depth: bit depth per pixel
2559 *
2560 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2561 * Useful in fbdev emulation code, since that deals in those values.
2562 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002563uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2564{
2565 uint32_t fmt;
2566
2567 switch (bpp) {
2568 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002569 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002570 break;
2571 case 16:
2572 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002573 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002574 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002575 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002576 break;
2577 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002578 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002579 break;
2580 case 32:
2581 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002582 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002583 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002584 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002585 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002586 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002587 break;
2588 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002589 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2590 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002591 break;
2592 }
2593
2594 return fmt;
2595}
2596EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2597
Dave Airlief453ba02008-11-07 14:05:41 -08002598/**
2599 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002600 * @dev: drm device for the ioctl
2601 * @data: data pointer for the ioctl
2602 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002603 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002604 * Add a new FB to the specified CRTC, given a user request. This is the
2605 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002606 *
2607 * Called by the user via ioctl.
2608 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002609 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002610 * Zero on success, errno on failure.
2611 */
2612int drm_mode_addfb(struct drm_device *dev,
2613 void *data, struct drm_file *file_priv)
2614{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002615 struct drm_mode_fb_cmd *or = data;
2616 struct drm_mode_fb_cmd2 r = {};
2617 struct drm_mode_config *config = &dev->mode_config;
2618 struct drm_framebuffer *fb;
2619 int ret = 0;
2620
2621 /* Use new struct with format internally */
2622 r.fb_id = or->fb_id;
2623 r.width = or->width;
2624 r.height = or->height;
2625 r.pitches[0] = or->pitch;
2626 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2627 r.handles[0] = or->handle;
2628
2629 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2630 return -EINVAL;
2631
Jesse Barnesacb4b992011-11-07 12:03:23 -08002632 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002633 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002634
2635 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002636 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002637
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002638 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2639 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002640 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002641 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002642 }
2643
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002644 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002645 or->fb_id = fb->base.id;
2646 list_add(&fb->filp_head, &file_priv->fbs);
2647 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002648 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002649
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002650 return ret;
2651}
2652
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002653static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002654{
2655 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2656
2657 switch (format) {
2658 case DRM_FORMAT_C8:
2659 case DRM_FORMAT_RGB332:
2660 case DRM_FORMAT_BGR233:
2661 case DRM_FORMAT_XRGB4444:
2662 case DRM_FORMAT_XBGR4444:
2663 case DRM_FORMAT_RGBX4444:
2664 case DRM_FORMAT_BGRX4444:
2665 case DRM_FORMAT_ARGB4444:
2666 case DRM_FORMAT_ABGR4444:
2667 case DRM_FORMAT_RGBA4444:
2668 case DRM_FORMAT_BGRA4444:
2669 case DRM_FORMAT_XRGB1555:
2670 case DRM_FORMAT_XBGR1555:
2671 case DRM_FORMAT_RGBX5551:
2672 case DRM_FORMAT_BGRX5551:
2673 case DRM_FORMAT_ARGB1555:
2674 case DRM_FORMAT_ABGR1555:
2675 case DRM_FORMAT_RGBA5551:
2676 case DRM_FORMAT_BGRA5551:
2677 case DRM_FORMAT_RGB565:
2678 case DRM_FORMAT_BGR565:
2679 case DRM_FORMAT_RGB888:
2680 case DRM_FORMAT_BGR888:
2681 case DRM_FORMAT_XRGB8888:
2682 case DRM_FORMAT_XBGR8888:
2683 case DRM_FORMAT_RGBX8888:
2684 case DRM_FORMAT_BGRX8888:
2685 case DRM_FORMAT_ARGB8888:
2686 case DRM_FORMAT_ABGR8888:
2687 case DRM_FORMAT_RGBA8888:
2688 case DRM_FORMAT_BGRA8888:
2689 case DRM_FORMAT_XRGB2101010:
2690 case DRM_FORMAT_XBGR2101010:
2691 case DRM_FORMAT_RGBX1010102:
2692 case DRM_FORMAT_BGRX1010102:
2693 case DRM_FORMAT_ARGB2101010:
2694 case DRM_FORMAT_ABGR2101010:
2695 case DRM_FORMAT_RGBA1010102:
2696 case DRM_FORMAT_BGRA1010102:
2697 case DRM_FORMAT_YUYV:
2698 case DRM_FORMAT_YVYU:
2699 case DRM_FORMAT_UYVY:
2700 case DRM_FORMAT_VYUY:
2701 case DRM_FORMAT_AYUV:
2702 case DRM_FORMAT_NV12:
2703 case DRM_FORMAT_NV21:
2704 case DRM_FORMAT_NV16:
2705 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002706 case DRM_FORMAT_NV24:
2707 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002708 case DRM_FORMAT_YUV410:
2709 case DRM_FORMAT_YVU410:
2710 case DRM_FORMAT_YUV411:
2711 case DRM_FORMAT_YVU411:
2712 case DRM_FORMAT_YUV420:
2713 case DRM_FORMAT_YVU420:
2714 case DRM_FORMAT_YUV422:
2715 case DRM_FORMAT_YVU422:
2716 case DRM_FORMAT_YUV444:
2717 case DRM_FORMAT_YVU444:
2718 return 0;
2719 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002720 DRM_DEBUG_KMS("invalid pixel format %s\n",
2721 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002722 return -EINVAL;
2723 }
2724}
2725
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002726static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002727{
2728 int ret, hsub, vsub, num_planes, i;
2729
2730 ret = format_check(r);
2731 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002732 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2733 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002734 return ret;
2735 }
2736
2737 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2738 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2739 num_planes = drm_format_num_planes(r->pixel_format);
2740
2741 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002742 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002743 return -EINVAL;
2744 }
2745
2746 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002747 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002748 return -EINVAL;
2749 }
2750
2751 for (i = 0; i < num_planes; i++) {
2752 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002753 unsigned int height = r->height / (i != 0 ? vsub : 1);
2754 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002755
2756 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002757 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002758 return -EINVAL;
2759 }
2760
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002761 if ((uint64_t) width * cpp > UINT_MAX)
2762 return -ERANGE;
2763
2764 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2765 return -ERANGE;
2766
2767 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002768 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002769 return -EINVAL;
2770 }
2771 }
2772
2773 return 0;
2774}
2775
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002776/**
2777 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002778 * @dev: drm device for the ioctl
2779 * @data: data pointer for the ioctl
2780 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002781 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002782 * Add a new FB to the specified CRTC, given a user request with format. This is
2783 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2784 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002785 *
2786 * Called by the user via ioctl.
2787 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002788 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002789 * Zero on success, errno on failure.
2790 */
2791int drm_mode_addfb2(struct drm_device *dev,
2792 void *data, struct drm_file *file_priv)
2793{
2794 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002795 struct drm_mode_config *config = &dev->mode_config;
2796 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002797 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002798
Dave Airliefb3b06c2011-02-08 13:55:21 +10002799 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2800 return -EINVAL;
2801
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002802 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2803 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2804 return -EINVAL;
2805 }
2806
Dave Airlief453ba02008-11-07 14:05:41 -08002807 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002808 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002809 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002810 return -EINVAL;
2811 }
2812 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002813 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002814 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002815 return -EINVAL;
2816 }
2817
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002818 ret = framebuffer_check(r);
2819 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002820 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002821
Dave Airlief453ba02008-11-07 14:05:41 -08002822 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002823 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002824 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002825 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002826 }
2827
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002828 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002829 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002830 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002831 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002832 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002833
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002834
Dave Airlief453ba02008-11-07 14:05:41 -08002835 return ret;
2836}
2837
2838/**
2839 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002840 * @dev: drm device for the ioctl
2841 * @data: data pointer for the ioctl
2842 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002843 *
Dave Airlief453ba02008-11-07 14:05:41 -08002844 * Remove the FB specified by the user.
2845 *
2846 * Called by the user via ioctl.
2847 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002848 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002849 * Zero on success, errno on failure.
2850 */
2851int drm_mode_rmfb(struct drm_device *dev,
2852 void *data, struct drm_file *file_priv)
2853{
Dave Airlief453ba02008-11-07 14:05:41 -08002854 struct drm_framebuffer *fb = NULL;
2855 struct drm_framebuffer *fbl = NULL;
2856 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002857 int found = 0;
2858
Dave Airliefb3b06c2011-02-08 13:55:21 +10002859 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2860 return -EINVAL;
2861
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002862 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002863 mutex_lock(&dev->mode_config.fb_lock);
2864 fb = __drm_framebuffer_lookup(dev, *id);
2865 if (!fb)
2866 goto fail_lookup;
2867
Dave Airlief453ba02008-11-07 14:05:41 -08002868 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2869 if (fb == fbl)
2870 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002871 if (!found)
2872 goto fail_lookup;
2873
2874 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2875 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002876
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002877 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002878 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002879 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002880
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002881 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002882
Daniel Vetter2b677e82012-12-10 21:16:05 +01002883 return 0;
2884
2885fail_lookup:
2886 mutex_unlock(&dev->mode_config.fb_lock);
2887 mutex_unlock(&file_priv->fbs_lock);
2888
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002889 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002890}
2891
2892/**
2893 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002894 * @dev: drm device for the ioctl
2895 * @data: data pointer for the ioctl
2896 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002897 *
Dave Airlief453ba02008-11-07 14:05:41 -08002898 * Lookup the FB given its ID and return info about it.
2899 *
2900 * Called by the user via ioctl.
2901 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002902 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002903 * Zero on success, errno on failure.
2904 */
2905int drm_mode_getfb(struct drm_device *dev,
2906 void *data, struct drm_file *file_priv)
2907{
2908 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002909 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002910 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002911
Dave Airliefb3b06c2011-02-08 13:55:21 +10002912 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2913 return -EINVAL;
2914
Daniel Vetter786b99e2012-12-02 21:53:40 +01002915 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002916 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002917 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002918
2919 r->height = fb->height;
2920 r->width = fb->width;
2921 r->depth = fb->depth;
2922 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002923 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002924 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01002925 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01002926 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02002927 ret = fb->funcs->create_handle(fb, file_priv,
2928 &r->handle);
2929 } else {
2930 /* GET_FB() is an unprivileged ioctl so we must not
2931 * return a buffer-handle to non-master processes! For
2932 * backwards-compatibility reasons, we cannot make
2933 * GET_FB() privileged, so just return an invalid handle
2934 * for non-masters. */
2935 r->handle = 0;
2936 ret = 0;
2937 }
2938 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002939 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002940 }
Dave Airlief453ba02008-11-07 14:05:41 -08002941
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002942 drm_framebuffer_unreference(fb);
2943
Dave Airlief453ba02008-11-07 14:05:41 -08002944 return ret;
2945}
2946
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002947/**
2948 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2949 * @dev: drm device for the ioctl
2950 * @data: data pointer for the ioctl
2951 * @file_priv: drm file for the ioctl call
2952 *
2953 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2954 * rectangle list. Generic userspace which does frontbuffer rendering must call
2955 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2956 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2957 *
2958 * Modesetting drivers which always update the frontbuffer do not need to
2959 * implement the corresponding ->dirty framebuffer callback.
2960 *
2961 * Called by the user via ioctl.
2962 *
2963 * Returns:
2964 * Zero on success, errno on failure.
2965 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002966int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2967 void *data, struct drm_file *file_priv)
2968{
2969 struct drm_clip_rect __user *clips_ptr;
2970 struct drm_clip_rect *clips = NULL;
2971 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002972 struct drm_framebuffer *fb;
2973 unsigned flags;
2974 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002975 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002976
Dave Airliefb3b06c2011-02-08 13:55:21 +10002977 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2978 return -EINVAL;
2979
Daniel Vetter786b99e2012-12-02 21:53:40 +01002980 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002981 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002982 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002983
2984 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002985 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002986
2987 if (!num_clips != !clips_ptr) {
2988 ret = -EINVAL;
2989 goto out_err1;
2990 }
2991
2992 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2993
2994 /* If userspace annotates copy, clips must come in pairs */
2995 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2996 ret = -EINVAL;
2997 goto out_err1;
2998 }
2999
3000 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003001 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3002 ret = -EINVAL;
3003 goto out_err1;
3004 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003005 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3006 if (!clips) {
3007 ret = -ENOMEM;
3008 goto out_err1;
3009 }
3010
3011 ret = copy_from_user(clips, clips_ptr,
3012 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003013 if (ret) {
3014 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003015 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003016 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003017 }
3018
3019 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003020 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3021 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003022 } else {
3023 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003024 }
3025
3026out_err2:
3027 kfree(clips);
3028out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003029 drm_framebuffer_unreference(fb);
3030
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003031 return ret;
3032}
3033
3034
Dave Airlief453ba02008-11-07 14:05:41 -08003035/**
3036 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003037 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003038 *
Dave Airlief453ba02008-11-07 14:05:41 -08003039 * Destroy all the FBs associated with @filp.
3040 *
3041 * Called by the user via ioctl.
3042 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003043 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003044 * Zero on success, errno on failure.
3045 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003046void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003047{
Dave Airlief453ba02008-11-07 14:05:41 -08003048 struct drm_device *dev = priv->minor->dev;
3049 struct drm_framebuffer *fb, *tfb;
3050
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003051 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003052 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003053
3054 mutex_lock(&dev->mode_config.fb_lock);
3055 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3056 __drm_framebuffer_unregister(dev, fb);
3057 mutex_unlock(&dev->mode_config.fb_lock);
3058
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003059 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003060
3061 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003062 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003063 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003064 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003065}
3066
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003067/**
3068 * drm_property_create - create a new property type
3069 * @dev: drm device
3070 * @flags: flags specifying the property type
3071 * @name: name of the property
3072 * @num_values: number of pre-defined values
3073 *
3074 * This creates a new generic drm property which can then be attached to a drm
3075 * object with drm_object_attach_property. The returned property object must be
3076 * freed with drm_property_destroy.
3077 *
3078 * Returns:
3079 * A pointer to the newly created property on success, NULL on failure.
3080 */
Dave Airlief453ba02008-11-07 14:05:41 -08003081struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3082 const char *name, int num_values)
3083{
3084 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003085 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003086
3087 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3088 if (!property)
3089 return NULL;
3090
Rob Clark98f75de2014-05-30 11:37:03 -04003091 property->dev = dev;
3092
Dave Airlief453ba02008-11-07 14:05:41 -08003093 if (num_values) {
3094 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3095 if (!property->values)
3096 goto fail;
3097 }
3098
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003099 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3100 if (ret)
3101 goto fail;
3102
Dave Airlief453ba02008-11-07 14:05:41 -08003103 property->flags = flags;
3104 property->num_values = num_values;
3105 INIT_LIST_HEAD(&property->enum_blob_list);
3106
Vinson Lee471dd2e2011-11-10 11:55:40 -08003107 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003108 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003109 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3110 }
Dave Airlief453ba02008-11-07 14:05:41 -08003111
3112 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003113
3114 WARN_ON(!drm_property_type_valid(property));
3115
Dave Airlief453ba02008-11-07 14:05:41 -08003116 return property;
3117fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003118 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003119 kfree(property);
3120 return NULL;
3121}
3122EXPORT_SYMBOL(drm_property_create);
3123
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003124/**
3125 * drm_property_create - create a new enumeration property type
3126 * @dev: drm device
3127 * @flags: flags specifying the property type
3128 * @name: name of the property
3129 * @props: enumeration lists with property values
3130 * @num_values: number of pre-defined values
3131 *
3132 * This creates a new generic drm property which can then be attached to a drm
3133 * object with drm_object_attach_property. The returned property object must be
3134 * freed with drm_property_destroy.
3135 *
3136 * Userspace is only allowed to set one of the predefined values for enumeration
3137 * properties.
3138 *
3139 * Returns:
3140 * A pointer to the newly created property on success, NULL on failure.
3141 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003142struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3143 const char *name,
3144 const struct drm_prop_enum_list *props,
3145 int num_values)
3146{
3147 struct drm_property *property;
3148 int i, ret;
3149
3150 flags |= DRM_MODE_PROP_ENUM;
3151
3152 property = drm_property_create(dev, flags, name, num_values);
3153 if (!property)
3154 return NULL;
3155
3156 for (i = 0; i < num_values; i++) {
3157 ret = drm_property_add_enum(property, i,
3158 props[i].type,
3159 props[i].name);
3160 if (ret) {
3161 drm_property_destroy(dev, property);
3162 return NULL;
3163 }
3164 }
3165
3166 return property;
3167}
3168EXPORT_SYMBOL(drm_property_create_enum);
3169
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003170/**
3171 * drm_property_create - create a new bitmask property type
3172 * @dev: drm device
3173 * @flags: flags specifying the property type
3174 * @name: name of the property
3175 * @props: enumeration lists with property bitflags
3176 * @num_values: number of pre-defined values
3177 *
3178 * This creates a new generic drm property which can then be attached to a drm
3179 * object with drm_object_attach_property. The returned property object must be
3180 * freed with drm_property_destroy.
3181 *
3182 * Compared to plain enumeration properties userspace is allowed to set any
3183 * or'ed together combination of the predefined property bitflag values
3184 *
3185 * Returns:
3186 * A pointer to the newly created property on success, NULL on failure.
3187 */
Rob Clark49e27542012-05-17 02:23:26 -06003188struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3189 int flags, const char *name,
3190 const struct drm_prop_enum_list *props,
3191 int num_values)
3192{
3193 struct drm_property *property;
3194 int i, ret;
3195
3196 flags |= DRM_MODE_PROP_BITMASK;
3197
3198 property = drm_property_create(dev, flags, name, num_values);
3199 if (!property)
3200 return NULL;
3201
3202 for (i = 0; i < num_values; i++) {
3203 ret = drm_property_add_enum(property, i,
3204 props[i].type,
3205 props[i].name);
3206 if (ret) {
3207 drm_property_destroy(dev, property);
3208 return NULL;
3209 }
3210 }
3211
3212 return property;
3213}
3214EXPORT_SYMBOL(drm_property_create_bitmask);
3215
Rob Clarkebc44cf2012-09-12 22:22:31 -05003216static struct drm_property *property_create_range(struct drm_device *dev,
3217 int flags, const char *name,
3218 uint64_t min, uint64_t max)
3219{
3220 struct drm_property *property;
3221
3222 property = drm_property_create(dev, flags, name, 2);
3223 if (!property)
3224 return NULL;
3225
3226 property->values[0] = min;
3227 property->values[1] = max;
3228
3229 return property;
3230}
3231
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003232/**
3233 * drm_property_create - create a new ranged property type
3234 * @dev: drm device
3235 * @flags: flags specifying the property type
3236 * @name: name of the property
3237 * @min: minimum value of the property
3238 * @max: maximum value of the property
3239 *
3240 * This creates a new generic drm property which can then be attached to a drm
3241 * object with drm_object_attach_property. The returned property object must be
3242 * freed with drm_property_destroy.
3243 *
3244 * Userspace is allowed to set any interger value in the (min, max) range
3245 * inclusive.
3246 *
3247 * Returns:
3248 * A pointer to the newly created property on success, NULL on failure.
3249 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003250struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3251 const char *name,
3252 uint64_t min, uint64_t max)
3253{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003254 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3255 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003256}
3257EXPORT_SYMBOL(drm_property_create_range);
3258
Rob Clarkebc44cf2012-09-12 22:22:31 -05003259struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3260 int flags, const char *name,
3261 int64_t min, int64_t max)
3262{
3263 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3264 name, I642U64(min), I642U64(max));
3265}
3266EXPORT_SYMBOL(drm_property_create_signed_range);
3267
Rob Clark98f75de2014-05-30 11:37:03 -04003268struct drm_property *drm_property_create_object(struct drm_device *dev,
3269 int flags, const char *name, uint32_t type)
3270{
3271 struct drm_property *property;
3272
3273 flags |= DRM_MODE_PROP_OBJECT;
3274
3275 property = drm_property_create(dev, flags, name, 1);
3276 if (!property)
3277 return NULL;
3278
3279 property->values[0] = type;
3280
3281 return property;
3282}
3283EXPORT_SYMBOL(drm_property_create_object);
3284
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003285/**
3286 * drm_property_add_enum - add a possible value to an enumeration property
3287 * @property: enumeration property to change
3288 * @index: index of the new enumeration
3289 * @value: value of the new enumeration
3290 * @name: symbolic name of the new enumeration
3291 *
3292 * This functions adds enumerations to a property.
3293 *
3294 * It's use is deprecated, drivers should use one of the more specific helpers
3295 * to directly create the property with all enumerations already attached.
3296 *
3297 * Returns:
3298 * Zero on success, error code on failure.
3299 */
Dave Airlief453ba02008-11-07 14:05:41 -08003300int drm_property_add_enum(struct drm_property *property, int index,
3301 uint64_t value, const char *name)
3302{
3303 struct drm_property_enum *prop_enum;
3304
Rob Clark5ea22f22014-05-30 11:34:01 -04003305 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3306 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003307 return -EINVAL;
3308
3309 /*
3310 * Bitmask enum properties have the additional constraint of values
3311 * from 0 to 63
3312 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003313 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3314 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003315 return -EINVAL;
3316
3317 if (!list_empty(&property->enum_blob_list)) {
3318 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3319 if (prop_enum->value == value) {
3320 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3321 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3322 return 0;
3323 }
3324 }
3325 }
3326
3327 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3328 if (!prop_enum)
3329 return -ENOMEM;
3330
3331 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3332 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3333 prop_enum->value = value;
3334
3335 property->values[index] = value;
3336 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3337 return 0;
3338}
3339EXPORT_SYMBOL(drm_property_add_enum);
3340
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003341/**
3342 * drm_property_destroy - destroy a drm property
3343 * @dev: drm device
3344 * @property: property to destry
3345 *
3346 * This function frees a property including any attached resources like
3347 * enumeration values.
3348 */
Dave Airlief453ba02008-11-07 14:05:41 -08003349void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3350{
3351 struct drm_property_enum *prop_enum, *pt;
3352
3353 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3354 list_del(&prop_enum->head);
3355 kfree(prop_enum);
3356 }
3357
3358 if (property->num_values)
3359 kfree(property->values);
3360 drm_mode_object_put(dev, &property->base);
3361 list_del(&property->head);
3362 kfree(property);
3363}
3364EXPORT_SYMBOL(drm_property_destroy);
3365
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003366/**
3367 * drm_object_attach_property - attach a property to a modeset object
3368 * @obj: drm modeset object
3369 * @property: property to attach
3370 * @init_val: initial value of the property
3371 *
3372 * This attaches the given property to the modeset object with the given initial
3373 * value. Currently this function cannot fail since the properties are stored in
3374 * a statically sized array.
3375 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003376void drm_object_attach_property(struct drm_mode_object *obj,
3377 struct drm_property *property,
3378 uint64_t init_val)
3379{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003380 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003381
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003382 if (count == DRM_OBJECT_MAX_PROPERTY) {
3383 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3384 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3385 "you see this message on the same object type.\n",
3386 obj->type);
3387 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003388 }
3389
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003390 obj->properties->ids[count] = property->base.id;
3391 obj->properties->values[count] = init_val;
3392 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003393}
3394EXPORT_SYMBOL(drm_object_attach_property);
3395
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003396/**
3397 * drm_object_property_set_value - set the value of a property
3398 * @obj: drm mode object to set property value for
3399 * @property: property to set
3400 * @val: value the property should be set to
3401 *
3402 * This functions sets a given property on a given object. This function only
3403 * changes the software state of the property, it does not call into the
3404 * driver's ->set_property callback.
3405 *
3406 * Returns:
3407 * Zero on success, error code on failure.
3408 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003409int drm_object_property_set_value(struct drm_mode_object *obj,
3410 struct drm_property *property, uint64_t val)
3411{
3412 int i;
3413
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003414 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003415 if (obj->properties->ids[i] == property->base.id) {
3416 obj->properties->values[i] = val;
3417 return 0;
3418 }
3419 }
3420
3421 return -EINVAL;
3422}
3423EXPORT_SYMBOL(drm_object_property_set_value);
3424
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003425/**
3426 * drm_object_property_get_value - retrieve the value of a property
3427 * @obj: drm mode object to get property value from
3428 * @property: property to retrieve
3429 * @val: storage for the property value
3430 *
3431 * This function retrieves the softare state of the given property for the given
3432 * property. Since there is no driver callback to retrieve the current property
3433 * value this might be out of sync with the hardware, depending upon the driver
3434 * and property.
3435 *
3436 * Returns:
3437 * Zero on success, error code on failure.
3438 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003439int drm_object_property_get_value(struct drm_mode_object *obj,
3440 struct drm_property *property, uint64_t *val)
3441{
3442 int i;
3443
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003444 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003445 if (obj->properties->ids[i] == property->base.id) {
3446 *val = obj->properties->values[i];
3447 return 0;
3448 }
3449 }
3450
3451 return -EINVAL;
3452}
3453EXPORT_SYMBOL(drm_object_property_get_value);
3454
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003455/**
3456 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3457 * @dev: DRM device
3458 * @data: ioctl data
3459 * @file_priv: DRM file info
3460 *
3461 * This function retrieves the current value for an connectors's property.
3462 *
3463 * Called by the user via ioctl.
3464 *
3465 * Returns:
3466 * Zero on success, errno on failure.
3467 */
Dave Airlief453ba02008-11-07 14:05:41 -08003468int drm_mode_getproperty_ioctl(struct drm_device *dev,
3469 void *data, struct drm_file *file_priv)
3470{
Dave Airlief453ba02008-11-07 14:05:41 -08003471 struct drm_mode_get_property *out_resp = data;
3472 struct drm_property *property;
3473 int enum_count = 0;
3474 int blob_count = 0;
3475 int value_count = 0;
3476 int ret = 0, i;
3477 int copied;
3478 struct drm_property_enum *prop_enum;
3479 struct drm_mode_property_enum __user *enum_ptr;
3480 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003481 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003482 uint64_t __user *values_ptr;
3483 uint32_t __user *blob_length_ptr;
3484
Dave Airliefb3b06c2011-02-08 13:55:21 +10003485 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3486 return -EINVAL;
3487
Daniel Vetter84849902012-12-02 00:28:11 +01003488 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003489 property = drm_property_find(dev, out_resp->prop_id);
3490 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003491 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003492 goto done;
3493 }
Dave Airlief453ba02008-11-07 14:05:41 -08003494
Rob Clark5ea22f22014-05-30 11:34:01 -04003495 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3496 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003497 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3498 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003499 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003500 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3501 blob_count++;
3502 }
3503
3504 value_count = property->num_values;
3505
3506 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3507 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3508 out_resp->flags = property->flags;
3509
3510 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003511 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003512 for (i = 0; i < value_count; i++) {
3513 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3514 ret = -EFAULT;
3515 goto done;
3516 }
3517 }
3518 }
3519 out_resp->count_values = value_count;
3520
Rob Clark5ea22f22014-05-30 11:34:01 -04003521 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3522 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003523 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3524 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003525 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003526 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3527
3528 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3529 ret = -EFAULT;
3530 goto done;
3531 }
3532
3533 if (copy_to_user(&enum_ptr[copied].name,
3534 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3535 ret = -EFAULT;
3536 goto done;
3537 }
3538 copied++;
3539 }
3540 }
3541 out_resp->count_enum_blobs = enum_count;
3542 }
3543
Rob Clark5ea22f22014-05-30 11:34:01 -04003544 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003545 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3546 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003547 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3548 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003549
3550 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3551 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3552 ret = -EFAULT;
3553 goto done;
3554 }
3555
3556 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3557 ret = -EFAULT;
3558 goto done;
3559 }
3560
3561 copied++;
3562 }
3563 }
3564 out_resp->count_enum_blobs = blob_count;
3565 }
3566done:
Daniel Vetter84849902012-12-02 00:28:11 +01003567 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003568 return ret;
3569}
3570
3571static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3572 void *data)
3573{
3574 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003575 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003576
3577 if (!length || !data)
3578 return NULL;
3579
3580 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3581 if (!blob)
3582 return NULL;
3583
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003584 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3585 if (ret) {
3586 kfree(blob);
3587 return NULL;
3588 }
3589
Dave Airlief453ba02008-11-07 14:05:41 -08003590 blob->length = length;
3591
3592 memcpy(blob->data, data, length);
3593
Dave Airlief453ba02008-11-07 14:05:41 -08003594 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3595 return blob;
3596}
3597
3598static void drm_property_destroy_blob(struct drm_device *dev,
3599 struct drm_property_blob *blob)
3600{
3601 drm_mode_object_put(dev, &blob->base);
3602 list_del(&blob->head);
3603 kfree(blob);
3604}
3605
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003606/**
3607 * drm_mode_getblob_ioctl - get the contents of a blob property value
3608 * @dev: DRM device
3609 * @data: ioctl data
3610 * @file_priv: DRM file info
3611 *
3612 * This function retrieves the contents of a blob property. The value stored in
3613 * an object's blob property is just a normal modeset object id.
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_getblob_ioctl(struct drm_device *dev,
3621 void *data, struct drm_file *file_priv)
3622{
Dave Airlief453ba02008-11-07 14:05:41 -08003623 struct drm_mode_get_blob *out_resp = data;
3624 struct drm_property_blob *blob;
3625 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003626 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003627
Dave Airliefb3b06c2011-02-08 13:55:21 +10003628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3629 return -EINVAL;
3630
Daniel Vetter84849902012-12-02 00:28:11 +01003631 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003632 blob = drm_property_blob_find(dev, out_resp->blob_id);
3633 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003634 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003635 goto done;
3636 }
Dave Airlief453ba02008-11-07 14:05:41 -08003637
3638 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003639 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003640 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3641 ret = -EFAULT;
3642 goto done;
3643 }
3644 }
3645 out_resp->length = blob->length;
3646
3647done:
Daniel Vetter84849902012-12-02 00:28:11 +01003648 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003649 return ret;
3650}
3651
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003652/**
3653 * drm_mode_connector_update_edid_property - update the edid property of a connector
3654 * @connector: drm connector
3655 * @edid: new value of the edid property
3656 *
3657 * This function creates a new blob modeset object and assigns its id to the
3658 * connector's edid property.
3659 *
3660 * Returns:
3661 * Zero on success, errno on failure.
3662 */
Dave Airlief453ba02008-11-07 14:05:41 -08003663int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3664 struct edid *edid)
3665{
3666 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003667 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003668
3669 if (connector->edid_blob_ptr)
3670 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3671
3672 /* Delete edid, when there is none. */
3673 if (!edid) {
3674 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003675 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003676 return ret;
3677 }
3678
Adam Jackson7466f4c2010-03-29 21:43:23 +00003679 size = EDID_LENGTH * (1 + edid->extensions);
3680 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3681 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003682 if (!connector->edid_blob_ptr)
3683 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003684
Rob Clark58495562012-10-11 20:50:56 -05003685 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003686 dev->mode_config.edid_property,
3687 connector->edid_blob_ptr->base.id);
3688
3689 return ret;
3690}
3691EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3692
Paulo Zanoni26a34812012-05-15 18:08:59 -03003693static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003694 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003695{
3696 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3697 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003698
3699 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003700 if (value < property->values[0] || value > property->values[1])
3701 return false;
3702 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003703 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3704 int64_t svalue = U642I64(value);
3705 if (svalue < U642I64(property->values[0]) ||
3706 svalue > U642I64(property->values[1]))
3707 return false;
3708 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003709 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003710 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003711 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003712 for (i = 0; i < property->num_values; i++)
3713 valid_mask |= (1ULL << property->values[i]);
3714 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003715 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003716 /* Only the driver knows */
3717 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003718 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3719 struct drm_mode_object *obj;
3720 /* a zero value for an object property translates to null: */
3721 if (value == 0)
3722 return true;
3723 /*
3724 * NOTE: use _object_find() directly to bypass restriction on
3725 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3726 * object this could race against object finalization, so it
3727 * simply tells us that the object *was* valid. Which is good
3728 * enough.
3729 */
3730 obj = _object_find(property->dev, value, property->values[0]);
3731 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003732 } else {
3733 int i;
3734 for (i = 0; i < property->num_values; i++)
3735 if (property->values[i] == value)
3736 return true;
3737 return false;
3738 }
3739}
3740
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003741/**
3742 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3743 * @dev: DRM device
3744 * @data: ioctl data
3745 * @file_priv: DRM file info
3746 *
3747 * This function sets the current value for a connectors's property. It also
3748 * calls into a driver's ->set_property callback to update the hardware state
3749 *
3750 * Called by the user via ioctl.
3751 *
3752 * Returns:
3753 * Zero on success, errno on failure.
3754 */
Dave Airlief453ba02008-11-07 14:05:41 -08003755int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3756 void *data, struct drm_file *file_priv)
3757{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003758 struct drm_mode_connector_set_property *conn_set_prop = data;
3759 struct drm_mode_obj_set_property obj_set_prop = {
3760 .value = conn_set_prop->value,
3761 .prop_id = conn_set_prop->prop_id,
3762 .obj_id = conn_set_prop->connector_id,
3763 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3764 };
Dave Airlief453ba02008-11-07 14:05:41 -08003765
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003766 /* It does all the locking and checking we need */
3767 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003768}
3769
Paulo Zanonic5431882012-05-15 18:09:02 -03003770static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3771 struct drm_property *property,
3772 uint64_t value)
3773{
3774 int ret = -EINVAL;
3775 struct drm_connector *connector = obj_to_connector(obj);
3776
3777 /* Do DPMS ourselves */
3778 if (property == connector->dev->mode_config.dpms_property) {
3779 if (connector->funcs->dpms)
3780 (*connector->funcs->dpms)(connector, (int)value);
3781 ret = 0;
3782 } else if (connector->funcs->set_property)
3783 ret = connector->funcs->set_property(connector, property, value);
3784
3785 /* store the property value if successful */
3786 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003787 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003788 return ret;
3789}
3790
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003791static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3792 struct drm_property *property,
3793 uint64_t value)
3794{
3795 int ret = -EINVAL;
3796 struct drm_crtc *crtc = obj_to_crtc(obj);
3797
3798 if (crtc->funcs->set_property)
3799 ret = crtc->funcs->set_property(crtc, property, value);
3800 if (!ret)
3801 drm_object_property_set_value(obj, property, value);
3802
3803 return ret;
3804}
3805
Rob Clark4d939142012-05-17 02:23:27 -06003806static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3807 struct drm_property *property,
3808 uint64_t value)
3809{
3810 int ret = -EINVAL;
3811 struct drm_plane *plane = obj_to_plane(obj);
3812
3813 if (plane->funcs->set_property)
3814 ret = plane->funcs->set_property(plane, property, value);
3815 if (!ret)
3816 drm_object_property_set_value(obj, property, value);
3817
3818 return ret;
3819}
3820
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003821/**
3822 * drm_mode_getproperty_ioctl - get the current value of a object's property
3823 * @dev: DRM device
3824 * @data: ioctl data
3825 * @file_priv: DRM file info
3826 *
3827 * This function retrieves the current value for an object's property. Compared
3828 * to the connector specific ioctl this one is extended to also work on crtc and
3829 * plane objects.
3830 *
3831 * Called by the user via ioctl.
3832 *
3833 * Returns:
3834 * Zero on success, errno on failure.
3835 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003836int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3837 struct drm_file *file_priv)
3838{
3839 struct drm_mode_obj_get_properties *arg = data;
3840 struct drm_mode_object *obj;
3841 int ret = 0;
3842 int i;
3843 int copied = 0;
3844 int props_count = 0;
3845 uint32_t __user *props_ptr;
3846 uint64_t __user *prop_values_ptr;
3847
3848 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3849 return -EINVAL;
3850
Daniel Vetter84849902012-12-02 00:28:11 +01003851 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003852
3853 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3854 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003855 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003856 goto out;
3857 }
3858 if (!obj->properties) {
3859 ret = -EINVAL;
3860 goto out;
3861 }
3862
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003863 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003864
3865 /* This ioctl is called twice, once to determine how much space is
3866 * needed, and the 2nd time to fill it. */
3867 if ((arg->count_props >= props_count) && props_count) {
3868 copied = 0;
3869 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3870 prop_values_ptr = (uint64_t __user *)(unsigned long)
3871 (arg->prop_values_ptr);
3872 for (i = 0; i < props_count; i++) {
3873 if (put_user(obj->properties->ids[i],
3874 props_ptr + copied)) {
3875 ret = -EFAULT;
3876 goto out;
3877 }
3878 if (put_user(obj->properties->values[i],
3879 prop_values_ptr + copied)) {
3880 ret = -EFAULT;
3881 goto out;
3882 }
3883 copied++;
3884 }
3885 }
3886 arg->count_props = props_count;
3887out:
Daniel Vetter84849902012-12-02 00:28:11 +01003888 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003889 return ret;
3890}
3891
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003892/**
3893 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3894 * @dev: DRM device
3895 * @data: ioctl data
3896 * @file_priv: DRM file info
3897 *
3898 * This function sets the current value for an object's property. It also calls
3899 * into a driver's ->set_property callback to update the hardware state.
3900 * Compared to the connector specific ioctl this one is extended to also work on
3901 * crtc and plane objects.
3902 *
3903 * Called by the user via ioctl.
3904 *
3905 * Returns:
3906 * Zero on success, errno on failure.
3907 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003908int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3909 struct drm_file *file_priv)
3910{
3911 struct drm_mode_obj_set_property *arg = data;
3912 struct drm_mode_object *arg_obj;
3913 struct drm_mode_object *prop_obj;
3914 struct drm_property *property;
3915 int ret = -EINVAL;
3916 int i;
3917
3918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3919 return -EINVAL;
3920
Daniel Vetter84849902012-12-02 00:28:11 +01003921 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003922
3923 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003924 if (!arg_obj) {
3925 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003926 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003927 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003928 if (!arg_obj->properties)
3929 goto out;
3930
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003931 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003932 if (arg_obj->properties->ids[i] == arg->prop_id)
3933 break;
3934
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003935 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003936 goto out;
3937
3938 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3939 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003940 if (!prop_obj) {
3941 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003942 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003943 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003944 property = obj_to_property(prop_obj);
3945
3946 if (!drm_property_change_is_valid(property, arg->value))
3947 goto out;
3948
3949 switch (arg_obj->type) {
3950 case DRM_MODE_OBJECT_CONNECTOR:
3951 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3952 arg->value);
3953 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003954 case DRM_MODE_OBJECT_CRTC:
3955 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3956 break;
Rob Clark4d939142012-05-17 02:23:27 -06003957 case DRM_MODE_OBJECT_PLANE:
3958 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3959 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003960 }
3961
3962out:
Daniel Vetter84849902012-12-02 00:28:11 +01003963 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003964 return ret;
3965}
3966
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003967/**
3968 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3969 * @connector: connector to attach
3970 * @encoder: encoder to attach @connector to
3971 *
3972 * This function links up a connector to an encoder. Note that the routing
3973 * restrictions between encoders and crtcs are exposed to userspace through the
3974 * possible_clones and possible_crtcs bitmasks.
3975 *
3976 * Returns:
3977 * Zero on success, errno on failure.
3978 */
Dave Airlief453ba02008-11-07 14:05:41 -08003979int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3980 struct drm_encoder *encoder)
3981{
3982 int i;
3983
3984 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3985 if (connector->encoder_ids[i] == 0) {
3986 connector->encoder_ids[i] = encoder->base.id;
3987 return 0;
3988 }
3989 }
3990 return -ENOMEM;
3991}
3992EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3993
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003994/**
3995 * drm_mode_crtc_set_gamma_size - set the gamma table size
3996 * @crtc: CRTC to set the gamma table size for
3997 * @gamma_size: size of the gamma table
3998 *
3999 * Drivers which support gamma tables should set this to the supported gamma
4000 * table size when initializing the CRTC. Currently the drm core only supports a
4001 * fixed gamma table size.
4002 *
4003 * Returns:
4004 * Zero on success, errno on failure.
4005 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004006int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004007 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004008{
4009 crtc->gamma_size = gamma_size;
4010
4011 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4012 if (!crtc->gamma_store) {
4013 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004014 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004015 }
4016
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004017 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004018}
4019EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4020
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004021/**
4022 * drm_mode_gamma_set_ioctl - set the gamma table
4023 * @dev: DRM device
4024 * @data: ioctl data
4025 * @file_priv: DRM file info
4026 *
4027 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4028 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4029 *
4030 * Called by the user via ioctl.
4031 *
4032 * Returns:
4033 * Zero on success, errno on failure.
4034 */
Dave Airlief453ba02008-11-07 14:05:41 -08004035int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4036 void *data, struct drm_file *file_priv)
4037{
4038 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004039 struct drm_crtc *crtc;
4040 void *r_base, *g_base, *b_base;
4041 int size;
4042 int ret = 0;
4043
Dave Airliefb3b06c2011-02-08 13:55:21 +10004044 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4045 return -EINVAL;
4046
Daniel Vetter84849902012-12-02 00:28:11 +01004047 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004048 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4049 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004050 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004051 goto out;
4052 }
Dave Airlief453ba02008-11-07 14:05:41 -08004053
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004054 if (crtc->funcs->gamma_set == NULL) {
4055 ret = -ENOSYS;
4056 goto out;
4057 }
4058
Dave Airlief453ba02008-11-07 14:05:41 -08004059 /* memcpy into gamma store */
4060 if (crtc_lut->gamma_size != crtc->gamma_size) {
4061 ret = -EINVAL;
4062 goto out;
4063 }
4064
4065 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4066 r_base = crtc->gamma_store;
4067 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4068 ret = -EFAULT;
4069 goto out;
4070 }
4071
4072 g_base = r_base + size;
4073 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4074 ret = -EFAULT;
4075 goto out;
4076 }
4077
4078 b_base = g_base + size;
4079 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4080 ret = -EFAULT;
4081 goto out;
4082 }
4083
James Simmons72034252010-08-03 01:33:19 +01004084 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004085
4086out:
Daniel Vetter84849902012-12-02 00:28:11 +01004087 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004088 return ret;
4089
4090}
4091
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004092/**
4093 * drm_mode_gamma_get_ioctl - get the gamma table
4094 * @dev: DRM device
4095 * @data: ioctl data
4096 * @file_priv: DRM file info
4097 *
4098 * Copy the current gamma table into the storage provided. This also provides
4099 * the gamma table size the driver expects, which can be used to size the
4100 * allocated storage.
4101 *
4102 * Called by the user via ioctl.
4103 *
4104 * Returns:
4105 * Zero on success, errno on failure.
4106 */
Dave Airlief453ba02008-11-07 14:05:41 -08004107int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4108 void *data, struct drm_file *file_priv)
4109{
4110 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004111 struct drm_crtc *crtc;
4112 void *r_base, *g_base, *b_base;
4113 int size;
4114 int ret = 0;
4115
Dave Airliefb3b06c2011-02-08 13:55:21 +10004116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4117 return -EINVAL;
4118
Daniel Vetter84849902012-12-02 00:28:11 +01004119 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004120 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4121 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004122 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004123 goto out;
4124 }
Dave Airlief453ba02008-11-07 14:05:41 -08004125
4126 /* memcpy into gamma store */
4127 if (crtc_lut->gamma_size != crtc->gamma_size) {
4128 ret = -EINVAL;
4129 goto out;
4130 }
4131
4132 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4133 r_base = crtc->gamma_store;
4134 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4135 ret = -EFAULT;
4136 goto out;
4137 }
4138
4139 g_base = r_base + size;
4140 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4141 ret = -EFAULT;
4142 goto out;
4143 }
4144
4145 b_base = g_base + size;
4146 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4147 ret = -EFAULT;
4148 goto out;
4149 }
4150out:
Daniel Vetter84849902012-12-02 00:28:11 +01004151 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004152 return ret;
4153}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004154
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004155/**
4156 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4157 * @dev: DRM device
4158 * @data: ioctl data
4159 * @file_priv: DRM file info
4160 *
4161 * This schedules an asynchronous update on a given CRTC, called page flip.
4162 * Optionally a drm event is generated to signal the completion of the event.
4163 * Generic drivers cannot assume that a pageflip with changed framebuffer
4164 * properties (including driver specific metadata like tiling layout) will work,
4165 * but some drivers support e.g. pixel format changes through the pageflip
4166 * ioctl.
4167 *
4168 * Called by the user via ioctl.
4169 *
4170 * Returns:
4171 * Zero on success, errno on failure.
4172 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004173int drm_mode_page_flip_ioctl(struct drm_device *dev,
4174 void *data, struct drm_file *file_priv)
4175{
4176 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004177 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004178 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004179 struct drm_pending_vblank_event *e = NULL;
4180 unsigned long flags;
4181 int ret = -EINVAL;
4182
4183 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4184 page_flip->reserved != 0)
4185 return -EINVAL;
4186
Keith Packard62f21042013-07-22 18:50:00 -07004187 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4188 return -EINVAL;
4189
Rob Clarka2b34e22013-10-05 16:36:52 -04004190 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4191 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004192 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004193
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004194 mutex_lock(&crtc->mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004195 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004196 /* The framebuffer is currently unbound, presumably
4197 * due to a hotplug event, that userspace has not
4198 * yet discovered.
4199 */
4200 ret = -EBUSY;
4201 goto out;
4202 }
4203
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004204 if (crtc->funcs->page_flip == NULL)
4205 goto out;
4206
Daniel Vetter786b99e2012-12-02 21:53:40 +01004207 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004208 if (!fb) {
4209 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004210 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004211 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004212
Damien Lespiauc11e9282013-09-25 16:45:30 +01004213 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4214 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004215 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004216
Matt Roperf4510a22014-04-01 15:22:40 -07004217 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004218 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4219 ret = -EINVAL;
4220 goto out;
4221 }
4222
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004223 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4224 ret = -ENOMEM;
4225 spin_lock_irqsave(&dev->event_lock, flags);
4226 if (file_priv->event_space < sizeof e->event) {
4227 spin_unlock_irqrestore(&dev->event_lock, flags);
4228 goto out;
4229 }
4230 file_priv->event_space -= sizeof e->event;
4231 spin_unlock_irqrestore(&dev->event_lock, flags);
4232
4233 e = kzalloc(sizeof *e, GFP_KERNEL);
4234 if (e == NULL) {
4235 spin_lock_irqsave(&dev->event_lock, flags);
4236 file_priv->event_space += sizeof e->event;
4237 spin_unlock_irqrestore(&dev->event_lock, flags);
4238 goto out;
4239 }
4240
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004241 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004242 e->event.base.length = sizeof e->event;
4243 e->event.user_data = page_flip->user_data;
4244 e->base.event = &e->event.base;
4245 e->base.file_priv = file_priv;
4246 e->base.destroy =
4247 (void (*) (struct drm_pending_event *)) kfree;
4248 }
4249
Matt Roperf4510a22014-04-01 15:22:40 -07004250 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004251 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004252 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004253 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4254 spin_lock_irqsave(&dev->event_lock, flags);
4255 file_priv->event_space += sizeof e->event;
4256 spin_unlock_irqrestore(&dev->event_lock, flags);
4257 kfree(e);
4258 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004259 /* Keep the old fb, don't unref it. */
4260 old_fb = NULL;
4261 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004262 /*
4263 * Warn if the driver hasn't properly updated the crtc->fb
4264 * field to reflect that the new framebuffer is now used.
4265 * Failing to do so will screw with the reference counting
4266 * on framebuffers.
4267 */
Matt Roperf4510a22014-04-01 15:22:40 -07004268 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004269 /* Unref only the old framebuffer. */
4270 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004271 }
4272
4273out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004274 if (fb)
4275 drm_framebuffer_unreference(fb);
4276 if (old_fb)
4277 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004278 mutex_unlock(&crtc->mutex);
4279
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004280 return ret;
4281}
Chris Wilsoneb033552011-01-24 15:11:08 +00004282
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004283/**
4284 * drm_mode_config_reset - call ->reset callbacks
4285 * @dev: drm device
4286 *
4287 * This functions calls all the crtc's, encoder's and connector's ->reset
4288 * callback. Drivers can use this in e.g. their driver load or resume code to
4289 * reset hardware and software state.
4290 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004291void drm_mode_config_reset(struct drm_device *dev)
4292{
4293 struct drm_crtc *crtc;
4294 struct drm_encoder *encoder;
4295 struct drm_connector *connector;
4296
4297 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4298 if (crtc->funcs->reset)
4299 crtc->funcs->reset(crtc);
4300
4301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4302 if (encoder->funcs->reset)
4303 encoder->funcs->reset(encoder);
4304
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004305 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4306 connector->status = connector_status_unknown;
4307
Chris Wilsoneb033552011-01-24 15:11:08 +00004308 if (connector->funcs->reset)
4309 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004310 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004311}
4312EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004313
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004314/**
4315 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4316 * @dev: DRM device
4317 * @data: ioctl data
4318 * @file_priv: DRM file info
4319 *
4320 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4321 * TTM or something else entirely) and returns the resulting buffer handle. This
4322 * handle can then be wrapped up into a framebuffer modeset object.
4323 *
4324 * Note that userspace is not allowed to use such objects for render
4325 * acceleration - drivers must create their own private ioctls for such a use
4326 * case.
4327 *
4328 * Called by the user via ioctl.
4329 *
4330 * Returns:
4331 * Zero on success, errno on failure.
4332 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004333int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4334 void *data, struct drm_file *file_priv)
4335{
4336 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004337 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004338
4339 if (!dev->driver->dumb_create)
4340 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004341 if (!args->width || !args->height || !args->bpp)
4342 return -EINVAL;
4343
4344 /* overflow checks for 32bit size calculations */
4345 cpp = DIV_ROUND_UP(args->bpp, 8);
4346 if (cpp > 0xffffffffU / args->width)
4347 return -EINVAL;
4348 stride = cpp * args->width;
4349 if (args->height > 0xffffffffU / stride)
4350 return -EINVAL;
4351
4352 /* test for wrap-around */
4353 size = args->height * stride;
4354 if (PAGE_ALIGN(size) == 0)
4355 return -EINVAL;
4356
Dave Airlieff72145b2011-02-07 12:16:14 +10004357 return dev->driver->dumb_create(file_priv, dev, args);
4358}
4359
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004360/**
4361 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4362 * @dev: DRM device
4363 * @data: ioctl data
4364 * @file_priv: DRM file info
4365 *
4366 * Allocate an offset in the drm device node's address space to be able to
4367 * memory map a dumb buffer.
4368 *
4369 * Called by the user via ioctl.
4370 *
4371 * Returns:
4372 * Zero on success, errno on failure.
4373 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004374int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4375 void *data, struct drm_file *file_priv)
4376{
4377 struct drm_mode_map_dumb *args = data;
4378
4379 /* call driver ioctl to get mmap offset */
4380 if (!dev->driver->dumb_map_offset)
4381 return -ENOSYS;
4382
4383 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4384}
4385
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004386/**
4387 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4388 * @dev: DRM device
4389 * @data: ioctl data
4390 * @file_priv: DRM file info
4391 *
4392 * This destroys the userspace handle for the given dumb backing storage buffer.
4393 * Since buffer objects must be reference counted in the kernel a buffer object
4394 * won't be immediately freed if a framebuffer modeset object still uses it.
4395 *
4396 * Called by the user via ioctl.
4397 *
4398 * Returns:
4399 * Zero on success, errno on failure.
4400 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004401int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4402 void *data, struct drm_file *file_priv)
4403{
4404 struct drm_mode_destroy_dumb *args = data;
4405
4406 if (!dev->driver->dumb_destroy)
4407 return -ENOSYS;
4408
4409 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4410}
Dave Airlie248dbc22011-11-29 20:02:54 +00004411
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004412/**
4413 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4414 * @format: pixel format (DRM_FORMAT_*)
4415 * @depth: storage for the depth value
4416 * @bpp: storage for the bpp value
4417 *
4418 * This only supports RGB formats here for compat with code that doesn't use
4419 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004420 */
4421void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4422 int *bpp)
4423{
4424 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004425 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004426 case DRM_FORMAT_RGB332:
4427 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004428 *depth = 8;
4429 *bpp = 8;
4430 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004431 case DRM_FORMAT_XRGB1555:
4432 case DRM_FORMAT_XBGR1555:
4433 case DRM_FORMAT_RGBX5551:
4434 case DRM_FORMAT_BGRX5551:
4435 case DRM_FORMAT_ARGB1555:
4436 case DRM_FORMAT_ABGR1555:
4437 case DRM_FORMAT_RGBA5551:
4438 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004439 *depth = 15;
4440 *bpp = 16;
4441 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004442 case DRM_FORMAT_RGB565:
4443 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004444 *depth = 16;
4445 *bpp = 16;
4446 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004447 case DRM_FORMAT_RGB888:
4448 case DRM_FORMAT_BGR888:
4449 *depth = 24;
4450 *bpp = 24;
4451 break;
4452 case DRM_FORMAT_XRGB8888:
4453 case DRM_FORMAT_XBGR8888:
4454 case DRM_FORMAT_RGBX8888:
4455 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004456 *depth = 24;
4457 *bpp = 32;
4458 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004459 case DRM_FORMAT_XRGB2101010:
4460 case DRM_FORMAT_XBGR2101010:
4461 case DRM_FORMAT_RGBX1010102:
4462 case DRM_FORMAT_BGRX1010102:
4463 case DRM_FORMAT_ARGB2101010:
4464 case DRM_FORMAT_ABGR2101010:
4465 case DRM_FORMAT_RGBA1010102:
4466 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004467 *depth = 30;
4468 *bpp = 32;
4469 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004470 case DRM_FORMAT_ARGB8888:
4471 case DRM_FORMAT_ABGR8888:
4472 case DRM_FORMAT_RGBA8888:
4473 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004474 *depth = 32;
4475 *bpp = 32;
4476 break;
4477 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004478 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4479 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004480 *depth = 0;
4481 *bpp = 0;
4482 break;
4483 }
4484}
4485EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004486
4487/**
4488 * drm_format_num_planes - get the number of planes for format
4489 * @format: pixel format (DRM_FORMAT_*)
4490 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004491 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004492 * The number of planes used by the specified pixel format.
4493 */
4494int drm_format_num_planes(uint32_t format)
4495{
4496 switch (format) {
4497 case DRM_FORMAT_YUV410:
4498 case DRM_FORMAT_YVU410:
4499 case DRM_FORMAT_YUV411:
4500 case DRM_FORMAT_YVU411:
4501 case DRM_FORMAT_YUV420:
4502 case DRM_FORMAT_YVU420:
4503 case DRM_FORMAT_YUV422:
4504 case DRM_FORMAT_YVU422:
4505 case DRM_FORMAT_YUV444:
4506 case DRM_FORMAT_YVU444:
4507 return 3;
4508 case DRM_FORMAT_NV12:
4509 case DRM_FORMAT_NV21:
4510 case DRM_FORMAT_NV16:
4511 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004512 case DRM_FORMAT_NV24:
4513 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004514 return 2;
4515 default:
4516 return 1;
4517 }
4518}
4519EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004520
4521/**
4522 * drm_format_plane_cpp - determine the bytes per pixel value
4523 * @format: pixel format (DRM_FORMAT_*)
4524 * @plane: plane index
4525 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004526 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004527 * The bytes per pixel value for the specified plane.
4528 */
4529int drm_format_plane_cpp(uint32_t format, int plane)
4530{
4531 unsigned int depth;
4532 int bpp;
4533
4534 if (plane >= drm_format_num_planes(format))
4535 return 0;
4536
4537 switch (format) {
4538 case DRM_FORMAT_YUYV:
4539 case DRM_FORMAT_YVYU:
4540 case DRM_FORMAT_UYVY:
4541 case DRM_FORMAT_VYUY:
4542 return 2;
4543 case DRM_FORMAT_NV12:
4544 case DRM_FORMAT_NV21:
4545 case DRM_FORMAT_NV16:
4546 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004547 case DRM_FORMAT_NV24:
4548 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004549 return plane ? 2 : 1;
4550 case DRM_FORMAT_YUV410:
4551 case DRM_FORMAT_YVU410:
4552 case DRM_FORMAT_YUV411:
4553 case DRM_FORMAT_YVU411:
4554 case DRM_FORMAT_YUV420:
4555 case DRM_FORMAT_YVU420:
4556 case DRM_FORMAT_YUV422:
4557 case DRM_FORMAT_YVU422:
4558 case DRM_FORMAT_YUV444:
4559 case DRM_FORMAT_YVU444:
4560 return 1;
4561 default:
4562 drm_fb_get_bpp_depth(format, &depth, &bpp);
4563 return bpp >> 3;
4564 }
4565}
4566EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004567
4568/**
4569 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4570 * @format: pixel format (DRM_FORMAT_*)
4571 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004572 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004573 * The horizontal chroma subsampling factor for the
4574 * specified pixel format.
4575 */
4576int drm_format_horz_chroma_subsampling(uint32_t format)
4577{
4578 switch (format) {
4579 case DRM_FORMAT_YUV411:
4580 case DRM_FORMAT_YVU411:
4581 case DRM_FORMAT_YUV410:
4582 case DRM_FORMAT_YVU410:
4583 return 4;
4584 case DRM_FORMAT_YUYV:
4585 case DRM_FORMAT_YVYU:
4586 case DRM_FORMAT_UYVY:
4587 case DRM_FORMAT_VYUY:
4588 case DRM_FORMAT_NV12:
4589 case DRM_FORMAT_NV21:
4590 case DRM_FORMAT_NV16:
4591 case DRM_FORMAT_NV61:
4592 case DRM_FORMAT_YUV422:
4593 case DRM_FORMAT_YVU422:
4594 case DRM_FORMAT_YUV420:
4595 case DRM_FORMAT_YVU420:
4596 return 2;
4597 default:
4598 return 1;
4599 }
4600}
4601EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4602
4603/**
4604 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4605 * @format: pixel format (DRM_FORMAT_*)
4606 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004607 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004608 * The vertical chroma subsampling factor for the
4609 * specified pixel format.
4610 */
4611int drm_format_vert_chroma_subsampling(uint32_t format)
4612{
4613 switch (format) {
4614 case DRM_FORMAT_YUV410:
4615 case DRM_FORMAT_YVU410:
4616 return 4;
4617 case DRM_FORMAT_YUV420:
4618 case DRM_FORMAT_YVU420:
4619 case DRM_FORMAT_NV12:
4620 case DRM_FORMAT_NV21:
4621 return 2;
4622 default:
4623 return 1;
4624 }
4625}
4626EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004627
4628/**
4629 * drm_mode_config_init - initialize DRM mode_configuration structure
4630 * @dev: DRM device
4631 *
4632 * Initialize @dev's mode_config structure, used for tracking the graphics
4633 * configuration of @dev.
4634 *
4635 * Since this initializes the modeset locks, no locking is possible. Which is no
4636 * problem, since this should happen single threaded at init time. It is the
4637 * driver's problem to ensure this guarantee.
4638 *
4639 */
4640void drm_mode_config_init(struct drm_device *dev)
4641{
4642 mutex_init(&dev->mode_config.mutex);
4643 mutex_init(&dev->mode_config.idr_mutex);
4644 mutex_init(&dev->mode_config.fb_lock);
4645 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4646 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4647 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004648 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004649 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4650 INIT_LIST_HEAD(&dev->mode_config.property_list);
4651 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4652 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4653 idr_init(&dev->mode_config.crtc_idr);
4654
4655 drm_modeset_lock_all(dev);
4656 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004657 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004658 drm_modeset_unlock_all(dev);
4659
4660 /* Just to be sure */
4661 dev->mode_config.num_fb = 0;
4662 dev->mode_config.num_connector = 0;
4663 dev->mode_config.num_crtc = 0;
4664 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004665 dev->mode_config.num_overlay_plane = 0;
4666 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004667}
4668EXPORT_SYMBOL(drm_mode_config_init);
4669
4670/**
4671 * drm_mode_config_cleanup - free up DRM mode_config info
4672 * @dev: DRM device
4673 *
4674 * Free up all the connectors and CRTCs associated with this DRM device, then
4675 * free up the framebuffers and associated buffer objects.
4676 *
4677 * Note that since this /should/ happen single-threaded at driver/device
4678 * teardown time, no locking is required. It's the driver's job to ensure that
4679 * this guarantee actually holds true.
4680 *
4681 * FIXME: cleanup any dangling user buffer objects too
4682 */
4683void drm_mode_config_cleanup(struct drm_device *dev)
4684{
4685 struct drm_connector *connector, *ot;
4686 struct drm_crtc *crtc, *ct;
4687 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004688 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004689 struct drm_framebuffer *fb, *fbt;
4690 struct drm_property *property, *pt;
4691 struct drm_property_blob *blob, *bt;
4692 struct drm_plane *plane, *plt;
4693
4694 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4695 head) {
4696 encoder->funcs->destroy(encoder);
4697 }
4698
Sean Paul3b336ec2013-08-14 16:47:37 -04004699 list_for_each_entry_safe(bridge, brt,
4700 &dev->mode_config.bridge_list, head) {
4701 bridge->funcs->destroy(bridge);
4702 }
4703
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004704 list_for_each_entry_safe(connector, ot,
4705 &dev->mode_config.connector_list, head) {
4706 connector->funcs->destroy(connector);
4707 }
4708
4709 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4710 head) {
4711 drm_property_destroy(dev, property);
4712 }
4713
4714 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4715 head) {
4716 drm_property_destroy_blob(dev, blob);
4717 }
4718
4719 /*
4720 * Single-threaded teardown context, so it's not required to grab the
4721 * fb_lock to protect against concurrent fb_list access. Contrary, it
4722 * would actually deadlock with the drm_framebuffer_cleanup function.
4723 *
4724 * Also, if there are any framebuffers left, that's a driver leak now,
4725 * so politely WARN about this.
4726 */
4727 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4728 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4729 drm_framebuffer_remove(fb);
4730 }
4731
4732 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4733 head) {
4734 plane->funcs->destroy(plane);
4735 }
4736
4737 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4738 crtc->funcs->destroy(crtc);
4739 }
4740
4741 idr_destroy(&dev->mode_config.crtc_idr);
4742}
4743EXPORT_SYMBOL(drm_mode_config_cleanup);