blob: 64ef120795289051e9c2ab4f71e1239127e931dc [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 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_fourcc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080039
Daniel Vetter84849902012-12-02 00:28:11 +010040/**
41 * drm_modeset_lock_all - take all modeset locks
42 * @dev: drm device
43 *
44 * This function takes all modeset locks, suitable where a more fine-grained
45 * scheme isn't (yet) implemented.
46 */
47void drm_modeset_lock_all(struct drm_device *dev)
48{
Daniel Vetter29494c12012-12-02 02:18:25 +010049 struct drm_crtc *crtc;
50
Daniel Vetter84849902012-12-02 00:28:11 +010051 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter29494c12012-12-02 02:18:25 +010052
53 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
54 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Daniel Vetter84849902012-12-02 00:28:11 +010055}
56EXPORT_SYMBOL(drm_modeset_lock_all);
57
58/**
59 * drm_modeset_unlock_all - drop all modeset locks
60 * @dev: device
61 */
62void drm_modeset_unlock_all(struct drm_device *dev)
63{
Daniel Vetter29494c12012-12-02 02:18:25 +010064 struct drm_crtc *crtc;
65
66 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
67 mutex_unlock(&crtc->mutex);
68
Daniel Vetter84849902012-12-02 00:28:11 +010069 mutex_unlock(&dev->mode_config.mutex);
70}
Daniel Vetter36206362012-12-10 20:42:17 +010071
Daniel Vetter84849902012-12-02 00:28:11 +010072EXPORT_SYMBOL(drm_modeset_unlock_all);
73
Dave Airlief453ba02008-11-07 14:05:41 -080074/* Avoid boilerplate. I'm tired of typing. */
75#define DRM_ENUM_NAME_FN(fnname, list) \
76 char *fnname(int val) \
77 { \
78 int i; \
79 for (i = 0; i < ARRAY_SIZE(list); i++) { \
80 if (list[i].type == val) \
81 return list[i].name; \
82 } \
83 return "(unknown)"; \
84 }
85
86/*
87 * Global properties
88 */
89static struct drm_prop_enum_list drm_dpms_enum_list[] =
90{ { DRM_MODE_DPMS_ON, "On" },
91 { DRM_MODE_DPMS_STANDBY, "Standby" },
92 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
93 { DRM_MODE_DPMS_OFF, "Off" }
94};
95
96DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
97
98/*
99 * Optional properties
100 */
101static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
102{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700103 { DRM_MODE_SCALE_NONE, "None" },
104 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
105 { DRM_MODE_SCALE_CENTER, "Center" },
106 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800107};
108
109static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
110{
111 { DRM_MODE_DITHERING_OFF, "Off" },
112 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000113 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800114};
115
116/*
117 * Non-global properties, but "required" for certain connectors.
118 */
119static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
120{
121 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
122 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
123 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
124};
125
126DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
127
128static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
129{
130 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
131 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
132 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
133};
134
135DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
136 drm_dvi_i_subconnector_enum_list)
137
138static struct drm_prop_enum_list drm_tv_select_enum_list[] =
139{
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
142 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
143 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200144 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800145};
146
147DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
148
149static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
150{
151 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
152 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
153 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
154 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200155 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800156};
157
158DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
159 drm_tv_subconnector_enum_list)
160
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000161static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
162 { DRM_MODE_DIRTY_OFF, "Off" },
163 { DRM_MODE_DIRTY_ON, "On" },
164 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
165};
166
167DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
168 drm_dirty_info_enum_list)
169
Dave Airlief453ba02008-11-07 14:05:41 -0800170struct drm_conn_prop_enum_list {
171 int type;
172 char *name;
173 int count;
174};
175
176/*
177 * Connector and encoder types.
178 */
179static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
180{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
181 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
182 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
183 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
184 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
185 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
186 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
187 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
188 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500189 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
190 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
191 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
192 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200193 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500194 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200195 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800196};
197
198static struct drm_prop_enum_list drm_encoder_enum_list[] =
199{ { DRM_MODE_ENCODER_NONE, "None" },
200 { DRM_MODE_ENCODER_DAC, "DAC" },
201 { DRM_MODE_ENCODER_TMDS, "TMDS" },
202 { DRM_MODE_ENCODER_LVDS, "LVDS" },
203 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200204 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800205};
206
207char *drm_get_encoder_name(struct drm_encoder *encoder)
208{
209 static char buf[32];
210
211 snprintf(buf, 32, "%s-%d",
212 drm_encoder_enum_list[encoder->encoder_type].name,
213 encoder->base.id);
214 return buf;
215}
Dave Airlie13a81952009-09-07 15:45:33 +1000216EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800217
218char *drm_get_connector_name(struct drm_connector *connector)
219{
220 static char buf[32];
221
222 snprintf(buf, 32, "%s-%d",
223 drm_connector_enum_list[connector->connector_type].name,
224 connector->connector_type_id);
225 return buf;
226}
227EXPORT_SYMBOL(drm_get_connector_name);
228
229char *drm_get_connector_status_name(enum drm_connector_status status)
230{
231 if (status == connector_status_connected)
232 return "connected";
233 else if (status == connector_status_disconnected)
234 return "disconnected";
235 else
236 return "unknown";
237}
238
239/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100240 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800241 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100242 * @obj: object pointer, used to generate unique ID
243 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800244 *
Dave Airlief453ba02008-11-07 14:05:41 -0800245 * Create a unique identifier based on @ptr in @dev's identifier space. Used
246 * for tracking modes, CRTCs and connectors.
247 *
248 * RETURNS:
249 * New unique (relative to other objects in @dev) integer identifier for the
250 * object.
251 */
252static int drm_mode_object_get(struct drm_device *dev,
253 struct drm_mode_object *obj, uint32_t obj_type)
254{
255 int new_id = 0;
256 int ret;
257
Dave Airlief453ba02008-11-07 14:05:41 -0800258again:
259 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
260 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200261 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800262 }
263
Jesse Barnesad2563c2009-01-19 17:21:45 +1000264 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800265 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100266
267 if (!ret) {
268 /*
269 * Set up the object linking under the protection of the idr
270 * lock so that other users can't see inconsistent state.
271 */
272 obj->id = new_id;
273 obj->type = obj_type;
274 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000275 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100276
Dave Airlief453ba02008-11-07 14:05:41 -0800277 if (ret == -EAGAIN)
278 goto again;
279
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100280 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800281}
282
283/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100284 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800285 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100286 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800287 *
Dave Airlief453ba02008-11-07 14:05:41 -0800288 * Free @id from @dev's unique identifier pool.
289 */
290static void drm_mode_object_put(struct drm_device *dev,
291 struct drm_mode_object *object)
292{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000293 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800294 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000295 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800296}
297
Daniel Vetter786b99e2012-12-02 21:53:40 +0100298/**
299 * drm_mode_object_find - look up a drm object with static lifetime
300 * @dev: drm device
301 * @id: id of the mode object
302 * @type: type of the mode object
303 *
304 * Note that framebuffers cannot be looked up with this functions - since those
305 * are reference counted, they need special treatment.
306 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200307struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
308 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800309{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000310 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800311
Daniel Vetter786b99e2012-12-02 21:53:40 +0100312 /* Framebuffers are reference counted and need their own lookup
313 * function.*/
314 WARN_ON(type == DRM_MODE_OBJECT_FB);
315
Jesse Barnesad2563c2009-01-19 17:21:45 +1000316 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800317 obj = idr_find(&dev->mode_config.crtc_idr, id);
318 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000319 obj = NULL;
320 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800321
322 return obj;
323}
324EXPORT_SYMBOL(drm_mode_object_find);
325
326/**
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * drm_framebuffer_init - initialize a framebuffer
328 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100329 * @fb: framebuffer to be initialized
330 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800331 *
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * Allocates an ID for the framebuffer's parent mode object, sets its mode
333 * functions & device file and adds it to the master fd list.
334 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100335 * IMPORTANT:
336 * This functions publishes the fb and makes it available for concurrent access
337 * by other users. Which means by this point the fb _must_ be fully set up -
338 * since all the fb attributes are invariant over its lifetime, no further
339 * locking but only correct reference counting is required.
340 *
Dave Airlief453ba02008-11-07 14:05:41 -0800341 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200342 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800343 */
344int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
345 const struct drm_framebuffer_funcs *funcs)
346{
347 int ret;
348
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100349 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000350 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100351 INIT_LIST_HEAD(&fb->filp_head);
352 fb->dev = dev;
353 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000354
Dave Airlief453ba02008-11-07 14:05:41 -0800355 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200356 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100357 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800358
Daniel Vetter2b677e82012-12-10 21:16:05 +0100359 /* Grab the idr reference. */
360 drm_framebuffer_reference(fb);
361
Dave Airlief453ba02008-11-07 14:05:41 -0800362 dev->mode_config.num_fb++;
363 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100364out:
365 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800366
367 return 0;
368}
369EXPORT_SYMBOL(drm_framebuffer_init);
370
Rob Clarkf7eff602012-09-05 21:48:38 +0000371static void drm_framebuffer_free(struct kref *kref)
372{
373 struct drm_framebuffer *fb =
374 container_of(kref, struct drm_framebuffer, refcount);
375 fb->funcs->destroy(fb);
376}
377
Daniel Vetter2b677e82012-12-10 21:16:05 +0100378static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
379 uint32_t id)
380{
381 struct drm_mode_object *obj = NULL;
382 struct drm_framebuffer *fb;
383
384 mutex_lock(&dev->mode_config.idr_mutex);
385 obj = idr_find(&dev->mode_config.crtc_idr, id);
386 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
387 fb = NULL;
388 else
389 fb = obj_to_fb(obj);
390 mutex_unlock(&dev->mode_config.idr_mutex);
391
392 return fb;
393}
394
Rob Clarkf7eff602012-09-05 21:48:38 +0000395/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100396 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
397 * @dev: drm device
398 * @id: id of the fb object
399 *
400 * If successful, this grabs an additional reference to the framebuffer -
401 * callers need to make sure to eventually unreference the returned framebuffer
402 * again.
403 */
404struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
405 uint32_t id)
406{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100407 struct drm_framebuffer *fb;
408
409 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100410 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100411 if (fb)
412 kref_get(&fb->refcount);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100413 mutex_unlock(&dev->mode_config.fb_lock);
414
415 return fb;
416}
417EXPORT_SYMBOL(drm_framebuffer_lookup);
418
419/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000420 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100421 * @fb: framebuffer to unref
422 *
423 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000424 */
425void drm_framebuffer_unreference(struct drm_framebuffer *fb)
426{
Rob Clarkf7eff602012-09-05 21:48:38 +0000427 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000428 kref_put(&fb->refcount, drm_framebuffer_free);
429}
430EXPORT_SYMBOL(drm_framebuffer_unreference);
431
432/**
433 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100434 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000435 */
436void drm_framebuffer_reference(struct drm_framebuffer *fb)
437{
438 DRM_DEBUG("FB ID: %d\n", fb->base.id);
439 kref_get(&fb->refcount);
440}
441EXPORT_SYMBOL(drm_framebuffer_reference);
442
Daniel Vetter2b677e82012-12-10 21:16:05 +0100443static void drm_framebuffer_free_bug(struct kref *kref)
444{
445 BUG();
446}
447
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100448static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
449{
450 DRM_DEBUG("FB ID: %d\n", fb->base.id);
451 kref_put(&fb->refcount, drm_framebuffer_free_bug);
452}
453
Daniel Vetter2b677e82012-12-10 21:16:05 +0100454/* dev->mode_config.fb_lock must be held! */
455static void __drm_framebuffer_unregister(struct drm_device *dev,
456 struct drm_framebuffer *fb)
457{
458 mutex_lock(&dev->mode_config.idr_mutex);
459 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
460 mutex_unlock(&dev->mode_config.idr_mutex);
461
462 fb->base.id = 0;
463
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100464 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100465}
466
Dave Airlief453ba02008-11-07 14:05:41 -0800467/**
Daniel Vetter36206362012-12-10 20:42:17 +0100468 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
469 * @fb: fb to unregister
470 *
471 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
472 * those used for fbdev. Note that the caller must hold a reference of it's own,
473 * i.e. the object may not be destroyed through this call (since it'll lead to a
474 * locking inversion).
475 */
476void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
477{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100478 struct drm_device *dev = fb->dev;
479
480 mutex_lock(&dev->mode_config.fb_lock);
481 /* Mark fb as reaped and drop idr ref. */
482 __drm_framebuffer_unregister(dev, fb);
483 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100484}
485EXPORT_SYMBOL(drm_framebuffer_unregister_private);
486
487/**
Dave Airlief453ba02008-11-07 14:05:41 -0800488 * drm_framebuffer_cleanup - remove a framebuffer object
489 * @fb: framebuffer to remove
490 *
Daniel Vetter36206362012-12-10 20:42:17 +0100491 * Cleanup references to a user-created framebuffer. This function is intended
492 * to be used from the drivers ->destroy callback.
493 *
494 * Note that this function does not remove the fb from active usuage - if it is
495 * still used anywhere, hilarity can ensue since userspace could call getfb on
496 * the id and get back -EINVAL. Obviously no concern at driver unload time.
497 *
498 * Also, the framebuffer will not be removed from the lookup idr - for
499 * user-created framebuffers this will happen in in the rmfb ioctl. For
500 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
501 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800502 */
503void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
504{
505 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100506
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100507 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000508 list_del(&fb->head);
509 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100510 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000511}
512EXPORT_SYMBOL(drm_framebuffer_cleanup);
513
514/**
515 * drm_framebuffer_remove - remove and unreference a framebuffer object
516 * @fb: framebuffer to remove
517 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000518 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100519 * using @fb, removes it, setting it to NULL. Then drops the reference to the
520 * passed-in framebuffer.
Rob Clarkf7eff602012-09-05 21:48:38 +0000521 */
522void drm_framebuffer_remove(struct drm_framebuffer *fb)
523{
524 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800525 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800526 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000527 struct drm_mode_set set;
528 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800529
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100530 WARN_ON(!drm_modeset_is_locked(dev));
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100531 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100532
Dave Airlief453ba02008-11-07 14:05:41 -0800533 /* remove from any CRTC */
534 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000535 if (crtc->fb == fb) {
536 /* should turn off the crtc */
537 memset(&set, 0, sizeof(struct drm_mode_set));
538 set.crtc = crtc;
539 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100540 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000541 if (ret)
542 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
543 }
Dave Airlief453ba02008-11-07 14:05:41 -0800544 }
545
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800546 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
547 if (plane->fb == fb) {
548 /* should turn off the crtc */
549 ret = plane->funcs->disable_plane(plane);
550 if (ret)
551 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600552 /* disconnect the plane from the fb and crtc: */
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100553 __drm_framebuffer_unreference(plane->fb);
Rob Clarka9971152011-12-13 20:19:35 -0600554 plane->fb = NULL;
555 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800556 }
557 }
558
Rob Clarkf7eff602012-09-05 21:48:38 +0000559 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800560}
Rob Clarkf7eff602012-09-05 21:48:38 +0000561EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800562
563/**
564 * drm_crtc_init - Initialise a new CRTC object
565 * @dev: DRM device
566 * @crtc: CRTC object to init
567 * @funcs: callbacks for the new CRTC
568 *
Dave Airlief453ba02008-11-07 14:05:41 -0800569 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200570 *
571 * RETURNS:
572 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800573 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200574int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800575 const struct drm_crtc_funcs *funcs)
576{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200577 int ret;
578
Dave Airlief453ba02008-11-07 14:05:41 -0800579 crtc->dev = dev;
580 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000581 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800582
Daniel Vetter84849902012-12-02 00:28:11 +0100583 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100584 mutex_init(&crtc->mutex);
585 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200586
587 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
588 if (ret)
589 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800590
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300591 crtc->base.properties = &crtc->properties;
592
Dave Airlief453ba02008-11-07 14:05:41 -0800593 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
594 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200595
596 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100597 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200598
599 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800600}
601EXPORT_SYMBOL(drm_crtc_init);
602
603/**
604 * drm_crtc_cleanup - Cleans up the core crtc usage.
605 * @crtc: CRTC to cleanup
606 *
Dave Airlief453ba02008-11-07 14:05:41 -0800607 * Cleanup @crtc. Removes from drm modesetting space
608 * does NOT free object, caller does that.
609 */
610void drm_crtc_cleanup(struct drm_crtc *crtc)
611{
612 struct drm_device *dev = crtc->dev;
613
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000614 kfree(crtc->gamma_store);
615 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800616
617 drm_mode_object_put(dev, &crtc->base);
618 list_del(&crtc->head);
619 dev->mode_config.num_crtc--;
620}
621EXPORT_SYMBOL(drm_crtc_cleanup);
622
623/**
624 * drm_mode_probed_add - add a mode to a connector's probed mode list
625 * @connector: connector the new mode
626 * @mode: mode data
627 *
Dave Airlief453ba02008-11-07 14:05:41 -0800628 * Add @mode to @connector's mode list for later use.
629 */
630void drm_mode_probed_add(struct drm_connector *connector,
631 struct drm_display_mode *mode)
632{
633 list_add(&mode->head, &connector->probed_modes);
634}
635EXPORT_SYMBOL(drm_mode_probed_add);
636
637/**
638 * drm_mode_remove - remove and free a mode
639 * @connector: connector list to modify
640 * @mode: mode to remove
641 *
Dave Airlief453ba02008-11-07 14:05:41 -0800642 * Remove @mode from @connector's mode list, then free it.
643 */
644void drm_mode_remove(struct drm_connector *connector,
645 struct drm_display_mode *mode)
646{
647 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100648 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800649}
650EXPORT_SYMBOL(drm_mode_remove);
651
652/**
653 * drm_connector_init - Init a preallocated connector
654 * @dev: DRM device
655 * @connector: the connector to init
656 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100657 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800658 *
Dave Airlief453ba02008-11-07 14:05:41 -0800659 * Initialises a preallocated connector. Connectors should be
660 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200661 *
662 * RETURNS:
663 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800664 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200665int drm_connector_init(struct drm_device *dev,
666 struct drm_connector *connector,
667 const struct drm_connector_funcs *funcs,
668 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800669{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670 int ret;
671
Daniel Vetter84849902012-12-02 00:28:11 +0100672 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800673
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200674 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
675 if (ret)
676 goto out;
677
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300678 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800679 connector->dev = dev;
680 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800681 connector->connector_type = connector_type;
682 connector->connector_type_id =
683 ++drm_connector_enum_list[connector_type].count; /* TODO */
684 INIT_LIST_HEAD(&connector->user_modes);
685 INIT_LIST_HEAD(&connector->probed_modes);
686 INIT_LIST_HEAD(&connector->modes);
687 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000688 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800689
690 list_add_tail(&connector->head, &dev->mode_config.connector_list);
691 dev->mode_config.num_connector++;
692
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200693 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500694 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200695 dev->mode_config.edid_property,
696 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800697
Rob Clark58495562012-10-11 20:50:56 -0500698 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800699 dev->mode_config.dpms_property, 0);
700
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200701 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100702 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703
704 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800705}
706EXPORT_SYMBOL(drm_connector_init);
707
708/**
709 * drm_connector_cleanup - cleans up an initialised connector
710 * @connector: connector to cleanup
711 *
Dave Airlief453ba02008-11-07 14:05:41 -0800712 * Cleans up the connector but doesn't free the object.
713 */
714void drm_connector_cleanup(struct drm_connector *connector)
715{
716 struct drm_device *dev = connector->dev;
717 struct drm_display_mode *mode, *t;
718
719 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
720 drm_mode_remove(connector, mode);
721
722 list_for_each_entry_safe(mode, t, &connector->modes, head)
723 drm_mode_remove(connector, mode);
724
725 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
726 drm_mode_remove(connector, mode);
727
Dave Airlief453ba02008-11-07 14:05:41 -0800728 drm_mode_object_put(dev, &connector->base);
729 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000730 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800731}
732EXPORT_SYMBOL(drm_connector_cleanup);
733
Dave Airliecbc7e222012-02-20 14:16:40 +0000734void drm_connector_unplug_all(struct drm_device *dev)
735{
736 struct drm_connector *connector;
737
738 /* taking the mode config mutex ends up in a clash with sysfs */
739 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
740 drm_sysfs_connector_remove(connector);
741
742}
743EXPORT_SYMBOL(drm_connector_unplug_all);
744
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200745int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000746 struct drm_encoder *encoder,
747 const struct drm_encoder_funcs *funcs,
748 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800749{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200750 int ret;
751
Daniel Vetter84849902012-12-02 00:28:11 +0100752 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800753
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200754 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
755 if (ret)
756 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800757
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200758 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800759 encoder->encoder_type = encoder_type;
760 encoder->funcs = funcs;
761
762 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
763 dev->mode_config.num_encoder++;
764
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200765 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100766 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200767
768 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800769}
770EXPORT_SYMBOL(drm_encoder_init);
771
772void drm_encoder_cleanup(struct drm_encoder *encoder)
773{
774 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100775 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800776 drm_mode_object_put(dev, &encoder->base);
777 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000778 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100779 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800780}
781EXPORT_SYMBOL(drm_encoder_cleanup);
782
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800783int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
784 unsigned long possible_crtcs,
785 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600786 const uint32_t *formats, uint32_t format_count,
787 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800788{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789 int ret;
790
Daniel Vetter84849902012-12-02 00:28:11 +0100791 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800792
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200793 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
794 if (ret)
795 goto out;
796
Rob Clark4d939142012-05-17 02:23:27 -0600797 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800798 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800799 plane->funcs = funcs;
800 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
801 GFP_KERNEL);
802 if (!plane->format_types) {
803 DRM_DEBUG_KMS("out of memory when allocating plane\n");
804 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200805 ret = -ENOMEM;
806 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800807 }
808
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800809 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800810 plane->format_count = format_count;
811 plane->possible_crtcs = possible_crtcs;
812
Rob Clark0a7eb242011-12-13 20:19:36 -0600813 /* private planes are not exposed to userspace, but depending on
814 * display hardware, might be convenient to allow sharing programming
815 * for the scanout engine with the crtc implementation.
816 */
817 if (!priv) {
818 list_add_tail(&plane->head, &dev->mode_config.plane_list);
819 dev->mode_config.num_plane++;
820 } else {
821 INIT_LIST_HEAD(&plane->head);
822 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800823
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200824 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100825 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800826
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200827 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800828}
829EXPORT_SYMBOL(drm_plane_init);
830
831void drm_plane_cleanup(struct drm_plane *plane)
832{
833 struct drm_device *dev = plane->dev;
834
Daniel Vetter84849902012-12-02 00:28:11 +0100835 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800836 kfree(plane->format_types);
837 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600838 /* if not added to a list, it must be a private plane */
839 if (!list_empty(&plane->head)) {
840 list_del(&plane->head);
841 dev->mode_config.num_plane--;
842 }
Daniel Vetter84849902012-12-02 00:28:11 +0100843 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800844}
845EXPORT_SYMBOL(drm_plane_cleanup);
846
Dave Airlief453ba02008-11-07 14:05:41 -0800847/**
848 * drm_mode_create - create a new display mode
849 * @dev: DRM device
850 *
Dave Airlief453ba02008-11-07 14:05:41 -0800851 * Create a new drm_display_mode, give it an ID, and return it.
852 *
853 * RETURNS:
854 * Pointer to new mode on success, NULL on error.
855 */
856struct drm_display_mode *drm_mode_create(struct drm_device *dev)
857{
858 struct drm_display_mode *nmode;
859
860 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
861 if (!nmode)
862 return NULL;
863
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200864 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
865 kfree(nmode);
866 return NULL;
867 }
868
Dave Airlief453ba02008-11-07 14:05:41 -0800869 return nmode;
870}
871EXPORT_SYMBOL(drm_mode_create);
872
873/**
874 * drm_mode_destroy - remove a mode
875 * @dev: DRM device
876 * @mode: mode to remove
877 *
Dave Airlief453ba02008-11-07 14:05:41 -0800878 * Free @mode's unique identifier, then free it.
879 */
880void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
881{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200882 if (!mode)
883 return;
884
Dave Airlief453ba02008-11-07 14:05:41 -0800885 drm_mode_object_put(dev, &mode->base);
886
887 kfree(mode);
888}
889EXPORT_SYMBOL(drm_mode_destroy);
890
891static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
892{
893 struct drm_property *edid;
894 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800895
896 /*
897 * Standard properties (apply to all connectors)
898 */
899 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
900 DRM_MODE_PROP_IMMUTABLE,
901 "EDID", 0);
902 dev->mode_config.edid_property = edid;
903
Sascha Hauer4a67d392012-02-06 10:58:17 +0100904 dpms = drm_property_create_enum(dev, 0,
905 "DPMS", drm_dpms_enum_list,
906 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800907 dev->mode_config.dpms_property = dpms;
908
909 return 0;
910}
911
912/**
913 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
914 * @dev: DRM device
915 *
916 * Called by a driver the first time a DVI-I connector is made.
917 */
918int drm_mode_create_dvi_i_properties(struct drm_device *dev)
919{
920 struct drm_property *dvi_i_selector;
921 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800922
923 if (dev->mode_config.dvi_i_select_subconnector_property)
924 return 0;
925
926 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100927 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800928 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100929 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800930 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800931 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
932
Sascha Hauer4a67d392012-02-06 10:58:17 +0100933 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800934 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100935 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800936 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800937 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
938
939 return 0;
940}
941EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
942
943/**
944 * drm_create_tv_properties - create TV specific connector properties
945 * @dev: DRM device
946 * @num_modes: number of different TV formats (modes) supported
947 * @modes: array of pointers to strings containing name of each format
948 *
949 * Called by a driver's TV initialization routine, this function creates
950 * the TV specific connector properties for a given device. Caller is
951 * responsible for allocating a list of format names and passing them to
952 * this routine.
953 */
954int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
955 char *modes[])
956{
957 struct drm_property *tv_selector;
958 struct drm_property *tv_subconnector;
959 int i;
960
961 if (dev->mode_config.tv_select_subconnector_property)
962 return 0;
963
964 /*
965 * Basic connector properties
966 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100967 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800968 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100969 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800970 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800971 dev->mode_config.tv_select_subconnector_property = tv_selector;
972
973 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100974 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
975 "subconnector",
976 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800977 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800978 dev->mode_config.tv_subconnector_property = tv_subconnector;
979
980 /*
981 * Other, TV specific properties: margins & TV modes.
982 */
983 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100984 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800985
986 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100987 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800988
989 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100990 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800991
992 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100993 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800994
995 dev->mode_config.tv_mode_property =
996 drm_property_create(dev, DRM_MODE_PROP_ENUM,
997 "mode", num_modes);
998 for (i = 0; i < num_modes; i++)
999 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1000 i, modes[i]);
1001
Francisco Jerezb6b79022009-08-02 04:19:20 +02001002 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001003 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001004
1005 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001006 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001007
1008 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001009 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001010
Francisco Jereza75f0232009-08-12 02:30:10 +02001011 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001012 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001013
1014 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001015 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001016
1017 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001018 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001019
Dave Airlief453ba02008-11-07 14:05:41 -08001020 return 0;
1021}
1022EXPORT_SYMBOL(drm_mode_create_tv_properties);
1023
1024/**
1025 * drm_mode_create_scaling_mode_property - create scaling mode property
1026 * @dev: DRM device
1027 *
1028 * Called by a driver the first time it's needed, must be attached to desired
1029 * connectors.
1030 */
1031int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1032{
1033 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001034
1035 if (dev->mode_config.scaling_mode_property)
1036 return 0;
1037
1038 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001039 drm_property_create_enum(dev, 0, "scaling mode",
1040 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001041 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001042
1043 dev->mode_config.scaling_mode_property = scaling_mode;
1044
1045 return 0;
1046}
1047EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1048
1049/**
1050 * drm_mode_create_dithering_property - create dithering property
1051 * @dev: DRM device
1052 *
1053 * Called by a driver the first time it's needed, must be attached to desired
1054 * connectors.
1055 */
1056int drm_mode_create_dithering_property(struct drm_device *dev)
1057{
1058 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001059
1060 if (dev->mode_config.dithering_mode_property)
1061 return 0;
1062
1063 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001064 drm_property_create_enum(dev, 0, "dithering",
1065 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001066 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001067 dev->mode_config.dithering_mode_property = dithering_mode;
1068
1069 return 0;
1070}
1071EXPORT_SYMBOL(drm_mode_create_dithering_property);
1072
1073/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001074 * drm_mode_create_dirty_property - create dirty property
1075 * @dev: DRM device
1076 *
1077 * Called by a driver the first time it's needed, must be attached to desired
1078 * connectors.
1079 */
1080int drm_mode_create_dirty_info_property(struct drm_device *dev)
1081{
1082 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001083
1084 if (dev->mode_config.dirty_info_property)
1085 return 0;
1086
1087 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001088 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001089 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001090 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001091 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001092 dev->mode_config.dirty_info_property = dirty_info;
1093
1094 return 0;
1095}
1096EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1097
1098/**
Dave Airlief453ba02008-11-07 14:05:41 -08001099 * drm_mode_config_init - initialize DRM mode_configuration structure
1100 * @dev: DRM device
1101 *
Dave Airlief453ba02008-11-07 14:05:41 -08001102 * Initialize @dev's mode_config structure, used for tracking the graphics
1103 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001104 *
1105 * Since this initializes the modeset locks, no locking is possible. Which is no
1106 * problem, since this should happen single threaded at init time. It is the
1107 * driver's problem to ensure this guarantee.
1108 *
Dave Airlief453ba02008-11-07 14:05:41 -08001109 */
1110void drm_mode_config_init(struct drm_device *dev)
1111{
1112 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001113 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001114 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001115 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001116 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1117 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1118 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1119 INIT_LIST_HEAD(&dev->mode_config.property_list);
1120 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001121 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001122 idr_init(&dev->mode_config.crtc_idr);
1123
Daniel Vetter84849902012-12-02 00:28:11 +01001124 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001125 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001126 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001127
1128 /* Just to be sure */
1129 dev->mode_config.num_fb = 0;
1130 dev->mode_config.num_connector = 0;
1131 dev->mode_config.num_crtc = 0;
1132 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001133}
1134EXPORT_SYMBOL(drm_mode_config_init);
1135
1136int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1137{
1138 uint32_t total_objects = 0;
1139
1140 total_objects += dev->mode_config.num_crtc;
1141 total_objects += dev->mode_config.num_connector;
1142 total_objects += dev->mode_config.num_encoder;
1143
Dave Airlief453ba02008-11-07 14:05:41 -08001144 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1145 if (!group->id_list)
1146 return -ENOMEM;
1147
1148 group->num_crtcs = 0;
1149 group->num_connectors = 0;
1150 group->num_encoders = 0;
1151 return 0;
1152}
1153
1154int drm_mode_group_init_legacy_group(struct drm_device *dev,
1155 struct drm_mode_group *group)
1156{
1157 struct drm_crtc *crtc;
1158 struct drm_encoder *encoder;
1159 struct drm_connector *connector;
1160 int ret;
1161
1162 if ((ret = drm_mode_group_init(dev, group)))
1163 return ret;
1164
1165 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1166 group->id_list[group->num_crtcs++] = crtc->base.id;
1167
1168 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1169 group->id_list[group->num_crtcs + group->num_encoders++] =
1170 encoder->base.id;
1171
1172 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1173 group->id_list[group->num_crtcs + group->num_encoders +
1174 group->num_connectors++] = connector->base.id;
1175
1176 return 0;
1177}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001178EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001179
1180/**
1181 * drm_mode_config_cleanup - free up DRM mode_config info
1182 * @dev: DRM device
1183 *
Dave Airlief453ba02008-11-07 14:05:41 -08001184 * Free up all the connectors and CRTCs associated with this DRM device, then
1185 * free up the framebuffers and associated buffer objects.
1186 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001187 * Note that since this /should/ happen single-threaded at driver/device
1188 * teardown time, no locking is required. It's the driver's job to ensure that
1189 * this guarantee actually holds true.
1190 *
Dave Airlief453ba02008-11-07 14:05:41 -08001191 * FIXME: cleanup any dangling user buffer objects too
1192 */
1193void drm_mode_config_cleanup(struct drm_device *dev)
1194{
1195 struct drm_connector *connector, *ot;
1196 struct drm_crtc *crtc, *ct;
1197 struct drm_encoder *encoder, *enct;
1198 struct drm_framebuffer *fb, *fbt;
1199 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001200 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001201
1202 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1203 head) {
1204 encoder->funcs->destroy(encoder);
1205 }
1206
1207 list_for_each_entry_safe(connector, ot,
1208 &dev->mode_config.connector_list, head) {
1209 connector->funcs->destroy(connector);
1210 }
1211
1212 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1213 head) {
1214 drm_property_destroy(dev, property);
1215 }
1216
Daniel Vetter2b677e82012-12-10 21:16:05 +01001217 /*
1218 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001219 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001220 * would actually deadlock with the drm_framebuffer_cleanup function.
1221 *
1222 * Also, if there are any framebuffers left, that's a driver leak now,
1223 * so politely WARN about this.
1224 */
1225 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001226 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001227 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001228 }
1229
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001230 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1231 head) {
1232 plane->funcs->destroy(plane);
1233 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001234
Chris Wilson31840092012-09-17 09:38:03 +00001235 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1236 crtc->funcs->destroy(crtc);
1237 }
1238
Sascha Hauer59ce0622012-02-01 11:38:20 +01001239 idr_remove_all(&dev->mode_config.crtc_idr);
1240 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001241}
1242EXPORT_SYMBOL(drm_mode_config_cleanup);
1243
Dave Airlief453ba02008-11-07 14:05:41 -08001244/**
1245 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1246 * @out: drm_mode_modeinfo struct to return to the user
1247 * @in: drm_display_mode to use
1248 *
Dave Airlief453ba02008-11-07 14:05:41 -08001249 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1250 * the user.
1251 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001252static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1253 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001254{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001255 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1256 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1257 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1258 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1259 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1260 "timing values too large for mode info\n");
1261
Dave Airlief453ba02008-11-07 14:05:41 -08001262 out->clock = in->clock;
1263 out->hdisplay = in->hdisplay;
1264 out->hsync_start = in->hsync_start;
1265 out->hsync_end = in->hsync_end;
1266 out->htotal = in->htotal;
1267 out->hskew = in->hskew;
1268 out->vdisplay = in->vdisplay;
1269 out->vsync_start = in->vsync_start;
1270 out->vsync_end = in->vsync_end;
1271 out->vtotal = in->vtotal;
1272 out->vscan = in->vscan;
1273 out->vrefresh = in->vrefresh;
1274 out->flags = in->flags;
1275 out->type = in->type;
1276 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1277 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1278}
1279
1280/**
1281 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1282 * @out: drm_display_mode to return to the user
1283 * @in: drm_mode_modeinfo to use
1284 *
Dave Airlief453ba02008-11-07 14:05:41 -08001285 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1286 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001287 *
1288 * RETURNS:
1289 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001290 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001291static int drm_crtc_convert_umode(struct drm_display_mode *out,
1292 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001293{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001294 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1295 return -ERANGE;
1296
Dave Airlief453ba02008-11-07 14:05:41 -08001297 out->clock = in->clock;
1298 out->hdisplay = in->hdisplay;
1299 out->hsync_start = in->hsync_start;
1300 out->hsync_end = in->hsync_end;
1301 out->htotal = in->htotal;
1302 out->hskew = in->hskew;
1303 out->vdisplay = in->vdisplay;
1304 out->vsync_start = in->vsync_start;
1305 out->vsync_end = in->vsync_end;
1306 out->vtotal = in->vtotal;
1307 out->vscan = in->vscan;
1308 out->vrefresh = in->vrefresh;
1309 out->flags = in->flags;
1310 out->type = in->type;
1311 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1312 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001313
1314 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001315}
1316
1317/**
1318 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001319 * @dev: drm device for the ioctl
1320 * @data: data pointer for the ioctl
1321 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001322 *
Dave Airlief453ba02008-11-07 14:05:41 -08001323 * Construct a set of configuration description structures and return
1324 * them to the user, including CRTC, connector and framebuffer configuration.
1325 *
1326 * Called by the user via ioctl.
1327 *
1328 * RETURNS:
1329 * Zero on success, errno on failure.
1330 */
1331int drm_mode_getresources(struct drm_device *dev, void *data,
1332 struct drm_file *file_priv)
1333{
1334 struct drm_mode_card_res *card_res = data;
1335 struct list_head *lh;
1336 struct drm_framebuffer *fb;
1337 struct drm_connector *connector;
1338 struct drm_crtc *crtc;
1339 struct drm_encoder *encoder;
1340 int ret = 0;
1341 int connector_count = 0;
1342 int crtc_count = 0;
1343 int fb_count = 0;
1344 int encoder_count = 0;
1345 int copied = 0, i;
1346 uint32_t __user *fb_id;
1347 uint32_t __user *crtc_id;
1348 uint32_t __user *connector_id;
1349 uint32_t __user *encoder_id;
1350 struct drm_mode_group *mode_group;
1351
Dave Airliefb3b06c2011-02-08 13:55:21 +10001352 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1353 return -EINVAL;
1354
Dave Airlief453ba02008-11-07 14:05:41 -08001355
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001356 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001357 /*
1358 * For the non-control nodes we need to limit the list of resources
1359 * by IDs in the group list for this node
1360 */
1361 list_for_each(lh, &file_priv->fbs)
1362 fb_count++;
1363
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001364 /* handle this in 4 parts */
1365 /* FBs */
1366 if (card_res->count_fbs >= fb_count) {
1367 copied = 0;
1368 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1369 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1370 if (put_user(fb->base.id, fb_id + copied)) {
1371 mutex_unlock(&file_priv->fbs_lock);
1372 return -EFAULT;
1373 }
1374 copied++;
1375 }
1376 }
1377 card_res->count_fbs = fb_count;
1378 mutex_unlock(&file_priv->fbs_lock);
1379
1380 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001381 mode_group = &file_priv->master->minor->mode_group;
1382 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1383
1384 list_for_each(lh, &dev->mode_config.crtc_list)
1385 crtc_count++;
1386
1387 list_for_each(lh, &dev->mode_config.connector_list)
1388 connector_count++;
1389
1390 list_for_each(lh, &dev->mode_config.encoder_list)
1391 encoder_count++;
1392 } else {
1393
1394 crtc_count = mode_group->num_crtcs;
1395 connector_count = mode_group->num_connectors;
1396 encoder_count = mode_group->num_encoders;
1397 }
1398
1399 card_res->max_height = dev->mode_config.max_height;
1400 card_res->min_height = dev->mode_config.min_height;
1401 card_res->max_width = dev->mode_config.max_width;
1402 card_res->min_width = dev->mode_config.min_width;
1403
Dave Airlief453ba02008-11-07 14:05:41 -08001404 /* CRTCs */
1405 if (card_res->count_crtcs >= crtc_count) {
1406 copied = 0;
1407 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1408 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1409 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1410 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001411 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001412 if (put_user(crtc->base.id, crtc_id + copied)) {
1413 ret = -EFAULT;
1414 goto out;
1415 }
1416 copied++;
1417 }
1418 } else {
1419 for (i = 0; i < mode_group->num_crtcs; i++) {
1420 if (put_user(mode_group->id_list[i],
1421 crtc_id + copied)) {
1422 ret = -EFAULT;
1423 goto out;
1424 }
1425 copied++;
1426 }
1427 }
1428 }
1429 card_res->count_crtcs = crtc_count;
1430
1431 /* Encoders */
1432 if (card_res->count_encoders >= encoder_count) {
1433 copied = 0;
1434 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1435 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1436 list_for_each_entry(encoder,
1437 &dev->mode_config.encoder_list,
1438 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001439 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1440 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001441 if (put_user(encoder->base.id, encoder_id +
1442 copied)) {
1443 ret = -EFAULT;
1444 goto out;
1445 }
1446 copied++;
1447 }
1448 } else {
1449 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1450 if (put_user(mode_group->id_list[i],
1451 encoder_id + copied)) {
1452 ret = -EFAULT;
1453 goto out;
1454 }
1455 copied++;
1456 }
1457
1458 }
1459 }
1460 card_res->count_encoders = encoder_count;
1461
1462 /* Connectors */
1463 if (card_res->count_connectors >= connector_count) {
1464 copied = 0;
1465 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1466 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1467 list_for_each_entry(connector,
1468 &dev->mode_config.connector_list,
1469 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001470 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1471 connector->base.id,
1472 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001473 if (put_user(connector->base.id,
1474 connector_id + copied)) {
1475 ret = -EFAULT;
1476 goto out;
1477 }
1478 copied++;
1479 }
1480 } else {
1481 int start = mode_group->num_crtcs +
1482 mode_group->num_encoders;
1483 for (i = start; i < start + mode_group->num_connectors; i++) {
1484 if (put_user(mode_group->id_list[i],
1485 connector_id + copied)) {
1486 ret = -EFAULT;
1487 goto out;
1488 }
1489 copied++;
1490 }
1491 }
1492 }
1493 card_res->count_connectors = connector_count;
1494
Jerome Glisse94401062010-07-15 15:43:25 -04001495 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001496 card_res->count_connectors, card_res->count_encoders);
1497
1498out:
Daniel Vetter84849902012-12-02 00:28:11 +01001499 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001500 return ret;
1501}
1502
1503/**
1504 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001505 * @dev: drm device for the ioctl
1506 * @data: data pointer for the ioctl
1507 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001508 *
Dave Airlief453ba02008-11-07 14:05:41 -08001509 * Construct a CRTC configuration structure to return to the user.
1510 *
1511 * Called by the user via ioctl.
1512 *
1513 * RETURNS:
1514 * Zero on success, errno on failure.
1515 */
1516int drm_mode_getcrtc(struct drm_device *dev,
1517 void *data, struct drm_file *file_priv)
1518{
1519 struct drm_mode_crtc *crtc_resp = data;
1520 struct drm_crtc *crtc;
1521 struct drm_mode_object *obj;
1522 int ret = 0;
1523
Dave Airliefb3b06c2011-02-08 13:55:21 +10001524 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1525 return -EINVAL;
1526
Daniel Vetter84849902012-12-02 00:28:11 +01001527 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001528
1529 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1530 DRM_MODE_OBJECT_CRTC);
1531 if (!obj) {
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535 crtc = obj_to_crtc(obj);
1536
1537 crtc_resp->x = crtc->x;
1538 crtc_resp->y = crtc->y;
1539 crtc_resp->gamma_size = crtc->gamma_size;
1540 if (crtc->fb)
1541 crtc_resp->fb_id = crtc->fb->base.id;
1542 else
1543 crtc_resp->fb_id = 0;
1544
1545 if (crtc->enabled) {
1546
1547 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1548 crtc_resp->mode_valid = 1;
1549
1550 } else {
1551 crtc_resp->mode_valid = 0;
1552 }
1553
1554out:
Daniel Vetter84849902012-12-02 00:28:11 +01001555 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001556 return ret;
1557}
1558
1559/**
1560 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001561 * @dev: drm device for the ioctl
1562 * @data: data pointer for the ioctl
1563 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001564 *
Dave Airlief453ba02008-11-07 14:05:41 -08001565 * Construct a connector configuration structure to return to the user.
1566 *
1567 * Called by the user via ioctl.
1568 *
1569 * RETURNS:
1570 * Zero on success, errno on failure.
1571 */
1572int drm_mode_getconnector(struct drm_device *dev, void *data,
1573 struct drm_file *file_priv)
1574{
1575 struct drm_mode_get_connector *out_resp = data;
1576 struct drm_mode_object *obj;
1577 struct drm_connector *connector;
1578 struct drm_display_mode *mode;
1579 int mode_count = 0;
1580 int props_count = 0;
1581 int encoders_count = 0;
1582 int ret = 0;
1583 int copied = 0;
1584 int i;
1585 struct drm_mode_modeinfo u_mode;
1586 struct drm_mode_modeinfo __user *mode_ptr;
1587 uint32_t __user *prop_ptr;
1588 uint64_t __user *prop_values;
1589 uint32_t __user *encoder_ptr;
1590
Dave Airliefb3b06c2011-02-08 13:55:21 +10001591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1592 return -EINVAL;
1593
Dave Airlief453ba02008-11-07 14:05:41 -08001594 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1595
Jerome Glisse94401062010-07-15 15:43:25 -04001596 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001597
Daniel Vetter84849902012-12-02 00:28:11 +01001598 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001599
1600 obj = drm_mode_object_find(dev, out_resp->connector_id,
1601 DRM_MODE_OBJECT_CONNECTOR);
1602 if (!obj) {
1603 ret = -EINVAL;
1604 goto out;
1605 }
1606 connector = obj_to_connector(obj);
1607
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001608 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001609
1610 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1611 if (connector->encoder_ids[i] != 0) {
1612 encoders_count++;
1613 }
1614 }
1615
1616 if (out_resp->count_modes == 0) {
1617 connector->funcs->fill_modes(connector,
1618 dev->mode_config.max_width,
1619 dev->mode_config.max_height);
1620 }
1621
1622 /* delayed so we get modes regardless of pre-fill_modes state */
1623 list_for_each_entry(mode, &connector->modes, head)
1624 mode_count++;
1625
1626 out_resp->connector_id = connector->base.id;
1627 out_resp->connector_type = connector->connector_type;
1628 out_resp->connector_type_id = connector->connector_type_id;
1629 out_resp->mm_width = connector->display_info.width_mm;
1630 out_resp->mm_height = connector->display_info.height_mm;
1631 out_resp->subpixel = connector->display_info.subpixel_order;
1632 out_resp->connection = connector->status;
1633 if (connector->encoder)
1634 out_resp->encoder_id = connector->encoder->base.id;
1635 else
1636 out_resp->encoder_id = 0;
1637
1638 /*
1639 * This ioctl is called twice, once to determine how much space is
1640 * needed, and the 2nd time to fill it.
1641 */
1642 if ((out_resp->count_modes >= mode_count) && mode_count) {
1643 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001644 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001645 list_for_each_entry(mode, &connector->modes, head) {
1646 drm_crtc_convert_to_umode(&u_mode, mode);
1647 if (copy_to_user(mode_ptr + copied,
1648 &u_mode, sizeof(u_mode))) {
1649 ret = -EFAULT;
1650 goto out;
1651 }
1652 copied++;
1653 }
1654 }
1655 out_resp->count_modes = mode_count;
1656
1657 if ((out_resp->count_props >= props_count) && props_count) {
1658 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001659 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1660 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001661 for (i = 0; i < connector->properties.count; i++) {
1662 if (put_user(connector->properties.ids[i],
1663 prop_ptr + copied)) {
1664 ret = -EFAULT;
1665 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001666 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001667
1668 if (put_user(connector->properties.values[i],
1669 prop_values + copied)) {
1670 ret = -EFAULT;
1671 goto out;
1672 }
1673 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001674 }
1675 }
1676 out_resp->count_props = props_count;
1677
1678 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1679 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001680 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001681 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1682 if (connector->encoder_ids[i] != 0) {
1683 if (put_user(connector->encoder_ids[i],
1684 encoder_ptr + copied)) {
1685 ret = -EFAULT;
1686 goto out;
1687 }
1688 copied++;
1689 }
1690 }
1691 }
1692 out_resp->count_encoders = encoders_count;
1693
1694out:
Daniel Vetter84849902012-12-02 00:28:11 +01001695 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001696 return ret;
1697}
1698
1699int drm_mode_getencoder(struct drm_device *dev, void *data,
1700 struct drm_file *file_priv)
1701{
1702 struct drm_mode_get_encoder *enc_resp = data;
1703 struct drm_mode_object *obj;
1704 struct drm_encoder *encoder;
1705 int ret = 0;
1706
Dave Airliefb3b06c2011-02-08 13:55:21 +10001707 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1708 return -EINVAL;
1709
Daniel Vetter84849902012-12-02 00:28:11 +01001710 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001711 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1712 DRM_MODE_OBJECT_ENCODER);
1713 if (!obj) {
1714 ret = -EINVAL;
1715 goto out;
1716 }
1717 encoder = obj_to_encoder(obj);
1718
1719 if (encoder->crtc)
1720 enc_resp->crtc_id = encoder->crtc->base.id;
1721 else
1722 enc_resp->crtc_id = 0;
1723 enc_resp->encoder_type = encoder->encoder_type;
1724 enc_resp->encoder_id = encoder->base.id;
1725 enc_resp->possible_crtcs = encoder->possible_crtcs;
1726 enc_resp->possible_clones = encoder->possible_clones;
1727
1728out:
Daniel Vetter84849902012-12-02 00:28:11 +01001729 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001730 return ret;
1731}
1732
1733/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001734 * drm_mode_getplane_res - get plane info
1735 * @dev: DRM device
1736 * @data: ioctl data
1737 * @file_priv: DRM file info
1738 *
1739 * Return an plane count and set of IDs.
1740 */
1741int drm_mode_getplane_res(struct drm_device *dev, void *data,
1742 struct drm_file *file_priv)
1743{
1744 struct drm_mode_get_plane_res *plane_resp = data;
1745 struct drm_mode_config *config;
1746 struct drm_plane *plane;
1747 uint32_t __user *plane_ptr;
1748 int copied = 0, ret = 0;
1749
1750 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1751 return -EINVAL;
1752
Daniel Vetter84849902012-12-02 00:28:11 +01001753 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001754 config = &dev->mode_config;
1755
1756 /*
1757 * This ioctl is called twice, once to determine how much space is
1758 * needed, and the 2nd time to fill it.
1759 */
1760 if (config->num_plane &&
1761 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001762 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001763
1764 list_for_each_entry(plane, &config->plane_list, head) {
1765 if (put_user(plane->base.id, plane_ptr + copied)) {
1766 ret = -EFAULT;
1767 goto out;
1768 }
1769 copied++;
1770 }
1771 }
1772 plane_resp->count_planes = config->num_plane;
1773
1774out:
Daniel Vetter84849902012-12-02 00:28:11 +01001775 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001776 return ret;
1777}
1778
1779/**
1780 * drm_mode_getplane - get plane info
1781 * @dev: DRM device
1782 * @data: ioctl data
1783 * @file_priv: DRM file info
1784 *
1785 * Return plane info, including formats supported, gamma size, any
1786 * current fb, etc.
1787 */
1788int drm_mode_getplane(struct drm_device *dev, void *data,
1789 struct drm_file *file_priv)
1790{
1791 struct drm_mode_get_plane *plane_resp = data;
1792 struct drm_mode_object *obj;
1793 struct drm_plane *plane;
1794 uint32_t __user *format_ptr;
1795 int ret = 0;
1796
1797 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1798 return -EINVAL;
1799
Daniel Vetter84849902012-12-02 00:28:11 +01001800 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001801 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1802 DRM_MODE_OBJECT_PLANE);
1803 if (!obj) {
1804 ret = -ENOENT;
1805 goto out;
1806 }
1807 plane = obj_to_plane(obj);
1808
1809 if (plane->crtc)
1810 plane_resp->crtc_id = plane->crtc->base.id;
1811 else
1812 plane_resp->crtc_id = 0;
1813
1814 if (plane->fb)
1815 plane_resp->fb_id = plane->fb->base.id;
1816 else
1817 plane_resp->fb_id = 0;
1818
1819 plane_resp->plane_id = plane->base.id;
1820 plane_resp->possible_crtcs = plane->possible_crtcs;
1821 plane_resp->gamma_size = plane->gamma_size;
1822
1823 /*
1824 * This ioctl is called twice, once to determine how much space is
1825 * needed, and the 2nd time to fill it.
1826 */
1827 if (plane->format_count &&
1828 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001829 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830 if (copy_to_user(format_ptr,
1831 plane->format_types,
1832 sizeof(uint32_t) * plane->format_count)) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836 }
1837 plane_resp->count_format_types = plane->format_count;
1838
1839out:
Daniel Vetter84849902012-12-02 00:28:11 +01001840 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001841 return ret;
1842}
1843
1844/**
1845 * drm_mode_setplane - set up or tear down an plane
1846 * @dev: DRM device
1847 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001848 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001849 *
1850 * Set plane info, including placement, fb, scaling, and other factors.
1851 * Or pass a NULL fb to disable.
1852 */
1853int drm_mode_setplane(struct drm_device *dev, void *data,
1854 struct drm_file *file_priv)
1855{
1856 struct drm_mode_set_plane *plane_req = data;
1857 struct drm_mode_object *obj;
1858 struct drm_plane *plane;
1859 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001860 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001861 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001862 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001863 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001864
1865 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1866 return -EINVAL;
1867
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001868 /*
1869 * First, find the plane, crtc, and fb objects. If not available,
1870 * we don't bother to call the driver.
1871 */
1872 obj = drm_mode_object_find(dev, plane_req->plane_id,
1873 DRM_MODE_OBJECT_PLANE);
1874 if (!obj) {
1875 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1876 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001877 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001878 }
1879 plane = obj_to_plane(obj);
1880
1881 /* No fb means shut it down */
1882 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001883 drm_modeset_lock_all(dev);
1884 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001885 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001886 plane->crtc = NULL;
1887 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001888 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001889 goto out;
1890 }
1891
1892 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1893 DRM_MODE_OBJECT_CRTC);
1894 if (!obj) {
1895 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1896 plane_req->crtc_id);
1897 ret = -ENOENT;
1898 goto out;
1899 }
1900 crtc = obj_to_crtc(obj);
1901
Daniel Vetter786b99e2012-12-02 21:53:40 +01001902 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1903 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1905 plane_req->fb_id);
1906 ret = -ENOENT;
1907 goto out;
1908 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001909
Ville Syrjälä62443be2011-12-20 00:06:47 +02001910 /* Check whether this plane supports the fb pixel format. */
1911 for (i = 0; i < plane->format_count; i++)
1912 if (fb->pixel_format == plane->format_types[i])
1913 break;
1914 if (i == plane->format_count) {
1915 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1916 ret = -EINVAL;
1917 goto out;
1918 }
1919
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001920 fb_width = fb->width << 16;
1921 fb_height = fb->height << 16;
1922
1923 /* Make sure source coordinates are inside the fb. */
1924 if (plane_req->src_w > fb_width ||
1925 plane_req->src_x > fb_width - plane_req->src_w ||
1926 plane_req->src_h > fb_height ||
1927 plane_req->src_y > fb_height - plane_req->src_h) {
1928 DRM_DEBUG_KMS("Invalid source coordinates "
1929 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1930 plane_req->src_w >> 16,
1931 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1932 plane_req->src_h >> 16,
1933 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1934 plane_req->src_x >> 16,
1935 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1936 plane_req->src_y >> 16,
1937 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1938 ret = -ENOSPC;
1939 goto out;
1940 }
1941
Ville Syrjälä687a0402011-12-20 00:06:45 +02001942 /* Give drivers some help against integer overflows */
1943 if (plane_req->crtc_w > INT_MAX ||
1944 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1945 plane_req->crtc_h > INT_MAX ||
1946 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1947 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1948 plane_req->crtc_w, plane_req->crtc_h,
1949 plane_req->crtc_x, plane_req->crtc_y);
1950 ret = -ERANGE;
1951 goto out;
1952 }
1953
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001954 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955 ret = plane->funcs->update_plane(plane, crtc, fb,
1956 plane_req->crtc_x, plane_req->crtc_y,
1957 plane_req->crtc_w, plane_req->crtc_h,
1958 plane_req->src_x, plane_req->src_y,
1959 plane_req->src_w, plane_req->src_h);
1960 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001961 old_fb = plane->fb;
1962 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001963 plane->crtc = crtc;
1964 plane->fb = fb;
1965 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001966 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001967
1968out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001969 if (fb)
1970 drm_framebuffer_unreference(fb);
1971 if (old_fb)
1972 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001973
1974 return ret;
1975}
1976
1977/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001978 * drm_mode_set_config_internal - helper to call ->set_config
1979 * @set: modeset config to set
1980 *
1981 * This is a little helper to wrap internal calls to the ->set_config driver
1982 * interface. The only thing it adds is correct refcounting dance.
1983 */
1984int drm_mode_set_config_internal(struct drm_mode_set *set)
1985{
1986 struct drm_crtc *crtc = set->crtc;
1987
1988 return crtc->funcs->set_config(set);
1989}
1990EXPORT_SYMBOL(drm_mode_set_config_internal);
1991
1992/**
Dave Airlief453ba02008-11-07 14:05:41 -08001993 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001994 * @dev: drm device for the ioctl
1995 * @data: data pointer for the ioctl
1996 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001997 *
Dave Airlief453ba02008-11-07 14:05:41 -08001998 * Build a new CRTC configuration based on user request.
1999 *
2000 * Called by the user via ioctl.
2001 *
2002 * RETURNS:
2003 * Zero on success, errno on failure.
2004 */
2005int drm_mode_setcrtc(struct drm_device *dev, void *data,
2006 struct drm_file *file_priv)
2007{
2008 struct drm_mode_config *config = &dev->mode_config;
2009 struct drm_mode_crtc *crtc_req = data;
2010 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002011 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002012 struct drm_connector **connector_set = NULL, *connector;
2013 struct drm_framebuffer *fb = NULL;
2014 struct drm_display_mode *mode = NULL;
2015 struct drm_mode_set set;
2016 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002017 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002018 int i;
2019
Dave Airliefb3b06c2011-02-08 13:55:21 +10002020 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2021 return -EINVAL;
2022
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002023 /* For some reason crtc x/y offsets are signed internally. */
2024 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2025 return -ERANGE;
2026
Daniel Vetter84849902012-12-02 00:28:11 +01002027 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002028 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2029 DRM_MODE_OBJECT_CRTC);
2030 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002031 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002032 ret = -EINVAL;
2033 goto out;
2034 }
2035 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002036 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002037
2038 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002039 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002040 /* If we have a mode we need a framebuffer. */
2041 /* If we pass -1, set the mode with the currently bound fb */
2042 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002043 if (!crtc->fb) {
2044 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2045 ret = -EINVAL;
2046 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002047 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002048 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002049 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002050 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2051 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002052 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2053 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002054 ret = -EINVAL;
2055 goto out;
2056 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002057 /* fb is protect by the mode_config lock, so drop the
2058 * ref immediately */
2059 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002060 }
2061
2062 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002063 if (!mode) {
2064 ret = -ENOMEM;
2065 goto out;
2066 }
2067
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002068 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2069 if (ret) {
2070 DRM_DEBUG_KMS("Invalid mode\n");
2071 goto out;
2072 }
2073
Dave Airlief453ba02008-11-07 14:05:41 -08002074 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002075
Rob Clark7c80e122012-09-04 16:35:56 +00002076 hdisplay = mode->hdisplay;
2077 vdisplay = mode->vdisplay;
2078
2079 if (crtc->invert_dimensions)
2080 swap(hdisplay, vdisplay);
2081
2082 if (hdisplay > fb->width ||
2083 vdisplay > fb->height ||
2084 crtc_req->x > fb->width - hdisplay ||
2085 crtc_req->y > fb->height - vdisplay) {
2086 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2087 fb->width, fb->height,
2088 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2089 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002090 ret = -ENOSPC;
2091 goto out;
2092 }
Dave Airlief453ba02008-11-07 14:05:41 -08002093 }
2094
2095 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002096 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002097 ret = -EINVAL;
2098 goto out;
2099 }
2100
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002101 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002102 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002103 crtc_req->count_connectors);
2104 ret = -EINVAL;
2105 goto out;
2106 }
2107
2108 if (crtc_req->count_connectors > 0) {
2109 u32 out_id;
2110
2111 /* Avoid unbounded kernel memory allocation */
2112 if (crtc_req->count_connectors > config->num_connector) {
2113 ret = -EINVAL;
2114 goto out;
2115 }
2116
2117 connector_set = kmalloc(crtc_req->count_connectors *
2118 sizeof(struct drm_connector *),
2119 GFP_KERNEL);
2120 if (!connector_set) {
2121 ret = -ENOMEM;
2122 goto out;
2123 }
2124
2125 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002126 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002127 if (get_user(out_id, &set_connectors_ptr[i])) {
2128 ret = -EFAULT;
2129 goto out;
2130 }
2131
2132 obj = drm_mode_object_find(dev, out_id,
2133 DRM_MODE_OBJECT_CONNECTOR);
2134 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002135 DRM_DEBUG_KMS("Connector id %d unknown\n",
2136 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002137 ret = -EINVAL;
2138 goto out;
2139 }
2140 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002141 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2142 connector->base.id,
2143 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002144
2145 connector_set[i] = connector;
2146 }
2147 }
2148
2149 set.crtc = crtc;
2150 set.x = crtc_req->x;
2151 set.y = crtc_req->y;
2152 set.mode = mode;
2153 set.connectors = connector_set;
2154 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002155 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002156 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002157
2158out:
2159 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002160 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002161 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002162 return ret;
2163}
2164
2165int drm_mode_cursor_ioctl(struct drm_device *dev,
2166 void *data, struct drm_file *file_priv)
2167{
2168 struct drm_mode_cursor *req = data;
2169 struct drm_mode_object *obj;
2170 struct drm_crtc *crtc;
2171 int ret = 0;
2172
Dave Airliefb3b06c2011-02-08 13:55:21 +10002173 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2174 return -EINVAL;
2175
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002176 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002177 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002178
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002179 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002180 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002181 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002182 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002183 }
2184 crtc = obj_to_crtc(obj);
2185
Daniel Vetterdac35662012-12-02 15:24:10 +01002186 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002187 if (req->flags & DRM_MODE_CURSOR_BO) {
2188 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002189 ret = -ENXIO;
2190 goto out;
2191 }
2192 /* Turns off the cursor if handle is 0 */
2193 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2194 req->width, req->height);
2195 }
2196
2197 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2198 if (crtc->funcs->cursor_move) {
2199 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2200 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002201 ret = -EFAULT;
2202 goto out;
2203 }
2204 }
2205out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002206 mutex_unlock(&crtc->mutex);
2207
Dave Airlief453ba02008-11-07 14:05:41 -08002208 return ret;
2209}
2210
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002211/* Original addfb only supported RGB formats, so figure out which one */
2212uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2213{
2214 uint32_t fmt;
2215
2216 switch (bpp) {
2217 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002218 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002219 break;
2220 case 16:
2221 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002222 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002223 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002224 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002225 break;
2226 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002227 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002228 break;
2229 case 32:
2230 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002231 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002232 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002233 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002234 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002235 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002236 break;
2237 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002238 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2239 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002240 break;
2241 }
2242
2243 return fmt;
2244}
2245EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2246
Dave Airlief453ba02008-11-07 14:05:41 -08002247/**
2248 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002249 * @dev: drm device for the ioctl
2250 * @data: data pointer for the ioctl
2251 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002252 *
Dave Airlief453ba02008-11-07 14:05:41 -08002253 * Add a new FB to the specified CRTC, given a user request.
2254 *
2255 * Called by the user via ioctl.
2256 *
2257 * RETURNS:
2258 * Zero on success, errno on failure.
2259 */
2260int drm_mode_addfb(struct drm_device *dev,
2261 void *data, struct drm_file *file_priv)
2262{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002263 struct drm_mode_fb_cmd *or = data;
2264 struct drm_mode_fb_cmd2 r = {};
2265 struct drm_mode_config *config = &dev->mode_config;
2266 struct drm_framebuffer *fb;
2267 int ret = 0;
2268
2269 /* Use new struct with format internally */
2270 r.fb_id = or->fb_id;
2271 r.width = or->width;
2272 r.height = or->height;
2273 r.pitches[0] = or->pitch;
2274 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2275 r.handles[0] = or->handle;
2276
2277 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2278 return -EINVAL;
2279
Jesse Barnesacb4b992011-11-07 12:03:23 -08002280 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002281 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002282
2283 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002284 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002285
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002286 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2287 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002288 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002289 drm_modeset_unlock_all(dev);
2290 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002291 }
2292
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002293 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002294 or->fb_id = fb->base.id;
2295 list_add(&fb->filp_head, &file_priv->fbs);
2296 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002297 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002298
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002299 return ret;
2300}
2301
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002302static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002303{
2304 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2305
2306 switch (format) {
2307 case DRM_FORMAT_C8:
2308 case DRM_FORMAT_RGB332:
2309 case DRM_FORMAT_BGR233:
2310 case DRM_FORMAT_XRGB4444:
2311 case DRM_FORMAT_XBGR4444:
2312 case DRM_FORMAT_RGBX4444:
2313 case DRM_FORMAT_BGRX4444:
2314 case DRM_FORMAT_ARGB4444:
2315 case DRM_FORMAT_ABGR4444:
2316 case DRM_FORMAT_RGBA4444:
2317 case DRM_FORMAT_BGRA4444:
2318 case DRM_FORMAT_XRGB1555:
2319 case DRM_FORMAT_XBGR1555:
2320 case DRM_FORMAT_RGBX5551:
2321 case DRM_FORMAT_BGRX5551:
2322 case DRM_FORMAT_ARGB1555:
2323 case DRM_FORMAT_ABGR1555:
2324 case DRM_FORMAT_RGBA5551:
2325 case DRM_FORMAT_BGRA5551:
2326 case DRM_FORMAT_RGB565:
2327 case DRM_FORMAT_BGR565:
2328 case DRM_FORMAT_RGB888:
2329 case DRM_FORMAT_BGR888:
2330 case DRM_FORMAT_XRGB8888:
2331 case DRM_FORMAT_XBGR8888:
2332 case DRM_FORMAT_RGBX8888:
2333 case DRM_FORMAT_BGRX8888:
2334 case DRM_FORMAT_ARGB8888:
2335 case DRM_FORMAT_ABGR8888:
2336 case DRM_FORMAT_RGBA8888:
2337 case DRM_FORMAT_BGRA8888:
2338 case DRM_FORMAT_XRGB2101010:
2339 case DRM_FORMAT_XBGR2101010:
2340 case DRM_FORMAT_RGBX1010102:
2341 case DRM_FORMAT_BGRX1010102:
2342 case DRM_FORMAT_ARGB2101010:
2343 case DRM_FORMAT_ABGR2101010:
2344 case DRM_FORMAT_RGBA1010102:
2345 case DRM_FORMAT_BGRA1010102:
2346 case DRM_FORMAT_YUYV:
2347 case DRM_FORMAT_YVYU:
2348 case DRM_FORMAT_UYVY:
2349 case DRM_FORMAT_VYUY:
2350 case DRM_FORMAT_AYUV:
2351 case DRM_FORMAT_NV12:
2352 case DRM_FORMAT_NV21:
2353 case DRM_FORMAT_NV16:
2354 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002355 case DRM_FORMAT_NV24:
2356 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002357 case DRM_FORMAT_YUV410:
2358 case DRM_FORMAT_YVU410:
2359 case DRM_FORMAT_YUV411:
2360 case DRM_FORMAT_YVU411:
2361 case DRM_FORMAT_YUV420:
2362 case DRM_FORMAT_YVU420:
2363 case DRM_FORMAT_YUV422:
2364 case DRM_FORMAT_YVU422:
2365 case DRM_FORMAT_YUV444:
2366 case DRM_FORMAT_YVU444:
2367 return 0;
2368 default:
2369 return -EINVAL;
2370 }
2371}
2372
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002373static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002374{
2375 int ret, hsub, vsub, num_planes, i;
2376
2377 ret = format_check(r);
2378 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002379 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002380 return ret;
2381 }
2382
2383 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2384 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2385 num_planes = drm_format_num_planes(r->pixel_format);
2386
2387 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002388 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002389 return -EINVAL;
2390 }
2391
2392 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002393 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002394 return -EINVAL;
2395 }
2396
2397 for (i = 0; i < num_planes; i++) {
2398 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002399 unsigned int height = r->height / (i != 0 ? vsub : 1);
2400 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002401
2402 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002403 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002404 return -EINVAL;
2405 }
2406
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002407 if ((uint64_t) width * cpp > UINT_MAX)
2408 return -ERANGE;
2409
2410 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2411 return -ERANGE;
2412
2413 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002414 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002415 return -EINVAL;
2416 }
2417 }
2418
2419 return 0;
2420}
2421
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002422/**
2423 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002424 * @dev: drm device for the ioctl
2425 * @data: data pointer for the ioctl
2426 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002427 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002428 * Add a new FB to the specified CRTC, given a user request with format.
2429 *
2430 * Called by the user via ioctl.
2431 *
2432 * RETURNS:
2433 * Zero on success, errno on failure.
2434 */
2435int drm_mode_addfb2(struct drm_device *dev,
2436 void *data, struct drm_file *file_priv)
2437{
2438 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002439 struct drm_mode_config *config = &dev->mode_config;
2440 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002441 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002442
Dave Airliefb3b06c2011-02-08 13:55:21 +10002443 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2444 return -EINVAL;
2445
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002446 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2447 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2448 return -EINVAL;
2449 }
2450
Dave Airlief453ba02008-11-07 14:05:41 -08002451 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002452 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002453 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002454 return -EINVAL;
2455 }
2456 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002457 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002458 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002459 return -EINVAL;
2460 }
2461
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002462 ret = framebuffer_check(r);
2463 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002464 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002465
Dave Airlief453ba02008-11-07 14:05:41 -08002466 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002467 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002468 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002469 drm_modeset_unlock_all(dev);
2470 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002471 }
2472
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002473 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002474 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002475 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002476 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002477 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002478
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002479
Dave Airlief453ba02008-11-07 14:05:41 -08002480 return ret;
2481}
2482
2483/**
2484 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002485 * @dev: drm device for the ioctl
2486 * @data: data pointer for the ioctl
2487 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002488 *
Dave Airlief453ba02008-11-07 14:05:41 -08002489 * Remove the FB specified by the user.
2490 *
2491 * Called by the user via ioctl.
2492 *
2493 * RETURNS:
2494 * Zero on success, errno on failure.
2495 */
2496int drm_mode_rmfb(struct drm_device *dev,
2497 void *data, struct drm_file *file_priv)
2498{
Dave Airlief453ba02008-11-07 14:05:41 -08002499 struct drm_framebuffer *fb = NULL;
2500 struct drm_framebuffer *fbl = NULL;
2501 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002502 int found = 0;
2503
Dave Airliefb3b06c2011-02-08 13:55:21 +10002504 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2505 return -EINVAL;
2506
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002507 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002508 mutex_lock(&dev->mode_config.fb_lock);
2509 fb = __drm_framebuffer_lookup(dev, *id);
2510 if (!fb)
2511 goto fail_lookup;
2512
Dave Airlief453ba02008-11-07 14:05:41 -08002513 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2514 if (fb == fbl)
2515 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002516 if (!found)
2517 goto fail_lookup;
2518
2519 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2520 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002521
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002522 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002523 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002524 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002525
Daniel Vetter2b677e82012-12-10 21:16:05 +01002526 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002527 drm_framebuffer_remove(fb);
Daniel Vetter84849902012-12-02 00:28:11 +01002528 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002529
Daniel Vetter2b677e82012-12-10 21:16:05 +01002530 return 0;
2531
2532fail_lookup:
2533 mutex_unlock(&dev->mode_config.fb_lock);
2534 mutex_unlock(&file_priv->fbs_lock);
2535
2536 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002537}
2538
2539/**
2540 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002541 * @dev: drm device for the ioctl
2542 * @data: data pointer for the ioctl
2543 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002544 *
Dave Airlief453ba02008-11-07 14:05:41 -08002545 * Lookup the FB given its ID and return info about it.
2546 *
2547 * Called by the user via ioctl.
2548 *
2549 * RETURNS:
2550 * Zero on success, errno on failure.
2551 */
2552int drm_mode_getfb(struct drm_device *dev,
2553 void *data, struct drm_file *file_priv)
2554{
2555 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002556 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002557 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002558
Dave Airliefb3b06c2011-02-08 13:55:21 +10002559 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2560 return -EINVAL;
2561
Daniel Vetter786b99e2012-12-02 21:53:40 +01002562 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002563 if (!fb)
2564 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002565
2566 r->height = fb->height;
2567 r->width = fb->width;
2568 r->depth = fb->depth;
2569 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002570 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002571 if (fb->funcs->create_handle)
2572 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2573 else
2574 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002575
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002576 drm_framebuffer_unreference(fb);
2577
Dave Airlief453ba02008-11-07 14:05:41 -08002578 return ret;
2579}
2580
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002581int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2582 void *data, struct drm_file *file_priv)
2583{
2584 struct drm_clip_rect __user *clips_ptr;
2585 struct drm_clip_rect *clips = NULL;
2586 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002587 struct drm_framebuffer *fb;
2588 unsigned flags;
2589 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002590 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002591
Dave Airliefb3b06c2011-02-08 13:55:21 +10002592 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2593 return -EINVAL;
2594
Daniel Vetter786b99e2012-12-02 21:53:40 +01002595 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002596 if (!fb)
2597 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002598
2599 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002600 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002601
2602 if (!num_clips != !clips_ptr) {
2603 ret = -EINVAL;
2604 goto out_err1;
2605 }
2606
2607 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2608
2609 /* If userspace annotates copy, clips must come in pairs */
2610 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2611 ret = -EINVAL;
2612 goto out_err1;
2613 }
2614
2615 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002616 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2617 ret = -EINVAL;
2618 goto out_err1;
2619 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002620 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2621 if (!clips) {
2622 ret = -ENOMEM;
2623 goto out_err1;
2624 }
2625
2626 ret = copy_from_user(clips, clips_ptr,
2627 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002628 if (ret) {
2629 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002630 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002631 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002632 }
2633
2634 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002635 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002636 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2637 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002638 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002639 } else {
2640 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002641 }
2642
2643out_err2:
2644 kfree(clips);
2645out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002646 drm_framebuffer_unreference(fb);
2647
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002648 return ret;
2649}
2650
2651
Dave Airlief453ba02008-11-07 14:05:41 -08002652/**
2653 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002654 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002655 *
Dave Airlief453ba02008-11-07 14:05:41 -08002656 * Destroy all the FBs associated with @filp.
2657 *
2658 * Called by the user via ioctl.
2659 *
2660 * RETURNS:
2661 * Zero on success, errno on failure.
2662 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002663void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002664{
Dave Airlief453ba02008-11-07 14:05:41 -08002665 struct drm_device *dev = priv->minor->dev;
2666 struct drm_framebuffer *fb, *tfb;
2667
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002668 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002669 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002670
2671 mutex_lock(&dev->mode_config.fb_lock);
2672 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2673 __drm_framebuffer_unregister(dev, fb);
2674 mutex_unlock(&dev->mode_config.fb_lock);
2675
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002676 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002677
2678 /* This will also drop the fpriv->fbs reference. */
Daniel Vetter7d331592012-12-10 23:44:22 +01002679 drm_modeset_lock_all(dev);
Rob Clarkf7eff602012-09-05 21:48:38 +00002680 drm_framebuffer_remove(fb);
Daniel Vetter7d331592012-12-10 23:44:22 +01002681 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002682 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002683 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002684}
2685
2686/**
2687 * drm_mode_attachmode - add a mode to the user mode list
2688 * @dev: DRM device
2689 * @connector: connector to add the mode to
2690 * @mode: mode to add
2691 *
2692 * Add @mode to @connector's user mode list.
2693 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002694static void drm_mode_attachmode(struct drm_device *dev,
2695 struct drm_connector *connector,
2696 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002697{
Dave Airlief453ba02008-11-07 14:05:41 -08002698 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002699}
2700
2701int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002702 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002703{
2704 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002705 int ret = 0;
2706 struct drm_display_mode *dup_mode, *next;
2707 LIST_HEAD(list);
2708
Dave Airlief453ba02008-11-07 14:05:41 -08002709 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2710 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002711 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002712 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002713 dup_mode = drm_mode_duplicate(dev, mode);
2714 if (!dup_mode) {
2715 ret = -ENOMEM;
2716 goto out;
2717 }
2718 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002719 }
2720 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002721
2722 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2723 if (!connector->encoder)
2724 continue;
2725 if (connector->encoder->crtc == crtc)
2726 list_move_tail(list.next, &connector->user_modes);
2727 }
2728
2729 WARN_ON(!list_empty(&list));
2730
2731 out:
2732 list_for_each_entry_safe(dup_mode, next, &list, head)
2733 drm_mode_destroy(dev, dup_mode);
2734
2735 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002736}
2737EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2738
2739static int drm_mode_detachmode(struct drm_device *dev,
2740 struct drm_connector *connector,
2741 struct drm_display_mode *mode)
2742{
2743 int found = 0;
2744 int ret = 0;
2745 struct drm_display_mode *match_mode, *t;
2746
2747 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2748 if (drm_mode_equal(match_mode, mode)) {
2749 list_del(&match_mode->head);
2750 drm_mode_destroy(dev, match_mode);
2751 found = 1;
2752 break;
2753 }
2754 }
2755
2756 if (!found)
2757 ret = -EINVAL;
2758
2759 return ret;
2760}
2761
2762int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2763{
2764 struct drm_connector *connector;
2765
2766 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2767 drm_mode_detachmode(dev, connector, mode);
2768 }
2769 return 0;
2770}
2771EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2772
2773/**
2774 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002775 * @dev: drm device for the ioctl
2776 * @data: data pointer for the ioctl
2777 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002778 *
2779 * This attaches a user specified mode to an connector.
2780 * Called by the user via ioctl.
2781 *
2782 * RETURNS:
2783 * Zero on success, errno on failure.
2784 */
2785int drm_mode_attachmode_ioctl(struct drm_device *dev,
2786 void *data, struct drm_file *file_priv)
2787{
2788 struct drm_mode_mode_cmd *mode_cmd = data;
2789 struct drm_connector *connector;
2790 struct drm_display_mode *mode;
2791 struct drm_mode_object *obj;
2792 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002793 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002794
Dave Airliefb3b06c2011-02-08 13:55:21 +10002795 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2796 return -EINVAL;
2797
Daniel Vetter84849902012-12-02 00:28:11 +01002798 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002799
2800 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2801 if (!obj) {
2802 ret = -EINVAL;
2803 goto out;
2804 }
2805 connector = obj_to_connector(obj);
2806
2807 mode = drm_mode_create(dev);
2808 if (!mode) {
2809 ret = -ENOMEM;
2810 goto out;
2811 }
2812
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002813 ret = drm_crtc_convert_umode(mode, umode);
2814 if (ret) {
2815 DRM_DEBUG_KMS("Invalid mode\n");
2816 drm_mode_destroy(dev, mode);
2817 goto out;
2818 }
Dave Airlief453ba02008-11-07 14:05:41 -08002819
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002820 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002821out:
Daniel Vetter84849902012-12-02 00:28:11 +01002822 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002823 return ret;
2824}
2825
2826
2827/**
2828 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002829 * @dev: drm device for the ioctl
2830 * @data: data pointer for the ioctl
2831 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002832 *
2833 * Called by the user via ioctl.
2834 *
2835 * RETURNS:
2836 * Zero on success, errno on failure.
2837 */
2838int drm_mode_detachmode_ioctl(struct drm_device *dev,
2839 void *data, struct drm_file *file_priv)
2840{
2841 struct drm_mode_object *obj;
2842 struct drm_mode_mode_cmd *mode_cmd = data;
2843 struct drm_connector *connector;
2844 struct drm_display_mode mode;
2845 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002846 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002847
Dave Airliefb3b06c2011-02-08 13:55:21 +10002848 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2849 return -EINVAL;
2850
Daniel Vetter84849902012-12-02 00:28:11 +01002851 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002852
2853 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2854 if (!obj) {
2855 ret = -EINVAL;
2856 goto out;
2857 }
2858 connector = obj_to_connector(obj);
2859
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002860 ret = drm_crtc_convert_umode(&mode, umode);
2861 if (ret) {
2862 DRM_DEBUG_KMS("Invalid mode\n");
2863 goto out;
2864 }
2865
Dave Airlief453ba02008-11-07 14:05:41 -08002866 ret = drm_mode_detachmode(dev, connector, &mode);
2867out:
Daniel Vetter84849902012-12-02 00:28:11 +01002868 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002869 return ret;
2870}
2871
2872struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2873 const char *name, int num_values)
2874{
2875 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002876 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002877
2878 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2879 if (!property)
2880 return NULL;
2881
2882 if (num_values) {
2883 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2884 if (!property->values)
2885 goto fail;
2886 }
2887
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002888 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2889 if (ret)
2890 goto fail;
2891
Dave Airlief453ba02008-11-07 14:05:41 -08002892 property->flags = flags;
2893 property->num_values = num_values;
2894 INIT_LIST_HEAD(&property->enum_blob_list);
2895
Vinson Lee471dd2e2011-11-10 11:55:40 -08002896 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002897 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002898 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2899 }
Dave Airlief453ba02008-11-07 14:05:41 -08002900
2901 list_add_tail(&property->head, &dev->mode_config.property_list);
2902 return property;
2903fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002904 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002905 kfree(property);
2906 return NULL;
2907}
2908EXPORT_SYMBOL(drm_property_create);
2909
Sascha Hauer4a67d392012-02-06 10:58:17 +01002910struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2911 const char *name,
2912 const struct drm_prop_enum_list *props,
2913 int num_values)
2914{
2915 struct drm_property *property;
2916 int i, ret;
2917
2918 flags |= DRM_MODE_PROP_ENUM;
2919
2920 property = drm_property_create(dev, flags, name, num_values);
2921 if (!property)
2922 return NULL;
2923
2924 for (i = 0; i < num_values; i++) {
2925 ret = drm_property_add_enum(property, i,
2926 props[i].type,
2927 props[i].name);
2928 if (ret) {
2929 drm_property_destroy(dev, property);
2930 return NULL;
2931 }
2932 }
2933
2934 return property;
2935}
2936EXPORT_SYMBOL(drm_property_create_enum);
2937
Rob Clark49e27542012-05-17 02:23:26 -06002938struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2939 int flags, const char *name,
2940 const struct drm_prop_enum_list *props,
2941 int num_values)
2942{
2943 struct drm_property *property;
2944 int i, ret;
2945
2946 flags |= DRM_MODE_PROP_BITMASK;
2947
2948 property = drm_property_create(dev, flags, name, num_values);
2949 if (!property)
2950 return NULL;
2951
2952 for (i = 0; i < num_values; i++) {
2953 ret = drm_property_add_enum(property, i,
2954 props[i].type,
2955 props[i].name);
2956 if (ret) {
2957 drm_property_destroy(dev, property);
2958 return NULL;
2959 }
2960 }
2961
2962 return property;
2963}
2964EXPORT_SYMBOL(drm_property_create_bitmask);
2965
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002966struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2967 const char *name,
2968 uint64_t min, uint64_t max)
2969{
2970 struct drm_property *property;
2971
2972 flags |= DRM_MODE_PROP_RANGE;
2973
2974 property = drm_property_create(dev, flags, name, 2);
2975 if (!property)
2976 return NULL;
2977
2978 property->values[0] = min;
2979 property->values[1] = max;
2980
2981 return property;
2982}
2983EXPORT_SYMBOL(drm_property_create_range);
2984
Dave Airlief453ba02008-11-07 14:05:41 -08002985int drm_property_add_enum(struct drm_property *property, int index,
2986 uint64_t value, const char *name)
2987{
2988 struct drm_property_enum *prop_enum;
2989
Rob Clark49e27542012-05-17 02:23:26 -06002990 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2991 return -EINVAL;
2992
2993 /*
2994 * Bitmask enum properties have the additional constraint of values
2995 * from 0 to 63
2996 */
2997 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002998 return -EINVAL;
2999
3000 if (!list_empty(&property->enum_blob_list)) {
3001 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3002 if (prop_enum->value == value) {
3003 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3004 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3005 return 0;
3006 }
3007 }
3008 }
3009
3010 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3011 if (!prop_enum)
3012 return -ENOMEM;
3013
3014 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3015 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3016 prop_enum->value = value;
3017
3018 property->values[index] = value;
3019 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3020 return 0;
3021}
3022EXPORT_SYMBOL(drm_property_add_enum);
3023
3024void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3025{
3026 struct drm_property_enum *prop_enum, *pt;
3027
3028 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3029 list_del(&prop_enum->head);
3030 kfree(prop_enum);
3031 }
3032
3033 if (property->num_values)
3034 kfree(property->values);
3035 drm_mode_object_put(dev, &property->base);
3036 list_del(&property->head);
3037 kfree(property);
3038}
3039EXPORT_SYMBOL(drm_property_destroy);
3040
Paulo Zanonic5431882012-05-15 18:09:02 -03003041void drm_object_attach_property(struct drm_mode_object *obj,
3042 struct drm_property *property,
3043 uint64_t init_val)
3044{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003045 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003046
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003047 if (count == DRM_OBJECT_MAX_PROPERTY) {
3048 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3049 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3050 "you see this message on the same object type.\n",
3051 obj->type);
3052 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003053 }
3054
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003055 obj->properties->ids[count] = property->base.id;
3056 obj->properties->values[count] = init_val;
3057 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003058}
3059EXPORT_SYMBOL(drm_object_attach_property);
3060
3061int drm_object_property_set_value(struct drm_mode_object *obj,
3062 struct drm_property *property, uint64_t val)
3063{
3064 int i;
3065
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003066 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003067 if (obj->properties->ids[i] == property->base.id) {
3068 obj->properties->values[i] = val;
3069 return 0;
3070 }
3071 }
3072
3073 return -EINVAL;
3074}
3075EXPORT_SYMBOL(drm_object_property_set_value);
3076
3077int drm_object_property_get_value(struct drm_mode_object *obj,
3078 struct drm_property *property, uint64_t *val)
3079{
3080 int i;
3081
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003082 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003083 if (obj->properties->ids[i] == property->base.id) {
3084 *val = obj->properties->values[i];
3085 return 0;
3086 }
3087 }
3088
3089 return -EINVAL;
3090}
3091EXPORT_SYMBOL(drm_object_property_get_value);
3092
Dave Airlief453ba02008-11-07 14:05:41 -08003093int drm_mode_getproperty_ioctl(struct drm_device *dev,
3094 void *data, struct drm_file *file_priv)
3095{
3096 struct drm_mode_object *obj;
3097 struct drm_mode_get_property *out_resp = data;
3098 struct drm_property *property;
3099 int enum_count = 0;
3100 int blob_count = 0;
3101 int value_count = 0;
3102 int ret = 0, i;
3103 int copied;
3104 struct drm_property_enum *prop_enum;
3105 struct drm_mode_property_enum __user *enum_ptr;
3106 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003107 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003108 uint64_t __user *values_ptr;
3109 uint32_t __user *blob_length_ptr;
3110
Dave Airliefb3b06c2011-02-08 13:55:21 +10003111 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3112 return -EINVAL;
3113
Daniel Vetter84849902012-12-02 00:28:11 +01003114 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003115 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3116 if (!obj) {
3117 ret = -EINVAL;
3118 goto done;
3119 }
3120 property = obj_to_property(obj);
3121
Rob Clark49e27542012-05-17 02:23:26 -06003122 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003123 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3124 enum_count++;
3125 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3126 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3127 blob_count++;
3128 }
3129
3130 value_count = property->num_values;
3131
3132 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3133 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3134 out_resp->flags = property->flags;
3135
3136 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003137 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003138 for (i = 0; i < value_count; i++) {
3139 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3140 ret = -EFAULT;
3141 goto done;
3142 }
3143 }
3144 }
3145 out_resp->count_values = value_count;
3146
Rob Clark49e27542012-05-17 02:23:26 -06003147 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003148 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3149 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003150 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003151 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3152
3153 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3154 ret = -EFAULT;
3155 goto done;
3156 }
3157
3158 if (copy_to_user(&enum_ptr[copied].name,
3159 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3160 ret = -EFAULT;
3161 goto done;
3162 }
3163 copied++;
3164 }
3165 }
3166 out_resp->count_enum_blobs = enum_count;
3167 }
3168
3169 if (property->flags & DRM_MODE_PROP_BLOB) {
3170 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3171 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003172 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3173 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003174
3175 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3176 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3177 ret = -EFAULT;
3178 goto done;
3179 }
3180
3181 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3182 ret = -EFAULT;
3183 goto done;
3184 }
3185
3186 copied++;
3187 }
3188 }
3189 out_resp->count_enum_blobs = blob_count;
3190 }
3191done:
Daniel Vetter84849902012-12-02 00:28:11 +01003192 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003193 return ret;
3194}
3195
3196static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3197 void *data)
3198{
3199 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003200 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003201
3202 if (!length || !data)
3203 return NULL;
3204
3205 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3206 if (!blob)
3207 return NULL;
3208
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003209 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3210 if (ret) {
3211 kfree(blob);
3212 return NULL;
3213 }
3214
Dave Airlief453ba02008-11-07 14:05:41 -08003215 blob->length = length;
3216
3217 memcpy(blob->data, data, length);
3218
Dave Airlief453ba02008-11-07 14:05:41 -08003219 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3220 return blob;
3221}
3222
3223static void drm_property_destroy_blob(struct drm_device *dev,
3224 struct drm_property_blob *blob)
3225{
3226 drm_mode_object_put(dev, &blob->base);
3227 list_del(&blob->head);
3228 kfree(blob);
3229}
3230
3231int drm_mode_getblob_ioctl(struct drm_device *dev,
3232 void *data, struct drm_file *file_priv)
3233{
3234 struct drm_mode_object *obj;
3235 struct drm_mode_get_blob *out_resp = data;
3236 struct drm_property_blob *blob;
3237 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003238 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003239
Dave Airliefb3b06c2011-02-08 13:55:21 +10003240 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3241 return -EINVAL;
3242
Daniel Vetter84849902012-12-02 00:28:11 +01003243 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003244 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3245 if (!obj) {
3246 ret = -EINVAL;
3247 goto done;
3248 }
3249 blob = obj_to_blob(obj);
3250
3251 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003252 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003253 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3254 ret = -EFAULT;
3255 goto done;
3256 }
3257 }
3258 out_resp->length = blob->length;
3259
3260done:
Daniel Vetter84849902012-12-02 00:28:11 +01003261 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003262 return ret;
3263}
3264
3265int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3266 struct edid *edid)
3267{
3268 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003269 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003270
3271 if (connector->edid_blob_ptr)
3272 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3273
3274 /* Delete edid, when there is none. */
3275 if (!edid) {
3276 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003277 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003278 return ret;
3279 }
3280
Adam Jackson7466f4c2010-03-29 21:43:23 +00003281 size = EDID_LENGTH * (1 + edid->extensions);
3282 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3283 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003284 if (!connector->edid_blob_ptr)
3285 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003286
Rob Clark58495562012-10-11 20:50:56 -05003287 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003288 dev->mode_config.edid_property,
3289 connector->edid_blob_ptr->base.id);
3290
3291 return ret;
3292}
3293EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3294
Paulo Zanoni26a34812012-05-15 18:08:59 -03003295static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003296 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003297{
3298 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3299 return false;
3300 if (property->flags & DRM_MODE_PROP_RANGE) {
3301 if (value < property->values[0] || value > property->values[1])
3302 return false;
3303 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003304 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3305 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003306 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003307 for (i = 0; i < property->num_values; i++)
3308 valid_mask |= (1ULL << property->values[i]);
3309 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003310 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3311 /* Only the driver knows */
3312 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003313 } else {
3314 int i;
3315 for (i = 0; i < property->num_values; i++)
3316 if (property->values[i] == value)
3317 return true;
3318 return false;
3319 }
3320}
3321
Dave Airlief453ba02008-11-07 14:05:41 -08003322int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3323 void *data, struct drm_file *file_priv)
3324{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003325 struct drm_mode_connector_set_property *conn_set_prop = data;
3326 struct drm_mode_obj_set_property obj_set_prop = {
3327 .value = conn_set_prop->value,
3328 .prop_id = conn_set_prop->prop_id,
3329 .obj_id = conn_set_prop->connector_id,
3330 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3331 };
Dave Airlief453ba02008-11-07 14:05:41 -08003332
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003333 /* It does all the locking and checking we need */
3334 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003335}
3336
Paulo Zanonic5431882012-05-15 18:09:02 -03003337static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3338 struct drm_property *property,
3339 uint64_t value)
3340{
3341 int ret = -EINVAL;
3342 struct drm_connector *connector = obj_to_connector(obj);
3343
3344 /* Do DPMS ourselves */
3345 if (property == connector->dev->mode_config.dpms_property) {
3346 if (connector->funcs->dpms)
3347 (*connector->funcs->dpms)(connector, (int)value);
3348 ret = 0;
3349 } else if (connector->funcs->set_property)
3350 ret = connector->funcs->set_property(connector, property, value);
3351
3352 /* store the property value if successful */
3353 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003354 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003355 return ret;
3356}
3357
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003358static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3359 struct drm_property *property,
3360 uint64_t value)
3361{
3362 int ret = -EINVAL;
3363 struct drm_crtc *crtc = obj_to_crtc(obj);
3364
3365 if (crtc->funcs->set_property)
3366 ret = crtc->funcs->set_property(crtc, property, value);
3367 if (!ret)
3368 drm_object_property_set_value(obj, property, value);
3369
3370 return ret;
3371}
3372
Rob Clark4d939142012-05-17 02:23:27 -06003373static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3374 struct drm_property *property,
3375 uint64_t value)
3376{
3377 int ret = -EINVAL;
3378 struct drm_plane *plane = obj_to_plane(obj);
3379
3380 if (plane->funcs->set_property)
3381 ret = plane->funcs->set_property(plane, property, value);
3382 if (!ret)
3383 drm_object_property_set_value(obj, property, value);
3384
3385 return ret;
3386}
3387
Paulo Zanonic5431882012-05-15 18:09:02 -03003388int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3389 struct drm_file *file_priv)
3390{
3391 struct drm_mode_obj_get_properties *arg = data;
3392 struct drm_mode_object *obj;
3393 int ret = 0;
3394 int i;
3395 int copied = 0;
3396 int props_count = 0;
3397 uint32_t __user *props_ptr;
3398 uint64_t __user *prop_values_ptr;
3399
3400 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3401 return -EINVAL;
3402
Daniel Vetter84849902012-12-02 00:28:11 +01003403 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003404
3405 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3406 if (!obj) {
3407 ret = -EINVAL;
3408 goto out;
3409 }
3410 if (!obj->properties) {
3411 ret = -EINVAL;
3412 goto out;
3413 }
3414
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003415 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003416
3417 /* This ioctl is called twice, once to determine how much space is
3418 * needed, and the 2nd time to fill it. */
3419 if ((arg->count_props >= props_count) && props_count) {
3420 copied = 0;
3421 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3422 prop_values_ptr = (uint64_t __user *)(unsigned long)
3423 (arg->prop_values_ptr);
3424 for (i = 0; i < props_count; i++) {
3425 if (put_user(obj->properties->ids[i],
3426 props_ptr + copied)) {
3427 ret = -EFAULT;
3428 goto out;
3429 }
3430 if (put_user(obj->properties->values[i],
3431 prop_values_ptr + copied)) {
3432 ret = -EFAULT;
3433 goto out;
3434 }
3435 copied++;
3436 }
3437 }
3438 arg->count_props = props_count;
3439out:
Daniel Vetter84849902012-12-02 00:28:11 +01003440 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003441 return ret;
3442}
3443
3444int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3445 struct drm_file *file_priv)
3446{
3447 struct drm_mode_obj_set_property *arg = data;
3448 struct drm_mode_object *arg_obj;
3449 struct drm_mode_object *prop_obj;
3450 struct drm_property *property;
3451 int ret = -EINVAL;
3452 int i;
3453
3454 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3455 return -EINVAL;
3456
Daniel Vetter84849902012-12-02 00:28:11 +01003457 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003458
3459 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3460 if (!arg_obj)
3461 goto out;
3462 if (!arg_obj->properties)
3463 goto out;
3464
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003465 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003466 if (arg_obj->properties->ids[i] == arg->prop_id)
3467 break;
3468
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003469 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003470 goto out;
3471
3472 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3473 DRM_MODE_OBJECT_PROPERTY);
3474 if (!prop_obj)
3475 goto out;
3476 property = obj_to_property(prop_obj);
3477
3478 if (!drm_property_change_is_valid(property, arg->value))
3479 goto out;
3480
3481 switch (arg_obj->type) {
3482 case DRM_MODE_OBJECT_CONNECTOR:
3483 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3484 arg->value);
3485 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003486 case DRM_MODE_OBJECT_CRTC:
3487 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3488 break;
Rob Clark4d939142012-05-17 02:23:27 -06003489 case DRM_MODE_OBJECT_PLANE:
3490 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3491 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003492 }
3493
3494out:
Daniel Vetter84849902012-12-02 00:28:11 +01003495 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003496 return ret;
3497}
3498
Dave Airlief453ba02008-11-07 14:05:41 -08003499int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3500 struct drm_encoder *encoder)
3501{
3502 int i;
3503
3504 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3505 if (connector->encoder_ids[i] == 0) {
3506 connector->encoder_ids[i] = encoder->base.id;
3507 return 0;
3508 }
3509 }
3510 return -ENOMEM;
3511}
3512EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3513
3514void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3515 struct drm_encoder *encoder)
3516{
3517 int i;
3518 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3519 if (connector->encoder_ids[i] == encoder->base.id) {
3520 connector->encoder_ids[i] = 0;
3521 if (connector->encoder == encoder)
3522 connector->encoder = NULL;
3523 break;
3524 }
3525 }
3526}
3527EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3528
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003529int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003530 int gamma_size)
3531{
3532 crtc->gamma_size = gamma_size;
3533
3534 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3535 if (!crtc->gamma_store) {
3536 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003537 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003538 }
3539
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003540 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003541}
3542EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3543
3544int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3545 void *data, struct drm_file *file_priv)
3546{
3547 struct drm_mode_crtc_lut *crtc_lut = data;
3548 struct drm_mode_object *obj;
3549 struct drm_crtc *crtc;
3550 void *r_base, *g_base, *b_base;
3551 int size;
3552 int ret = 0;
3553
Dave Airliefb3b06c2011-02-08 13:55:21 +10003554 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3555 return -EINVAL;
3556
Daniel Vetter84849902012-12-02 00:28:11 +01003557 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003558 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3559 if (!obj) {
3560 ret = -EINVAL;
3561 goto out;
3562 }
3563 crtc = obj_to_crtc(obj);
3564
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003565 if (crtc->funcs->gamma_set == NULL) {
3566 ret = -ENOSYS;
3567 goto out;
3568 }
3569
Dave Airlief453ba02008-11-07 14:05:41 -08003570 /* memcpy into gamma store */
3571 if (crtc_lut->gamma_size != crtc->gamma_size) {
3572 ret = -EINVAL;
3573 goto out;
3574 }
3575
3576 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3577 r_base = crtc->gamma_store;
3578 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3579 ret = -EFAULT;
3580 goto out;
3581 }
3582
3583 g_base = r_base + size;
3584 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3585 ret = -EFAULT;
3586 goto out;
3587 }
3588
3589 b_base = g_base + size;
3590 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3591 ret = -EFAULT;
3592 goto out;
3593 }
3594
James Simmons72034252010-08-03 01:33:19 +01003595 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003596
3597out:
Daniel Vetter84849902012-12-02 00:28:11 +01003598 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003599 return ret;
3600
3601}
3602
3603int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3604 void *data, struct drm_file *file_priv)
3605{
3606 struct drm_mode_crtc_lut *crtc_lut = data;
3607 struct drm_mode_object *obj;
3608 struct drm_crtc *crtc;
3609 void *r_base, *g_base, *b_base;
3610 int size;
3611 int ret = 0;
3612
Dave Airliefb3b06c2011-02-08 13:55:21 +10003613 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3614 return -EINVAL;
3615
Daniel Vetter84849902012-12-02 00:28:11 +01003616 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003617 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3618 if (!obj) {
3619 ret = -EINVAL;
3620 goto out;
3621 }
3622 crtc = obj_to_crtc(obj);
3623
3624 /* memcpy into gamma store */
3625 if (crtc_lut->gamma_size != crtc->gamma_size) {
3626 ret = -EINVAL;
3627 goto out;
3628 }
3629
3630 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3631 r_base = crtc->gamma_store;
3632 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3633 ret = -EFAULT;
3634 goto out;
3635 }
3636
3637 g_base = r_base + size;
3638 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3639 ret = -EFAULT;
3640 goto out;
3641 }
3642
3643 b_base = g_base + size;
3644 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3645 ret = -EFAULT;
3646 goto out;
3647 }
3648out:
Daniel Vetter84849902012-12-02 00:28:11 +01003649 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003650 return ret;
3651}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003652
3653int drm_mode_page_flip_ioctl(struct drm_device *dev,
3654 void *data, struct drm_file *file_priv)
3655{
3656 struct drm_mode_crtc_page_flip *page_flip = data;
3657 struct drm_mode_object *obj;
3658 struct drm_crtc *crtc;
3659 struct drm_framebuffer *fb;
3660 struct drm_pending_vblank_event *e = NULL;
3661 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003662 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003663 int ret = -EINVAL;
3664
3665 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3666 page_flip->reserved != 0)
3667 return -EINVAL;
3668
Daniel Vetter84849902012-12-02 00:28:11 +01003669 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003670 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3671 if (!obj)
3672 goto out;
3673 crtc = obj_to_crtc(obj);
3674
Chris Wilson90c1efd2010-07-17 20:23:26 +01003675 if (crtc->fb == NULL) {
3676 /* The framebuffer is currently unbound, presumably
3677 * due to a hotplug event, that userspace has not
3678 * yet discovered.
3679 */
3680 ret = -EBUSY;
3681 goto out;
3682 }
3683
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003684 if (crtc->funcs->page_flip == NULL)
3685 goto out;
3686
Daniel Vetter786b99e2012-12-02 21:53:40 +01003687 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3688 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003689 goto out;
Daniel Vetter786b99e2012-12-02 21:53:40 +01003690 /* fb is protect by the mode_config lock, so drop the ref immediately */
3691 drm_framebuffer_unreference(fb);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003692
Rob Clark7c80e122012-09-04 16:35:56 +00003693 hdisplay = crtc->mode.hdisplay;
3694 vdisplay = crtc->mode.vdisplay;
3695
3696 if (crtc->invert_dimensions)
3697 swap(hdisplay, vdisplay);
3698
3699 if (hdisplay > fb->width ||
3700 vdisplay > fb->height ||
3701 crtc->x > fb->width - hdisplay ||
3702 crtc->y > fb->height - vdisplay) {
3703 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3704 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3705 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003706 ret = -ENOSPC;
3707 goto out;
3708 }
3709
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003710 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3711 ret = -ENOMEM;
3712 spin_lock_irqsave(&dev->event_lock, flags);
3713 if (file_priv->event_space < sizeof e->event) {
3714 spin_unlock_irqrestore(&dev->event_lock, flags);
3715 goto out;
3716 }
3717 file_priv->event_space -= sizeof e->event;
3718 spin_unlock_irqrestore(&dev->event_lock, flags);
3719
3720 e = kzalloc(sizeof *e, GFP_KERNEL);
3721 if (e == NULL) {
3722 spin_lock_irqsave(&dev->event_lock, flags);
3723 file_priv->event_space += sizeof e->event;
3724 spin_unlock_irqrestore(&dev->event_lock, flags);
3725 goto out;
3726 }
3727
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003728 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003729 e->event.base.length = sizeof e->event;
3730 e->event.user_data = page_flip->user_data;
3731 e->base.event = &e->event.base;
3732 e->base.file_priv = file_priv;
3733 e->base.destroy =
3734 (void (*) (struct drm_pending_event *)) kfree;
3735 }
3736
3737 ret = crtc->funcs->page_flip(crtc, fb, e);
3738 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003739 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3740 spin_lock_irqsave(&dev->event_lock, flags);
3741 file_priv->event_space += sizeof e->event;
3742 spin_unlock_irqrestore(&dev->event_lock, flags);
3743 kfree(e);
3744 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003745 }
3746
3747out:
Daniel Vetter84849902012-12-02 00:28:11 +01003748 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003749 return ret;
3750}
Chris Wilsoneb033552011-01-24 15:11:08 +00003751
3752void drm_mode_config_reset(struct drm_device *dev)
3753{
3754 struct drm_crtc *crtc;
3755 struct drm_encoder *encoder;
3756 struct drm_connector *connector;
3757
3758 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3759 if (crtc->funcs->reset)
3760 crtc->funcs->reset(crtc);
3761
3762 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3763 if (encoder->funcs->reset)
3764 encoder->funcs->reset(encoder);
3765
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003766 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3767 connector->status = connector_status_unknown;
3768
Chris Wilsoneb033552011-01-24 15:11:08 +00003769 if (connector->funcs->reset)
3770 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003771 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003772}
3773EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003774
3775int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3776 void *data, struct drm_file *file_priv)
3777{
3778 struct drm_mode_create_dumb *args = data;
3779
3780 if (!dev->driver->dumb_create)
3781 return -ENOSYS;
3782 return dev->driver->dumb_create(file_priv, dev, args);
3783}
3784
3785int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3786 void *data, struct drm_file *file_priv)
3787{
3788 struct drm_mode_map_dumb *args = data;
3789
3790 /* call driver ioctl to get mmap offset */
3791 if (!dev->driver->dumb_map_offset)
3792 return -ENOSYS;
3793
3794 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3795}
3796
3797int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3798 void *data, struct drm_file *file_priv)
3799{
3800 struct drm_mode_destroy_dumb *args = data;
3801
3802 if (!dev->driver->dumb_destroy)
3803 return -ENOSYS;
3804
3805 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3806}
Dave Airlie248dbc22011-11-29 20:02:54 +00003807
3808/*
3809 * Just need to support RGB formats here for compat with code that doesn't
3810 * use pixel formats directly yet.
3811 */
3812void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3813 int *bpp)
3814{
3815 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003816 case DRM_FORMAT_RGB332:
3817 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003818 *depth = 8;
3819 *bpp = 8;
3820 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003821 case DRM_FORMAT_XRGB1555:
3822 case DRM_FORMAT_XBGR1555:
3823 case DRM_FORMAT_RGBX5551:
3824 case DRM_FORMAT_BGRX5551:
3825 case DRM_FORMAT_ARGB1555:
3826 case DRM_FORMAT_ABGR1555:
3827 case DRM_FORMAT_RGBA5551:
3828 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003829 *depth = 15;
3830 *bpp = 16;
3831 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003832 case DRM_FORMAT_RGB565:
3833 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003834 *depth = 16;
3835 *bpp = 16;
3836 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003837 case DRM_FORMAT_RGB888:
3838 case DRM_FORMAT_BGR888:
3839 *depth = 24;
3840 *bpp = 24;
3841 break;
3842 case DRM_FORMAT_XRGB8888:
3843 case DRM_FORMAT_XBGR8888:
3844 case DRM_FORMAT_RGBX8888:
3845 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003846 *depth = 24;
3847 *bpp = 32;
3848 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003849 case DRM_FORMAT_XRGB2101010:
3850 case DRM_FORMAT_XBGR2101010:
3851 case DRM_FORMAT_RGBX1010102:
3852 case DRM_FORMAT_BGRX1010102:
3853 case DRM_FORMAT_ARGB2101010:
3854 case DRM_FORMAT_ABGR2101010:
3855 case DRM_FORMAT_RGBA1010102:
3856 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003857 *depth = 30;
3858 *bpp = 32;
3859 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003860 case DRM_FORMAT_ARGB8888:
3861 case DRM_FORMAT_ABGR8888:
3862 case DRM_FORMAT_RGBA8888:
3863 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003864 *depth = 32;
3865 *bpp = 32;
3866 break;
3867 default:
3868 DRM_DEBUG_KMS("unsupported pixel format\n");
3869 *depth = 0;
3870 *bpp = 0;
3871 break;
3872 }
3873}
3874EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003875
3876/**
3877 * drm_format_num_planes - get the number of planes for format
3878 * @format: pixel format (DRM_FORMAT_*)
3879 *
3880 * RETURNS:
3881 * The number of planes used by the specified pixel format.
3882 */
3883int drm_format_num_planes(uint32_t format)
3884{
3885 switch (format) {
3886 case DRM_FORMAT_YUV410:
3887 case DRM_FORMAT_YVU410:
3888 case DRM_FORMAT_YUV411:
3889 case DRM_FORMAT_YVU411:
3890 case DRM_FORMAT_YUV420:
3891 case DRM_FORMAT_YVU420:
3892 case DRM_FORMAT_YUV422:
3893 case DRM_FORMAT_YVU422:
3894 case DRM_FORMAT_YUV444:
3895 case DRM_FORMAT_YVU444:
3896 return 3;
3897 case DRM_FORMAT_NV12:
3898 case DRM_FORMAT_NV21:
3899 case DRM_FORMAT_NV16:
3900 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003901 case DRM_FORMAT_NV24:
3902 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003903 return 2;
3904 default:
3905 return 1;
3906 }
3907}
3908EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003909
3910/**
3911 * drm_format_plane_cpp - determine the bytes per pixel value
3912 * @format: pixel format (DRM_FORMAT_*)
3913 * @plane: plane index
3914 *
3915 * RETURNS:
3916 * The bytes per pixel value for the specified plane.
3917 */
3918int drm_format_plane_cpp(uint32_t format, int plane)
3919{
3920 unsigned int depth;
3921 int bpp;
3922
3923 if (plane >= drm_format_num_planes(format))
3924 return 0;
3925
3926 switch (format) {
3927 case DRM_FORMAT_YUYV:
3928 case DRM_FORMAT_YVYU:
3929 case DRM_FORMAT_UYVY:
3930 case DRM_FORMAT_VYUY:
3931 return 2;
3932 case DRM_FORMAT_NV12:
3933 case DRM_FORMAT_NV21:
3934 case DRM_FORMAT_NV16:
3935 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003936 case DRM_FORMAT_NV24:
3937 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003938 return plane ? 2 : 1;
3939 case DRM_FORMAT_YUV410:
3940 case DRM_FORMAT_YVU410:
3941 case DRM_FORMAT_YUV411:
3942 case DRM_FORMAT_YVU411:
3943 case DRM_FORMAT_YUV420:
3944 case DRM_FORMAT_YVU420:
3945 case DRM_FORMAT_YUV422:
3946 case DRM_FORMAT_YVU422:
3947 case DRM_FORMAT_YUV444:
3948 case DRM_FORMAT_YVU444:
3949 return 1;
3950 default:
3951 drm_fb_get_bpp_depth(format, &depth, &bpp);
3952 return bpp >> 3;
3953 }
3954}
3955EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003956
3957/**
3958 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3959 * @format: pixel format (DRM_FORMAT_*)
3960 *
3961 * RETURNS:
3962 * The horizontal chroma subsampling factor for the
3963 * specified pixel format.
3964 */
3965int drm_format_horz_chroma_subsampling(uint32_t format)
3966{
3967 switch (format) {
3968 case DRM_FORMAT_YUV411:
3969 case DRM_FORMAT_YVU411:
3970 case DRM_FORMAT_YUV410:
3971 case DRM_FORMAT_YVU410:
3972 return 4;
3973 case DRM_FORMAT_YUYV:
3974 case DRM_FORMAT_YVYU:
3975 case DRM_FORMAT_UYVY:
3976 case DRM_FORMAT_VYUY:
3977 case DRM_FORMAT_NV12:
3978 case DRM_FORMAT_NV21:
3979 case DRM_FORMAT_NV16:
3980 case DRM_FORMAT_NV61:
3981 case DRM_FORMAT_YUV422:
3982 case DRM_FORMAT_YVU422:
3983 case DRM_FORMAT_YUV420:
3984 case DRM_FORMAT_YVU420:
3985 return 2;
3986 default:
3987 return 1;
3988 }
3989}
3990EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3991
3992/**
3993 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3994 * @format: pixel format (DRM_FORMAT_*)
3995 *
3996 * RETURNS:
3997 * The vertical chroma subsampling factor for the
3998 * specified pixel format.
3999 */
4000int drm_format_vert_chroma_subsampling(uint32_t format)
4001{
4002 switch (format) {
4003 case DRM_FORMAT_YUV410:
4004 case DRM_FORMAT_YVU410:
4005 return 4;
4006 case DRM_FORMAT_YUV420:
4007 case DRM_FORMAT_YVU420:
4008 case DRM_FORMAT_NV12:
4009 case DRM_FORMAT_NV21:
4010 return 2;
4011 default:
4012 return 1;
4013 }
4014}
4015EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);