blob: 9865313958720c188f42cf3c389cb5e987dd88fc [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/**
260 * drm_get_encoder_name - return a string for encoder
Jani Nikulae5748942014-05-14 16:58:20 +0300261 * @encoder: the encoder to get name for
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100262 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000263const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800264{
Jani Nikulae5748942014-05-14 16:58:20 +0300265 return encoder->name;
Dave Airlief453ba02008-11-07 14:05:41 -0800266}
Dave Airlie13a81952009-09-07 15:45:33 +1000267EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800268
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100269/**
270 * drm_get_connector_name - return a string for connector
Jani Nikula2abdd312014-05-14 16:58:19 +0300271 * @connector: the connector to get name for
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100272 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000273const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800274{
Jani Nikula2abdd312014-05-14 16:58:19 +0300275 return connector->name;
Dave Airlief453ba02008-11-07 14:05:41 -0800276}
277EXPORT_SYMBOL(drm_get_connector_name);
278
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100279/**
280 * drm_get_connector_status_name - return a string for connector status
281 * @status: connector status to compute name of
282 *
283 * In contrast to the other drm_get_*_name functions this one here returns a
284 * const pointer and hence is threadsafe.
285 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000286const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800287{
288 if (status == connector_status_connected)
289 return "connected";
290 else if (status == connector_status_disconnected)
291 return "disconnected";
292 else
293 return "unknown";
294}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000295EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800296
Jesse Barnesac1bb362014-02-10 15:32:44 -0800297/**
298 * drm_get_subpixel_order_name - return a string for a given subpixel enum
299 * @order: enum of subpixel_order
300 *
301 * Note you could abuse this and return something out of bounds, but that
302 * would be a caller error. No unscrubbed user data should make it here.
303 */
304const char *drm_get_subpixel_order_name(enum subpixel_order order)
305{
306 return drm_subpixel_enum_list[order].name;
307}
308EXPORT_SYMBOL(drm_get_subpixel_order_name);
309
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300310static char printable_char(int c)
311{
312 return isascii(c) && isprint(c) ? c : '?';
313}
314
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100315/**
316 * drm_get_format_name - return a string for drm fourcc format
317 * @format: format to compute name of
318 *
319 * Note that the buffer used by this function is globally shared and owned by
320 * the function itself.
321 *
322 * FIXME: This isn't really multithreading safe.
323 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000324const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300325{
326 static char buf[32];
327
328 snprintf(buf, sizeof(buf),
329 "%c%c%c%c %s-endian (0x%08x)",
330 printable_char(format & 0xff),
331 printable_char((format >> 8) & 0xff),
332 printable_char((format >> 16) & 0xff),
333 printable_char((format >> 24) & 0x7f),
334 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
335 format);
336
337 return buf;
338}
339EXPORT_SYMBOL(drm_get_format_name);
340
Dave Airlief453ba02008-11-07 14:05:41 -0800341/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100342 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800343 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100344 * @obj: object pointer, used to generate unique ID
345 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800346 *
Dave Airlief453ba02008-11-07 14:05:41 -0800347 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100348 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
349 * modeset identifiers are _not_ reference counted. Hence don't use this for
350 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800351 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100352 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800353 * New unique (relative to other objects in @dev) integer identifier for the
354 * object.
355 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100356int drm_mode_object_get(struct drm_device *dev,
357 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800358{
Dave Airlief453ba02008-11-07 14:05:41 -0800359 int ret;
360
Jesse Barnesad2563c2009-01-19 17:21:45 +1000361 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800362 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
363 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100364 /*
365 * Set up the object linking under the protection of the idr
366 * lock so that other users can't see inconsistent state.
367 */
Tejun Heo2e928812013-02-27 17:04:08 -0800368 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100369 obj->type = obj_type;
370 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000371 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100372
Tejun Heo2e928812013-02-27 17:04:08 -0800373 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800374}
375
376/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100377 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800378 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100379 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800380 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100381 * Free @id from @dev's unique identifier pool. Note that despite the _get
382 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
383 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800384 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100385void drm_mode_object_put(struct drm_device *dev,
386 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800387{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000388 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800389 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000390 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800391}
392
Daniel Vetter786b99e2012-12-02 21:53:40 +0100393/**
394 * drm_mode_object_find - look up a drm object with static lifetime
395 * @dev: drm device
396 * @id: id of the mode object
397 * @type: type of the mode object
398 *
399 * Note that framebuffers cannot be looked up with this functions - since those
400 * are reference counted, they need special treatment.
401 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200402struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
403 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800404{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000405 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800406
Daniel Vetter786b99e2012-12-02 21:53:40 +0100407 /* Framebuffers are reference counted and need their own lookup
408 * function.*/
409 WARN_ON(type == DRM_MODE_OBJECT_FB);
410
Jesse Barnesad2563c2009-01-19 17:21:45 +1000411 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800412 obj = idr_find(&dev->mode_config.crtc_idr, id);
413 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000414 obj = NULL;
415 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800416
417 return obj;
418}
419EXPORT_SYMBOL(drm_mode_object_find);
420
421/**
Dave Airlief453ba02008-11-07 14:05:41 -0800422 * drm_framebuffer_init - initialize a framebuffer
423 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100424 * @fb: framebuffer to be initialized
425 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800426 *
Dave Airlief453ba02008-11-07 14:05:41 -0800427 * Allocates an ID for the framebuffer's parent mode object, sets its mode
428 * functions & device file and adds it to the master fd list.
429 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100430 * IMPORTANT:
431 * This functions publishes the fb and makes it available for concurrent access
432 * by other users. Which means by this point the fb _must_ be fully set up -
433 * since all the fb attributes are invariant over its lifetime, no further
434 * locking but only correct reference counting is required.
435 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100436 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200437 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800438 */
439int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
440 const struct drm_framebuffer_funcs *funcs)
441{
442 int ret;
443
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100444 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000445 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100446 INIT_LIST_HEAD(&fb->filp_head);
447 fb->dev = dev;
448 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000449
Dave Airlief453ba02008-11-07 14:05:41 -0800450 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200451 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100452 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800453
Daniel Vetter2b677e82012-12-10 21:16:05 +0100454 /* Grab the idr reference. */
455 drm_framebuffer_reference(fb);
456
Dave Airlief453ba02008-11-07 14:05:41 -0800457 dev->mode_config.num_fb++;
458 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100459out:
460 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800461
462 return 0;
463}
464EXPORT_SYMBOL(drm_framebuffer_init);
465
Rob Clarkf7eff602012-09-05 21:48:38 +0000466static void drm_framebuffer_free(struct kref *kref)
467{
468 struct drm_framebuffer *fb =
469 container_of(kref, struct drm_framebuffer, refcount);
470 fb->funcs->destroy(fb);
471}
472
Daniel Vetter2b677e82012-12-10 21:16:05 +0100473static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
474 uint32_t id)
475{
476 struct drm_mode_object *obj = NULL;
477 struct drm_framebuffer *fb;
478
479 mutex_lock(&dev->mode_config.idr_mutex);
480 obj = idr_find(&dev->mode_config.crtc_idr, id);
481 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
482 fb = NULL;
483 else
484 fb = obj_to_fb(obj);
485 mutex_unlock(&dev->mode_config.idr_mutex);
486
487 return fb;
488}
489
Rob Clarkf7eff602012-09-05 21:48:38 +0000490/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100491 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
492 * @dev: drm device
493 * @id: id of the fb object
494 *
495 * If successful, this grabs an additional reference to the framebuffer -
496 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100497 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100498 */
499struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
500 uint32_t id)
501{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100502 struct drm_framebuffer *fb;
503
504 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100505 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100506 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000507 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100508 mutex_unlock(&dev->mode_config.fb_lock);
509
510 return fb;
511}
512EXPORT_SYMBOL(drm_framebuffer_lookup);
513
514/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000515 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100516 * @fb: framebuffer to unref
517 *
518 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000519 */
520void drm_framebuffer_unreference(struct drm_framebuffer *fb)
521{
Rob Clarkf7eff602012-09-05 21:48:38 +0000522 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000523 kref_put(&fb->refcount, drm_framebuffer_free);
524}
525EXPORT_SYMBOL(drm_framebuffer_unreference);
526
527/**
528 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100529 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100530 *
531 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000532 */
533void drm_framebuffer_reference(struct drm_framebuffer *fb)
534{
535 DRM_DEBUG("FB ID: %d\n", fb->base.id);
536 kref_get(&fb->refcount);
537}
538EXPORT_SYMBOL(drm_framebuffer_reference);
539
Daniel Vetter2b677e82012-12-10 21:16:05 +0100540static void drm_framebuffer_free_bug(struct kref *kref)
541{
542 BUG();
543}
544
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100545static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
546{
547 DRM_DEBUG("FB ID: %d\n", fb->base.id);
548 kref_put(&fb->refcount, drm_framebuffer_free_bug);
549}
550
Daniel Vetter2b677e82012-12-10 21:16:05 +0100551/* dev->mode_config.fb_lock must be held! */
552static void __drm_framebuffer_unregister(struct drm_device *dev,
553 struct drm_framebuffer *fb)
554{
555 mutex_lock(&dev->mode_config.idr_mutex);
556 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
557 mutex_unlock(&dev->mode_config.idr_mutex);
558
559 fb->base.id = 0;
560
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100561 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100562}
563
Dave Airlief453ba02008-11-07 14:05:41 -0800564/**
Daniel Vetter36206362012-12-10 20:42:17 +0100565 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
566 * @fb: fb to unregister
567 *
568 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
569 * those used for fbdev. Note that the caller must hold a reference of it's own,
570 * i.e. the object may not be destroyed through this call (since it'll lead to a
571 * locking inversion).
572 */
573void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
574{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100575 struct drm_device *dev = fb->dev;
576
577 mutex_lock(&dev->mode_config.fb_lock);
578 /* Mark fb as reaped and drop idr ref. */
579 __drm_framebuffer_unregister(dev, fb);
580 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100581}
582EXPORT_SYMBOL(drm_framebuffer_unregister_private);
583
584/**
Dave Airlief453ba02008-11-07 14:05:41 -0800585 * drm_framebuffer_cleanup - remove a framebuffer object
586 * @fb: framebuffer to remove
587 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100588 * Cleanup framebuffer. This function is intended to be used from the drivers
589 * ->destroy callback. It can also be used to clean up driver private
590 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100591 *
592 * Note that this function does not remove the fb from active usuage - if it is
593 * still used anywhere, hilarity can ensue since userspace could call getfb on
594 * the id and get back -EINVAL. Obviously no concern at driver unload time.
595 *
596 * Also, the framebuffer will not be removed from the lookup idr - for
597 * user-created framebuffers this will happen in in the rmfb ioctl. For
598 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
599 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800600 */
601void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
602{
603 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100604
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100605 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000606 list_del(&fb->head);
607 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100608 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000609}
610EXPORT_SYMBOL(drm_framebuffer_cleanup);
611
612/**
613 * drm_framebuffer_remove - remove and unreference a framebuffer object
614 * @fb: framebuffer to remove
615 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000616 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100617 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100618 * passed-in framebuffer. Might take the modeset locks.
619 *
620 * Note that this function optimizes the cleanup away if the caller holds the
621 * last reference to the framebuffer. It is also guaranteed to not take the
622 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000623 */
624void drm_framebuffer_remove(struct drm_framebuffer *fb)
625{
626 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800627 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800628 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000629 struct drm_mode_set set;
630 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800631
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100632 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100633
Daniel Vetterb62584e2012-12-11 16:51:35 +0100634 /*
635 * drm ABI mandates that we remove any deleted framebuffers from active
636 * useage. But since most sane clients only remove framebuffers they no
637 * longer need, try to optimize this away.
638 *
639 * Since we're holding a reference ourselves, observing a refcount of 1
640 * means that we're the last holder and can skip it. Also, the refcount
641 * can never increase from 1 again, so we don't need any barriers or
642 * locks.
643 *
644 * Note that userspace could try to race with use and instate a new
645 * usage _after_ we've cleared all current ones. End result will be an
646 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
647 * in this manner.
648 */
649 if (atomic_read(&fb->refcount.refcount) > 1) {
650 drm_modeset_lock_all(dev);
651 /* remove from any CRTC */
652 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700653 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100654 /* should turn off the crtc */
655 memset(&set, 0, sizeof(struct drm_mode_set));
656 set.crtc = crtc;
657 set.fb = NULL;
658 ret = drm_mode_set_config_internal(&set);
659 if (ret)
660 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
661 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000662 }
Dave Airlief453ba02008-11-07 14:05:41 -0800663
Daniel Vetterb62584e2012-12-11 16:51:35 +0100664 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300665 if (plane->fb == fb)
666 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800667 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100668 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800669 }
670
Rob Clarkf7eff602012-09-05 21:48:38 +0000671 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800672}
Rob Clarkf7eff602012-09-05 21:48:38 +0000673EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800674
675/**
Matt Ropere13161a2014-04-01 15:22:38 -0700676 * drm_crtc_init_with_planes - Initialise a new CRTC object with
677 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800678 * @dev: DRM device
679 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700680 * @primary: Primary plane for CRTC
681 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800682 * @funcs: callbacks for the new CRTC
683 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000684 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200685 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100686 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200687 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800688 */
Matt Ropere13161a2014-04-01 15:22:38 -0700689int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
690 struct drm_plane *primary,
691 void *cursor,
692 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800693{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200694 int ret;
695
Dave Airlief453ba02008-11-07 14:05:41 -0800696 crtc->dev = dev;
697 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000698 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800699
Daniel Vetter84849902012-12-02 00:28:11 +0100700 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100701 mutex_init(&crtc->mutex);
702 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703
704 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
705 if (ret)
706 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800707
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300708 crtc->base.properties = &crtc->properties;
709
Dave Airlief453ba02008-11-07 14:05:41 -0800710 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
711 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200712
Matt Ropere13161a2014-04-01 15:22:38 -0700713 crtc->primary = primary;
714 if (primary)
715 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
716
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200717 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100718 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200719
720 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800721}
Matt Ropere13161a2014-04-01 15:22:38 -0700722EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800723
724/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000725 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800726 * @crtc: CRTC to cleanup
727 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000728 * This function cleans up @crtc and removes it from the DRM mode setting
729 * core. Note that the function does *not* free the crtc structure itself,
730 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800731 */
732void drm_crtc_cleanup(struct drm_crtc *crtc)
733{
734 struct drm_device *dev = crtc->dev;
735
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000736 kfree(crtc->gamma_store);
737 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800738
739 drm_mode_object_put(dev, &crtc->base);
740 list_del(&crtc->head);
741 dev->mode_config.num_crtc--;
742}
743EXPORT_SYMBOL(drm_crtc_cleanup);
744
745/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000746 * drm_crtc_index - find the index of a registered CRTC
747 * @crtc: CRTC to find index for
748 *
749 * Given a registered CRTC, return the index of that CRTC within a DRM
750 * device's list of CRTCs.
751 */
752unsigned int drm_crtc_index(struct drm_crtc *crtc)
753{
754 unsigned int index = 0;
755 struct drm_crtc *tmp;
756
757 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
758 if (tmp == crtc)
759 return index;
760
761 index++;
762 }
763
764 BUG();
765}
766EXPORT_SYMBOL(drm_crtc_index);
767
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100768/*
Dave Airlief453ba02008-11-07 14:05:41 -0800769 * drm_mode_remove - remove and free a mode
770 * @connector: connector list to modify
771 * @mode: mode to remove
772 *
Dave Airlief453ba02008-11-07 14:05:41 -0800773 * Remove @mode from @connector's mode list, then free it.
774 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100775static void drm_mode_remove(struct drm_connector *connector,
776 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800777{
778 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100779 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800780}
Dave Airlief453ba02008-11-07 14:05:41 -0800781
782/**
783 * drm_connector_init - Init a preallocated connector
784 * @dev: DRM device
785 * @connector: the connector to init
786 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100787 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800788 *
Dave Airlief453ba02008-11-07 14:05:41 -0800789 * Initialises a preallocated connector. Connectors should be
790 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200791 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100792 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200793 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800794 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200795int drm_connector_init(struct drm_device *dev,
796 struct drm_connector *connector,
797 const struct drm_connector_funcs *funcs,
798 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800799{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200800 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400801 struct ida *connector_ida =
802 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200803
Daniel Vetter84849902012-12-02 00:28:11 +0100804 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800805
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200806 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
807 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300808 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200809
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300810 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800811 connector->dev = dev;
812 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800813 connector->connector_type = connector_type;
814 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400815 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
816 if (connector->connector_type_id < 0) {
817 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300818 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400819 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300820 connector->name =
821 kasprintf(GFP_KERNEL, "%s-%d",
822 drm_connector_enum_list[connector_type].name,
823 connector->connector_type_id);
824 if (!connector->name) {
825 ret = -ENOMEM;
826 goto out_put;
827 }
828
Dave Airlief453ba02008-11-07 14:05:41 -0800829 INIT_LIST_HEAD(&connector->probed_modes);
830 INIT_LIST_HEAD(&connector->modes);
831 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000832 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800833
834 list_add_tail(&connector->head, &dev->mode_config.connector_list);
835 dev->mode_config.num_connector++;
836
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200837 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500838 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200839 dev->mode_config.edid_property,
840 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800841
Rob Clark58495562012-10-11 20:50:56 -0500842 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800843 dev->mode_config.dpms_property, 0);
844
Jani Nikula2abdd312014-05-14 16:58:19 +0300845out_put:
846 if (ret)
847 drm_mode_object_put(dev, &connector->base);
848
849out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100850 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200851
852 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800853}
854EXPORT_SYMBOL(drm_connector_init);
855
856/**
857 * drm_connector_cleanup - cleans up an initialised connector
858 * @connector: connector to cleanup
859 *
Dave Airlief453ba02008-11-07 14:05:41 -0800860 * Cleans up the connector but doesn't free the object.
861 */
862void drm_connector_cleanup(struct drm_connector *connector)
863{
864 struct drm_device *dev = connector->dev;
865 struct drm_display_mode *mode, *t;
866
867 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
868 drm_mode_remove(connector, mode);
869
870 list_for_each_entry_safe(mode, t, &connector->modes, head)
871 drm_mode_remove(connector, mode);
872
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400873 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
874 connector->connector_type_id);
875
Dave Airlief453ba02008-11-07 14:05:41 -0800876 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300877 kfree(connector->name);
878 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800879 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000880 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800881}
882EXPORT_SYMBOL(drm_connector_cleanup);
883
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100884/**
885 * drm_connector_unplug_all - unregister connector userspace interfaces
886 * @dev: drm device
887 *
888 * This function unregisters all connector userspace interfaces in sysfs. Should
889 * be call when the device is disconnected, e.g. from an usb driver's
890 * ->disconnect callback.
891 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000892void drm_connector_unplug_all(struct drm_device *dev)
893{
894 struct drm_connector *connector;
895
896 /* taking the mode config mutex ends up in a clash with sysfs */
897 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
898 drm_sysfs_connector_remove(connector);
899
900}
901EXPORT_SYMBOL(drm_connector_unplug_all);
902
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100903/**
904 * drm_bridge_init - initialize a drm transcoder/bridge
905 * @dev: drm device
906 * @bridge: transcoder/bridge to set up
907 * @funcs: bridge function table
908 *
909 * Initialises a preallocated bridge. Bridges should be
910 * subclassed as part of driver connector objects.
911 *
912 * Returns:
913 * Zero on success, error code on failure.
914 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400915int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
916 const struct drm_bridge_funcs *funcs)
917{
918 int ret;
919
920 drm_modeset_lock_all(dev);
921
922 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
923 if (ret)
924 goto out;
925
926 bridge->dev = dev;
927 bridge->funcs = funcs;
928
929 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
930 dev->mode_config.num_bridge++;
931
932 out:
933 drm_modeset_unlock_all(dev);
934 return ret;
935}
936EXPORT_SYMBOL(drm_bridge_init);
937
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100938/**
939 * drm_bridge_cleanup - cleans up an initialised bridge
940 * @bridge: bridge to cleanup
941 *
942 * Cleans up the bridge but doesn't free the object.
943 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400944void drm_bridge_cleanup(struct drm_bridge *bridge)
945{
946 struct drm_device *dev = bridge->dev;
947
948 drm_modeset_lock_all(dev);
949 drm_mode_object_put(dev, &bridge->base);
950 list_del(&bridge->head);
951 dev->mode_config.num_bridge--;
952 drm_modeset_unlock_all(dev);
953}
954EXPORT_SYMBOL(drm_bridge_cleanup);
955
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100956/**
957 * drm_encoder_init - Init a preallocated encoder
958 * @dev: drm device
959 * @encoder: the encoder to init
960 * @funcs: callbacks for this encoder
961 * @encoder_type: user visible type of the encoder
962 *
963 * Initialises a preallocated encoder. Encoder should be
964 * subclassed as part of driver encoder objects.
965 *
966 * Returns:
967 * Zero on success, error code on failure.
968 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200969int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000970 struct drm_encoder *encoder,
971 const struct drm_encoder_funcs *funcs,
972 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800973{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200974 int ret;
975
Daniel Vetter84849902012-12-02 00:28:11 +0100976 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800977
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200978 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
979 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +0300980 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800981
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200982 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800983 encoder->encoder_type = encoder_type;
984 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +0300985 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
986 drm_encoder_enum_list[encoder_type].name,
987 encoder->base.id);
988 if (!encoder->name) {
989 ret = -ENOMEM;
990 goto out_put;
991 }
Dave Airlief453ba02008-11-07 14:05:41 -0800992
993 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
994 dev->mode_config.num_encoder++;
995
Jani Nikulae5748942014-05-14 16:58:20 +0300996out_put:
997 if (ret)
998 drm_mode_object_put(dev, &encoder->base);
999
1000out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001001 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001002
1003 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001004}
1005EXPORT_SYMBOL(drm_encoder_init);
1006
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001007/**
1008 * drm_encoder_cleanup - cleans up an initialised encoder
1009 * @encoder: encoder to cleanup
1010 *
1011 * Cleans up the encoder but doesn't free the object.
1012 */
Dave Airlief453ba02008-11-07 14:05:41 -08001013void drm_encoder_cleanup(struct drm_encoder *encoder)
1014{
1015 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001016 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001017 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001018 kfree(encoder->name);
1019 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001020 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001021 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001022 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001023}
1024EXPORT_SYMBOL(drm_encoder_cleanup);
1025
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001026/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001027 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001028 * @dev: DRM device
1029 * @plane: plane object to init
1030 * @possible_crtcs: bitmask of possible CRTCs
1031 * @funcs: callbacks for the new plane
1032 * @formats: array of supported formats (%DRM_FORMAT_*)
1033 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001034 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001035 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001036 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001037 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001038 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001039 * Zero on success, error code on failure.
1040 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001041int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1042 unsigned long possible_crtcs,
1043 const struct drm_plane_funcs *funcs,
1044 const uint32_t *formats, uint32_t format_count,
1045 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001046{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001047 int ret;
1048
Daniel Vetter84849902012-12-02 00:28:11 +01001049 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001050
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001051 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1052 if (ret)
1053 goto out;
1054
Rob Clark4d939142012-05-17 02:23:27 -06001055 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001056 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001057 plane->funcs = funcs;
1058 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1059 GFP_KERNEL);
1060 if (!plane->format_types) {
1061 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1062 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001063 ret = -ENOMEM;
1064 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001065 }
1066
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001067 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001068 plane->format_count = format_count;
1069 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001070 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001071
Matt Roperdc415ff2014-04-01 15:22:36 -07001072 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1073 dev->mode_config.num_total_plane++;
1074 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1075 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001076
Rob Clark9922ab52014-04-01 20:16:57 -04001077 drm_object_attach_property(&plane->base,
1078 dev->mode_config.plane_type_property,
1079 plane->type);
1080
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001081 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001082 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001083
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001084 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001085}
Matt Roperdc415ff2014-04-01 15:22:36 -07001086EXPORT_SYMBOL(drm_universal_plane_init);
1087
1088/**
1089 * drm_plane_init - Initialize a legacy plane
1090 * @dev: DRM device
1091 * @plane: plane object to init
1092 * @possible_crtcs: bitmask of possible CRTCs
1093 * @funcs: callbacks for the new plane
1094 * @formats: array of supported formats (%DRM_FORMAT_*)
1095 * @format_count: number of elements in @formats
1096 * @is_primary: plane type (primary vs overlay)
1097 *
1098 * Legacy API to initialize a DRM plane.
1099 *
1100 * New drivers should call drm_universal_plane_init() instead.
1101 *
1102 * Returns:
1103 * Zero on success, error code on failure.
1104 */
1105int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1106 unsigned long possible_crtcs,
1107 const struct drm_plane_funcs *funcs,
1108 const uint32_t *formats, uint32_t format_count,
1109 bool is_primary)
1110{
1111 enum drm_plane_type type;
1112
1113 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1114 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1115 formats, format_count, type);
1116}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001117EXPORT_SYMBOL(drm_plane_init);
1118
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001119/**
1120 * drm_plane_cleanup - Clean up the core plane usage
1121 * @plane: plane to cleanup
1122 *
1123 * This function cleans up @plane and removes it from the DRM mode setting
1124 * core. Note that the function does *not* free the plane structure itself,
1125 * this is the responsibility of the caller.
1126 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001127void drm_plane_cleanup(struct drm_plane *plane)
1128{
1129 struct drm_device *dev = plane->dev;
1130
Daniel Vetter84849902012-12-02 00:28:11 +01001131 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001132 kfree(plane->format_types);
1133 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001134
1135 BUG_ON(list_empty(&plane->head));
1136
1137 list_del(&plane->head);
1138 dev->mode_config.num_total_plane--;
1139 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1140 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001141 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001142}
1143EXPORT_SYMBOL(drm_plane_cleanup);
1144
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001145/**
1146 * drm_plane_force_disable - Forcibly disable a plane
1147 * @plane: plane to disable
1148 *
1149 * Forces the plane to be disabled.
1150 *
1151 * Used when the plane's current framebuffer is destroyed,
1152 * and when restoring fbdev mode.
1153 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001154void drm_plane_force_disable(struct drm_plane *plane)
1155{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001156 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001157 int ret;
1158
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001159 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001160 return;
1161
1162 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001163 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001164 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001165 return;
1166 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001167 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001168 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001169 plane->fb = NULL;
1170 plane->crtc = NULL;
1171}
1172EXPORT_SYMBOL(drm_plane_force_disable);
1173
Dave Airlief453ba02008-11-07 14:05:41 -08001174static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1175{
1176 struct drm_property *edid;
1177 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001178
1179 /*
1180 * Standard properties (apply to all connectors)
1181 */
1182 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1183 DRM_MODE_PROP_IMMUTABLE,
1184 "EDID", 0);
1185 dev->mode_config.edid_property = edid;
1186
Sascha Hauer4a67d392012-02-06 10:58:17 +01001187 dpms = drm_property_create_enum(dev, 0,
1188 "DPMS", drm_dpms_enum_list,
1189 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001190 dev->mode_config.dpms_property = dpms;
1191
1192 return 0;
1193}
1194
Rob Clark9922ab52014-04-01 20:16:57 -04001195static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1196{
1197 struct drm_property *type;
1198
1199 /*
1200 * Standard properties (apply to all planes)
1201 */
1202 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1203 "type", drm_plane_type_enum_list,
1204 ARRAY_SIZE(drm_plane_type_enum_list));
1205 dev->mode_config.plane_type_property = type;
1206
1207 return 0;
1208}
1209
Dave Airlief453ba02008-11-07 14:05:41 -08001210/**
1211 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1212 * @dev: DRM device
1213 *
1214 * Called by a driver the first time a DVI-I connector is made.
1215 */
1216int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1217{
1218 struct drm_property *dvi_i_selector;
1219 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001220
1221 if (dev->mode_config.dvi_i_select_subconnector_property)
1222 return 0;
1223
1224 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001225 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001226 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001227 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001228 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001229 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1230
Sascha Hauer4a67d392012-02-06 10:58:17 +01001231 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001232 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001233 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001234 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001235 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1236
1237 return 0;
1238}
1239EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1240
1241/**
1242 * drm_create_tv_properties - create TV specific connector properties
1243 * @dev: DRM device
1244 * @num_modes: number of different TV formats (modes) supported
1245 * @modes: array of pointers to strings containing name of each format
1246 *
1247 * Called by a driver's TV initialization routine, this function creates
1248 * the TV specific connector properties for a given device. Caller is
1249 * responsible for allocating a list of format names and passing them to
1250 * this routine.
1251 */
1252int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1253 char *modes[])
1254{
1255 struct drm_property *tv_selector;
1256 struct drm_property *tv_subconnector;
1257 int i;
1258
1259 if (dev->mode_config.tv_select_subconnector_property)
1260 return 0;
1261
1262 /*
1263 * Basic connector properties
1264 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001265 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001266 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001267 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001268 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001269 dev->mode_config.tv_select_subconnector_property = tv_selector;
1270
1271 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001272 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1273 "subconnector",
1274 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001275 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001276 dev->mode_config.tv_subconnector_property = tv_subconnector;
1277
1278 /*
1279 * Other, TV specific properties: margins & TV modes.
1280 */
1281 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001282 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001283
1284 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001285 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001286
1287 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001288 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001289
1290 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001291 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001292
1293 dev->mode_config.tv_mode_property =
1294 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1295 "mode", num_modes);
1296 for (i = 0; i < num_modes; i++)
1297 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1298 i, modes[i]);
1299
Francisco Jerezb6b79022009-08-02 04:19:20 +02001300 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001301 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001302
1303 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001304 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001305
1306 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001307 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001308
Francisco Jereza75f0232009-08-12 02:30:10 +02001309 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001310 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001311
1312 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001313 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001314
1315 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001316 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001317
Dave Airlief453ba02008-11-07 14:05:41 -08001318 return 0;
1319}
1320EXPORT_SYMBOL(drm_mode_create_tv_properties);
1321
1322/**
1323 * drm_mode_create_scaling_mode_property - create scaling mode property
1324 * @dev: DRM device
1325 *
1326 * Called by a driver the first time it's needed, must be attached to desired
1327 * connectors.
1328 */
1329int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1330{
1331 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001332
1333 if (dev->mode_config.scaling_mode_property)
1334 return 0;
1335
1336 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001337 drm_property_create_enum(dev, 0, "scaling mode",
1338 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001339 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001340
1341 dev->mode_config.scaling_mode_property = scaling_mode;
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1346
1347/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001348 * drm_mode_create_dirty_property - create dirty property
1349 * @dev: DRM device
1350 *
1351 * Called by a driver the first time it's needed, must be attached to desired
1352 * connectors.
1353 */
1354int drm_mode_create_dirty_info_property(struct drm_device *dev)
1355{
1356 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001357
1358 if (dev->mode_config.dirty_info_property)
1359 return 0;
1360
1361 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001362 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001363 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001364 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001365 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001366 dev->mode_config.dirty_info_property = dirty_info;
1367
1368 return 0;
1369}
1370EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1371
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001372static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001373{
1374 uint32_t total_objects = 0;
1375
1376 total_objects += dev->mode_config.num_crtc;
1377 total_objects += dev->mode_config.num_connector;
1378 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001379 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001380
Dave Airlief453ba02008-11-07 14:05:41 -08001381 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1382 if (!group->id_list)
1383 return -ENOMEM;
1384
1385 group->num_crtcs = 0;
1386 group->num_connectors = 0;
1387 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001388 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001389 return 0;
1390}
1391
Dave Airliead222792014-05-02 13:22:19 +10001392void drm_mode_group_destroy(struct drm_mode_group *group)
1393{
1394 kfree(group->id_list);
1395 group->id_list = NULL;
1396}
1397
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001398/*
1399 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1400 * the drm core's responsibility to set up mode control groups.
1401 */
Dave Airlief453ba02008-11-07 14:05:41 -08001402int drm_mode_group_init_legacy_group(struct drm_device *dev,
1403 struct drm_mode_group *group)
1404{
1405 struct drm_crtc *crtc;
1406 struct drm_encoder *encoder;
1407 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001408 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001409 int ret;
1410
1411 if ((ret = drm_mode_group_init(dev, group)))
1412 return ret;
1413
1414 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1415 group->id_list[group->num_crtcs++] = crtc->base.id;
1416
1417 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1418 group->id_list[group->num_crtcs + group->num_encoders++] =
1419 encoder->base.id;
1420
1421 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1422 group->id_list[group->num_crtcs + group->num_encoders +
1423 group->num_connectors++] = connector->base.id;
1424
Sean Paul3b336ec2013-08-14 16:47:37 -04001425 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1426 group->id_list[group->num_crtcs + group->num_encoders +
1427 group->num_connectors + group->num_bridges++] =
1428 bridge->base.id;
1429
Dave Airlief453ba02008-11-07 14:05:41 -08001430 return 0;
1431}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001432EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001433
1434/**
Dave Airlief453ba02008-11-07 14:05:41 -08001435 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1436 * @out: drm_mode_modeinfo struct to return to the user
1437 * @in: drm_display_mode to use
1438 *
Dave Airlief453ba02008-11-07 14:05:41 -08001439 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1440 * the user.
1441 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001442static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1443 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001444{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001445 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1446 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1447 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1448 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1449 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1450 "timing values too large for mode info\n");
1451
Dave Airlief453ba02008-11-07 14:05:41 -08001452 out->clock = in->clock;
1453 out->hdisplay = in->hdisplay;
1454 out->hsync_start = in->hsync_start;
1455 out->hsync_end = in->hsync_end;
1456 out->htotal = in->htotal;
1457 out->hskew = in->hskew;
1458 out->vdisplay = in->vdisplay;
1459 out->vsync_start = in->vsync_start;
1460 out->vsync_end = in->vsync_end;
1461 out->vtotal = in->vtotal;
1462 out->vscan = in->vscan;
1463 out->vrefresh = in->vrefresh;
1464 out->flags = in->flags;
1465 out->type = in->type;
1466 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1467 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1468}
1469
1470/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001471 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001472 * @out: drm_display_mode to return to the user
1473 * @in: drm_mode_modeinfo to use
1474 *
Dave Airlief453ba02008-11-07 14:05:41 -08001475 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1476 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001477 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001478 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001479 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001480 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001481static int drm_crtc_convert_umode(struct drm_display_mode *out,
1482 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001483{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001484 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1485 return -ERANGE;
1486
Damien Lespiau5848ad42013-09-27 12:11:50 +01001487 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1488 return -EINVAL;
1489
Dave Airlief453ba02008-11-07 14:05:41 -08001490 out->clock = in->clock;
1491 out->hdisplay = in->hdisplay;
1492 out->hsync_start = in->hsync_start;
1493 out->hsync_end = in->hsync_end;
1494 out->htotal = in->htotal;
1495 out->hskew = in->hskew;
1496 out->vdisplay = in->vdisplay;
1497 out->vsync_start = in->vsync_start;
1498 out->vsync_end = in->vsync_end;
1499 out->vtotal = in->vtotal;
1500 out->vscan = in->vscan;
1501 out->vrefresh = in->vrefresh;
1502 out->flags = in->flags;
1503 out->type = in->type;
1504 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1505 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001506
1507 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001508}
1509
1510/**
1511 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001512 * @dev: drm device for the ioctl
1513 * @data: data pointer for the ioctl
1514 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001515 *
Dave Airlief453ba02008-11-07 14:05:41 -08001516 * Construct a set of configuration description structures and return
1517 * them to the user, including CRTC, connector and framebuffer configuration.
1518 *
1519 * Called by the user via ioctl.
1520 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001521 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001522 * Zero on success, errno on failure.
1523 */
1524int drm_mode_getresources(struct drm_device *dev, void *data,
1525 struct drm_file *file_priv)
1526{
1527 struct drm_mode_card_res *card_res = data;
1528 struct list_head *lh;
1529 struct drm_framebuffer *fb;
1530 struct drm_connector *connector;
1531 struct drm_crtc *crtc;
1532 struct drm_encoder *encoder;
1533 int ret = 0;
1534 int connector_count = 0;
1535 int crtc_count = 0;
1536 int fb_count = 0;
1537 int encoder_count = 0;
1538 int copied = 0, i;
1539 uint32_t __user *fb_id;
1540 uint32_t __user *crtc_id;
1541 uint32_t __user *connector_id;
1542 uint32_t __user *encoder_id;
1543 struct drm_mode_group *mode_group;
1544
Dave Airliefb3b06c2011-02-08 13:55:21 +10001545 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1546 return -EINVAL;
1547
Dave Airlief453ba02008-11-07 14:05:41 -08001548
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001549 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001550 /*
1551 * For the non-control nodes we need to limit the list of resources
1552 * by IDs in the group list for this node
1553 */
1554 list_for_each(lh, &file_priv->fbs)
1555 fb_count++;
1556
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001557 /* handle this in 4 parts */
1558 /* FBs */
1559 if (card_res->count_fbs >= fb_count) {
1560 copied = 0;
1561 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1562 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1563 if (put_user(fb->base.id, fb_id + copied)) {
1564 mutex_unlock(&file_priv->fbs_lock);
1565 return -EFAULT;
1566 }
1567 copied++;
1568 }
1569 }
1570 card_res->count_fbs = fb_count;
1571 mutex_unlock(&file_priv->fbs_lock);
1572
1573 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001574 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001575
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001576 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001577 list_for_each(lh, &dev->mode_config.crtc_list)
1578 crtc_count++;
1579
1580 list_for_each(lh, &dev->mode_config.connector_list)
1581 connector_count++;
1582
1583 list_for_each(lh, &dev->mode_config.encoder_list)
1584 encoder_count++;
1585 } else {
1586
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001587 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001588 crtc_count = mode_group->num_crtcs;
1589 connector_count = mode_group->num_connectors;
1590 encoder_count = mode_group->num_encoders;
1591 }
1592
1593 card_res->max_height = dev->mode_config.max_height;
1594 card_res->min_height = dev->mode_config.min_height;
1595 card_res->max_width = dev->mode_config.max_width;
1596 card_res->min_width = dev->mode_config.min_width;
1597
Dave Airlief453ba02008-11-07 14:05:41 -08001598 /* CRTCs */
1599 if (card_res->count_crtcs >= crtc_count) {
1600 copied = 0;
1601 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001602 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001603 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1604 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001605 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001606 if (put_user(crtc->base.id, crtc_id + copied)) {
1607 ret = -EFAULT;
1608 goto out;
1609 }
1610 copied++;
1611 }
1612 } else {
1613 for (i = 0; i < mode_group->num_crtcs; i++) {
1614 if (put_user(mode_group->id_list[i],
1615 crtc_id + copied)) {
1616 ret = -EFAULT;
1617 goto out;
1618 }
1619 copied++;
1620 }
1621 }
1622 }
1623 card_res->count_crtcs = crtc_count;
1624
1625 /* Encoders */
1626 if (card_res->count_encoders >= encoder_count) {
1627 copied = 0;
1628 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001629 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001630 list_for_each_entry(encoder,
1631 &dev->mode_config.encoder_list,
1632 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001633 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001634 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001635 if (put_user(encoder->base.id, encoder_id +
1636 copied)) {
1637 ret = -EFAULT;
1638 goto out;
1639 }
1640 copied++;
1641 }
1642 } else {
1643 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1644 if (put_user(mode_group->id_list[i],
1645 encoder_id + copied)) {
1646 ret = -EFAULT;
1647 goto out;
1648 }
1649 copied++;
1650 }
1651
1652 }
1653 }
1654 card_res->count_encoders = encoder_count;
1655
1656 /* Connectors */
1657 if (card_res->count_connectors >= connector_count) {
1658 copied = 0;
1659 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001660 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001661 list_for_each_entry(connector,
1662 &dev->mode_config.connector_list,
1663 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001664 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1665 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001666 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001667 if (put_user(connector->base.id,
1668 connector_id + copied)) {
1669 ret = -EFAULT;
1670 goto out;
1671 }
1672 copied++;
1673 }
1674 } else {
1675 int start = mode_group->num_crtcs +
1676 mode_group->num_encoders;
1677 for (i = start; i < start + mode_group->num_connectors; i++) {
1678 if (put_user(mode_group->id_list[i],
1679 connector_id + copied)) {
1680 ret = -EFAULT;
1681 goto out;
1682 }
1683 copied++;
1684 }
1685 }
1686 }
1687 card_res->count_connectors = connector_count;
1688
Jerome Glisse94401062010-07-15 15:43:25 -04001689 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001690 card_res->count_connectors, card_res->count_encoders);
1691
1692out:
Daniel Vetter84849902012-12-02 00:28:11 +01001693 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001694 return ret;
1695}
1696
1697/**
1698 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001699 * @dev: drm device for the ioctl
1700 * @data: data pointer for the ioctl
1701 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001702 *
Dave Airlief453ba02008-11-07 14:05:41 -08001703 * Construct a CRTC configuration structure to return to the user.
1704 *
1705 * Called by the user via ioctl.
1706 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001707 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001708 * Zero on success, errno on failure.
1709 */
1710int drm_mode_getcrtc(struct drm_device *dev,
1711 void *data, struct drm_file *file_priv)
1712{
1713 struct drm_mode_crtc *crtc_resp = data;
1714 struct drm_crtc *crtc;
1715 struct drm_mode_object *obj;
1716 int ret = 0;
1717
Dave Airliefb3b06c2011-02-08 13:55:21 +10001718 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1719 return -EINVAL;
1720
Daniel Vetter84849902012-12-02 00:28:11 +01001721 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001722
1723 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1724 DRM_MODE_OBJECT_CRTC);
1725 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001726 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001727 goto out;
1728 }
1729 crtc = obj_to_crtc(obj);
1730
1731 crtc_resp->x = crtc->x;
1732 crtc_resp->y = crtc->y;
1733 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001734 if (crtc->primary->fb)
1735 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001736 else
1737 crtc_resp->fb_id = 0;
1738
1739 if (crtc->enabled) {
1740
1741 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1742 crtc_resp->mode_valid = 1;
1743
1744 } else {
1745 crtc_resp->mode_valid = 0;
1746 }
1747
1748out:
Daniel Vetter84849902012-12-02 00:28:11 +01001749 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001750 return ret;
1751}
1752
Damien Lespiau61d8e322013-09-25 16:45:22 +01001753static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1754 const struct drm_file *file_priv)
1755{
1756 /*
1757 * If user-space hasn't configured the driver to expose the stereo 3D
1758 * modes, don't expose them.
1759 */
1760 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1761 return false;
1762
1763 return true;
1764}
1765
Dave Airlief453ba02008-11-07 14:05:41 -08001766/**
1767 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001768 * @dev: drm device for the ioctl
1769 * @data: data pointer for the ioctl
1770 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001771 *
Dave Airlief453ba02008-11-07 14:05:41 -08001772 * Construct a connector configuration structure to return to the user.
1773 *
1774 * Called by the user via ioctl.
1775 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001776 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001777 * Zero on success, errno on failure.
1778 */
1779int drm_mode_getconnector(struct drm_device *dev, void *data,
1780 struct drm_file *file_priv)
1781{
1782 struct drm_mode_get_connector *out_resp = data;
1783 struct drm_mode_object *obj;
1784 struct drm_connector *connector;
1785 struct drm_display_mode *mode;
1786 int mode_count = 0;
1787 int props_count = 0;
1788 int encoders_count = 0;
1789 int ret = 0;
1790 int copied = 0;
1791 int i;
1792 struct drm_mode_modeinfo u_mode;
1793 struct drm_mode_modeinfo __user *mode_ptr;
1794 uint32_t __user *prop_ptr;
1795 uint64_t __user *prop_values;
1796 uint32_t __user *encoder_ptr;
1797
Dave Airliefb3b06c2011-02-08 13:55:21 +10001798 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1799 return -EINVAL;
1800
Dave Airlief453ba02008-11-07 14:05:41 -08001801 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1802
Jerome Glisse94401062010-07-15 15:43:25 -04001803 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001804
Daniel Vetter7b240562012-12-12 00:35:33 +01001805 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001806
1807 obj = drm_mode_object_find(dev, out_resp->connector_id,
1808 DRM_MODE_OBJECT_CONNECTOR);
1809 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001810 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001811 goto out;
1812 }
1813 connector = obj_to_connector(obj);
1814
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001815 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001816
1817 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1818 if (connector->encoder_ids[i] != 0) {
1819 encoders_count++;
1820 }
1821 }
1822
1823 if (out_resp->count_modes == 0) {
1824 connector->funcs->fill_modes(connector,
1825 dev->mode_config.max_width,
1826 dev->mode_config.max_height);
1827 }
1828
1829 /* delayed so we get modes regardless of pre-fill_modes state */
1830 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001831 if (drm_mode_expose_to_userspace(mode, file_priv))
1832 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001833
1834 out_resp->connector_id = connector->base.id;
1835 out_resp->connector_type = connector->connector_type;
1836 out_resp->connector_type_id = connector->connector_type_id;
1837 out_resp->mm_width = connector->display_info.width_mm;
1838 out_resp->mm_height = connector->display_info.height_mm;
1839 out_resp->subpixel = connector->display_info.subpixel_order;
1840 out_resp->connection = connector->status;
1841 if (connector->encoder)
1842 out_resp->encoder_id = connector->encoder->base.id;
1843 else
1844 out_resp->encoder_id = 0;
1845
1846 /*
1847 * This ioctl is called twice, once to determine how much space is
1848 * needed, and the 2nd time to fill it.
1849 */
1850 if ((out_resp->count_modes >= mode_count) && mode_count) {
1851 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001852 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001853 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001854 if (!drm_mode_expose_to_userspace(mode, file_priv))
1855 continue;
1856
Dave Airlief453ba02008-11-07 14:05:41 -08001857 drm_crtc_convert_to_umode(&u_mode, mode);
1858 if (copy_to_user(mode_ptr + copied,
1859 &u_mode, sizeof(u_mode))) {
1860 ret = -EFAULT;
1861 goto out;
1862 }
1863 copied++;
1864 }
1865 }
1866 out_resp->count_modes = mode_count;
1867
1868 if ((out_resp->count_props >= props_count) && props_count) {
1869 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001870 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1871 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001872 for (i = 0; i < connector->properties.count; i++) {
1873 if (put_user(connector->properties.ids[i],
1874 prop_ptr + copied)) {
1875 ret = -EFAULT;
1876 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001877 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001878
1879 if (put_user(connector->properties.values[i],
1880 prop_values + copied)) {
1881 ret = -EFAULT;
1882 goto out;
1883 }
1884 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001885 }
1886 }
1887 out_resp->count_props = props_count;
1888
1889 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1890 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001891 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001892 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1893 if (connector->encoder_ids[i] != 0) {
1894 if (put_user(connector->encoder_ids[i],
1895 encoder_ptr + copied)) {
1896 ret = -EFAULT;
1897 goto out;
1898 }
1899 copied++;
1900 }
1901 }
1902 }
1903 out_resp->count_encoders = encoders_count;
1904
1905out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001906 mutex_unlock(&dev->mode_config.mutex);
1907
Dave Airlief453ba02008-11-07 14:05:41 -08001908 return ret;
1909}
1910
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001911/**
1912 * drm_mode_getencoder - get encoder configuration
1913 * @dev: drm device for the ioctl
1914 * @data: data pointer for the ioctl
1915 * @file_priv: drm file for the ioctl call
1916 *
1917 * Construct a encoder configuration structure to return to the user.
1918 *
1919 * Called by the user via ioctl.
1920 *
1921 * Returns:
1922 * Zero on success, errno on failure.
1923 */
Dave Airlief453ba02008-11-07 14:05:41 -08001924int drm_mode_getencoder(struct drm_device *dev, void *data,
1925 struct drm_file *file_priv)
1926{
1927 struct drm_mode_get_encoder *enc_resp = data;
1928 struct drm_mode_object *obj;
1929 struct drm_encoder *encoder;
1930 int ret = 0;
1931
Dave Airliefb3b06c2011-02-08 13:55:21 +10001932 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1933 return -EINVAL;
1934
Daniel Vetter84849902012-12-02 00:28:11 +01001935 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001936 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1937 DRM_MODE_OBJECT_ENCODER);
1938 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001939 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001940 goto out;
1941 }
1942 encoder = obj_to_encoder(obj);
1943
1944 if (encoder->crtc)
1945 enc_resp->crtc_id = encoder->crtc->base.id;
1946 else
1947 enc_resp->crtc_id = 0;
1948 enc_resp->encoder_type = encoder->encoder_type;
1949 enc_resp->encoder_id = encoder->base.id;
1950 enc_resp->possible_crtcs = encoder->possible_crtcs;
1951 enc_resp->possible_clones = encoder->possible_clones;
1952
1953out:
Daniel Vetter84849902012-12-02 00:28:11 +01001954 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001955 return ret;
1956}
1957
1958/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001959 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001960 * @dev: DRM device
1961 * @data: ioctl data
1962 * @file_priv: DRM file info
1963 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001964 * Construct a list of plane ids to return to the user.
1965 *
1966 * Called by the user via ioctl.
1967 *
1968 * Returns:
1969 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001970 */
1971int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001972 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001973{
1974 struct drm_mode_get_plane_res *plane_resp = data;
1975 struct drm_mode_config *config;
1976 struct drm_plane *plane;
1977 uint32_t __user *plane_ptr;
1978 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07001979 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001980
1981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1982 return -EINVAL;
1983
Daniel Vetter84849902012-12-02 00:28:11 +01001984 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001985 config = &dev->mode_config;
1986
Matt Roper681e7ec2014-04-01 15:22:42 -07001987 if (file_priv->universal_planes)
1988 num_planes = config->num_total_plane;
1989 else
1990 num_planes = config->num_overlay_plane;
1991
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001992 /*
1993 * This ioctl is called twice, once to determine how much space is
1994 * needed, and the 2nd time to fill it.
1995 */
Matt Roper681e7ec2014-04-01 15:22:42 -07001996 if (num_planes &&
1997 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001998 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001999
2000 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002001 /*
2002 * Unless userspace set the 'universal planes'
2003 * capability bit, only advertise overlays.
2004 */
2005 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2006 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002007 continue;
2008
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002009 if (put_user(plane->base.id, plane_ptr + copied)) {
2010 ret = -EFAULT;
2011 goto out;
2012 }
2013 copied++;
2014 }
2015 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002016 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002017
2018out:
Daniel Vetter84849902012-12-02 00:28:11 +01002019 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002020 return ret;
2021}
2022
2023/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002024 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002025 * @dev: DRM device
2026 * @data: ioctl data
2027 * @file_priv: DRM file info
2028 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002029 * Construct a plane configuration structure to return to the user.
2030 *
2031 * Called by the user via ioctl.
2032 *
2033 * Returns:
2034 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035 */
2036int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002037 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002038{
2039 struct drm_mode_get_plane *plane_resp = data;
2040 struct drm_mode_object *obj;
2041 struct drm_plane *plane;
2042 uint32_t __user *format_ptr;
2043 int ret = 0;
2044
2045 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2046 return -EINVAL;
2047
Daniel Vetter84849902012-12-02 00:28:11 +01002048 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002049 obj = drm_mode_object_find(dev, plane_resp->plane_id,
2050 DRM_MODE_OBJECT_PLANE);
2051 if (!obj) {
2052 ret = -ENOENT;
2053 goto out;
2054 }
2055 plane = obj_to_plane(obj);
2056
2057 if (plane->crtc)
2058 plane_resp->crtc_id = plane->crtc->base.id;
2059 else
2060 plane_resp->crtc_id = 0;
2061
2062 if (plane->fb)
2063 plane_resp->fb_id = plane->fb->base.id;
2064 else
2065 plane_resp->fb_id = 0;
2066
2067 plane_resp->plane_id = plane->base.id;
2068 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002069 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002070
2071 /*
2072 * This ioctl is called twice, once to determine how much space is
2073 * needed, and the 2nd time to fill it.
2074 */
2075 if (plane->format_count &&
2076 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002077 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002078 if (copy_to_user(format_ptr,
2079 plane->format_types,
2080 sizeof(uint32_t) * plane->format_count)) {
2081 ret = -EFAULT;
2082 goto out;
2083 }
2084 }
2085 plane_resp->count_format_types = plane->format_count;
2086
2087out:
Daniel Vetter84849902012-12-02 00:28:11 +01002088 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002089 return ret;
2090}
2091
2092/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002093 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002094 * @dev: DRM device
2095 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002096 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002097 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002098 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002099 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002100 *
2101 * Returns:
2102 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002103 */
2104int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002105 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002106{
2107 struct drm_mode_set_plane *plane_req = data;
2108 struct drm_mode_object *obj;
2109 struct drm_plane *plane;
2110 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002111 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002112 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002113 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002114 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002115
2116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2117 return -EINVAL;
2118
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002119 /*
2120 * First, find the plane, crtc, and fb objects. If not available,
2121 * we don't bother to call the driver.
2122 */
2123 obj = drm_mode_object_find(dev, plane_req->plane_id,
2124 DRM_MODE_OBJECT_PLANE);
2125 if (!obj) {
2126 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2127 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002128 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002129 }
2130 plane = obj_to_plane(obj);
2131
2132 /* No fb means shut it down */
2133 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002134 drm_modeset_lock_all(dev);
2135 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002136 ret = plane->funcs->disable_plane(plane);
2137 if (!ret) {
2138 plane->crtc = NULL;
2139 plane->fb = NULL;
2140 } else {
2141 old_fb = NULL;
2142 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002143 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002144 goto out;
2145 }
2146
2147 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2148 DRM_MODE_OBJECT_CRTC);
2149 if (!obj) {
2150 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2151 plane_req->crtc_id);
2152 ret = -ENOENT;
2153 goto out;
2154 }
2155 crtc = obj_to_crtc(obj);
2156
Daniel Vetter786b99e2012-12-02 21:53:40 +01002157 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2158 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002159 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2160 plane_req->fb_id);
2161 ret = -ENOENT;
2162 goto out;
2163 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002164
Ville Syrjälä62443be2011-12-20 00:06:47 +02002165 /* Check whether this plane supports the fb pixel format. */
2166 for (i = 0; i < plane->format_count; i++)
2167 if (fb->pixel_format == plane->format_types[i])
2168 break;
2169 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002170 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2171 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002172 ret = -EINVAL;
2173 goto out;
2174 }
2175
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002176 fb_width = fb->width << 16;
2177 fb_height = fb->height << 16;
2178
2179 /* Make sure source coordinates are inside the fb. */
2180 if (plane_req->src_w > fb_width ||
2181 plane_req->src_x > fb_width - plane_req->src_w ||
2182 plane_req->src_h > fb_height ||
2183 plane_req->src_y > fb_height - plane_req->src_h) {
2184 DRM_DEBUG_KMS("Invalid source coordinates "
2185 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2186 plane_req->src_w >> 16,
2187 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2188 plane_req->src_h >> 16,
2189 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2190 plane_req->src_x >> 16,
2191 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2192 plane_req->src_y >> 16,
2193 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2194 ret = -ENOSPC;
2195 goto out;
2196 }
2197
Ville Syrjälä687a0402011-12-20 00:06:45 +02002198 /* Give drivers some help against integer overflows */
2199 if (plane_req->crtc_w > INT_MAX ||
2200 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2201 plane_req->crtc_h > INT_MAX ||
2202 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2203 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2204 plane_req->crtc_w, plane_req->crtc_h,
2205 plane_req->crtc_x, plane_req->crtc_y);
2206 ret = -ERANGE;
2207 goto out;
2208 }
2209
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002210 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002211 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002212 ret = plane->funcs->update_plane(plane, crtc, fb,
2213 plane_req->crtc_x, plane_req->crtc_y,
2214 plane_req->crtc_w, plane_req->crtc_h,
2215 plane_req->src_x, plane_req->src_y,
2216 plane_req->src_w, plane_req->src_h);
2217 if (!ret) {
2218 plane->crtc = crtc;
2219 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002220 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002221 } else {
2222 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002223 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002224 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002225
2226out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002227 if (fb)
2228 drm_framebuffer_unreference(fb);
2229 if (old_fb)
2230 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002231
2232 return ret;
2233}
2234
2235/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002236 * drm_mode_set_config_internal - helper to call ->set_config
2237 * @set: modeset config to set
2238 *
2239 * This is a little helper to wrap internal calls to the ->set_config driver
2240 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002241 *
2242 * Returns:
2243 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002244 */
2245int drm_mode_set_config_internal(struct drm_mode_set *set)
2246{
2247 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002248 struct drm_framebuffer *fb;
2249 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002250 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002251
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002252 /*
2253 * NOTE: ->set_config can also disable other crtcs (if we steal all
2254 * connectors from it), hence we need to refcount the fbs across all
2255 * crtcs. Atomic modeset will have saner semantics ...
2256 */
2257 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002258 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002259
Daniel Vetterb0d12322012-12-11 01:07:12 +01002260 fb = set->fb;
2261
2262 ret = crtc->funcs->set_config(set);
2263 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002264 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002265 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002266 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002267
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002268 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002269 if (tmp->primary->fb)
2270 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002271 if (tmp->old_fb)
2272 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002273 }
2274
2275 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002276}
2277EXPORT_SYMBOL(drm_mode_set_config_internal);
2278
Matt Roperaf936292014-04-01 15:22:34 -07002279/**
2280 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2281 * CRTC viewport
2282 * @crtc: CRTC that framebuffer will be displayed on
2283 * @x: x panning
2284 * @y: y panning
2285 * @mode: mode that framebuffer will be displayed under
2286 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002287 */
Matt Roperaf936292014-04-01 15:22:34 -07002288int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2289 int x, int y,
2290 const struct drm_display_mode *mode,
2291 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002292
2293{
2294 int hdisplay, vdisplay;
2295
2296 hdisplay = mode->hdisplay;
2297 vdisplay = mode->vdisplay;
2298
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002299 if (drm_mode_is_stereo(mode)) {
2300 struct drm_display_mode adjusted = *mode;
2301
2302 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2303 hdisplay = adjusted.crtc_hdisplay;
2304 vdisplay = adjusted.crtc_vdisplay;
2305 }
2306
Damien Lespiauc11e9282013-09-25 16:45:30 +01002307 if (crtc->invert_dimensions)
2308 swap(hdisplay, vdisplay);
2309
2310 if (hdisplay > fb->width ||
2311 vdisplay > fb->height ||
2312 x > fb->width - hdisplay ||
2313 y > fb->height - vdisplay) {
2314 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2315 fb->width, fb->height, hdisplay, vdisplay, x, y,
2316 crtc->invert_dimensions ? " (inverted)" : "");
2317 return -ENOSPC;
2318 }
2319
2320 return 0;
2321}
Matt Roperaf936292014-04-01 15:22:34 -07002322EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002323
Daniel Vetter2d13b672012-12-11 13:47:23 +01002324/**
Dave Airlief453ba02008-11-07 14:05:41 -08002325 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002326 * @dev: drm device for the ioctl
2327 * @data: data pointer for the ioctl
2328 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002329 *
Dave Airlief453ba02008-11-07 14:05:41 -08002330 * Build a new CRTC configuration based on user request.
2331 *
2332 * Called by the user via ioctl.
2333 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002334 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002335 * Zero on success, errno on failure.
2336 */
2337int drm_mode_setcrtc(struct drm_device *dev, void *data,
2338 struct drm_file *file_priv)
2339{
2340 struct drm_mode_config *config = &dev->mode_config;
2341 struct drm_mode_crtc *crtc_req = data;
2342 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002343 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002344 struct drm_connector **connector_set = NULL, *connector;
2345 struct drm_framebuffer *fb = NULL;
2346 struct drm_display_mode *mode = NULL;
2347 struct drm_mode_set set;
2348 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002349 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002350 int i;
2351
Dave Airliefb3b06c2011-02-08 13:55:21 +10002352 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2353 return -EINVAL;
2354
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002355 /* For some reason crtc x/y offsets are signed internally. */
2356 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2357 return -ERANGE;
2358
Daniel Vetter84849902012-12-02 00:28:11 +01002359 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002360 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2361 DRM_MODE_OBJECT_CRTC);
2362 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002363 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002364 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002365 goto out;
2366 }
2367 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002368 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002369
2370 if (crtc_req->mode_valid) {
2371 /* If we have a mode we need a framebuffer. */
2372 /* If we pass -1, set the mode with the currently bound fb */
2373 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002374 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002375 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2376 ret = -EINVAL;
2377 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002378 }
Matt Roperf4510a22014-04-01 15:22:40 -07002379 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002380 /* Make refcounting symmetric with the lookup path. */
2381 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002382 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002383 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2384 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002385 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2386 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002387 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002388 goto out;
2389 }
Dave Airlief453ba02008-11-07 14:05:41 -08002390 }
2391
2392 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002393 if (!mode) {
2394 ret = -ENOMEM;
2395 goto out;
2396 }
2397
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002398 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2399 if (ret) {
2400 DRM_DEBUG_KMS("Invalid mode\n");
2401 goto out;
2402 }
2403
Dave Airlief453ba02008-11-07 14:05:41 -08002404 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002405
Damien Lespiauc11e9282013-09-25 16:45:30 +01002406 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2407 mode, fb);
2408 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002409 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002410
Dave Airlief453ba02008-11-07 14:05:41 -08002411 }
2412
2413 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002414 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002415 ret = -EINVAL;
2416 goto out;
2417 }
2418
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002419 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002420 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002421 crtc_req->count_connectors);
2422 ret = -EINVAL;
2423 goto out;
2424 }
2425
2426 if (crtc_req->count_connectors > 0) {
2427 u32 out_id;
2428
2429 /* Avoid unbounded kernel memory allocation */
2430 if (crtc_req->count_connectors > config->num_connector) {
2431 ret = -EINVAL;
2432 goto out;
2433 }
2434
2435 connector_set = kmalloc(crtc_req->count_connectors *
2436 sizeof(struct drm_connector *),
2437 GFP_KERNEL);
2438 if (!connector_set) {
2439 ret = -ENOMEM;
2440 goto out;
2441 }
2442
2443 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002444 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002445 if (get_user(out_id, &set_connectors_ptr[i])) {
2446 ret = -EFAULT;
2447 goto out;
2448 }
2449
2450 obj = drm_mode_object_find(dev, out_id,
2451 DRM_MODE_OBJECT_CONNECTOR);
2452 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002453 DRM_DEBUG_KMS("Connector id %d unknown\n",
2454 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002455 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002456 goto out;
2457 }
2458 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002459 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2460 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002461 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002462
2463 connector_set[i] = connector;
2464 }
2465 }
2466
2467 set.crtc = crtc;
2468 set.x = crtc_req->x;
2469 set.y = crtc_req->y;
2470 set.mode = mode;
2471 set.connectors = connector_set;
2472 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002473 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002474 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002475
2476out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002477 if (fb)
2478 drm_framebuffer_unreference(fb);
2479
Dave Airlief453ba02008-11-07 14:05:41 -08002480 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002481 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002482 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002483 return ret;
2484}
2485
Dave Airlie4c813d42013-06-20 11:48:52 +10002486static int drm_mode_cursor_common(struct drm_device *dev,
2487 struct drm_mode_cursor2 *req,
2488 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002489{
Dave Airlief453ba02008-11-07 14:05:41 -08002490 struct drm_mode_object *obj;
2491 struct drm_crtc *crtc;
2492 int ret = 0;
2493
Dave Airliefb3b06c2011-02-08 13:55:21 +10002494 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2495 return -EINVAL;
2496
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002497 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002498 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002499
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002500 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002501 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002502 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002503 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002504 }
2505 crtc = obj_to_crtc(obj);
2506
Daniel Vetterdac35662012-12-02 15:24:10 +01002507 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002508 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002509 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002510 ret = -ENXIO;
2511 goto out;
2512 }
2513 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002514 if (crtc->funcs->cursor_set2)
2515 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2516 req->width, req->height, req->hot_x, req->hot_y);
2517 else
2518 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2519 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002520 }
2521
2522 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2523 if (crtc->funcs->cursor_move) {
2524 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2525 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002526 ret = -EFAULT;
2527 goto out;
2528 }
2529 }
2530out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002531 mutex_unlock(&crtc->mutex);
2532
Dave Airlief453ba02008-11-07 14:05:41 -08002533 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002534
2535}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002536
2537
2538/**
2539 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2540 * @dev: drm device for the ioctl
2541 * @data: data pointer for the ioctl
2542 * @file_priv: drm file for the ioctl call
2543 *
2544 * Set the cursor configuration based on user request.
2545 *
2546 * Called by the user via ioctl.
2547 *
2548 * Returns:
2549 * Zero on success, errno on failure.
2550 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002551int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002552 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002553{
2554 struct drm_mode_cursor *req = data;
2555 struct drm_mode_cursor2 new_req;
2556
2557 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2558 new_req.hot_x = new_req.hot_y = 0;
2559
2560 return drm_mode_cursor_common(dev, &new_req, file_priv);
2561}
2562
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002563/**
2564 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2565 * @dev: drm device for the ioctl
2566 * @data: data pointer for the ioctl
2567 * @file_priv: drm file for the ioctl call
2568 *
2569 * Set the cursor configuration based on user request. This implements the 2nd
2570 * version of the cursor ioctl, which allows userspace to additionally specify
2571 * the hotspot of the pointer.
2572 *
2573 * Called by the user via ioctl.
2574 *
2575 * Returns:
2576 * Zero on success, errno on failure.
2577 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002578int drm_mode_cursor2_ioctl(struct drm_device *dev,
2579 void *data, struct drm_file *file_priv)
2580{
2581 struct drm_mode_cursor2 *req = data;
2582 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002583}
2584
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002585/**
2586 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2587 * @bpp: bits per pixels
2588 * @depth: bit depth per pixel
2589 *
2590 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2591 * Useful in fbdev emulation code, since that deals in those values.
2592 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002593uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2594{
2595 uint32_t fmt;
2596
2597 switch (bpp) {
2598 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002599 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002600 break;
2601 case 16:
2602 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002603 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002604 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002605 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002606 break;
2607 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002608 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002609 break;
2610 case 32:
2611 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002612 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002613 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002614 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002615 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002616 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002617 break;
2618 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002619 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2620 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002621 break;
2622 }
2623
2624 return fmt;
2625}
2626EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2627
Dave Airlief453ba02008-11-07 14:05:41 -08002628/**
2629 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002630 * @dev: drm device for the ioctl
2631 * @data: data pointer for the ioctl
2632 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002633 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002634 * Add a new FB to the specified CRTC, given a user request. This is the
2635 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002636 *
2637 * Called by the user via ioctl.
2638 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002639 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002640 * Zero on success, errno on failure.
2641 */
2642int drm_mode_addfb(struct drm_device *dev,
2643 void *data, struct drm_file *file_priv)
2644{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002645 struct drm_mode_fb_cmd *or = data;
2646 struct drm_mode_fb_cmd2 r = {};
2647 struct drm_mode_config *config = &dev->mode_config;
2648 struct drm_framebuffer *fb;
2649 int ret = 0;
2650
2651 /* Use new struct with format internally */
2652 r.fb_id = or->fb_id;
2653 r.width = or->width;
2654 r.height = or->height;
2655 r.pitches[0] = or->pitch;
2656 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2657 r.handles[0] = or->handle;
2658
2659 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2660 return -EINVAL;
2661
Jesse Barnesacb4b992011-11-07 12:03:23 -08002662 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002663 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002664
2665 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002666 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002667
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002668 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2669 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002670 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002671 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002672 }
2673
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002674 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002675 or->fb_id = fb->base.id;
2676 list_add(&fb->filp_head, &file_priv->fbs);
2677 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002678 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002679
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002680 return ret;
2681}
2682
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002683static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002684{
2685 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2686
2687 switch (format) {
2688 case DRM_FORMAT_C8:
2689 case DRM_FORMAT_RGB332:
2690 case DRM_FORMAT_BGR233:
2691 case DRM_FORMAT_XRGB4444:
2692 case DRM_FORMAT_XBGR4444:
2693 case DRM_FORMAT_RGBX4444:
2694 case DRM_FORMAT_BGRX4444:
2695 case DRM_FORMAT_ARGB4444:
2696 case DRM_FORMAT_ABGR4444:
2697 case DRM_FORMAT_RGBA4444:
2698 case DRM_FORMAT_BGRA4444:
2699 case DRM_FORMAT_XRGB1555:
2700 case DRM_FORMAT_XBGR1555:
2701 case DRM_FORMAT_RGBX5551:
2702 case DRM_FORMAT_BGRX5551:
2703 case DRM_FORMAT_ARGB1555:
2704 case DRM_FORMAT_ABGR1555:
2705 case DRM_FORMAT_RGBA5551:
2706 case DRM_FORMAT_BGRA5551:
2707 case DRM_FORMAT_RGB565:
2708 case DRM_FORMAT_BGR565:
2709 case DRM_FORMAT_RGB888:
2710 case DRM_FORMAT_BGR888:
2711 case DRM_FORMAT_XRGB8888:
2712 case DRM_FORMAT_XBGR8888:
2713 case DRM_FORMAT_RGBX8888:
2714 case DRM_FORMAT_BGRX8888:
2715 case DRM_FORMAT_ARGB8888:
2716 case DRM_FORMAT_ABGR8888:
2717 case DRM_FORMAT_RGBA8888:
2718 case DRM_FORMAT_BGRA8888:
2719 case DRM_FORMAT_XRGB2101010:
2720 case DRM_FORMAT_XBGR2101010:
2721 case DRM_FORMAT_RGBX1010102:
2722 case DRM_FORMAT_BGRX1010102:
2723 case DRM_FORMAT_ARGB2101010:
2724 case DRM_FORMAT_ABGR2101010:
2725 case DRM_FORMAT_RGBA1010102:
2726 case DRM_FORMAT_BGRA1010102:
2727 case DRM_FORMAT_YUYV:
2728 case DRM_FORMAT_YVYU:
2729 case DRM_FORMAT_UYVY:
2730 case DRM_FORMAT_VYUY:
2731 case DRM_FORMAT_AYUV:
2732 case DRM_FORMAT_NV12:
2733 case DRM_FORMAT_NV21:
2734 case DRM_FORMAT_NV16:
2735 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002736 case DRM_FORMAT_NV24:
2737 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002738 case DRM_FORMAT_YUV410:
2739 case DRM_FORMAT_YVU410:
2740 case DRM_FORMAT_YUV411:
2741 case DRM_FORMAT_YVU411:
2742 case DRM_FORMAT_YUV420:
2743 case DRM_FORMAT_YVU420:
2744 case DRM_FORMAT_YUV422:
2745 case DRM_FORMAT_YVU422:
2746 case DRM_FORMAT_YUV444:
2747 case DRM_FORMAT_YVU444:
2748 return 0;
2749 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002750 DRM_DEBUG_KMS("invalid pixel format %s\n",
2751 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002752 return -EINVAL;
2753 }
2754}
2755
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002756static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002757{
2758 int ret, hsub, vsub, num_planes, i;
2759
2760 ret = format_check(r);
2761 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002762 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2763 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002764 return ret;
2765 }
2766
2767 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2768 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2769 num_planes = drm_format_num_planes(r->pixel_format);
2770
2771 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002772 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002773 return -EINVAL;
2774 }
2775
2776 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002777 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002778 return -EINVAL;
2779 }
2780
2781 for (i = 0; i < num_planes; i++) {
2782 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002783 unsigned int height = r->height / (i != 0 ? vsub : 1);
2784 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002785
2786 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002787 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002788 return -EINVAL;
2789 }
2790
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002791 if ((uint64_t) width * cpp > UINT_MAX)
2792 return -ERANGE;
2793
2794 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2795 return -ERANGE;
2796
2797 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002798 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002799 return -EINVAL;
2800 }
2801 }
2802
2803 return 0;
2804}
2805
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002806/**
2807 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002808 * @dev: drm device for the ioctl
2809 * @data: data pointer for the ioctl
2810 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002811 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002812 * Add a new FB to the specified CRTC, given a user request with format. This is
2813 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2814 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002815 *
2816 * Called by the user via ioctl.
2817 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002818 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002819 * Zero on success, errno on failure.
2820 */
2821int drm_mode_addfb2(struct drm_device *dev,
2822 void *data, struct drm_file *file_priv)
2823{
2824 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002825 struct drm_mode_config *config = &dev->mode_config;
2826 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002827 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002828
Dave Airliefb3b06c2011-02-08 13:55:21 +10002829 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2830 return -EINVAL;
2831
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002832 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2833 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2834 return -EINVAL;
2835 }
2836
Dave Airlief453ba02008-11-07 14:05:41 -08002837 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002838 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002839 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002840 return -EINVAL;
2841 }
2842 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002843 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002844 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002845 return -EINVAL;
2846 }
2847
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002848 ret = framebuffer_check(r);
2849 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002850 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002851
Dave Airlief453ba02008-11-07 14:05:41 -08002852 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002853 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002854 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002855 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002856 }
2857
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002858 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002859 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002860 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002861 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002862 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002863
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002864
Dave Airlief453ba02008-11-07 14:05:41 -08002865 return ret;
2866}
2867
2868/**
2869 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002870 * @dev: drm device for the ioctl
2871 * @data: data pointer for the ioctl
2872 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002873 *
Dave Airlief453ba02008-11-07 14:05:41 -08002874 * Remove the FB specified by the user.
2875 *
2876 * Called by the user via ioctl.
2877 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002878 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002879 * Zero on success, errno on failure.
2880 */
2881int drm_mode_rmfb(struct drm_device *dev,
2882 void *data, struct drm_file *file_priv)
2883{
Dave Airlief453ba02008-11-07 14:05:41 -08002884 struct drm_framebuffer *fb = NULL;
2885 struct drm_framebuffer *fbl = NULL;
2886 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002887 int found = 0;
2888
Dave Airliefb3b06c2011-02-08 13:55:21 +10002889 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2890 return -EINVAL;
2891
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002892 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002893 mutex_lock(&dev->mode_config.fb_lock);
2894 fb = __drm_framebuffer_lookup(dev, *id);
2895 if (!fb)
2896 goto fail_lookup;
2897
Dave Airlief453ba02008-11-07 14:05:41 -08002898 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2899 if (fb == fbl)
2900 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002901 if (!found)
2902 goto fail_lookup;
2903
2904 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2905 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002906
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002907 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002908 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002909 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002910
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002911 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002912
Daniel Vetter2b677e82012-12-10 21:16:05 +01002913 return 0;
2914
2915fail_lookup:
2916 mutex_unlock(&dev->mode_config.fb_lock);
2917 mutex_unlock(&file_priv->fbs_lock);
2918
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002919 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002920}
2921
2922/**
2923 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002924 * @dev: drm device for the ioctl
2925 * @data: data pointer for the ioctl
2926 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002927 *
Dave Airlief453ba02008-11-07 14:05:41 -08002928 * Lookup the FB given its ID and return info about it.
2929 *
2930 * Called by the user via ioctl.
2931 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002932 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002933 * Zero on success, errno on failure.
2934 */
2935int drm_mode_getfb(struct drm_device *dev,
2936 void *data, struct drm_file *file_priv)
2937{
2938 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002939 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002940 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002941
Dave Airliefb3b06c2011-02-08 13:55:21 +10002942 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2943 return -EINVAL;
2944
Daniel Vetter786b99e2012-12-02 21:53:40 +01002945 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002946 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002947 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002948
2949 r->height = fb->height;
2950 r->width = fb->width;
2951 r->depth = fb->depth;
2952 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002953 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002954 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01002955 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01002956 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02002957 ret = fb->funcs->create_handle(fb, file_priv,
2958 &r->handle);
2959 } else {
2960 /* GET_FB() is an unprivileged ioctl so we must not
2961 * return a buffer-handle to non-master processes! For
2962 * backwards-compatibility reasons, we cannot make
2963 * GET_FB() privileged, so just return an invalid handle
2964 * for non-masters. */
2965 r->handle = 0;
2966 ret = 0;
2967 }
2968 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002969 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002970 }
Dave Airlief453ba02008-11-07 14:05:41 -08002971
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002972 drm_framebuffer_unreference(fb);
2973
Dave Airlief453ba02008-11-07 14:05:41 -08002974 return ret;
2975}
2976
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002977/**
2978 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2979 * @dev: drm device for the ioctl
2980 * @data: data pointer for the ioctl
2981 * @file_priv: drm file for the ioctl call
2982 *
2983 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2984 * rectangle list. Generic userspace which does frontbuffer rendering must call
2985 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2986 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2987 *
2988 * Modesetting drivers which always update the frontbuffer do not need to
2989 * implement the corresponding ->dirty framebuffer callback.
2990 *
2991 * Called by the user via ioctl.
2992 *
2993 * Returns:
2994 * Zero on success, errno on failure.
2995 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002996int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2997 void *data, struct drm_file *file_priv)
2998{
2999 struct drm_clip_rect __user *clips_ptr;
3000 struct drm_clip_rect *clips = NULL;
3001 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003002 struct drm_framebuffer *fb;
3003 unsigned flags;
3004 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003005 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003006
Dave Airliefb3b06c2011-02-08 13:55:21 +10003007 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3008 return -EINVAL;
3009
Daniel Vetter786b99e2012-12-02 21:53:40 +01003010 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003011 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003012 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003013
3014 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003015 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003016
3017 if (!num_clips != !clips_ptr) {
3018 ret = -EINVAL;
3019 goto out_err1;
3020 }
3021
3022 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3023
3024 /* If userspace annotates copy, clips must come in pairs */
3025 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3026 ret = -EINVAL;
3027 goto out_err1;
3028 }
3029
3030 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003031 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3032 ret = -EINVAL;
3033 goto out_err1;
3034 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003035 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3036 if (!clips) {
3037 ret = -ENOMEM;
3038 goto out_err1;
3039 }
3040
3041 ret = copy_from_user(clips, clips_ptr,
3042 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003043 if (ret) {
3044 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003045 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003046 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003047 }
3048
3049 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003050 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3051 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003052 } else {
3053 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003054 }
3055
3056out_err2:
3057 kfree(clips);
3058out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003059 drm_framebuffer_unreference(fb);
3060
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003061 return ret;
3062}
3063
3064
Dave Airlief453ba02008-11-07 14:05:41 -08003065/**
3066 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003067 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003068 *
Dave Airlief453ba02008-11-07 14:05:41 -08003069 * Destroy all the FBs associated with @filp.
3070 *
3071 * Called by the user via ioctl.
3072 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003073 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003074 * Zero on success, errno on failure.
3075 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003076void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003077{
Dave Airlief453ba02008-11-07 14:05:41 -08003078 struct drm_device *dev = priv->minor->dev;
3079 struct drm_framebuffer *fb, *tfb;
3080
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003081 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003082 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003083
3084 mutex_lock(&dev->mode_config.fb_lock);
3085 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3086 __drm_framebuffer_unregister(dev, fb);
3087 mutex_unlock(&dev->mode_config.fb_lock);
3088
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003089 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003090
3091 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003092 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003093 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003094 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003095}
3096
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003097/**
3098 * drm_property_create - create a new property type
3099 * @dev: drm device
3100 * @flags: flags specifying the property type
3101 * @name: name of the property
3102 * @num_values: number of pre-defined values
3103 *
3104 * This creates a new generic drm property which can then be attached to a drm
3105 * object with drm_object_attach_property. The returned property object must be
3106 * freed with drm_property_destroy.
3107 *
3108 * Returns:
3109 * A pointer to the newly created property on success, NULL on failure.
3110 */
Dave Airlief453ba02008-11-07 14:05:41 -08003111struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3112 const char *name, int num_values)
3113{
3114 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003115 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003116
3117 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3118 if (!property)
3119 return NULL;
3120
3121 if (num_values) {
3122 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3123 if (!property->values)
3124 goto fail;
3125 }
3126
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003127 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3128 if (ret)
3129 goto fail;
3130
Dave Airlief453ba02008-11-07 14:05:41 -08003131 property->flags = flags;
3132 property->num_values = num_values;
3133 INIT_LIST_HEAD(&property->enum_blob_list);
3134
Vinson Lee471dd2e2011-11-10 11:55:40 -08003135 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003136 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003137 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3138 }
Dave Airlief453ba02008-11-07 14:05:41 -08003139
3140 list_add_tail(&property->head, &dev->mode_config.property_list);
3141 return property;
3142fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003143 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003144 kfree(property);
3145 return NULL;
3146}
3147EXPORT_SYMBOL(drm_property_create);
3148
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003149/**
3150 * drm_property_create - create a new enumeration property type
3151 * @dev: drm device
3152 * @flags: flags specifying the property type
3153 * @name: name of the property
3154 * @props: enumeration lists with property values
3155 * @num_values: number of pre-defined values
3156 *
3157 * This creates a new generic drm property which can then be attached to a drm
3158 * object with drm_object_attach_property. The returned property object must be
3159 * freed with drm_property_destroy.
3160 *
3161 * Userspace is only allowed to set one of the predefined values for enumeration
3162 * properties.
3163 *
3164 * Returns:
3165 * A pointer to the newly created property on success, NULL on failure.
3166 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003167struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3168 const char *name,
3169 const struct drm_prop_enum_list *props,
3170 int num_values)
3171{
3172 struct drm_property *property;
3173 int i, ret;
3174
3175 flags |= DRM_MODE_PROP_ENUM;
3176
3177 property = drm_property_create(dev, flags, name, num_values);
3178 if (!property)
3179 return NULL;
3180
3181 for (i = 0; i < num_values; i++) {
3182 ret = drm_property_add_enum(property, i,
3183 props[i].type,
3184 props[i].name);
3185 if (ret) {
3186 drm_property_destroy(dev, property);
3187 return NULL;
3188 }
3189 }
3190
3191 return property;
3192}
3193EXPORT_SYMBOL(drm_property_create_enum);
3194
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003195/**
3196 * drm_property_create - create a new bitmask property type
3197 * @dev: drm device
3198 * @flags: flags specifying the property type
3199 * @name: name of the property
3200 * @props: enumeration lists with property bitflags
3201 * @num_values: number of pre-defined values
3202 *
3203 * This creates a new generic drm property which can then be attached to a drm
3204 * object with drm_object_attach_property. The returned property object must be
3205 * freed with drm_property_destroy.
3206 *
3207 * Compared to plain enumeration properties userspace is allowed to set any
3208 * or'ed together combination of the predefined property bitflag values
3209 *
3210 * Returns:
3211 * A pointer to the newly created property on success, NULL on failure.
3212 */
Rob Clark49e27542012-05-17 02:23:26 -06003213struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3214 int flags, const char *name,
3215 const struct drm_prop_enum_list *props,
3216 int num_values)
3217{
3218 struct drm_property *property;
3219 int i, ret;
3220
3221 flags |= DRM_MODE_PROP_BITMASK;
3222
3223 property = drm_property_create(dev, flags, name, num_values);
3224 if (!property)
3225 return NULL;
3226
3227 for (i = 0; i < num_values; i++) {
3228 ret = drm_property_add_enum(property, i,
3229 props[i].type,
3230 props[i].name);
3231 if (ret) {
3232 drm_property_destroy(dev, property);
3233 return NULL;
3234 }
3235 }
3236
3237 return property;
3238}
3239EXPORT_SYMBOL(drm_property_create_bitmask);
3240
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003241/**
3242 * drm_property_create - create a new ranged property type
3243 * @dev: drm device
3244 * @flags: flags specifying the property type
3245 * @name: name of the property
3246 * @min: minimum value of the property
3247 * @max: maximum value of the property
3248 *
3249 * This creates a new generic drm property which can then be attached to a drm
3250 * object with drm_object_attach_property. The returned property object must be
3251 * freed with drm_property_destroy.
3252 *
3253 * Userspace is allowed to set any interger value in the (min, max) range
3254 * inclusive.
3255 *
3256 * Returns:
3257 * A pointer to the newly created property on success, NULL on failure.
3258 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003259struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3260 const char *name,
3261 uint64_t min, uint64_t max)
3262{
3263 struct drm_property *property;
3264
3265 flags |= DRM_MODE_PROP_RANGE;
3266
3267 property = drm_property_create(dev, flags, name, 2);
3268 if (!property)
3269 return NULL;
3270
3271 property->values[0] = min;
3272 property->values[1] = max;
3273
3274 return property;
3275}
3276EXPORT_SYMBOL(drm_property_create_range);
3277
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003278/**
3279 * drm_property_add_enum - add a possible value to an enumeration property
3280 * @property: enumeration property to change
3281 * @index: index of the new enumeration
3282 * @value: value of the new enumeration
3283 * @name: symbolic name of the new enumeration
3284 *
3285 * This functions adds enumerations to a property.
3286 *
3287 * It's use is deprecated, drivers should use one of the more specific helpers
3288 * to directly create the property with all enumerations already attached.
3289 *
3290 * Returns:
3291 * Zero on success, error code on failure.
3292 */
Dave Airlief453ba02008-11-07 14:05:41 -08003293int drm_property_add_enum(struct drm_property *property, int index,
3294 uint64_t value, const char *name)
3295{
3296 struct drm_property_enum *prop_enum;
3297
Rob Clark49e27542012-05-17 02:23:26 -06003298 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3299 return -EINVAL;
3300
3301 /*
3302 * Bitmask enum properties have the additional constraint of values
3303 * from 0 to 63
3304 */
3305 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003306 return -EINVAL;
3307
3308 if (!list_empty(&property->enum_blob_list)) {
3309 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3310 if (prop_enum->value == value) {
3311 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3312 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3313 return 0;
3314 }
3315 }
3316 }
3317
3318 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3319 if (!prop_enum)
3320 return -ENOMEM;
3321
3322 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3323 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3324 prop_enum->value = value;
3325
3326 property->values[index] = value;
3327 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3328 return 0;
3329}
3330EXPORT_SYMBOL(drm_property_add_enum);
3331
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003332/**
3333 * drm_property_destroy - destroy a drm property
3334 * @dev: drm device
3335 * @property: property to destry
3336 *
3337 * This function frees a property including any attached resources like
3338 * enumeration values.
3339 */
Dave Airlief453ba02008-11-07 14:05:41 -08003340void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3341{
3342 struct drm_property_enum *prop_enum, *pt;
3343
3344 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3345 list_del(&prop_enum->head);
3346 kfree(prop_enum);
3347 }
3348
3349 if (property->num_values)
3350 kfree(property->values);
3351 drm_mode_object_put(dev, &property->base);
3352 list_del(&property->head);
3353 kfree(property);
3354}
3355EXPORT_SYMBOL(drm_property_destroy);
3356
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003357/**
3358 * drm_object_attach_property - attach a property to a modeset object
3359 * @obj: drm modeset object
3360 * @property: property to attach
3361 * @init_val: initial value of the property
3362 *
3363 * This attaches the given property to the modeset object with the given initial
3364 * value. Currently this function cannot fail since the properties are stored in
3365 * a statically sized array.
3366 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003367void drm_object_attach_property(struct drm_mode_object *obj,
3368 struct drm_property *property,
3369 uint64_t init_val)
3370{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003371 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003372
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003373 if (count == DRM_OBJECT_MAX_PROPERTY) {
3374 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3375 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3376 "you see this message on the same object type.\n",
3377 obj->type);
3378 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003379 }
3380
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003381 obj->properties->ids[count] = property->base.id;
3382 obj->properties->values[count] = init_val;
3383 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003384}
3385EXPORT_SYMBOL(drm_object_attach_property);
3386
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003387/**
3388 * drm_object_property_set_value - set the value of a property
3389 * @obj: drm mode object to set property value for
3390 * @property: property to set
3391 * @val: value the property should be set to
3392 *
3393 * This functions sets a given property on a given object. This function only
3394 * changes the software state of the property, it does not call into the
3395 * driver's ->set_property callback.
3396 *
3397 * Returns:
3398 * Zero on success, error code on failure.
3399 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003400int drm_object_property_set_value(struct drm_mode_object *obj,
3401 struct drm_property *property, uint64_t val)
3402{
3403 int i;
3404
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003405 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003406 if (obj->properties->ids[i] == property->base.id) {
3407 obj->properties->values[i] = val;
3408 return 0;
3409 }
3410 }
3411
3412 return -EINVAL;
3413}
3414EXPORT_SYMBOL(drm_object_property_set_value);
3415
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003416/**
3417 * drm_object_property_get_value - retrieve the value of a property
3418 * @obj: drm mode object to get property value from
3419 * @property: property to retrieve
3420 * @val: storage for the property value
3421 *
3422 * This function retrieves the softare state of the given property for the given
3423 * property. Since there is no driver callback to retrieve the current property
3424 * value this might be out of sync with the hardware, depending upon the driver
3425 * and property.
3426 *
3427 * Returns:
3428 * Zero on success, error code on failure.
3429 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003430int drm_object_property_get_value(struct drm_mode_object *obj,
3431 struct drm_property *property, uint64_t *val)
3432{
3433 int i;
3434
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003435 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003436 if (obj->properties->ids[i] == property->base.id) {
3437 *val = obj->properties->values[i];
3438 return 0;
3439 }
3440 }
3441
3442 return -EINVAL;
3443}
3444EXPORT_SYMBOL(drm_object_property_get_value);
3445
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003446/**
3447 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3448 * @dev: DRM device
3449 * @data: ioctl data
3450 * @file_priv: DRM file info
3451 *
3452 * This function retrieves the current value for an connectors's property.
3453 *
3454 * Called by the user via ioctl.
3455 *
3456 * Returns:
3457 * Zero on success, errno on failure.
3458 */
Dave Airlief453ba02008-11-07 14:05:41 -08003459int drm_mode_getproperty_ioctl(struct drm_device *dev,
3460 void *data, struct drm_file *file_priv)
3461{
3462 struct drm_mode_object *obj;
3463 struct drm_mode_get_property *out_resp = data;
3464 struct drm_property *property;
3465 int enum_count = 0;
3466 int blob_count = 0;
3467 int value_count = 0;
3468 int ret = 0, i;
3469 int copied;
3470 struct drm_property_enum *prop_enum;
3471 struct drm_mode_property_enum __user *enum_ptr;
3472 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003473 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003474 uint64_t __user *values_ptr;
3475 uint32_t __user *blob_length_ptr;
3476
Dave Airliefb3b06c2011-02-08 13:55:21 +10003477 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3478 return -EINVAL;
3479
Daniel Vetter84849902012-12-02 00:28:11 +01003480 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003481 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3482 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003483 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003484 goto done;
3485 }
3486 property = obj_to_property(obj);
3487
Rob Clark49e27542012-05-17 02:23:26 -06003488 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003489 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3490 enum_count++;
3491 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3492 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3493 blob_count++;
3494 }
3495
3496 value_count = property->num_values;
3497
3498 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3499 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3500 out_resp->flags = property->flags;
3501
3502 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003503 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003504 for (i = 0; i < value_count; i++) {
3505 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3506 ret = -EFAULT;
3507 goto done;
3508 }
3509 }
3510 }
3511 out_resp->count_values = value_count;
3512
Rob Clark49e27542012-05-17 02:23:26 -06003513 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003514 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3515 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003516 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003517 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3518
3519 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3520 ret = -EFAULT;
3521 goto done;
3522 }
3523
3524 if (copy_to_user(&enum_ptr[copied].name,
3525 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3526 ret = -EFAULT;
3527 goto done;
3528 }
3529 copied++;
3530 }
3531 }
3532 out_resp->count_enum_blobs = enum_count;
3533 }
3534
3535 if (property->flags & DRM_MODE_PROP_BLOB) {
3536 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3537 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003538 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3539 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003540
3541 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3542 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3543 ret = -EFAULT;
3544 goto done;
3545 }
3546
3547 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3548 ret = -EFAULT;
3549 goto done;
3550 }
3551
3552 copied++;
3553 }
3554 }
3555 out_resp->count_enum_blobs = blob_count;
3556 }
3557done:
Daniel Vetter84849902012-12-02 00:28:11 +01003558 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003559 return ret;
3560}
3561
3562static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3563 void *data)
3564{
3565 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003566 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003567
3568 if (!length || !data)
3569 return NULL;
3570
3571 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3572 if (!blob)
3573 return NULL;
3574
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003575 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3576 if (ret) {
3577 kfree(blob);
3578 return NULL;
3579 }
3580
Dave Airlief453ba02008-11-07 14:05:41 -08003581 blob->length = length;
3582
3583 memcpy(blob->data, data, length);
3584
Dave Airlief453ba02008-11-07 14:05:41 -08003585 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3586 return blob;
3587}
3588
3589static void drm_property_destroy_blob(struct drm_device *dev,
3590 struct drm_property_blob *blob)
3591{
3592 drm_mode_object_put(dev, &blob->base);
3593 list_del(&blob->head);
3594 kfree(blob);
3595}
3596
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003597/**
3598 * drm_mode_getblob_ioctl - get the contents of a blob property value
3599 * @dev: DRM device
3600 * @data: ioctl data
3601 * @file_priv: DRM file info
3602 *
3603 * This function retrieves the contents of a blob property. The value stored in
3604 * an object's blob property is just a normal modeset object id.
3605 *
3606 * Called by the user via ioctl.
3607 *
3608 * Returns:
3609 * Zero on success, errno on failure.
3610 */
Dave Airlief453ba02008-11-07 14:05:41 -08003611int drm_mode_getblob_ioctl(struct drm_device *dev,
3612 void *data, struct drm_file *file_priv)
3613{
3614 struct drm_mode_object *obj;
3615 struct drm_mode_get_blob *out_resp = data;
3616 struct drm_property_blob *blob;
3617 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003618 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003619
Dave Airliefb3b06c2011-02-08 13:55:21 +10003620 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3621 return -EINVAL;
3622
Daniel Vetter84849902012-12-02 00:28:11 +01003623 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003624 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3625 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003626 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003627 goto done;
3628 }
3629 blob = obj_to_blob(obj);
3630
3631 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003632 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003633 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3634 ret = -EFAULT;
3635 goto done;
3636 }
3637 }
3638 out_resp->length = blob->length;
3639
3640done:
Daniel Vetter84849902012-12-02 00:28:11 +01003641 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003642 return ret;
3643}
3644
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003645/**
3646 * drm_mode_connector_update_edid_property - update the edid property of a connector
3647 * @connector: drm connector
3648 * @edid: new value of the edid property
3649 *
3650 * This function creates a new blob modeset object and assigns its id to the
3651 * connector's edid property.
3652 *
3653 * Returns:
3654 * Zero on success, errno on failure.
3655 */
Dave Airlief453ba02008-11-07 14:05:41 -08003656int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3657 struct edid *edid)
3658{
3659 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003660 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003661
3662 if (connector->edid_blob_ptr)
3663 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3664
3665 /* Delete edid, when there is none. */
3666 if (!edid) {
3667 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003668 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003669 return ret;
3670 }
3671
Adam Jackson7466f4c2010-03-29 21:43:23 +00003672 size = EDID_LENGTH * (1 + edid->extensions);
3673 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3674 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003675 if (!connector->edid_blob_ptr)
3676 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003677
Rob Clark58495562012-10-11 20:50:56 -05003678 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003679 dev->mode_config.edid_property,
3680 connector->edid_blob_ptr->base.id);
3681
3682 return ret;
3683}
3684EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3685
Paulo Zanoni26a34812012-05-15 18:08:59 -03003686static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003687 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003688{
3689 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3690 return false;
3691 if (property->flags & DRM_MODE_PROP_RANGE) {
3692 if (value < property->values[0] || value > property->values[1])
3693 return false;
3694 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003695 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3696 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003697 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003698 for (i = 0; i < property->num_values; i++)
3699 valid_mask |= (1ULL << property->values[i]);
3700 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003701 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3702 /* Only the driver knows */
3703 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003704 } else {
3705 int i;
3706 for (i = 0; i < property->num_values; i++)
3707 if (property->values[i] == value)
3708 return true;
3709 return false;
3710 }
3711}
3712
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003713/**
3714 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3715 * @dev: DRM device
3716 * @data: ioctl data
3717 * @file_priv: DRM file info
3718 *
3719 * This function sets the current value for a connectors's property. It also
3720 * calls into a driver's ->set_property callback to update the hardware state
3721 *
3722 * Called by the user via ioctl.
3723 *
3724 * Returns:
3725 * Zero on success, errno on failure.
3726 */
Dave Airlief453ba02008-11-07 14:05:41 -08003727int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3728 void *data, struct drm_file *file_priv)
3729{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003730 struct drm_mode_connector_set_property *conn_set_prop = data;
3731 struct drm_mode_obj_set_property obj_set_prop = {
3732 .value = conn_set_prop->value,
3733 .prop_id = conn_set_prop->prop_id,
3734 .obj_id = conn_set_prop->connector_id,
3735 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3736 };
Dave Airlief453ba02008-11-07 14:05:41 -08003737
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003738 /* It does all the locking and checking we need */
3739 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003740}
3741
Paulo Zanonic5431882012-05-15 18:09:02 -03003742static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3743 struct drm_property *property,
3744 uint64_t value)
3745{
3746 int ret = -EINVAL;
3747 struct drm_connector *connector = obj_to_connector(obj);
3748
3749 /* Do DPMS ourselves */
3750 if (property == connector->dev->mode_config.dpms_property) {
3751 if (connector->funcs->dpms)
3752 (*connector->funcs->dpms)(connector, (int)value);
3753 ret = 0;
3754 } else if (connector->funcs->set_property)
3755 ret = connector->funcs->set_property(connector, property, value);
3756
3757 /* store the property value if successful */
3758 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003759 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003760 return ret;
3761}
3762
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003763static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3764 struct drm_property *property,
3765 uint64_t value)
3766{
3767 int ret = -EINVAL;
3768 struct drm_crtc *crtc = obj_to_crtc(obj);
3769
3770 if (crtc->funcs->set_property)
3771 ret = crtc->funcs->set_property(crtc, property, value);
3772 if (!ret)
3773 drm_object_property_set_value(obj, property, value);
3774
3775 return ret;
3776}
3777
Rob Clark4d939142012-05-17 02:23:27 -06003778static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3779 struct drm_property *property,
3780 uint64_t value)
3781{
3782 int ret = -EINVAL;
3783 struct drm_plane *plane = obj_to_plane(obj);
3784
3785 if (plane->funcs->set_property)
3786 ret = plane->funcs->set_property(plane, property, value);
3787 if (!ret)
3788 drm_object_property_set_value(obj, property, value);
3789
3790 return ret;
3791}
3792
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003793/**
3794 * drm_mode_getproperty_ioctl - get the current value of a object's property
3795 * @dev: DRM device
3796 * @data: ioctl data
3797 * @file_priv: DRM file info
3798 *
3799 * This function retrieves the current value for an object's property. Compared
3800 * to the connector specific ioctl this one is extended to also work on crtc and
3801 * plane objects.
3802 *
3803 * Called by the user via ioctl.
3804 *
3805 * Returns:
3806 * Zero on success, errno on failure.
3807 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003808int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3809 struct drm_file *file_priv)
3810{
3811 struct drm_mode_obj_get_properties *arg = data;
3812 struct drm_mode_object *obj;
3813 int ret = 0;
3814 int i;
3815 int copied = 0;
3816 int props_count = 0;
3817 uint32_t __user *props_ptr;
3818 uint64_t __user *prop_values_ptr;
3819
3820 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3821 return -EINVAL;
3822
Daniel Vetter84849902012-12-02 00:28:11 +01003823 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003824
3825 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3826 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003827 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003828 goto out;
3829 }
3830 if (!obj->properties) {
3831 ret = -EINVAL;
3832 goto out;
3833 }
3834
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003835 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003836
3837 /* This ioctl is called twice, once to determine how much space is
3838 * needed, and the 2nd time to fill it. */
3839 if ((arg->count_props >= props_count) && props_count) {
3840 copied = 0;
3841 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3842 prop_values_ptr = (uint64_t __user *)(unsigned long)
3843 (arg->prop_values_ptr);
3844 for (i = 0; i < props_count; i++) {
3845 if (put_user(obj->properties->ids[i],
3846 props_ptr + copied)) {
3847 ret = -EFAULT;
3848 goto out;
3849 }
3850 if (put_user(obj->properties->values[i],
3851 prop_values_ptr + copied)) {
3852 ret = -EFAULT;
3853 goto out;
3854 }
3855 copied++;
3856 }
3857 }
3858 arg->count_props = props_count;
3859out:
Daniel Vetter84849902012-12-02 00:28:11 +01003860 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003861 return ret;
3862}
3863
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003864/**
3865 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3866 * @dev: DRM device
3867 * @data: ioctl data
3868 * @file_priv: DRM file info
3869 *
3870 * This function sets the current value for an object's property. It also calls
3871 * into a driver's ->set_property callback to update the hardware state.
3872 * Compared to the connector specific ioctl this one is extended to also work on
3873 * crtc and plane objects.
3874 *
3875 * Called by the user via ioctl.
3876 *
3877 * Returns:
3878 * Zero on success, errno on failure.
3879 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003880int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3881 struct drm_file *file_priv)
3882{
3883 struct drm_mode_obj_set_property *arg = data;
3884 struct drm_mode_object *arg_obj;
3885 struct drm_mode_object *prop_obj;
3886 struct drm_property *property;
3887 int ret = -EINVAL;
3888 int i;
3889
3890 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3891 return -EINVAL;
3892
Daniel Vetter84849902012-12-02 00:28:11 +01003893 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003894
3895 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003896 if (!arg_obj) {
3897 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003898 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003899 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003900 if (!arg_obj->properties)
3901 goto out;
3902
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003903 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003904 if (arg_obj->properties->ids[i] == arg->prop_id)
3905 break;
3906
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003907 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003908 goto out;
3909
3910 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3911 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003912 if (!prop_obj) {
3913 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003914 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003915 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003916 property = obj_to_property(prop_obj);
3917
3918 if (!drm_property_change_is_valid(property, arg->value))
3919 goto out;
3920
3921 switch (arg_obj->type) {
3922 case DRM_MODE_OBJECT_CONNECTOR:
3923 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3924 arg->value);
3925 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003926 case DRM_MODE_OBJECT_CRTC:
3927 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3928 break;
Rob Clark4d939142012-05-17 02:23:27 -06003929 case DRM_MODE_OBJECT_PLANE:
3930 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3931 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003932 }
3933
3934out:
Daniel Vetter84849902012-12-02 00:28:11 +01003935 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003936 return ret;
3937}
3938
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003939/**
3940 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3941 * @connector: connector to attach
3942 * @encoder: encoder to attach @connector to
3943 *
3944 * This function links up a connector to an encoder. Note that the routing
3945 * restrictions between encoders and crtcs are exposed to userspace through the
3946 * possible_clones and possible_crtcs bitmasks.
3947 *
3948 * Returns:
3949 * Zero on success, errno on failure.
3950 */
Dave Airlief453ba02008-11-07 14:05:41 -08003951int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3952 struct drm_encoder *encoder)
3953{
3954 int i;
3955
3956 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3957 if (connector->encoder_ids[i] == 0) {
3958 connector->encoder_ids[i] = encoder->base.id;
3959 return 0;
3960 }
3961 }
3962 return -ENOMEM;
3963}
3964EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3965
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003966/**
3967 * drm_mode_crtc_set_gamma_size - set the gamma table size
3968 * @crtc: CRTC to set the gamma table size for
3969 * @gamma_size: size of the gamma table
3970 *
3971 * Drivers which support gamma tables should set this to the supported gamma
3972 * table size when initializing the CRTC. Currently the drm core only supports a
3973 * fixed gamma table size.
3974 *
3975 * Returns:
3976 * Zero on success, errno on failure.
3977 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003978int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003979 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08003980{
3981 crtc->gamma_size = gamma_size;
3982
3983 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3984 if (!crtc->gamma_store) {
3985 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003986 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003987 }
3988
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003989 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003990}
3991EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3992
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003993/**
3994 * drm_mode_gamma_set_ioctl - set the gamma table
3995 * @dev: DRM device
3996 * @data: ioctl data
3997 * @file_priv: DRM file info
3998 *
3999 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4000 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4001 *
4002 * Called by the user via ioctl.
4003 *
4004 * Returns:
4005 * Zero on success, errno on failure.
4006 */
Dave Airlief453ba02008-11-07 14:05:41 -08004007int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4008 void *data, struct drm_file *file_priv)
4009{
4010 struct drm_mode_crtc_lut *crtc_lut = data;
4011 struct drm_mode_object *obj;
4012 struct drm_crtc *crtc;
4013 void *r_base, *g_base, *b_base;
4014 int size;
4015 int ret = 0;
4016
Dave Airliefb3b06c2011-02-08 13:55:21 +10004017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4018 return -EINVAL;
4019
Daniel Vetter84849902012-12-02 00:28:11 +01004020 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004021 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4022 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004023 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004024 goto out;
4025 }
4026 crtc = obj_to_crtc(obj);
4027
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004028 if (crtc->funcs->gamma_set == NULL) {
4029 ret = -ENOSYS;
4030 goto out;
4031 }
4032
Dave Airlief453ba02008-11-07 14:05:41 -08004033 /* memcpy into gamma store */
4034 if (crtc_lut->gamma_size != crtc->gamma_size) {
4035 ret = -EINVAL;
4036 goto out;
4037 }
4038
4039 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4040 r_base = crtc->gamma_store;
4041 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4042 ret = -EFAULT;
4043 goto out;
4044 }
4045
4046 g_base = r_base + size;
4047 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4048 ret = -EFAULT;
4049 goto out;
4050 }
4051
4052 b_base = g_base + size;
4053 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4054 ret = -EFAULT;
4055 goto out;
4056 }
4057
James Simmons72034252010-08-03 01:33:19 +01004058 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004059
4060out:
Daniel Vetter84849902012-12-02 00:28:11 +01004061 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004062 return ret;
4063
4064}
4065
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004066/**
4067 * drm_mode_gamma_get_ioctl - get the gamma table
4068 * @dev: DRM device
4069 * @data: ioctl data
4070 * @file_priv: DRM file info
4071 *
4072 * Copy the current gamma table into the storage provided. This also provides
4073 * the gamma table size the driver expects, which can be used to size the
4074 * allocated storage.
4075 *
4076 * Called by the user via ioctl.
4077 *
4078 * Returns:
4079 * Zero on success, errno on failure.
4080 */
Dave Airlief453ba02008-11-07 14:05:41 -08004081int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4082 void *data, struct drm_file *file_priv)
4083{
4084 struct drm_mode_crtc_lut *crtc_lut = data;
4085 struct drm_mode_object *obj;
4086 struct drm_crtc *crtc;
4087 void *r_base, *g_base, *b_base;
4088 int size;
4089 int ret = 0;
4090
Dave Airliefb3b06c2011-02-08 13:55:21 +10004091 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4092 return -EINVAL;
4093
Daniel Vetter84849902012-12-02 00:28:11 +01004094 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004095 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4096 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004097 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004098 goto out;
4099 }
4100 crtc = obj_to_crtc(obj);
4101
4102 /* memcpy into gamma store */
4103 if (crtc_lut->gamma_size != crtc->gamma_size) {
4104 ret = -EINVAL;
4105 goto out;
4106 }
4107
4108 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4109 r_base = crtc->gamma_store;
4110 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4111 ret = -EFAULT;
4112 goto out;
4113 }
4114
4115 g_base = r_base + size;
4116 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4117 ret = -EFAULT;
4118 goto out;
4119 }
4120
4121 b_base = g_base + size;
4122 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4123 ret = -EFAULT;
4124 goto out;
4125 }
4126out:
Daniel Vetter84849902012-12-02 00:28:11 +01004127 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004128 return ret;
4129}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004130
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004131/**
4132 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4133 * @dev: DRM device
4134 * @data: ioctl data
4135 * @file_priv: DRM file info
4136 *
4137 * This schedules an asynchronous update on a given CRTC, called page flip.
4138 * Optionally a drm event is generated to signal the completion of the event.
4139 * Generic drivers cannot assume that a pageflip with changed framebuffer
4140 * properties (including driver specific metadata like tiling layout) will work,
4141 * but some drivers support e.g. pixel format changes through the pageflip
4142 * ioctl.
4143 *
4144 * Called by the user via ioctl.
4145 *
4146 * Returns:
4147 * Zero on success, errno on failure.
4148 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004149int drm_mode_page_flip_ioctl(struct drm_device *dev,
4150 void *data, struct drm_file *file_priv)
4151{
4152 struct drm_mode_crtc_page_flip *page_flip = data;
4153 struct drm_mode_object *obj;
4154 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004155 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004156 struct drm_pending_vblank_event *e = NULL;
4157 unsigned long flags;
4158 int ret = -EINVAL;
4159
4160 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4161 page_flip->reserved != 0)
4162 return -EINVAL;
4163
Keith Packard62f21042013-07-22 18:50:00 -07004164 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4165 return -EINVAL;
4166
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004167 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4168 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004169 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004170 crtc = obj_to_crtc(obj);
4171
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004172 mutex_lock(&crtc->mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004173 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004174 /* The framebuffer is currently unbound, presumably
4175 * due to a hotplug event, that userspace has not
4176 * yet discovered.
4177 */
4178 ret = -EBUSY;
4179 goto out;
4180 }
4181
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004182 if (crtc->funcs->page_flip == NULL)
4183 goto out;
4184
Daniel Vetter786b99e2012-12-02 21:53:40 +01004185 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004186 if (!fb) {
4187 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004188 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004189 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004190
Damien Lespiauc11e9282013-09-25 16:45:30 +01004191 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4192 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004193 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004194
Matt Roperf4510a22014-04-01 15:22:40 -07004195 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004196 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4197 ret = -EINVAL;
4198 goto out;
4199 }
4200
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004201 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4202 ret = -ENOMEM;
4203 spin_lock_irqsave(&dev->event_lock, flags);
4204 if (file_priv->event_space < sizeof e->event) {
4205 spin_unlock_irqrestore(&dev->event_lock, flags);
4206 goto out;
4207 }
4208 file_priv->event_space -= sizeof e->event;
4209 spin_unlock_irqrestore(&dev->event_lock, flags);
4210
4211 e = kzalloc(sizeof *e, GFP_KERNEL);
4212 if (e == NULL) {
4213 spin_lock_irqsave(&dev->event_lock, flags);
4214 file_priv->event_space += sizeof e->event;
4215 spin_unlock_irqrestore(&dev->event_lock, flags);
4216 goto out;
4217 }
4218
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004219 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004220 e->event.base.length = sizeof e->event;
4221 e->event.user_data = page_flip->user_data;
4222 e->base.event = &e->event.base;
4223 e->base.file_priv = file_priv;
4224 e->base.destroy =
4225 (void (*) (struct drm_pending_event *)) kfree;
4226 }
4227
Matt Roperf4510a22014-04-01 15:22:40 -07004228 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004229 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004230 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004231 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4232 spin_lock_irqsave(&dev->event_lock, flags);
4233 file_priv->event_space += sizeof e->event;
4234 spin_unlock_irqrestore(&dev->event_lock, flags);
4235 kfree(e);
4236 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004237 /* Keep the old fb, don't unref it. */
4238 old_fb = NULL;
4239 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004240 /*
4241 * Warn if the driver hasn't properly updated the crtc->fb
4242 * field to reflect that the new framebuffer is now used.
4243 * Failing to do so will screw with the reference counting
4244 * on framebuffers.
4245 */
Matt Roperf4510a22014-04-01 15:22:40 -07004246 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004247 /* Unref only the old framebuffer. */
4248 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004249 }
4250
4251out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004252 if (fb)
4253 drm_framebuffer_unreference(fb);
4254 if (old_fb)
4255 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004256 mutex_unlock(&crtc->mutex);
4257
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004258 return ret;
4259}
Chris Wilsoneb033552011-01-24 15:11:08 +00004260
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004261/**
4262 * drm_mode_config_reset - call ->reset callbacks
4263 * @dev: drm device
4264 *
4265 * This functions calls all the crtc's, encoder's and connector's ->reset
4266 * callback. Drivers can use this in e.g. their driver load or resume code to
4267 * reset hardware and software state.
4268 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004269void drm_mode_config_reset(struct drm_device *dev)
4270{
4271 struct drm_crtc *crtc;
4272 struct drm_encoder *encoder;
4273 struct drm_connector *connector;
4274
4275 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4276 if (crtc->funcs->reset)
4277 crtc->funcs->reset(crtc);
4278
4279 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4280 if (encoder->funcs->reset)
4281 encoder->funcs->reset(encoder);
4282
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004283 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4284 connector->status = connector_status_unknown;
4285
Chris Wilsoneb033552011-01-24 15:11:08 +00004286 if (connector->funcs->reset)
4287 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004288 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004289}
4290EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004291
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004292/**
4293 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4294 * @dev: DRM device
4295 * @data: ioctl data
4296 * @file_priv: DRM file info
4297 *
4298 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4299 * TTM or something else entirely) and returns the resulting buffer handle. This
4300 * handle can then be wrapped up into a framebuffer modeset object.
4301 *
4302 * Note that userspace is not allowed to use such objects for render
4303 * acceleration - drivers must create their own private ioctls for such a use
4304 * case.
4305 *
4306 * Called by the user via ioctl.
4307 *
4308 * Returns:
4309 * Zero on success, errno on failure.
4310 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004311int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4312 void *data, struct drm_file *file_priv)
4313{
4314 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004315 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004316
4317 if (!dev->driver->dumb_create)
4318 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004319 if (!args->width || !args->height || !args->bpp)
4320 return -EINVAL;
4321
4322 /* overflow checks for 32bit size calculations */
4323 cpp = DIV_ROUND_UP(args->bpp, 8);
4324 if (cpp > 0xffffffffU / args->width)
4325 return -EINVAL;
4326 stride = cpp * args->width;
4327 if (args->height > 0xffffffffU / stride)
4328 return -EINVAL;
4329
4330 /* test for wrap-around */
4331 size = args->height * stride;
4332 if (PAGE_ALIGN(size) == 0)
4333 return -EINVAL;
4334
Dave Airlieff72145b2011-02-07 12:16:14 +10004335 return dev->driver->dumb_create(file_priv, dev, args);
4336}
4337
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004338/**
4339 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4340 * @dev: DRM device
4341 * @data: ioctl data
4342 * @file_priv: DRM file info
4343 *
4344 * Allocate an offset in the drm device node's address space to be able to
4345 * memory map a dumb buffer.
4346 *
4347 * Called by the user via ioctl.
4348 *
4349 * Returns:
4350 * Zero on success, errno on failure.
4351 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004352int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4353 void *data, struct drm_file *file_priv)
4354{
4355 struct drm_mode_map_dumb *args = data;
4356
4357 /* call driver ioctl to get mmap offset */
4358 if (!dev->driver->dumb_map_offset)
4359 return -ENOSYS;
4360
4361 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4362}
4363
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004364/**
4365 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4366 * @dev: DRM device
4367 * @data: ioctl data
4368 * @file_priv: DRM file info
4369 *
4370 * This destroys the userspace handle for the given dumb backing storage buffer.
4371 * Since buffer objects must be reference counted in the kernel a buffer object
4372 * won't be immediately freed if a framebuffer modeset object still uses it.
4373 *
4374 * Called by the user via ioctl.
4375 *
4376 * Returns:
4377 * Zero on success, errno on failure.
4378 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004379int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4380 void *data, struct drm_file *file_priv)
4381{
4382 struct drm_mode_destroy_dumb *args = data;
4383
4384 if (!dev->driver->dumb_destroy)
4385 return -ENOSYS;
4386
4387 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4388}
Dave Airlie248dbc22011-11-29 20:02:54 +00004389
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004390/**
4391 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4392 * @format: pixel format (DRM_FORMAT_*)
4393 * @depth: storage for the depth value
4394 * @bpp: storage for the bpp value
4395 *
4396 * This only supports RGB formats here for compat with code that doesn't use
4397 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004398 */
4399void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4400 int *bpp)
4401{
4402 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004403 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004404 case DRM_FORMAT_RGB332:
4405 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004406 *depth = 8;
4407 *bpp = 8;
4408 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004409 case DRM_FORMAT_XRGB1555:
4410 case DRM_FORMAT_XBGR1555:
4411 case DRM_FORMAT_RGBX5551:
4412 case DRM_FORMAT_BGRX5551:
4413 case DRM_FORMAT_ARGB1555:
4414 case DRM_FORMAT_ABGR1555:
4415 case DRM_FORMAT_RGBA5551:
4416 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004417 *depth = 15;
4418 *bpp = 16;
4419 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004420 case DRM_FORMAT_RGB565:
4421 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004422 *depth = 16;
4423 *bpp = 16;
4424 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004425 case DRM_FORMAT_RGB888:
4426 case DRM_FORMAT_BGR888:
4427 *depth = 24;
4428 *bpp = 24;
4429 break;
4430 case DRM_FORMAT_XRGB8888:
4431 case DRM_FORMAT_XBGR8888:
4432 case DRM_FORMAT_RGBX8888:
4433 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004434 *depth = 24;
4435 *bpp = 32;
4436 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004437 case DRM_FORMAT_XRGB2101010:
4438 case DRM_FORMAT_XBGR2101010:
4439 case DRM_FORMAT_RGBX1010102:
4440 case DRM_FORMAT_BGRX1010102:
4441 case DRM_FORMAT_ARGB2101010:
4442 case DRM_FORMAT_ABGR2101010:
4443 case DRM_FORMAT_RGBA1010102:
4444 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004445 *depth = 30;
4446 *bpp = 32;
4447 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004448 case DRM_FORMAT_ARGB8888:
4449 case DRM_FORMAT_ABGR8888:
4450 case DRM_FORMAT_RGBA8888:
4451 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004452 *depth = 32;
4453 *bpp = 32;
4454 break;
4455 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004456 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4457 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004458 *depth = 0;
4459 *bpp = 0;
4460 break;
4461 }
4462}
4463EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004464
4465/**
4466 * drm_format_num_planes - get the number of planes for format
4467 * @format: pixel format (DRM_FORMAT_*)
4468 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004469 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004470 * The number of planes used by the specified pixel format.
4471 */
4472int drm_format_num_planes(uint32_t format)
4473{
4474 switch (format) {
4475 case DRM_FORMAT_YUV410:
4476 case DRM_FORMAT_YVU410:
4477 case DRM_FORMAT_YUV411:
4478 case DRM_FORMAT_YVU411:
4479 case DRM_FORMAT_YUV420:
4480 case DRM_FORMAT_YVU420:
4481 case DRM_FORMAT_YUV422:
4482 case DRM_FORMAT_YVU422:
4483 case DRM_FORMAT_YUV444:
4484 case DRM_FORMAT_YVU444:
4485 return 3;
4486 case DRM_FORMAT_NV12:
4487 case DRM_FORMAT_NV21:
4488 case DRM_FORMAT_NV16:
4489 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004490 case DRM_FORMAT_NV24:
4491 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004492 return 2;
4493 default:
4494 return 1;
4495 }
4496}
4497EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004498
4499/**
4500 * drm_format_plane_cpp - determine the bytes per pixel value
4501 * @format: pixel format (DRM_FORMAT_*)
4502 * @plane: plane index
4503 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004504 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004505 * The bytes per pixel value for the specified plane.
4506 */
4507int drm_format_plane_cpp(uint32_t format, int plane)
4508{
4509 unsigned int depth;
4510 int bpp;
4511
4512 if (plane >= drm_format_num_planes(format))
4513 return 0;
4514
4515 switch (format) {
4516 case DRM_FORMAT_YUYV:
4517 case DRM_FORMAT_YVYU:
4518 case DRM_FORMAT_UYVY:
4519 case DRM_FORMAT_VYUY:
4520 return 2;
4521 case DRM_FORMAT_NV12:
4522 case DRM_FORMAT_NV21:
4523 case DRM_FORMAT_NV16:
4524 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004525 case DRM_FORMAT_NV24:
4526 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004527 return plane ? 2 : 1;
4528 case DRM_FORMAT_YUV410:
4529 case DRM_FORMAT_YVU410:
4530 case DRM_FORMAT_YUV411:
4531 case DRM_FORMAT_YVU411:
4532 case DRM_FORMAT_YUV420:
4533 case DRM_FORMAT_YVU420:
4534 case DRM_FORMAT_YUV422:
4535 case DRM_FORMAT_YVU422:
4536 case DRM_FORMAT_YUV444:
4537 case DRM_FORMAT_YVU444:
4538 return 1;
4539 default:
4540 drm_fb_get_bpp_depth(format, &depth, &bpp);
4541 return bpp >> 3;
4542 }
4543}
4544EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004545
4546/**
4547 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4548 * @format: pixel format (DRM_FORMAT_*)
4549 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004550 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004551 * The horizontal chroma subsampling factor for the
4552 * specified pixel format.
4553 */
4554int drm_format_horz_chroma_subsampling(uint32_t format)
4555{
4556 switch (format) {
4557 case DRM_FORMAT_YUV411:
4558 case DRM_FORMAT_YVU411:
4559 case DRM_FORMAT_YUV410:
4560 case DRM_FORMAT_YVU410:
4561 return 4;
4562 case DRM_FORMAT_YUYV:
4563 case DRM_FORMAT_YVYU:
4564 case DRM_FORMAT_UYVY:
4565 case DRM_FORMAT_VYUY:
4566 case DRM_FORMAT_NV12:
4567 case DRM_FORMAT_NV21:
4568 case DRM_FORMAT_NV16:
4569 case DRM_FORMAT_NV61:
4570 case DRM_FORMAT_YUV422:
4571 case DRM_FORMAT_YVU422:
4572 case DRM_FORMAT_YUV420:
4573 case DRM_FORMAT_YVU420:
4574 return 2;
4575 default:
4576 return 1;
4577 }
4578}
4579EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4580
4581/**
4582 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4583 * @format: pixel format (DRM_FORMAT_*)
4584 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004585 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004586 * The vertical chroma subsampling factor for the
4587 * specified pixel format.
4588 */
4589int drm_format_vert_chroma_subsampling(uint32_t format)
4590{
4591 switch (format) {
4592 case DRM_FORMAT_YUV410:
4593 case DRM_FORMAT_YVU410:
4594 return 4;
4595 case DRM_FORMAT_YUV420:
4596 case DRM_FORMAT_YVU420:
4597 case DRM_FORMAT_NV12:
4598 case DRM_FORMAT_NV21:
4599 return 2;
4600 default:
4601 return 1;
4602 }
4603}
4604EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004605
4606/**
4607 * drm_mode_config_init - initialize DRM mode_configuration structure
4608 * @dev: DRM device
4609 *
4610 * Initialize @dev's mode_config structure, used for tracking the graphics
4611 * configuration of @dev.
4612 *
4613 * Since this initializes the modeset locks, no locking is possible. Which is no
4614 * problem, since this should happen single threaded at init time. It is the
4615 * driver's problem to ensure this guarantee.
4616 *
4617 */
4618void drm_mode_config_init(struct drm_device *dev)
4619{
4620 mutex_init(&dev->mode_config.mutex);
4621 mutex_init(&dev->mode_config.idr_mutex);
4622 mutex_init(&dev->mode_config.fb_lock);
4623 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4624 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4625 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004626 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004627 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4628 INIT_LIST_HEAD(&dev->mode_config.property_list);
4629 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4630 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4631 idr_init(&dev->mode_config.crtc_idr);
4632
4633 drm_modeset_lock_all(dev);
4634 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004635 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004636 drm_modeset_unlock_all(dev);
4637
4638 /* Just to be sure */
4639 dev->mode_config.num_fb = 0;
4640 dev->mode_config.num_connector = 0;
4641 dev->mode_config.num_crtc = 0;
4642 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004643 dev->mode_config.num_overlay_plane = 0;
4644 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004645}
4646EXPORT_SYMBOL(drm_mode_config_init);
4647
4648/**
4649 * drm_mode_config_cleanup - free up DRM mode_config info
4650 * @dev: DRM device
4651 *
4652 * Free up all the connectors and CRTCs associated with this DRM device, then
4653 * free up the framebuffers and associated buffer objects.
4654 *
4655 * Note that since this /should/ happen single-threaded at driver/device
4656 * teardown time, no locking is required. It's the driver's job to ensure that
4657 * this guarantee actually holds true.
4658 *
4659 * FIXME: cleanup any dangling user buffer objects too
4660 */
4661void drm_mode_config_cleanup(struct drm_device *dev)
4662{
4663 struct drm_connector *connector, *ot;
4664 struct drm_crtc *crtc, *ct;
4665 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004666 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004667 struct drm_framebuffer *fb, *fbt;
4668 struct drm_property *property, *pt;
4669 struct drm_property_blob *blob, *bt;
4670 struct drm_plane *plane, *plt;
4671
4672 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4673 head) {
4674 encoder->funcs->destroy(encoder);
4675 }
4676
Sean Paul3b336ec2013-08-14 16:47:37 -04004677 list_for_each_entry_safe(bridge, brt,
4678 &dev->mode_config.bridge_list, head) {
4679 bridge->funcs->destroy(bridge);
4680 }
4681
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004682 list_for_each_entry_safe(connector, ot,
4683 &dev->mode_config.connector_list, head) {
4684 connector->funcs->destroy(connector);
4685 }
4686
4687 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4688 head) {
4689 drm_property_destroy(dev, property);
4690 }
4691
4692 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4693 head) {
4694 drm_property_destroy_blob(dev, blob);
4695 }
4696
4697 /*
4698 * Single-threaded teardown context, so it's not required to grab the
4699 * fb_lock to protect against concurrent fb_list access. Contrary, it
4700 * would actually deadlock with the drm_framebuffer_cleanup function.
4701 *
4702 * Also, if there are any framebuffers left, that's a driver leak now,
4703 * so politely WARN about this.
4704 */
4705 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4706 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4707 drm_framebuffer_remove(fb);
4708 }
4709
4710 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4711 head) {
4712 plane->funcs->destroy(plane);
4713 }
4714
4715 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4716 crtc->funcs->destroy(crtc);
4717 }
4718
4719 idr_destroy(&dev->mode_config.crtc_idr);
4720}
4721EXPORT_SYMBOL(drm_mode_config_cleanup);