blob: a50f7553b31da64f13b68d577f9344062a22376f [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
448/* dev->mode_config.fb_lock must be held! */
449static void __drm_framebuffer_unregister(struct drm_device *dev,
450 struct drm_framebuffer *fb)
451{
452 mutex_lock(&dev->mode_config.idr_mutex);
453 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
454 mutex_unlock(&dev->mode_config.idr_mutex);
455
456 fb->base.id = 0;
457
458 kref_put(&fb->refcount, drm_framebuffer_free_bug);
459}
460
Dave Airlief453ba02008-11-07 14:05:41 -0800461/**
Daniel Vetter36206362012-12-10 20:42:17 +0100462 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
463 * @fb: fb to unregister
464 *
465 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
466 * those used for fbdev. Note that the caller must hold a reference of it's own,
467 * i.e. the object may not be destroyed through this call (since it'll lead to a
468 * locking inversion).
469 */
470void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
471{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100472 struct drm_device *dev = fb->dev;
473
474 mutex_lock(&dev->mode_config.fb_lock);
475 /* Mark fb as reaped and drop idr ref. */
476 __drm_framebuffer_unregister(dev, fb);
477 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100478}
479EXPORT_SYMBOL(drm_framebuffer_unregister_private);
480
481/**
Dave Airlief453ba02008-11-07 14:05:41 -0800482 * drm_framebuffer_cleanup - remove a framebuffer object
483 * @fb: framebuffer to remove
484 *
Daniel Vetter36206362012-12-10 20:42:17 +0100485 * Cleanup references to a user-created framebuffer. This function is intended
486 * to be used from the drivers ->destroy callback.
487 *
488 * Note that this function does not remove the fb from active usuage - if it is
489 * still used anywhere, hilarity can ensue since userspace could call getfb on
490 * the id and get back -EINVAL. Obviously no concern at driver unload time.
491 *
492 * Also, the framebuffer will not be removed from the lookup idr - for
493 * user-created framebuffers this will happen in in the rmfb ioctl. For
494 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
495 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800496 */
497void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
498{
499 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100500
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100501 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000502 list_del(&fb->head);
503 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100504 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000505}
506EXPORT_SYMBOL(drm_framebuffer_cleanup);
507
508/**
509 * drm_framebuffer_remove - remove and unreference a framebuffer object
510 * @fb: framebuffer to remove
511 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000512 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100513 * using @fb, removes it, setting it to NULL. Then drops the reference to the
514 * passed-in framebuffer.
Rob Clarkf7eff602012-09-05 21:48:38 +0000515 */
516void drm_framebuffer_remove(struct drm_framebuffer *fb)
517{
518 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800519 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800520 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000521 struct drm_mode_set set;
522 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800523
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100524 WARN_ON(!drm_modeset_is_locked(dev));
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100525 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100526
Dave Airlief453ba02008-11-07 14:05:41 -0800527 /* remove from any CRTC */
528 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000529 if (crtc->fb == fb) {
530 /* should turn off the crtc */
531 memset(&set, 0, sizeof(struct drm_mode_set));
532 set.crtc = crtc;
533 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100534 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000535 if (ret)
536 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
537 }
Dave Airlief453ba02008-11-07 14:05:41 -0800538 }
539
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800540 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
541 if (plane->fb == fb) {
542 /* should turn off the crtc */
543 ret = plane->funcs->disable_plane(plane);
544 if (ret)
545 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600546 /* disconnect the plane from the fb and crtc: */
547 plane->fb = NULL;
548 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800549 }
550 }
551
Rob Clarkf7eff602012-09-05 21:48:38 +0000552 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800553}
Rob Clarkf7eff602012-09-05 21:48:38 +0000554EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800555
556/**
557 * drm_crtc_init - Initialise a new CRTC object
558 * @dev: DRM device
559 * @crtc: CRTC object to init
560 * @funcs: callbacks for the new CRTC
561 *
Dave Airlief453ba02008-11-07 14:05:41 -0800562 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200563 *
564 * RETURNS:
565 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800566 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200567int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800568 const struct drm_crtc_funcs *funcs)
569{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200570 int ret;
571
Dave Airlief453ba02008-11-07 14:05:41 -0800572 crtc->dev = dev;
573 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000574 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800575
Daniel Vetter84849902012-12-02 00:28:11 +0100576 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100577 mutex_init(&crtc->mutex);
578 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200579
580 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
581 if (ret)
582 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800583
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300584 crtc->base.properties = &crtc->properties;
585
Dave Airlief453ba02008-11-07 14:05:41 -0800586 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
587 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200588
589 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100590 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200591
592 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800593}
594EXPORT_SYMBOL(drm_crtc_init);
595
596/**
597 * drm_crtc_cleanup - Cleans up the core crtc usage.
598 * @crtc: CRTC to cleanup
599 *
Dave Airlief453ba02008-11-07 14:05:41 -0800600 * Cleanup @crtc. Removes from drm modesetting space
601 * does NOT free object, caller does that.
602 */
603void drm_crtc_cleanup(struct drm_crtc *crtc)
604{
605 struct drm_device *dev = crtc->dev;
606
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000607 kfree(crtc->gamma_store);
608 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800609
610 drm_mode_object_put(dev, &crtc->base);
611 list_del(&crtc->head);
612 dev->mode_config.num_crtc--;
613}
614EXPORT_SYMBOL(drm_crtc_cleanup);
615
616/**
617 * drm_mode_probed_add - add a mode to a connector's probed mode list
618 * @connector: connector the new mode
619 * @mode: mode data
620 *
Dave Airlief453ba02008-11-07 14:05:41 -0800621 * Add @mode to @connector's mode list for later use.
622 */
623void drm_mode_probed_add(struct drm_connector *connector,
624 struct drm_display_mode *mode)
625{
626 list_add(&mode->head, &connector->probed_modes);
627}
628EXPORT_SYMBOL(drm_mode_probed_add);
629
630/**
631 * drm_mode_remove - remove and free a mode
632 * @connector: connector list to modify
633 * @mode: mode to remove
634 *
Dave Airlief453ba02008-11-07 14:05:41 -0800635 * Remove @mode from @connector's mode list, then free it.
636 */
637void drm_mode_remove(struct drm_connector *connector,
638 struct drm_display_mode *mode)
639{
640 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100641 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800642}
643EXPORT_SYMBOL(drm_mode_remove);
644
645/**
646 * drm_connector_init - Init a preallocated connector
647 * @dev: DRM device
648 * @connector: the connector to init
649 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100650 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800651 *
Dave Airlief453ba02008-11-07 14:05:41 -0800652 * Initialises a preallocated connector. Connectors should be
653 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200654 *
655 * RETURNS:
656 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800657 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200658int drm_connector_init(struct drm_device *dev,
659 struct drm_connector *connector,
660 const struct drm_connector_funcs *funcs,
661 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800662{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200663 int ret;
664
Daniel Vetter84849902012-12-02 00:28:11 +0100665 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800666
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200667 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
668 if (ret)
669 goto out;
670
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300671 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800672 connector->dev = dev;
673 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800674 connector->connector_type = connector_type;
675 connector->connector_type_id =
676 ++drm_connector_enum_list[connector_type].count; /* TODO */
677 INIT_LIST_HEAD(&connector->user_modes);
678 INIT_LIST_HEAD(&connector->probed_modes);
679 INIT_LIST_HEAD(&connector->modes);
680 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000681 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800682
683 list_add_tail(&connector->head, &dev->mode_config.connector_list);
684 dev->mode_config.num_connector++;
685
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200686 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500687 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200688 dev->mode_config.edid_property,
689 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800690
Rob Clark58495562012-10-11 20:50:56 -0500691 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800692 dev->mode_config.dpms_property, 0);
693
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200694 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100695 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200696
697 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800698}
699EXPORT_SYMBOL(drm_connector_init);
700
701/**
702 * drm_connector_cleanup - cleans up an initialised connector
703 * @connector: connector to cleanup
704 *
Dave Airlief453ba02008-11-07 14:05:41 -0800705 * Cleans up the connector but doesn't free the object.
706 */
707void drm_connector_cleanup(struct drm_connector *connector)
708{
709 struct drm_device *dev = connector->dev;
710 struct drm_display_mode *mode, *t;
711
712 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
713 drm_mode_remove(connector, mode);
714
715 list_for_each_entry_safe(mode, t, &connector->modes, head)
716 drm_mode_remove(connector, mode);
717
718 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
719 drm_mode_remove(connector, mode);
720
Dave Airlief453ba02008-11-07 14:05:41 -0800721 drm_mode_object_put(dev, &connector->base);
722 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000723 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800724}
725EXPORT_SYMBOL(drm_connector_cleanup);
726
Dave Airliecbc7e222012-02-20 14:16:40 +0000727void drm_connector_unplug_all(struct drm_device *dev)
728{
729 struct drm_connector *connector;
730
731 /* taking the mode config mutex ends up in a clash with sysfs */
732 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
733 drm_sysfs_connector_remove(connector);
734
735}
736EXPORT_SYMBOL(drm_connector_unplug_all);
737
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200738int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000739 struct drm_encoder *encoder,
740 const struct drm_encoder_funcs *funcs,
741 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800742{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200743 int ret;
744
Daniel Vetter84849902012-12-02 00:28:11 +0100745 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800746
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200747 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
748 if (ret)
749 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800750
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200751 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800752 encoder->encoder_type = encoder_type;
753 encoder->funcs = funcs;
754
755 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
756 dev->mode_config.num_encoder++;
757
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200758 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100759 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760
761 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800762}
763EXPORT_SYMBOL(drm_encoder_init);
764
765void drm_encoder_cleanup(struct drm_encoder *encoder)
766{
767 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100768 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800769 drm_mode_object_put(dev, &encoder->base);
770 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000771 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100772 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800773}
774EXPORT_SYMBOL(drm_encoder_cleanup);
775
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800776int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
777 unsigned long possible_crtcs,
778 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600779 const uint32_t *formats, uint32_t format_count,
780 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800781{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200782 int ret;
783
Daniel Vetter84849902012-12-02 00:28:11 +0100784 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800785
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200786 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
787 if (ret)
788 goto out;
789
Rob Clark4d939142012-05-17 02:23:27 -0600790 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800791 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800792 plane->funcs = funcs;
793 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
794 GFP_KERNEL);
795 if (!plane->format_types) {
796 DRM_DEBUG_KMS("out of memory when allocating plane\n");
797 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200798 ret = -ENOMEM;
799 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800800 }
801
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800802 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800803 plane->format_count = format_count;
804 plane->possible_crtcs = possible_crtcs;
805
Rob Clark0a7eb242011-12-13 20:19:36 -0600806 /* private planes are not exposed to userspace, but depending on
807 * display hardware, might be convenient to allow sharing programming
808 * for the scanout engine with the crtc implementation.
809 */
810 if (!priv) {
811 list_add_tail(&plane->head, &dev->mode_config.plane_list);
812 dev->mode_config.num_plane++;
813 } else {
814 INIT_LIST_HEAD(&plane->head);
815 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800816
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200817 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100818 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800819
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200820 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800821}
822EXPORT_SYMBOL(drm_plane_init);
823
824void drm_plane_cleanup(struct drm_plane *plane)
825{
826 struct drm_device *dev = plane->dev;
827
Daniel Vetter84849902012-12-02 00:28:11 +0100828 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800829 kfree(plane->format_types);
830 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600831 /* if not added to a list, it must be a private plane */
832 if (!list_empty(&plane->head)) {
833 list_del(&plane->head);
834 dev->mode_config.num_plane--;
835 }
Daniel Vetter84849902012-12-02 00:28:11 +0100836 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800837}
838EXPORT_SYMBOL(drm_plane_cleanup);
839
Dave Airlief453ba02008-11-07 14:05:41 -0800840/**
841 * drm_mode_create - create a new display mode
842 * @dev: DRM device
843 *
Dave Airlief453ba02008-11-07 14:05:41 -0800844 * Create a new drm_display_mode, give it an ID, and return it.
845 *
846 * RETURNS:
847 * Pointer to new mode on success, NULL on error.
848 */
849struct drm_display_mode *drm_mode_create(struct drm_device *dev)
850{
851 struct drm_display_mode *nmode;
852
853 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
854 if (!nmode)
855 return NULL;
856
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200857 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
858 kfree(nmode);
859 return NULL;
860 }
861
Dave Airlief453ba02008-11-07 14:05:41 -0800862 return nmode;
863}
864EXPORT_SYMBOL(drm_mode_create);
865
866/**
867 * drm_mode_destroy - remove a mode
868 * @dev: DRM device
869 * @mode: mode to remove
870 *
Dave Airlief453ba02008-11-07 14:05:41 -0800871 * Free @mode's unique identifier, then free it.
872 */
873void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
874{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200875 if (!mode)
876 return;
877
Dave Airlief453ba02008-11-07 14:05:41 -0800878 drm_mode_object_put(dev, &mode->base);
879
880 kfree(mode);
881}
882EXPORT_SYMBOL(drm_mode_destroy);
883
884static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
885{
886 struct drm_property *edid;
887 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800888
889 /*
890 * Standard properties (apply to all connectors)
891 */
892 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
893 DRM_MODE_PROP_IMMUTABLE,
894 "EDID", 0);
895 dev->mode_config.edid_property = edid;
896
Sascha Hauer4a67d392012-02-06 10:58:17 +0100897 dpms = drm_property_create_enum(dev, 0,
898 "DPMS", drm_dpms_enum_list,
899 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800900 dev->mode_config.dpms_property = dpms;
901
902 return 0;
903}
904
905/**
906 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
907 * @dev: DRM device
908 *
909 * Called by a driver the first time a DVI-I connector is made.
910 */
911int drm_mode_create_dvi_i_properties(struct drm_device *dev)
912{
913 struct drm_property *dvi_i_selector;
914 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800915
916 if (dev->mode_config.dvi_i_select_subconnector_property)
917 return 0;
918
919 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100920 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800921 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100922 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800923 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800924 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
925
Sascha Hauer4a67d392012-02-06 10:58:17 +0100926 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800927 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100928 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800929 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800930 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
931
932 return 0;
933}
934EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
935
936/**
937 * drm_create_tv_properties - create TV specific connector properties
938 * @dev: DRM device
939 * @num_modes: number of different TV formats (modes) supported
940 * @modes: array of pointers to strings containing name of each format
941 *
942 * Called by a driver's TV initialization routine, this function creates
943 * the TV specific connector properties for a given device. Caller is
944 * responsible for allocating a list of format names and passing them to
945 * this routine.
946 */
947int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
948 char *modes[])
949{
950 struct drm_property *tv_selector;
951 struct drm_property *tv_subconnector;
952 int i;
953
954 if (dev->mode_config.tv_select_subconnector_property)
955 return 0;
956
957 /*
958 * Basic connector properties
959 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100960 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800961 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100962 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800963 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800964 dev->mode_config.tv_select_subconnector_property = tv_selector;
965
966 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100967 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
968 "subconnector",
969 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800970 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800971 dev->mode_config.tv_subconnector_property = tv_subconnector;
972
973 /*
974 * Other, TV specific properties: margins & TV modes.
975 */
976 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100977 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800978
979 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100980 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800981
982 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100983 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800984
985 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100986 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800987
988 dev->mode_config.tv_mode_property =
989 drm_property_create(dev, DRM_MODE_PROP_ENUM,
990 "mode", num_modes);
991 for (i = 0; i < num_modes; i++)
992 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
993 i, modes[i]);
994
Francisco Jerezb6b79022009-08-02 04:19:20 +0200995 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100996 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200997
998 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100999 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001000
1001 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001002 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001003
Francisco Jereza75f0232009-08-12 02:30:10 +02001004 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001005 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001006
1007 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001008 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001009
1010 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001011 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001012
Dave Airlief453ba02008-11-07 14:05:41 -08001013 return 0;
1014}
1015EXPORT_SYMBOL(drm_mode_create_tv_properties);
1016
1017/**
1018 * drm_mode_create_scaling_mode_property - create scaling mode property
1019 * @dev: DRM device
1020 *
1021 * Called by a driver the first time it's needed, must be attached to desired
1022 * connectors.
1023 */
1024int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1025{
1026 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001027
1028 if (dev->mode_config.scaling_mode_property)
1029 return 0;
1030
1031 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001032 drm_property_create_enum(dev, 0, "scaling mode",
1033 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001034 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001035
1036 dev->mode_config.scaling_mode_property = scaling_mode;
1037
1038 return 0;
1039}
1040EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1041
1042/**
1043 * drm_mode_create_dithering_property - create dithering property
1044 * @dev: DRM device
1045 *
1046 * Called by a driver the first time it's needed, must be attached to desired
1047 * connectors.
1048 */
1049int drm_mode_create_dithering_property(struct drm_device *dev)
1050{
1051 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001052
1053 if (dev->mode_config.dithering_mode_property)
1054 return 0;
1055
1056 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001057 drm_property_create_enum(dev, 0, "dithering",
1058 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001059 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001060 dev->mode_config.dithering_mode_property = dithering_mode;
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL(drm_mode_create_dithering_property);
1065
1066/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001067 * drm_mode_create_dirty_property - create dirty property
1068 * @dev: DRM device
1069 *
1070 * Called by a driver the first time it's needed, must be attached to desired
1071 * connectors.
1072 */
1073int drm_mode_create_dirty_info_property(struct drm_device *dev)
1074{
1075 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001076
1077 if (dev->mode_config.dirty_info_property)
1078 return 0;
1079
1080 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001081 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001082 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001083 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001084 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001085 dev->mode_config.dirty_info_property = dirty_info;
1086
1087 return 0;
1088}
1089EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1090
1091/**
Dave Airlief453ba02008-11-07 14:05:41 -08001092 * drm_mode_config_init - initialize DRM mode_configuration structure
1093 * @dev: DRM device
1094 *
Dave Airlief453ba02008-11-07 14:05:41 -08001095 * Initialize @dev's mode_config structure, used for tracking the graphics
1096 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001097 *
1098 * Since this initializes the modeset locks, no locking is possible. Which is no
1099 * problem, since this should happen single threaded at init time. It is the
1100 * driver's problem to ensure this guarantee.
1101 *
Dave Airlief453ba02008-11-07 14:05:41 -08001102 */
1103void drm_mode_config_init(struct drm_device *dev)
1104{
1105 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001106 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001107 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001108 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001109 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1110 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1111 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1112 INIT_LIST_HEAD(&dev->mode_config.property_list);
1113 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001114 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001115 idr_init(&dev->mode_config.crtc_idr);
1116
Daniel Vetter84849902012-12-02 00:28:11 +01001117 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001118 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001119 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001120
1121 /* Just to be sure */
1122 dev->mode_config.num_fb = 0;
1123 dev->mode_config.num_connector = 0;
1124 dev->mode_config.num_crtc = 0;
1125 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001126}
1127EXPORT_SYMBOL(drm_mode_config_init);
1128
1129int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1130{
1131 uint32_t total_objects = 0;
1132
1133 total_objects += dev->mode_config.num_crtc;
1134 total_objects += dev->mode_config.num_connector;
1135 total_objects += dev->mode_config.num_encoder;
1136
Dave Airlief453ba02008-11-07 14:05:41 -08001137 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1138 if (!group->id_list)
1139 return -ENOMEM;
1140
1141 group->num_crtcs = 0;
1142 group->num_connectors = 0;
1143 group->num_encoders = 0;
1144 return 0;
1145}
1146
1147int drm_mode_group_init_legacy_group(struct drm_device *dev,
1148 struct drm_mode_group *group)
1149{
1150 struct drm_crtc *crtc;
1151 struct drm_encoder *encoder;
1152 struct drm_connector *connector;
1153 int ret;
1154
1155 if ((ret = drm_mode_group_init(dev, group)))
1156 return ret;
1157
1158 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1159 group->id_list[group->num_crtcs++] = crtc->base.id;
1160
1161 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1162 group->id_list[group->num_crtcs + group->num_encoders++] =
1163 encoder->base.id;
1164
1165 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1166 group->id_list[group->num_crtcs + group->num_encoders +
1167 group->num_connectors++] = connector->base.id;
1168
1169 return 0;
1170}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001171EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001172
1173/**
1174 * drm_mode_config_cleanup - free up DRM mode_config info
1175 * @dev: DRM device
1176 *
Dave Airlief453ba02008-11-07 14:05:41 -08001177 * Free up all the connectors and CRTCs associated with this DRM device, then
1178 * free up the framebuffers and associated buffer objects.
1179 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001180 * Note that since this /should/ happen single-threaded at driver/device
1181 * teardown time, no locking is required. It's the driver's job to ensure that
1182 * this guarantee actually holds true.
1183 *
Dave Airlief453ba02008-11-07 14:05:41 -08001184 * FIXME: cleanup any dangling user buffer objects too
1185 */
1186void drm_mode_config_cleanup(struct drm_device *dev)
1187{
1188 struct drm_connector *connector, *ot;
1189 struct drm_crtc *crtc, *ct;
1190 struct drm_encoder *encoder, *enct;
1191 struct drm_framebuffer *fb, *fbt;
1192 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001193 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001194
1195 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1196 head) {
1197 encoder->funcs->destroy(encoder);
1198 }
1199
1200 list_for_each_entry_safe(connector, ot,
1201 &dev->mode_config.connector_list, head) {
1202 connector->funcs->destroy(connector);
1203 }
1204
1205 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1206 head) {
1207 drm_property_destroy(dev, property);
1208 }
1209
Daniel Vetter2b677e82012-12-10 21:16:05 +01001210 /*
1211 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001212 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001213 * would actually deadlock with the drm_framebuffer_cleanup function.
1214 *
1215 * Also, if there are any framebuffers left, that's a driver leak now,
1216 * so politely WARN about this.
1217 */
1218 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001219 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001220 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001221 }
1222
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001223 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1224 head) {
1225 plane->funcs->destroy(plane);
1226 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001227
Chris Wilson31840092012-09-17 09:38:03 +00001228 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1229 crtc->funcs->destroy(crtc);
1230 }
1231
Sascha Hauer59ce0622012-02-01 11:38:20 +01001232 idr_remove_all(&dev->mode_config.crtc_idr);
1233 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001234}
1235EXPORT_SYMBOL(drm_mode_config_cleanup);
1236
Dave Airlief453ba02008-11-07 14:05:41 -08001237/**
1238 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1239 * @out: drm_mode_modeinfo struct to return to the user
1240 * @in: drm_display_mode to use
1241 *
Dave Airlief453ba02008-11-07 14:05:41 -08001242 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1243 * the user.
1244 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001245static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1246 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001247{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001248 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1249 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1250 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1251 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1252 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1253 "timing values too large for mode info\n");
1254
Dave Airlief453ba02008-11-07 14:05:41 -08001255 out->clock = in->clock;
1256 out->hdisplay = in->hdisplay;
1257 out->hsync_start = in->hsync_start;
1258 out->hsync_end = in->hsync_end;
1259 out->htotal = in->htotal;
1260 out->hskew = in->hskew;
1261 out->vdisplay = in->vdisplay;
1262 out->vsync_start = in->vsync_start;
1263 out->vsync_end = in->vsync_end;
1264 out->vtotal = in->vtotal;
1265 out->vscan = in->vscan;
1266 out->vrefresh = in->vrefresh;
1267 out->flags = in->flags;
1268 out->type = in->type;
1269 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1270 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1271}
1272
1273/**
1274 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1275 * @out: drm_display_mode to return to the user
1276 * @in: drm_mode_modeinfo to use
1277 *
Dave Airlief453ba02008-11-07 14:05:41 -08001278 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1279 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001280 *
1281 * RETURNS:
1282 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001283 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001284static int drm_crtc_convert_umode(struct drm_display_mode *out,
1285 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001286{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001287 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1288 return -ERANGE;
1289
Dave Airlief453ba02008-11-07 14:05:41 -08001290 out->clock = in->clock;
1291 out->hdisplay = in->hdisplay;
1292 out->hsync_start = in->hsync_start;
1293 out->hsync_end = in->hsync_end;
1294 out->htotal = in->htotal;
1295 out->hskew = in->hskew;
1296 out->vdisplay = in->vdisplay;
1297 out->vsync_start = in->vsync_start;
1298 out->vsync_end = in->vsync_end;
1299 out->vtotal = in->vtotal;
1300 out->vscan = in->vscan;
1301 out->vrefresh = in->vrefresh;
1302 out->flags = in->flags;
1303 out->type = in->type;
1304 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1305 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001306
1307 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001308}
1309
1310/**
1311 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001312 * @dev: drm device for the ioctl
1313 * @data: data pointer for the ioctl
1314 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001315 *
Dave Airlief453ba02008-11-07 14:05:41 -08001316 * Construct a set of configuration description structures and return
1317 * them to the user, including CRTC, connector and framebuffer configuration.
1318 *
1319 * Called by the user via ioctl.
1320 *
1321 * RETURNS:
1322 * Zero on success, errno on failure.
1323 */
1324int drm_mode_getresources(struct drm_device *dev, void *data,
1325 struct drm_file *file_priv)
1326{
1327 struct drm_mode_card_res *card_res = data;
1328 struct list_head *lh;
1329 struct drm_framebuffer *fb;
1330 struct drm_connector *connector;
1331 struct drm_crtc *crtc;
1332 struct drm_encoder *encoder;
1333 int ret = 0;
1334 int connector_count = 0;
1335 int crtc_count = 0;
1336 int fb_count = 0;
1337 int encoder_count = 0;
1338 int copied = 0, i;
1339 uint32_t __user *fb_id;
1340 uint32_t __user *crtc_id;
1341 uint32_t __user *connector_id;
1342 uint32_t __user *encoder_id;
1343 struct drm_mode_group *mode_group;
1344
Dave Airliefb3b06c2011-02-08 13:55:21 +10001345 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1346 return -EINVAL;
1347
Dave Airlief453ba02008-11-07 14:05:41 -08001348
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001349 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001350 /*
1351 * For the non-control nodes we need to limit the list of resources
1352 * by IDs in the group list for this node
1353 */
1354 list_for_each(lh, &file_priv->fbs)
1355 fb_count++;
1356
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001357 /* handle this in 4 parts */
1358 /* FBs */
1359 if (card_res->count_fbs >= fb_count) {
1360 copied = 0;
1361 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1362 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1363 if (put_user(fb->base.id, fb_id + copied)) {
1364 mutex_unlock(&file_priv->fbs_lock);
1365 return -EFAULT;
1366 }
1367 copied++;
1368 }
1369 }
1370 card_res->count_fbs = fb_count;
1371 mutex_unlock(&file_priv->fbs_lock);
1372
1373 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001374 mode_group = &file_priv->master->minor->mode_group;
1375 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1376
1377 list_for_each(lh, &dev->mode_config.crtc_list)
1378 crtc_count++;
1379
1380 list_for_each(lh, &dev->mode_config.connector_list)
1381 connector_count++;
1382
1383 list_for_each(lh, &dev->mode_config.encoder_list)
1384 encoder_count++;
1385 } else {
1386
1387 crtc_count = mode_group->num_crtcs;
1388 connector_count = mode_group->num_connectors;
1389 encoder_count = mode_group->num_encoders;
1390 }
1391
1392 card_res->max_height = dev->mode_config.max_height;
1393 card_res->min_height = dev->mode_config.min_height;
1394 card_res->max_width = dev->mode_config.max_width;
1395 card_res->min_width = dev->mode_config.min_width;
1396
Dave Airlief453ba02008-11-07 14:05:41 -08001397 /* CRTCs */
1398 if (card_res->count_crtcs >= crtc_count) {
1399 copied = 0;
1400 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1401 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1402 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1403 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001404 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001405 if (put_user(crtc->base.id, crtc_id + copied)) {
1406 ret = -EFAULT;
1407 goto out;
1408 }
1409 copied++;
1410 }
1411 } else {
1412 for (i = 0; i < mode_group->num_crtcs; i++) {
1413 if (put_user(mode_group->id_list[i],
1414 crtc_id + copied)) {
1415 ret = -EFAULT;
1416 goto out;
1417 }
1418 copied++;
1419 }
1420 }
1421 }
1422 card_res->count_crtcs = crtc_count;
1423
1424 /* Encoders */
1425 if (card_res->count_encoders >= encoder_count) {
1426 copied = 0;
1427 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1428 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1429 list_for_each_entry(encoder,
1430 &dev->mode_config.encoder_list,
1431 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001432 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1433 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001434 if (put_user(encoder->base.id, encoder_id +
1435 copied)) {
1436 ret = -EFAULT;
1437 goto out;
1438 }
1439 copied++;
1440 }
1441 } else {
1442 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1443 if (put_user(mode_group->id_list[i],
1444 encoder_id + copied)) {
1445 ret = -EFAULT;
1446 goto out;
1447 }
1448 copied++;
1449 }
1450
1451 }
1452 }
1453 card_res->count_encoders = encoder_count;
1454
1455 /* Connectors */
1456 if (card_res->count_connectors >= connector_count) {
1457 copied = 0;
1458 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1459 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1460 list_for_each_entry(connector,
1461 &dev->mode_config.connector_list,
1462 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001463 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1464 connector->base.id,
1465 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001466 if (put_user(connector->base.id,
1467 connector_id + copied)) {
1468 ret = -EFAULT;
1469 goto out;
1470 }
1471 copied++;
1472 }
1473 } else {
1474 int start = mode_group->num_crtcs +
1475 mode_group->num_encoders;
1476 for (i = start; i < start + mode_group->num_connectors; i++) {
1477 if (put_user(mode_group->id_list[i],
1478 connector_id + copied)) {
1479 ret = -EFAULT;
1480 goto out;
1481 }
1482 copied++;
1483 }
1484 }
1485 }
1486 card_res->count_connectors = connector_count;
1487
Jerome Glisse94401062010-07-15 15:43:25 -04001488 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001489 card_res->count_connectors, card_res->count_encoders);
1490
1491out:
Daniel Vetter84849902012-12-02 00:28:11 +01001492 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001493 return ret;
1494}
1495
1496/**
1497 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001498 * @dev: drm device for the ioctl
1499 * @data: data pointer for the ioctl
1500 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001501 *
Dave Airlief453ba02008-11-07 14:05:41 -08001502 * Construct a CRTC configuration structure to return to the user.
1503 *
1504 * Called by the user via ioctl.
1505 *
1506 * RETURNS:
1507 * Zero on success, errno on failure.
1508 */
1509int drm_mode_getcrtc(struct drm_device *dev,
1510 void *data, struct drm_file *file_priv)
1511{
1512 struct drm_mode_crtc *crtc_resp = data;
1513 struct drm_crtc *crtc;
1514 struct drm_mode_object *obj;
1515 int ret = 0;
1516
Dave Airliefb3b06c2011-02-08 13:55:21 +10001517 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1518 return -EINVAL;
1519
Daniel Vetter84849902012-12-02 00:28:11 +01001520 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001521
1522 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1523 DRM_MODE_OBJECT_CRTC);
1524 if (!obj) {
1525 ret = -EINVAL;
1526 goto out;
1527 }
1528 crtc = obj_to_crtc(obj);
1529
1530 crtc_resp->x = crtc->x;
1531 crtc_resp->y = crtc->y;
1532 crtc_resp->gamma_size = crtc->gamma_size;
1533 if (crtc->fb)
1534 crtc_resp->fb_id = crtc->fb->base.id;
1535 else
1536 crtc_resp->fb_id = 0;
1537
1538 if (crtc->enabled) {
1539
1540 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1541 crtc_resp->mode_valid = 1;
1542
1543 } else {
1544 crtc_resp->mode_valid = 0;
1545 }
1546
1547out:
Daniel Vetter84849902012-12-02 00:28:11 +01001548 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001549 return ret;
1550}
1551
1552/**
1553 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001554 * @dev: drm device for the ioctl
1555 * @data: data pointer for the ioctl
1556 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001557 *
Dave Airlief453ba02008-11-07 14:05:41 -08001558 * Construct a connector configuration structure to return to the user.
1559 *
1560 * Called by the user via ioctl.
1561 *
1562 * RETURNS:
1563 * Zero on success, errno on failure.
1564 */
1565int drm_mode_getconnector(struct drm_device *dev, void *data,
1566 struct drm_file *file_priv)
1567{
1568 struct drm_mode_get_connector *out_resp = data;
1569 struct drm_mode_object *obj;
1570 struct drm_connector *connector;
1571 struct drm_display_mode *mode;
1572 int mode_count = 0;
1573 int props_count = 0;
1574 int encoders_count = 0;
1575 int ret = 0;
1576 int copied = 0;
1577 int i;
1578 struct drm_mode_modeinfo u_mode;
1579 struct drm_mode_modeinfo __user *mode_ptr;
1580 uint32_t __user *prop_ptr;
1581 uint64_t __user *prop_values;
1582 uint32_t __user *encoder_ptr;
1583
Dave Airliefb3b06c2011-02-08 13:55:21 +10001584 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1585 return -EINVAL;
1586
Dave Airlief453ba02008-11-07 14:05:41 -08001587 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1588
Jerome Glisse94401062010-07-15 15:43:25 -04001589 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001590
Daniel Vetter84849902012-12-02 00:28:11 +01001591 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001592
1593 obj = drm_mode_object_find(dev, out_resp->connector_id,
1594 DRM_MODE_OBJECT_CONNECTOR);
1595 if (!obj) {
1596 ret = -EINVAL;
1597 goto out;
1598 }
1599 connector = obj_to_connector(obj);
1600
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001601 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001602
1603 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1604 if (connector->encoder_ids[i] != 0) {
1605 encoders_count++;
1606 }
1607 }
1608
1609 if (out_resp->count_modes == 0) {
1610 connector->funcs->fill_modes(connector,
1611 dev->mode_config.max_width,
1612 dev->mode_config.max_height);
1613 }
1614
1615 /* delayed so we get modes regardless of pre-fill_modes state */
1616 list_for_each_entry(mode, &connector->modes, head)
1617 mode_count++;
1618
1619 out_resp->connector_id = connector->base.id;
1620 out_resp->connector_type = connector->connector_type;
1621 out_resp->connector_type_id = connector->connector_type_id;
1622 out_resp->mm_width = connector->display_info.width_mm;
1623 out_resp->mm_height = connector->display_info.height_mm;
1624 out_resp->subpixel = connector->display_info.subpixel_order;
1625 out_resp->connection = connector->status;
1626 if (connector->encoder)
1627 out_resp->encoder_id = connector->encoder->base.id;
1628 else
1629 out_resp->encoder_id = 0;
1630
1631 /*
1632 * This ioctl is called twice, once to determine how much space is
1633 * needed, and the 2nd time to fill it.
1634 */
1635 if ((out_resp->count_modes >= mode_count) && mode_count) {
1636 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001637 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001638 list_for_each_entry(mode, &connector->modes, head) {
1639 drm_crtc_convert_to_umode(&u_mode, mode);
1640 if (copy_to_user(mode_ptr + copied,
1641 &u_mode, sizeof(u_mode))) {
1642 ret = -EFAULT;
1643 goto out;
1644 }
1645 copied++;
1646 }
1647 }
1648 out_resp->count_modes = mode_count;
1649
1650 if ((out_resp->count_props >= props_count) && props_count) {
1651 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001652 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1653 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001654 for (i = 0; i < connector->properties.count; i++) {
1655 if (put_user(connector->properties.ids[i],
1656 prop_ptr + copied)) {
1657 ret = -EFAULT;
1658 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001659 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001660
1661 if (put_user(connector->properties.values[i],
1662 prop_values + copied)) {
1663 ret = -EFAULT;
1664 goto out;
1665 }
1666 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001667 }
1668 }
1669 out_resp->count_props = props_count;
1670
1671 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1672 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001673 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001674 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1675 if (connector->encoder_ids[i] != 0) {
1676 if (put_user(connector->encoder_ids[i],
1677 encoder_ptr + copied)) {
1678 ret = -EFAULT;
1679 goto out;
1680 }
1681 copied++;
1682 }
1683 }
1684 }
1685 out_resp->count_encoders = encoders_count;
1686
1687out:
Daniel Vetter84849902012-12-02 00:28:11 +01001688 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001689 return ret;
1690}
1691
1692int drm_mode_getencoder(struct drm_device *dev, void *data,
1693 struct drm_file *file_priv)
1694{
1695 struct drm_mode_get_encoder *enc_resp = data;
1696 struct drm_mode_object *obj;
1697 struct drm_encoder *encoder;
1698 int ret = 0;
1699
Dave Airliefb3b06c2011-02-08 13:55:21 +10001700 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1701 return -EINVAL;
1702
Daniel Vetter84849902012-12-02 00:28:11 +01001703 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001704 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1705 DRM_MODE_OBJECT_ENCODER);
1706 if (!obj) {
1707 ret = -EINVAL;
1708 goto out;
1709 }
1710 encoder = obj_to_encoder(obj);
1711
1712 if (encoder->crtc)
1713 enc_resp->crtc_id = encoder->crtc->base.id;
1714 else
1715 enc_resp->crtc_id = 0;
1716 enc_resp->encoder_type = encoder->encoder_type;
1717 enc_resp->encoder_id = encoder->base.id;
1718 enc_resp->possible_crtcs = encoder->possible_crtcs;
1719 enc_resp->possible_clones = encoder->possible_clones;
1720
1721out:
Daniel Vetter84849902012-12-02 00:28:11 +01001722 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001723 return ret;
1724}
1725
1726/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001727 * drm_mode_getplane_res - get plane info
1728 * @dev: DRM device
1729 * @data: ioctl data
1730 * @file_priv: DRM file info
1731 *
1732 * Return an plane count and set of IDs.
1733 */
1734int drm_mode_getplane_res(struct drm_device *dev, void *data,
1735 struct drm_file *file_priv)
1736{
1737 struct drm_mode_get_plane_res *plane_resp = data;
1738 struct drm_mode_config *config;
1739 struct drm_plane *plane;
1740 uint32_t __user *plane_ptr;
1741 int copied = 0, ret = 0;
1742
1743 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1744 return -EINVAL;
1745
Daniel Vetter84849902012-12-02 00:28:11 +01001746 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001747 config = &dev->mode_config;
1748
1749 /*
1750 * This ioctl is called twice, once to determine how much space is
1751 * needed, and the 2nd time to fill it.
1752 */
1753 if (config->num_plane &&
1754 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001755 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001756
1757 list_for_each_entry(plane, &config->plane_list, head) {
1758 if (put_user(plane->base.id, plane_ptr + copied)) {
1759 ret = -EFAULT;
1760 goto out;
1761 }
1762 copied++;
1763 }
1764 }
1765 plane_resp->count_planes = config->num_plane;
1766
1767out:
Daniel Vetter84849902012-12-02 00:28:11 +01001768 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001769 return ret;
1770}
1771
1772/**
1773 * drm_mode_getplane - get plane info
1774 * @dev: DRM device
1775 * @data: ioctl data
1776 * @file_priv: DRM file info
1777 *
1778 * Return plane info, including formats supported, gamma size, any
1779 * current fb, etc.
1780 */
1781int drm_mode_getplane(struct drm_device *dev, void *data,
1782 struct drm_file *file_priv)
1783{
1784 struct drm_mode_get_plane *plane_resp = data;
1785 struct drm_mode_object *obj;
1786 struct drm_plane *plane;
1787 uint32_t __user *format_ptr;
1788 int ret = 0;
1789
1790 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1791 return -EINVAL;
1792
Daniel Vetter84849902012-12-02 00:28:11 +01001793 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001794 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1795 DRM_MODE_OBJECT_PLANE);
1796 if (!obj) {
1797 ret = -ENOENT;
1798 goto out;
1799 }
1800 plane = obj_to_plane(obj);
1801
1802 if (plane->crtc)
1803 plane_resp->crtc_id = plane->crtc->base.id;
1804 else
1805 plane_resp->crtc_id = 0;
1806
1807 if (plane->fb)
1808 plane_resp->fb_id = plane->fb->base.id;
1809 else
1810 plane_resp->fb_id = 0;
1811
1812 plane_resp->plane_id = plane->base.id;
1813 plane_resp->possible_crtcs = plane->possible_crtcs;
1814 plane_resp->gamma_size = plane->gamma_size;
1815
1816 /*
1817 * This ioctl is called twice, once to determine how much space is
1818 * needed, and the 2nd time to fill it.
1819 */
1820 if (plane->format_count &&
1821 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001822 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001823 if (copy_to_user(format_ptr,
1824 plane->format_types,
1825 sizeof(uint32_t) * plane->format_count)) {
1826 ret = -EFAULT;
1827 goto out;
1828 }
1829 }
1830 plane_resp->count_format_types = plane->format_count;
1831
1832out:
Daniel Vetter84849902012-12-02 00:28:11 +01001833 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001834 return ret;
1835}
1836
1837/**
1838 * drm_mode_setplane - set up or tear down an plane
1839 * @dev: DRM device
1840 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001841 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001842 *
1843 * Set plane info, including placement, fb, scaling, and other factors.
1844 * Or pass a NULL fb to disable.
1845 */
1846int drm_mode_setplane(struct drm_device *dev, void *data,
1847 struct drm_file *file_priv)
1848{
1849 struct drm_mode_set_plane *plane_req = data;
1850 struct drm_mode_object *obj;
1851 struct drm_plane *plane;
1852 struct drm_crtc *crtc;
1853 struct drm_framebuffer *fb;
1854 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001855 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001856 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001857
1858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1859 return -EINVAL;
1860
Daniel Vetter84849902012-12-02 00:28:11 +01001861 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001862
1863 /*
1864 * First, find the plane, crtc, and fb objects. If not available,
1865 * we don't bother to call the driver.
1866 */
1867 obj = drm_mode_object_find(dev, plane_req->plane_id,
1868 DRM_MODE_OBJECT_PLANE);
1869 if (!obj) {
1870 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1871 plane_req->plane_id);
1872 ret = -ENOENT;
1873 goto out;
1874 }
1875 plane = obj_to_plane(obj);
1876
1877 /* No fb means shut it down */
1878 if (!plane_req->fb_id) {
1879 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001880 plane->crtc = NULL;
1881 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001882 goto out;
1883 }
1884
1885 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1886 DRM_MODE_OBJECT_CRTC);
1887 if (!obj) {
1888 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1889 plane_req->crtc_id);
1890 ret = -ENOENT;
1891 goto out;
1892 }
1893 crtc = obj_to_crtc(obj);
1894
Daniel Vetter786b99e2012-12-02 21:53:40 +01001895 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1896 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001897 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1898 plane_req->fb_id);
1899 ret = -ENOENT;
1900 goto out;
1901 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01001902 /* fb is protect by the mode_config lock, so drop the ref immediately */
1903 drm_framebuffer_unreference(fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904
Ville Syrjälä62443be2011-12-20 00:06:47 +02001905 /* Check whether this plane supports the fb pixel format. */
1906 for (i = 0; i < plane->format_count; i++)
1907 if (fb->pixel_format == plane->format_types[i])
1908 break;
1909 if (i == plane->format_count) {
1910 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1911 ret = -EINVAL;
1912 goto out;
1913 }
1914
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001915 fb_width = fb->width << 16;
1916 fb_height = fb->height << 16;
1917
1918 /* Make sure source coordinates are inside the fb. */
1919 if (plane_req->src_w > fb_width ||
1920 plane_req->src_x > fb_width - plane_req->src_w ||
1921 plane_req->src_h > fb_height ||
1922 plane_req->src_y > fb_height - plane_req->src_h) {
1923 DRM_DEBUG_KMS("Invalid source coordinates "
1924 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1925 plane_req->src_w >> 16,
1926 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1927 plane_req->src_h >> 16,
1928 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1929 plane_req->src_x >> 16,
1930 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1931 plane_req->src_y >> 16,
1932 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1933 ret = -ENOSPC;
1934 goto out;
1935 }
1936
Ville Syrjälä687a0402011-12-20 00:06:45 +02001937 /* Give drivers some help against integer overflows */
1938 if (plane_req->crtc_w > INT_MAX ||
1939 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1940 plane_req->crtc_h > INT_MAX ||
1941 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1942 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1943 plane_req->crtc_w, plane_req->crtc_h,
1944 plane_req->crtc_x, plane_req->crtc_y);
1945 ret = -ERANGE;
1946 goto out;
1947 }
1948
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001949 ret = plane->funcs->update_plane(plane, crtc, fb,
1950 plane_req->crtc_x, plane_req->crtc_y,
1951 plane_req->crtc_w, plane_req->crtc_h,
1952 plane_req->src_x, plane_req->src_y,
1953 plane_req->src_w, plane_req->src_h);
1954 if (!ret) {
1955 plane->crtc = crtc;
1956 plane->fb = fb;
1957 }
1958
1959out:
Daniel Vetter84849902012-12-02 00:28:11 +01001960 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001961
1962 return ret;
1963}
1964
1965/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001966 * drm_mode_set_config_internal - helper to call ->set_config
1967 * @set: modeset config to set
1968 *
1969 * This is a little helper to wrap internal calls to the ->set_config driver
1970 * interface. The only thing it adds is correct refcounting dance.
1971 */
1972int drm_mode_set_config_internal(struct drm_mode_set *set)
1973{
1974 struct drm_crtc *crtc = set->crtc;
1975
1976 return crtc->funcs->set_config(set);
1977}
1978EXPORT_SYMBOL(drm_mode_set_config_internal);
1979
1980/**
Dave Airlief453ba02008-11-07 14:05:41 -08001981 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001982 * @dev: drm device for the ioctl
1983 * @data: data pointer for the ioctl
1984 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001985 *
Dave Airlief453ba02008-11-07 14:05:41 -08001986 * Build a new CRTC configuration based on user request.
1987 *
1988 * Called by the user via ioctl.
1989 *
1990 * RETURNS:
1991 * Zero on success, errno on failure.
1992 */
1993int drm_mode_setcrtc(struct drm_device *dev, void *data,
1994 struct drm_file *file_priv)
1995{
1996 struct drm_mode_config *config = &dev->mode_config;
1997 struct drm_mode_crtc *crtc_req = data;
1998 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001999 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002000 struct drm_connector **connector_set = NULL, *connector;
2001 struct drm_framebuffer *fb = NULL;
2002 struct drm_display_mode *mode = NULL;
2003 struct drm_mode_set set;
2004 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002005 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002006 int i;
2007
Dave Airliefb3b06c2011-02-08 13:55:21 +10002008 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2009 return -EINVAL;
2010
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002011 /* For some reason crtc x/y offsets are signed internally. */
2012 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2013 return -ERANGE;
2014
Daniel Vetter84849902012-12-02 00:28:11 +01002015 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002016 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2017 DRM_MODE_OBJECT_CRTC);
2018 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002019 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002020 ret = -EINVAL;
2021 goto out;
2022 }
2023 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002024 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002025
2026 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002027 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002028 /* If we have a mode we need a framebuffer. */
2029 /* If we pass -1, set the mode with the currently bound fb */
2030 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002031 if (!crtc->fb) {
2032 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2033 ret = -EINVAL;
2034 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002035 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002036 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002037 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002038 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2039 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002040 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2041 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002042 ret = -EINVAL;
2043 goto out;
2044 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002045 /* fb is protect by the mode_config lock, so drop the
2046 * ref immediately */
2047 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002048 }
2049
2050 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002051 if (!mode) {
2052 ret = -ENOMEM;
2053 goto out;
2054 }
2055
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002056 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2057 if (ret) {
2058 DRM_DEBUG_KMS("Invalid mode\n");
2059 goto out;
2060 }
2061
Dave Airlief453ba02008-11-07 14:05:41 -08002062 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002063
Rob Clark7c80e122012-09-04 16:35:56 +00002064 hdisplay = mode->hdisplay;
2065 vdisplay = mode->vdisplay;
2066
2067 if (crtc->invert_dimensions)
2068 swap(hdisplay, vdisplay);
2069
2070 if (hdisplay > fb->width ||
2071 vdisplay > fb->height ||
2072 crtc_req->x > fb->width - hdisplay ||
2073 crtc_req->y > fb->height - vdisplay) {
2074 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2075 fb->width, fb->height,
2076 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2077 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002078 ret = -ENOSPC;
2079 goto out;
2080 }
Dave Airlief453ba02008-11-07 14:05:41 -08002081 }
2082
2083 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002084 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002085 ret = -EINVAL;
2086 goto out;
2087 }
2088
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002089 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002090 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002091 crtc_req->count_connectors);
2092 ret = -EINVAL;
2093 goto out;
2094 }
2095
2096 if (crtc_req->count_connectors > 0) {
2097 u32 out_id;
2098
2099 /* Avoid unbounded kernel memory allocation */
2100 if (crtc_req->count_connectors > config->num_connector) {
2101 ret = -EINVAL;
2102 goto out;
2103 }
2104
2105 connector_set = kmalloc(crtc_req->count_connectors *
2106 sizeof(struct drm_connector *),
2107 GFP_KERNEL);
2108 if (!connector_set) {
2109 ret = -ENOMEM;
2110 goto out;
2111 }
2112
2113 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002114 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 if (get_user(out_id, &set_connectors_ptr[i])) {
2116 ret = -EFAULT;
2117 goto out;
2118 }
2119
2120 obj = drm_mode_object_find(dev, out_id,
2121 DRM_MODE_OBJECT_CONNECTOR);
2122 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002123 DRM_DEBUG_KMS("Connector id %d unknown\n",
2124 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002125 ret = -EINVAL;
2126 goto out;
2127 }
2128 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002129 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2130 connector->base.id,
2131 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002132
2133 connector_set[i] = connector;
2134 }
2135 }
2136
2137 set.crtc = crtc;
2138 set.x = crtc_req->x;
2139 set.y = crtc_req->y;
2140 set.mode = mode;
2141 set.connectors = connector_set;
2142 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002143 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002144 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002145
2146out:
2147 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002148 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002149 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002150 return ret;
2151}
2152
2153int drm_mode_cursor_ioctl(struct drm_device *dev,
2154 void *data, struct drm_file *file_priv)
2155{
2156 struct drm_mode_cursor *req = data;
2157 struct drm_mode_object *obj;
2158 struct drm_crtc *crtc;
2159 int ret = 0;
2160
Dave Airliefb3b06c2011-02-08 13:55:21 +10002161 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2162 return -EINVAL;
2163
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002164 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002165 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002166
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002167 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002168 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002169 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002170 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002171 }
2172 crtc = obj_to_crtc(obj);
2173
Daniel Vetterdac35662012-12-02 15:24:10 +01002174 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002175 if (req->flags & DRM_MODE_CURSOR_BO) {
2176 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002177 ret = -ENXIO;
2178 goto out;
2179 }
2180 /* Turns off the cursor if handle is 0 */
2181 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2182 req->width, req->height);
2183 }
2184
2185 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2186 if (crtc->funcs->cursor_move) {
2187 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2188 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002189 ret = -EFAULT;
2190 goto out;
2191 }
2192 }
2193out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002194 mutex_unlock(&crtc->mutex);
2195
Dave Airlief453ba02008-11-07 14:05:41 -08002196 return ret;
2197}
2198
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002199/* Original addfb only supported RGB formats, so figure out which one */
2200uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2201{
2202 uint32_t fmt;
2203
2204 switch (bpp) {
2205 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002206 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002207 break;
2208 case 16:
2209 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002210 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002211 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002212 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002213 break;
2214 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002215 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002216 break;
2217 case 32:
2218 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002219 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002220 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002221 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002222 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002223 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002224 break;
2225 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002226 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2227 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002228 break;
2229 }
2230
2231 return fmt;
2232}
2233EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2234
Dave Airlief453ba02008-11-07 14:05:41 -08002235/**
2236 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002237 * @dev: drm device for the ioctl
2238 * @data: data pointer for the ioctl
2239 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002240 *
Dave Airlief453ba02008-11-07 14:05:41 -08002241 * Add a new FB to the specified CRTC, given a user request.
2242 *
2243 * Called by the user via ioctl.
2244 *
2245 * RETURNS:
2246 * Zero on success, errno on failure.
2247 */
2248int drm_mode_addfb(struct drm_device *dev,
2249 void *data, struct drm_file *file_priv)
2250{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002251 struct drm_mode_fb_cmd *or = data;
2252 struct drm_mode_fb_cmd2 r = {};
2253 struct drm_mode_config *config = &dev->mode_config;
2254 struct drm_framebuffer *fb;
2255 int ret = 0;
2256
2257 /* Use new struct with format internally */
2258 r.fb_id = or->fb_id;
2259 r.width = or->width;
2260 r.height = or->height;
2261 r.pitches[0] = or->pitch;
2262 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2263 r.handles[0] = or->handle;
2264
2265 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2266 return -EINVAL;
2267
Jesse Barnesacb4b992011-11-07 12:03:23 -08002268 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002269 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002270
2271 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002272 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002273
Daniel Vetter84849902012-12-02 00:28:11 +01002274 drm_modeset_lock_all(dev);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002275
2276 /* TODO check buffer is sufficiently large */
2277 /* TODO setup destructor callback */
2278
2279 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2280 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002281 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002282 drm_modeset_unlock_all(dev);
2283 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002284 }
2285
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002286 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002287 or->fb_id = fb->base.id;
2288 list_add(&fb->filp_head, &file_priv->fbs);
2289 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002290 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002291 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002292
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002293 return ret;
2294}
2295
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002296static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002297{
2298 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2299
2300 switch (format) {
2301 case DRM_FORMAT_C8:
2302 case DRM_FORMAT_RGB332:
2303 case DRM_FORMAT_BGR233:
2304 case DRM_FORMAT_XRGB4444:
2305 case DRM_FORMAT_XBGR4444:
2306 case DRM_FORMAT_RGBX4444:
2307 case DRM_FORMAT_BGRX4444:
2308 case DRM_FORMAT_ARGB4444:
2309 case DRM_FORMAT_ABGR4444:
2310 case DRM_FORMAT_RGBA4444:
2311 case DRM_FORMAT_BGRA4444:
2312 case DRM_FORMAT_XRGB1555:
2313 case DRM_FORMAT_XBGR1555:
2314 case DRM_FORMAT_RGBX5551:
2315 case DRM_FORMAT_BGRX5551:
2316 case DRM_FORMAT_ARGB1555:
2317 case DRM_FORMAT_ABGR1555:
2318 case DRM_FORMAT_RGBA5551:
2319 case DRM_FORMAT_BGRA5551:
2320 case DRM_FORMAT_RGB565:
2321 case DRM_FORMAT_BGR565:
2322 case DRM_FORMAT_RGB888:
2323 case DRM_FORMAT_BGR888:
2324 case DRM_FORMAT_XRGB8888:
2325 case DRM_FORMAT_XBGR8888:
2326 case DRM_FORMAT_RGBX8888:
2327 case DRM_FORMAT_BGRX8888:
2328 case DRM_FORMAT_ARGB8888:
2329 case DRM_FORMAT_ABGR8888:
2330 case DRM_FORMAT_RGBA8888:
2331 case DRM_FORMAT_BGRA8888:
2332 case DRM_FORMAT_XRGB2101010:
2333 case DRM_FORMAT_XBGR2101010:
2334 case DRM_FORMAT_RGBX1010102:
2335 case DRM_FORMAT_BGRX1010102:
2336 case DRM_FORMAT_ARGB2101010:
2337 case DRM_FORMAT_ABGR2101010:
2338 case DRM_FORMAT_RGBA1010102:
2339 case DRM_FORMAT_BGRA1010102:
2340 case DRM_FORMAT_YUYV:
2341 case DRM_FORMAT_YVYU:
2342 case DRM_FORMAT_UYVY:
2343 case DRM_FORMAT_VYUY:
2344 case DRM_FORMAT_AYUV:
2345 case DRM_FORMAT_NV12:
2346 case DRM_FORMAT_NV21:
2347 case DRM_FORMAT_NV16:
2348 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002349 case DRM_FORMAT_NV24:
2350 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002351 case DRM_FORMAT_YUV410:
2352 case DRM_FORMAT_YVU410:
2353 case DRM_FORMAT_YUV411:
2354 case DRM_FORMAT_YVU411:
2355 case DRM_FORMAT_YUV420:
2356 case DRM_FORMAT_YVU420:
2357 case DRM_FORMAT_YUV422:
2358 case DRM_FORMAT_YVU422:
2359 case DRM_FORMAT_YUV444:
2360 case DRM_FORMAT_YVU444:
2361 return 0;
2362 default:
2363 return -EINVAL;
2364 }
2365}
2366
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002367static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002368{
2369 int ret, hsub, vsub, num_planes, i;
2370
2371 ret = format_check(r);
2372 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002373 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002374 return ret;
2375 }
2376
2377 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2378 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2379 num_planes = drm_format_num_planes(r->pixel_format);
2380
2381 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002382 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002383 return -EINVAL;
2384 }
2385
2386 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002387 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002388 return -EINVAL;
2389 }
2390
2391 for (i = 0; i < num_planes; i++) {
2392 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002393 unsigned int height = r->height / (i != 0 ? vsub : 1);
2394 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002395
2396 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002397 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002398 return -EINVAL;
2399 }
2400
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002401 if ((uint64_t) width * cpp > UINT_MAX)
2402 return -ERANGE;
2403
2404 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2405 return -ERANGE;
2406
2407 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002408 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002409 return -EINVAL;
2410 }
2411 }
2412
2413 return 0;
2414}
2415
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002416/**
2417 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002418 * @dev: drm device for the ioctl
2419 * @data: data pointer for the ioctl
2420 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002421 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002422 * Add a new FB to the specified CRTC, given a user request with format.
2423 *
2424 * Called by the user via ioctl.
2425 *
2426 * RETURNS:
2427 * Zero on success, errno on failure.
2428 */
2429int drm_mode_addfb2(struct drm_device *dev,
2430 void *data, struct drm_file *file_priv)
2431{
2432 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002433 struct drm_mode_config *config = &dev->mode_config;
2434 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002435 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002436
Dave Airliefb3b06c2011-02-08 13:55:21 +10002437 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2438 return -EINVAL;
2439
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002440 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2441 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2442 return -EINVAL;
2443 }
2444
Dave Airlief453ba02008-11-07 14:05:41 -08002445 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002446 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002447 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002448 return -EINVAL;
2449 }
2450 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002451 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002452 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002453 return -EINVAL;
2454 }
2455
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002456 ret = framebuffer_check(r);
2457 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002458 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002459
Daniel Vetter84849902012-12-02 00:28:11 +01002460 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002461
Dave Airlief453ba02008-11-07 14:05:41 -08002462 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002463 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002464 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002465 drm_modeset_unlock_all(dev);
2466 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002467 }
2468
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002469 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002470 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002471 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002472 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002473 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002474
Daniel Vetter84849902012-12-02 00:28:11 +01002475 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002476
Dave Airlief453ba02008-11-07 14:05:41 -08002477 return ret;
2478}
2479
2480/**
2481 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002482 * @dev: drm device for the ioctl
2483 * @data: data pointer for the ioctl
2484 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002485 *
Dave Airlief453ba02008-11-07 14:05:41 -08002486 * Remove the FB specified by the user.
2487 *
2488 * Called by the user via ioctl.
2489 *
2490 * RETURNS:
2491 * Zero on success, errno on failure.
2492 */
2493int drm_mode_rmfb(struct drm_device *dev,
2494 void *data, struct drm_file *file_priv)
2495{
Dave Airlief453ba02008-11-07 14:05:41 -08002496 struct drm_framebuffer *fb = NULL;
2497 struct drm_framebuffer *fbl = NULL;
2498 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002499 int found = 0;
2500
Dave Airliefb3b06c2011-02-08 13:55:21 +10002501 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2502 return -EINVAL;
2503
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002504 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002505 mutex_lock(&dev->mode_config.fb_lock);
2506 fb = __drm_framebuffer_lookup(dev, *id);
2507 if (!fb)
2508 goto fail_lookup;
2509
Dave Airlief453ba02008-11-07 14:05:41 -08002510 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2511 if (fb == fbl)
2512 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002513 if (!found)
2514 goto fail_lookup;
2515
2516 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2517 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002518
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002519 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002520 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002521 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002522
Daniel Vetter2b677e82012-12-10 21:16:05 +01002523 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002524 drm_framebuffer_remove(fb);
Daniel Vetter84849902012-12-02 00:28:11 +01002525 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002526
Daniel Vetter2b677e82012-12-10 21:16:05 +01002527 return 0;
2528
2529fail_lookup:
2530 mutex_unlock(&dev->mode_config.fb_lock);
2531 mutex_unlock(&file_priv->fbs_lock);
2532
2533 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002534}
2535
2536/**
2537 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002538 * @dev: drm device for the ioctl
2539 * @data: data pointer for the ioctl
2540 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002541 *
Dave Airlief453ba02008-11-07 14:05:41 -08002542 * Lookup the FB given its ID and return info about it.
2543 *
2544 * Called by the user via ioctl.
2545 *
2546 * RETURNS:
2547 * Zero on success, errno on failure.
2548 */
2549int drm_mode_getfb(struct drm_device *dev,
2550 void *data, struct drm_file *file_priv)
2551{
2552 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002553 struct drm_framebuffer *fb;
2554 int ret = 0;
2555
Dave Airliefb3b06c2011-02-08 13:55:21 +10002556 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2557 return -EINVAL;
2558
Daniel Vetter84849902012-12-02 00:28:11 +01002559 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002560 fb = drm_framebuffer_lookup(dev, r->fb_id);
2561 if (!fb) {
Dave Airlief453ba02008-11-07 14:05:41 -08002562 ret = -EINVAL;
2563 goto out;
2564 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002565 /* fb is protect by the mode_config lock, so drop the ref immediately */
2566 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002567
2568 r->height = fb->height;
2569 r->width = fb->width;
2570 r->depth = fb->depth;
2571 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002572 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002573 if (fb->funcs->create_handle)
2574 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2575 else
2576 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002577
2578out:
Daniel Vetter84849902012-12-02 00:28:11 +01002579 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002580 return ret;
2581}
2582
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002583int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2584 void *data, struct drm_file *file_priv)
2585{
2586 struct drm_clip_rect __user *clips_ptr;
2587 struct drm_clip_rect *clips = NULL;
2588 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002589 struct drm_framebuffer *fb;
2590 unsigned flags;
2591 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002592 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002593
Dave Airliefb3b06c2011-02-08 13:55:21 +10002594 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2595 return -EINVAL;
2596
Daniel Vetter84849902012-12-02 00:28:11 +01002597 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002598 fb = drm_framebuffer_lookup(dev, r->fb_id);
2599 if (!fb) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002600 ret = -EINVAL;
2601 goto out_err1;
2602 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002603 /* fb is protect by the mode_config lock, so drop the ref immediately */
2604 drm_framebuffer_unreference(fb);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002605
2606 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002607 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002608
2609 if (!num_clips != !clips_ptr) {
2610 ret = -EINVAL;
2611 goto out_err1;
2612 }
2613
2614 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2615
2616 /* If userspace annotates copy, clips must come in pairs */
2617 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2618 ret = -EINVAL;
2619 goto out_err1;
2620 }
2621
2622 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002623 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2624 ret = -EINVAL;
2625 goto out_err1;
2626 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002627 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2628 if (!clips) {
2629 ret = -ENOMEM;
2630 goto out_err1;
2631 }
2632
2633 ret = copy_from_user(clips, clips_ptr,
2634 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002635 if (ret) {
2636 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002637 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002638 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002639 }
2640
2641 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002642 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2643 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002644 } else {
2645 ret = -ENOSYS;
2646 goto out_err2;
2647 }
2648
2649out_err2:
2650 kfree(clips);
2651out_err1:
Daniel Vetter84849902012-12-02 00:28:11 +01002652 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002653 return ret;
2654}
2655
2656
Dave Airlief453ba02008-11-07 14:05:41 -08002657/**
2658 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002659 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002660 *
Dave Airlief453ba02008-11-07 14:05:41 -08002661 * Destroy all the FBs associated with @filp.
2662 *
2663 * Called by the user via ioctl.
2664 *
2665 * RETURNS:
2666 * Zero on success, errno on failure.
2667 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002668void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002669{
Dave Airlief453ba02008-11-07 14:05:41 -08002670 struct drm_device *dev = priv->minor->dev;
2671 struct drm_framebuffer *fb, *tfb;
2672
Daniel Vetter84849902012-12-02 00:28:11 +01002673 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002674 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002675 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002676
2677 mutex_lock(&dev->mode_config.fb_lock);
2678 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2679 __drm_framebuffer_unregister(dev, fb);
2680 mutex_unlock(&dev->mode_config.fb_lock);
2681
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002682 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002683
2684 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002685 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002686 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002687 mutex_unlock(&priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002688 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002689}
2690
2691/**
2692 * drm_mode_attachmode - add a mode to the user mode list
2693 * @dev: DRM device
2694 * @connector: connector to add the mode to
2695 * @mode: mode to add
2696 *
2697 * Add @mode to @connector's user mode list.
2698 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002699static void drm_mode_attachmode(struct drm_device *dev,
2700 struct drm_connector *connector,
2701 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002702{
Dave Airlief453ba02008-11-07 14:05:41 -08002703 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002704}
2705
2706int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002707 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002708{
2709 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002710 int ret = 0;
2711 struct drm_display_mode *dup_mode, *next;
2712 LIST_HEAD(list);
2713
Dave Airlief453ba02008-11-07 14:05:41 -08002714 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2715 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002716 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002717 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002718 dup_mode = drm_mode_duplicate(dev, mode);
2719 if (!dup_mode) {
2720 ret = -ENOMEM;
2721 goto out;
2722 }
2723 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002724 }
2725 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002726
2727 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2728 if (!connector->encoder)
2729 continue;
2730 if (connector->encoder->crtc == crtc)
2731 list_move_tail(list.next, &connector->user_modes);
2732 }
2733
2734 WARN_ON(!list_empty(&list));
2735
2736 out:
2737 list_for_each_entry_safe(dup_mode, next, &list, head)
2738 drm_mode_destroy(dev, dup_mode);
2739
2740 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002741}
2742EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2743
2744static int drm_mode_detachmode(struct drm_device *dev,
2745 struct drm_connector *connector,
2746 struct drm_display_mode *mode)
2747{
2748 int found = 0;
2749 int ret = 0;
2750 struct drm_display_mode *match_mode, *t;
2751
2752 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2753 if (drm_mode_equal(match_mode, mode)) {
2754 list_del(&match_mode->head);
2755 drm_mode_destroy(dev, match_mode);
2756 found = 1;
2757 break;
2758 }
2759 }
2760
2761 if (!found)
2762 ret = -EINVAL;
2763
2764 return ret;
2765}
2766
2767int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2768{
2769 struct drm_connector *connector;
2770
2771 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2772 drm_mode_detachmode(dev, connector, mode);
2773 }
2774 return 0;
2775}
2776EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2777
2778/**
2779 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002780 * @dev: drm device for the ioctl
2781 * @data: data pointer for the ioctl
2782 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002783 *
2784 * This attaches a user specified mode to an connector.
2785 * Called by the user via ioctl.
2786 *
2787 * RETURNS:
2788 * Zero on success, errno on failure.
2789 */
2790int drm_mode_attachmode_ioctl(struct drm_device *dev,
2791 void *data, struct drm_file *file_priv)
2792{
2793 struct drm_mode_mode_cmd *mode_cmd = data;
2794 struct drm_connector *connector;
2795 struct drm_display_mode *mode;
2796 struct drm_mode_object *obj;
2797 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002798 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002799
Dave Airliefb3b06c2011-02-08 13:55:21 +10002800 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2801 return -EINVAL;
2802
Daniel Vetter84849902012-12-02 00:28:11 +01002803 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002804
2805 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2806 if (!obj) {
2807 ret = -EINVAL;
2808 goto out;
2809 }
2810 connector = obj_to_connector(obj);
2811
2812 mode = drm_mode_create(dev);
2813 if (!mode) {
2814 ret = -ENOMEM;
2815 goto out;
2816 }
2817
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002818 ret = drm_crtc_convert_umode(mode, umode);
2819 if (ret) {
2820 DRM_DEBUG_KMS("Invalid mode\n");
2821 drm_mode_destroy(dev, mode);
2822 goto out;
2823 }
Dave Airlief453ba02008-11-07 14:05:41 -08002824
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002825 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002826out:
Daniel Vetter84849902012-12-02 00:28:11 +01002827 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002828 return ret;
2829}
2830
2831
2832/**
2833 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002834 * @dev: drm device for the ioctl
2835 * @data: data pointer for the ioctl
2836 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002837 *
2838 * Called by the user via ioctl.
2839 *
2840 * RETURNS:
2841 * Zero on success, errno on failure.
2842 */
2843int drm_mode_detachmode_ioctl(struct drm_device *dev,
2844 void *data, struct drm_file *file_priv)
2845{
2846 struct drm_mode_object *obj;
2847 struct drm_mode_mode_cmd *mode_cmd = data;
2848 struct drm_connector *connector;
2849 struct drm_display_mode mode;
2850 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002851 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002852
Dave Airliefb3b06c2011-02-08 13:55:21 +10002853 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2854 return -EINVAL;
2855
Daniel Vetter84849902012-12-02 00:28:11 +01002856 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002857
2858 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2859 if (!obj) {
2860 ret = -EINVAL;
2861 goto out;
2862 }
2863 connector = obj_to_connector(obj);
2864
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002865 ret = drm_crtc_convert_umode(&mode, umode);
2866 if (ret) {
2867 DRM_DEBUG_KMS("Invalid mode\n");
2868 goto out;
2869 }
2870
Dave Airlief453ba02008-11-07 14:05:41 -08002871 ret = drm_mode_detachmode(dev, connector, &mode);
2872out:
Daniel Vetter84849902012-12-02 00:28:11 +01002873 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002874 return ret;
2875}
2876
2877struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2878 const char *name, int num_values)
2879{
2880 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002881 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002882
2883 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2884 if (!property)
2885 return NULL;
2886
2887 if (num_values) {
2888 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2889 if (!property->values)
2890 goto fail;
2891 }
2892
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002893 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2894 if (ret)
2895 goto fail;
2896
Dave Airlief453ba02008-11-07 14:05:41 -08002897 property->flags = flags;
2898 property->num_values = num_values;
2899 INIT_LIST_HEAD(&property->enum_blob_list);
2900
Vinson Lee471dd2e2011-11-10 11:55:40 -08002901 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002902 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002903 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2904 }
Dave Airlief453ba02008-11-07 14:05:41 -08002905
2906 list_add_tail(&property->head, &dev->mode_config.property_list);
2907 return property;
2908fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002909 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002910 kfree(property);
2911 return NULL;
2912}
2913EXPORT_SYMBOL(drm_property_create);
2914
Sascha Hauer4a67d392012-02-06 10:58:17 +01002915struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2916 const char *name,
2917 const struct drm_prop_enum_list *props,
2918 int num_values)
2919{
2920 struct drm_property *property;
2921 int i, ret;
2922
2923 flags |= DRM_MODE_PROP_ENUM;
2924
2925 property = drm_property_create(dev, flags, name, num_values);
2926 if (!property)
2927 return NULL;
2928
2929 for (i = 0; i < num_values; i++) {
2930 ret = drm_property_add_enum(property, i,
2931 props[i].type,
2932 props[i].name);
2933 if (ret) {
2934 drm_property_destroy(dev, property);
2935 return NULL;
2936 }
2937 }
2938
2939 return property;
2940}
2941EXPORT_SYMBOL(drm_property_create_enum);
2942
Rob Clark49e27542012-05-17 02:23:26 -06002943struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2944 int flags, const char *name,
2945 const struct drm_prop_enum_list *props,
2946 int num_values)
2947{
2948 struct drm_property *property;
2949 int i, ret;
2950
2951 flags |= DRM_MODE_PROP_BITMASK;
2952
2953 property = drm_property_create(dev, flags, name, num_values);
2954 if (!property)
2955 return NULL;
2956
2957 for (i = 0; i < num_values; i++) {
2958 ret = drm_property_add_enum(property, i,
2959 props[i].type,
2960 props[i].name);
2961 if (ret) {
2962 drm_property_destroy(dev, property);
2963 return NULL;
2964 }
2965 }
2966
2967 return property;
2968}
2969EXPORT_SYMBOL(drm_property_create_bitmask);
2970
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002971struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2972 const char *name,
2973 uint64_t min, uint64_t max)
2974{
2975 struct drm_property *property;
2976
2977 flags |= DRM_MODE_PROP_RANGE;
2978
2979 property = drm_property_create(dev, flags, name, 2);
2980 if (!property)
2981 return NULL;
2982
2983 property->values[0] = min;
2984 property->values[1] = max;
2985
2986 return property;
2987}
2988EXPORT_SYMBOL(drm_property_create_range);
2989
Dave Airlief453ba02008-11-07 14:05:41 -08002990int drm_property_add_enum(struct drm_property *property, int index,
2991 uint64_t value, const char *name)
2992{
2993 struct drm_property_enum *prop_enum;
2994
Rob Clark49e27542012-05-17 02:23:26 -06002995 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2996 return -EINVAL;
2997
2998 /*
2999 * Bitmask enum properties have the additional constraint of values
3000 * from 0 to 63
3001 */
3002 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003003 return -EINVAL;
3004
3005 if (!list_empty(&property->enum_blob_list)) {
3006 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3007 if (prop_enum->value == value) {
3008 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3009 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3010 return 0;
3011 }
3012 }
3013 }
3014
3015 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3016 if (!prop_enum)
3017 return -ENOMEM;
3018
3019 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3020 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3021 prop_enum->value = value;
3022
3023 property->values[index] = value;
3024 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3025 return 0;
3026}
3027EXPORT_SYMBOL(drm_property_add_enum);
3028
3029void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3030{
3031 struct drm_property_enum *prop_enum, *pt;
3032
3033 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3034 list_del(&prop_enum->head);
3035 kfree(prop_enum);
3036 }
3037
3038 if (property->num_values)
3039 kfree(property->values);
3040 drm_mode_object_put(dev, &property->base);
3041 list_del(&property->head);
3042 kfree(property);
3043}
3044EXPORT_SYMBOL(drm_property_destroy);
3045
Paulo Zanonic5431882012-05-15 18:09:02 -03003046void drm_object_attach_property(struct drm_mode_object *obj,
3047 struct drm_property *property,
3048 uint64_t init_val)
3049{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003050 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003051
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003052 if (count == DRM_OBJECT_MAX_PROPERTY) {
3053 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3054 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3055 "you see this message on the same object type.\n",
3056 obj->type);
3057 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003058 }
3059
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003060 obj->properties->ids[count] = property->base.id;
3061 obj->properties->values[count] = init_val;
3062 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003063}
3064EXPORT_SYMBOL(drm_object_attach_property);
3065
3066int drm_object_property_set_value(struct drm_mode_object *obj,
3067 struct drm_property *property, uint64_t val)
3068{
3069 int i;
3070
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003071 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003072 if (obj->properties->ids[i] == property->base.id) {
3073 obj->properties->values[i] = val;
3074 return 0;
3075 }
3076 }
3077
3078 return -EINVAL;
3079}
3080EXPORT_SYMBOL(drm_object_property_set_value);
3081
3082int drm_object_property_get_value(struct drm_mode_object *obj,
3083 struct drm_property *property, uint64_t *val)
3084{
3085 int i;
3086
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003087 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003088 if (obj->properties->ids[i] == property->base.id) {
3089 *val = obj->properties->values[i];
3090 return 0;
3091 }
3092 }
3093
3094 return -EINVAL;
3095}
3096EXPORT_SYMBOL(drm_object_property_get_value);
3097
Dave Airlief453ba02008-11-07 14:05:41 -08003098int drm_mode_getproperty_ioctl(struct drm_device *dev,
3099 void *data, struct drm_file *file_priv)
3100{
3101 struct drm_mode_object *obj;
3102 struct drm_mode_get_property *out_resp = data;
3103 struct drm_property *property;
3104 int enum_count = 0;
3105 int blob_count = 0;
3106 int value_count = 0;
3107 int ret = 0, i;
3108 int copied;
3109 struct drm_property_enum *prop_enum;
3110 struct drm_mode_property_enum __user *enum_ptr;
3111 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003112 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003113 uint64_t __user *values_ptr;
3114 uint32_t __user *blob_length_ptr;
3115
Dave Airliefb3b06c2011-02-08 13:55:21 +10003116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3117 return -EINVAL;
3118
Daniel Vetter84849902012-12-02 00:28:11 +01003119 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003120 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3121 if (!obj) {
3122 ret = -EINVAL;
3123 goto done;
3124 }
3125 property = obj_to_property(obj);
3126
Rob Clark49e27542012-05-17 02:23:26 -06003127 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003128 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3129 enum_count++;
3130 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3131 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3132 blob_count++;
3133 }
3134
3135 value_count = property->num_values;
3136
3137 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3138 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3139 out_resp->flags = property->flags;
3140
3141 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003142 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003143 for (i = 0; i < value_count; i++) {
3144 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3145 ret = -EFAULT;
3146 goto done;
3147 }
3148 }
3149 }
3150 out_resp->count_values = value_count;
3151
Rob Clark49e27542012-05-17 02:23:26 -06003152 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003153 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3154 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003155 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003156 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3157
3158 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3159 ret = -EFAULT;
3160 goto done;
3161 }
3162
3163 if (copy_to_user(&enum_ptr[copied].name,
3164 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3165 ret = -EFAULT;
3166 goto done;
3167 }
3168 copied++;
3169 }
3170 }
3171 out_resp->count_enum_blobs = enum_count;
3172 }
3173
3174 if (property->flags & DRM_MODE_PROP_BLOB) {
3175 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3176 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003177 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3178 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003179
3180 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3181 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3182 ret = -EFAULT;
3183 goto done;
3184 }
3185
3186 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3187 ret = -EFAULT;
3188 goto done;
3189 }
3190
3191 copied++;
3192 }
3193 }
3194 out_resp->count_enum_blobs = blob_count;
3195 }
3196done:
Daniel Vetter84849902012-12-02 00:28:11 +01003197 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003198 return ret;
3199}
3200
3201static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3202 void *data)
3203{
3204 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003205 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003206
3207 if (!length || !data)
3208 return NULL;
3209
3210 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3211 if (!blob)
3212 return NULL;
3213
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003214 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3215 if (ret) {
3216 kfree(blob);
3217 return NULL;
3218 }
3219
Dave Airlief453ba02008-11-07 14:05:41 -08003220 blob->length = length;
3221
3222 memcpy(blob->data, data, length);
3223
Dave Airlief453ba02008-11-07 14:05:41 -08003224 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3225 return blob;
3226}
3227
3228static void drm_property_destroy_blob(struct drm_device *dev,
3229 struct drm_property_blob *blob)
3230{
3231 drm_mode_object_put(dev, &blob->base);
3232 list_del(&blob->head);
3233 kfree(blob);
3234}
3235
3236int drm_mode_getblob_ioctl(struct drm_device *dev,
3237 void *data, struct drm_file *file_priv)
3238{
3239 struct drm_mode_object *obj;
3240 struct drm_mode_get_blob *out_resp = data;
3241 struct drm_property_blob *blob;
3242 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003243 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003244
Dave Airliefb3b06c2011-02-08 13:55:21 +10003245 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3246 return -EINVAL;
3247
Daniel Vetter84849902012-12-02 00:28:11 +01003248 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003249 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3250 if (!obj) {
3251 ret = -EINVAL;
3252 goto done;
3253 }
3254 blob = obj_to_blob(obj);
3255
3256 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003257 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003258 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3259 ret = -EFAULT;
3260 goto done;
3261 }
3262 }
3263 out_resp->length = blob->length;
3264
3265done:
Daniel Vetter84849902012-12-02 00:28:11 +01003266 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003267 return ret;
3268}
3269
3270int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3271 struct edid *edid)
3272{
3273 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003274 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003275
3276 if (connector->edid_blob_ptr)
3277 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3278
3279 /* Delete edid, when there is none. */
3280 if (!edid) {
3281 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003282 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003283 return ret;
3284 }
3285
Adam Jackson7466f4c2010-03-29 21:43:23 +00003286 size = EDID_LENGTH * (1 + edid->extensions);
3287 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3288 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003289 if (!connector->edid_blob_ptr)
3290 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003291
Rob Clark58495562012-10-11 20:50:56 -05003292 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003293 dev->mode_config.edid_property,
3294 connector->edid_blob_ptr->base.id);
3295
3296 return ret;
3297}
3298EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3299
Paulo Zanoni26a34812012-05-15 18:08:59 -03003300static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003301 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003302{
3303 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3304 return false;
3305 if (property->flags & DRM_MODE_PROP_RANGE) {
3306 if (value < property->values[0] || value > property->values[1])
3307 return false;
3308 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003309 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3310 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003311 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003312 for (i = 0; i < property->num_values; i++)
3313 valid_mask |= (1ULL << property->values[i]);
3314 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003315 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3316 /* Only the driver knows */
3317 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003318 } else {
3319 int i;
3320 for (i = 0; i < property->num_values; i++)
3321 if (property->values[i] == value)
3322 return true;
3323 return false;
3324 }
3325}
3326
Dave Airlief453ba02008-11-07 14:05:41 -08003327int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3328 void *data, struct drm_file *file_priv)
3329{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003330 struct drm_mode_connector_set_property *conn_set_prop = data;
3331 struct drm_mode_obj_set_property obj_set_prop = {
3332 .value = conn_set_prop->value,
3333 .prop_id = conn_set_prop->prop_id,
3334 .obj_id = conn_set_prop->connector_id,
3335 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3336 };
Dave Airlief453ba02008-11-07 14:05:41 -08003337
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003338 /* It does all the locking and checking we need */
3339 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003340}
3341
Paulo Zanonic5431882012-05-15 18:09:02 -03003342static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3343 struct drm_property *property,
3344 uint64_t value)
3345{
3346 int ret = -EINVAL;
3347 struct drm_connector *connector = obj_to_connector(obj);
3348
3349 /* Do DPMS ourselves */
3350 if (property == connector->dev->mode_config.dpms_property) {
3351 if (connector->funcs->dpms)
3352 (*connector->funcs->dpms)(connector, (int)value);
3353 ret = 0;
3354 } else if (connector->funcs->set_property)
3355 ret = connector->funcs->set_property(connector, property, value);
3356
3357 /* store the property value if successful */
3358 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003359 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003360 return ret;
3361}
3362
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003363static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3364 struct drm_property *property,
3365 uint64_t value)
3366{
3367 int ret = -EINVAL;
3368 struct drm_crtc *crtc = obj_to_crtc(obj);
3369
3370 if (crtc->funcs->set_property)
3371 ret = crtc->funcs->set_property(crtc, property, value);
3372 if (!ret)
3373 drm_object_property_set_value(obj, property, value);
3374
3375 return ret;
3376}
3377
Rob Clark4d939142012-05-17 02:23:27 -06003378static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3379 struct drm_property *property,
3380 uint64_t value)
3381{
3382 int ret = -EINVAL;
3383 struct drm_plane *plane = obj_to_plane(obj);
3384
3385 if (plane->funcs->set_property)
3386 ret = plane->funcs->set_property(plane, property, value);
3387 if (!ret)
3388 drm_object_property_set_value(obj, property, value);
3389
3390 return ret;
3391}
3392
Paulo Zanonic5431882012-05-15 18:09:02 -03003393int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3394 struct drm_file *file_priv)
3395{
3396 struct drm_mode_obj_get_properties *arg = data;
3397 struct drm_mode_object *obj;
3398 int ret = 0;
3399 int i;
3400 int copied = 0;
3401 int props_count = 0;
3402 uint32_t __user *props_ptr;
3403 uint64_t __user *prop_values_ptr;
3404
3405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3406 return -EINVAL;
3407
Daniel Vetter84849902012-12-02 00:28:11 +01003408 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003409
3410 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3411 if (!obj) {
3412 ret = -EINVAL;
3413 goto out;
3414 }
3415 if (!obj->properties) {
3416 ret = -EINVAL;
3417 goto out;
3418 }
3419
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003420 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003421
3422 /* This ioctl is called twice, once to determine how much space is
3423 * needed, and the 2nd time to fill it. */
3424 if ((arg->count_props >= props_count) && props_count) {
3425 copied = 0;
3426 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3427 prop_values_ptr = (uint64_t __user *)(unsigned long)
3428 (arg->prop_values_ptr);
3429 for (i = 0; i < props_count; i++) {
3430 if (put_user(obj->properties->ids[i],
3431 props_ptr + copied)) {
3432 ret = -EFAULT;
3433 goto out;
3434 }
3435 if (put_user(obj->properties->values[i],
3436 prop_values_ptr + copied)) {
3437 ret = -EFAULT;
3438 goto out;
3439 }
3440 copied++;
3441 }
3442 }
3443 arg->count_props = props_count;
3444out:
Daniel Vetter84849902012-12-02 00:28:11 +01003445 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003446 return ret;
3447}
3448
3449int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3450 struct drm_file *file_priv)
3451{
3452 struct drm_mode_obj_set_property *arg = data;
3453 struct drm_mode_object *arg_obj;
3454 struct drm_mode_object *prop_obj;
3455 struct drm_property *property;
3456 int ret = -EINVAL;
3457 int i;
3458
3459 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3460 return -EINVAL;
3461
Daniel Vetter84849902012-12-02 00:28:11 +01003462 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003463
3464 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3465 if (!arg_obj)
3466 goto out;
3467 if (!arg_obj->properties)
3468 goto out;
3469
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003470 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003471 if (arg_obj->properties->ids[i] == arg->prop_id)
3472 break;
3473
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003474 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003475 goto out;
3476
3477 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3478 DRM_MODE_OBJECT_PROPERTY);
3479 if (!prop_obj)
3480 goto out;
3481 property = obj_to_property(prop_obj);
3482
3483 if (!drm_property_change_is_valid(property, arg->value))
3484 goto out;
3485
3486 switch (arg_obj->type) {
3487 case DRM_MODE_OBJECT_CONNECTOR:
3488 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3489 arg->value);
3490 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003491 case DRM_MODE_OBJECT_CRTC:
3492 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3493 break;
Rob Clark4d939142012-05-17 02:23:27 -06003494 case DRM_MODE_OBJECT_PLANE:
3495 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3496 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003497 }
3498
3499out:
Daniel Vetter84849902012-12-02 00:28:11 +01003500 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003501 return ret;
3502}
3503
Dave Airlief453ba02008-11-07 14:05:41 -08003504int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3505 struct drm_encoder *encoder)
3506{
3507 int i;
3508
3509 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3510 if (connector->encoder_ids[i] == 0) {
3511 connector->encoder_ids[i] = encoder->base.id;
3512 return 0;
3513 }
3514 }
3515 return -ENOMEM;
3516}
3517EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3518
3519void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3520 struct drm_encoder *encoder)
3521{
3522 int i;
3523 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3524 if (connector->encoder_ids[i] == encoder->base.id) {
3525 connector->encoder_ids[i] = 0;
3526 if (connector->encoder == encoder)
3527 connector->encoder = NULL;
3528 break;
3529 }
3530 }
3531}
3532EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3533
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003534int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003535 int gamma_size)
3536{
3537 crtc->gamma_size = gamma_size;
3538
3539 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3540 if (!crtc->gamma_store) {
3541 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003542 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003543 }
3544
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003545 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003546}
3547EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3548
3549int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3550 void *data, struct drm_file *file_priv)
3551{
3552 struct drm_mode_crtc_lut *crtc_lut = data;
3553 struct drm_mode_object *obj;
3554 struct drm_crtc *crtc;
3555 void *r_base, *g_base, *b_base;
3556 int size;
3557 int ret = 0;
3558
Dave Airliefb3b06c2011-02-08 13:55:21 +10003559 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3560 return -EINVAL;
3561
Daniel Vetter84849902012-12-02 00:28:11 +01003562 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003563 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3564 if (!obj) {
3565 ret = -EINVAL;
3566 goto out;
3567 }
3568 crtc = obj_to_crtc(obj);
3569
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003570 if (crtc->funcs->gamma_set == NULL) {
3571 ret = -ENOSYS;
3572 goto out;
3573 }
3574
Dave Airlief453ba02008-11-07 14:05:41 -08003575 /* memcpy into gamma store */
3576 if (crtc_lut->gamma_size != crtc->gamma_size) {
3577 ret = -EINVAL;
3578 goto out;
3579 }
3580
3581 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3582 r_base = crtc->gamma_store;
3583 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3584 ret = -EFAULT;
3585 goto out;
3586 }
3587
3588 g_base = r_base + size;
3589 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3590 ret = -EFAULT;
3591 goto out;
3592 }
3593
3594 b_base = g_base + size;
3595 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3596 ret = -EFAULT;
3597 goto out;
3598 }
3599
James Simmons72034252010-08-03 01:33:19 +01003600 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003601
3602out:
Daniel Vetter84849902012-12-02 00:28:11 +01003603 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003604 return ret;
3605
3606}
3607
3608int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3609 void *data, struct drm_file *file_priv)
3610{
3611 struct drm_mode_crtc_lut *crtc_lut = data;
3612 struct drm_mode_object *obj;
3613 struct drm_crtc *crtc;
3614 void *r_base, *g_base, *b_base;
3615 int size;
3616 int ret = 0;
3617
Dave Airliefb3b06c2011-02-08 13:55:21 +10003618 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3619 return -EINVAL;
3620
Daniel Vetter84849902012-12-02 00:28:11 +01003621 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003622 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3623 if (!obj) {
3624 ret = -EINVAL;
3625 goto out;
3626 }
3627 crtc = obj_to_crtc(obj);
3628
3629 /* memcpy into gamma store */
3630 if (crtc_lut->gamma_size != crtc->gamma_size) {
3631 ret = -EINVAL;
3632 goto out;
3633 }
3634
3635 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3636 r_base = crtc->gamma_store;
3637 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3638 ret = -EFAULT;
3639 goto out;
3640 }
3641
3642 g_base = r_base + size;
3643 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3644 ret = -EFAULT;
3645 goto out;
3646 }
3647
3648 b_base = g_base + size;
3649 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3650 ret = -EFAULT;
3651 goto out;
3652 }
3653out:
Daniel Vetter84849902012-12-02 00:28:11 +01003654 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003655 return ret;
3656}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003657
3658int drm_mode_page_flip_ioctl(struct drm_device *dev,
3659 void *data, struct drm_file *file_priv)
3660{
3661 struct drm_mode_crtc_page_flip *page_flip = data;
3662 struct drm_mode_object *obj;
3663 struct drm_crtc *crtc;
3664 struct drm_framebuffer *fb;
3665 struct drm_pending_vblank_event *e = NULL;
3666 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003667 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003668 int ret = -EINVAL;
3669
3670 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3671 page_flip->reserved != 0)
3672 return -EINVAL;
3673
Daniel Vetter84849902012-12-02 00:28:11 +01003674 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003675 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3676 if (!obj)
3677 goto out;
3678 crtc = obj_to_crtc(obj);
3679
Chris Wilson90c1efd2010-07-17 20:23:26 +01003680 if (crtc->fb == NULL) {
3681 /* The framebuffer is currently unbound, presumably
3682 * due to a hotplug event, that userspace has not
3683 * yet discovered.
3684 */
3685 ret = -EBUSY;
3686 goto out;
3687 }
3688
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003689 if (crtc->funcs->page_flip == NULL)
3690 goto out;
3691
Daniel Vetter786b99e2012-12-02 21:53:40 +01003692 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3693 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003694 goto out;
Daniel Vetter786b99e2012-12-02 21:53:40 +01003695 /* fb is protect by the mode_config lock, so drop the ref immediately */
3696 drm_framebuffer_unreference(fb);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003697
Rob Clark7c80e122012-09-04 16:35:56 +00003698 hdisplay = crtc->mode.hdisplay;
3699 vdisplay = crtc->mode.vdisplay;
3700
3701 if (crtc->invert_dimensions)
3702 swap(hdisplay, vdisplay);
3703
3704 if (hdisplay > fb->width ||
3705 vdisplay > fb->height ||
3706 crtc->x > fb->width - hdisplay ||
3707 crtc->y > fb->height - vdisplay) {
3708 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3709 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3710 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003711 ret = -ENOSPC;
3712 goto out;
3713 }
3714
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003715 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3716 ret = -ENOMEM;
3717 spin_lock_irqsave(&dev->event_lock, flags);
3718 if (file_priv->event_space < sizeof e->event) {
3719 spin_unlock_irqrestore(&dev->event_lock, flags);
3720 goto out;
3721 }
3722 file_priv->event_space -= sizeof e->event;
3723 spin_unlock_irqrestore(&dev->event_lock, flags);
3724
3725 e = kzalloc(sizeof *e, GFP_KERNEL);
3726 if (e == NULL) {
3727 spin_lock_irqsave(&dev->event_lock, flags);
3728 file_priv->event_space += sizeof e->event;
3729 spin_unlock_irqrestore(&dev->event_lock, flags);
3730 goto out;
3731 }
3732
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003733 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003734 e->event.base.length = sizeof e->event;
3735 e->event.user_data = page_flip->user_data;
3736 e->base.event = &e->event.base;
3737 e->base.file_priv = file_priv;
3738 e->base.destroy =
3739 (void (*) (struct drm_pending_event *)) kfree;
3740 }
3741
3742 ret = crtc->funcs->page_flip(crtc, fb, e);
3743 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003744 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3745 spin_lock_irqsave(&dev->event_lock, flags);
3746 file_priv->event_space += sizeof e->event;
3747 spin_unlock_irqrestore(&dev->event_lock, flags);
3748 kfree(e);
3749 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003750 }
3751
3752out:
Daniel Vetter84849902012-12-02 00:28:11 +01003753 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003754 return ret;
3755}
Chris Wilsoneb0335562011-01-24 15:11:08 +00003756
3757void drm_mode_config_reset(struct drm_device *dev)
3758{
3759 struct drm_crtc *crtc;
3760 struct drm_encoder *encoder;
3761 struct drm_connector *connector;
3762
3763 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3764 if (crtc->funcs->reset)
3765 crtc->funcs->reset(crtc);
3766
3767 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3768 if (encoder->funcs->reset)
3769 encoder->funcs->reset(encoder);
3770
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003771 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3772 connector->status = connector_status_unknown;
3773
Chris Wilsoneb0335562011-01-24 15:11:08 +00003774 if (connector->funcs->reset)
3775 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003776 }
Chris Wilsoneb0335562011-01-24 15:11:08 +00003777}
3778EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003779
3780int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3781 void *data, struct drm_file *file_priv)
3782{
3783 struct drm_mode_create_dumb *args = data;
3784
3785 if (!dev->driver->dumb_create)
3786 return -ENOSYS;
3787 return dev->driver->dumb_create(file_priv, dev, args);
3788}
3789
3790int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3791 void *data, struct drm_file *file_priv)
3792{
3793 struct drm_mode_map_dumb *args = data;
3794
3795 /* call driver ioctl to get mmap offset */
3796 if (!dev->driver->dumb_map_offset)
3797 return -ENOSYS;
3798
3799 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3800}
3801
3802int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3803 void *data, struct drm_file *file_priv)
3804{
3805 struct drm_mode_destroy_dumb *args = data;
3806
3807 if (!dev->driver->dumb_destroy)
3808 return -ENOSYS;
3809
3810 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3811}
Dave Airlie248dbc22011-11-29 20:02:54 +00003812
3813/*
3814 * Just need to support RGB formats here for compat with code that doesn't
3815 * use pixel formats directly yet.
3816 */
3817void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3818 int *bpp)
3819{
3820 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003821 case DRM_FORMAT_RGB332:
3822 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003823 *depth = 8;
3824 *bpp = 8;
3825 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003826 case DRM_FORMAT_XRGB1555:
3827 case DRM_FORMAT_XBGR1555:
3828 case DRM_FORMAT_RGBX5551:
3829 case DRM_FORMAT_BGRX5551:
3830 case DRM_FORMAT_ARGB1555:
3831 case DRM_FORMAT_ABGR1555:
3832 case DRM_FORMAT_RGBA5551:
3833 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003834 *depth = 15;
3835 *bpp = 16;
3836 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003837 case DRM_FORMAT_RGB565:
3838 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003839 *depth = 16;
3840 *bpp = 16;
3841 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003842 case DRM_FORMAT_RGB888:
3843 case DRM_FORMAT_BGR888:
3844 *depth = 24;
3845 *bpp = 24;
3846 break;
3847 case DRM_FORMAT_XRGB8888:
3848 case DRM_FORMAT_XBGR8888:
3849 case DRM_FORMAT_RGBX8888:
3850 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003851 *depth = 24;
3852 *bpp = 32;
3853 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003854 case DRM_FORMAT_XRGB2101010:
3855 case DRM_FORMAT_XBGR2101010:
3856 case DRM_FORMAT_RGBX1010102:
3857 case DRM_FORMAT_BGRX1010102:
3858 case DRM_FORMAT_ARGB2101010:
3859 case DRM_FORMAT_ABGR2101010:
3860 case DRM_FORMAT_RGBA1010102:
3861 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003862 *depth = 30;
3863 *bpp = 32;
3864 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003865 case DRM_FORMAT_ARGB8888:
3866 case DRM_FORMAT_ABGR8888:
3867 case DRM_FORMAT_RGBA8888:
3868 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003869 *depth = 32;
3870 *bpp = 32;
3871 break;
3872 default:
3873 DRM_DEBUG_KMS("unsupported pixel format\n");
3874 *depth = 0;
3875 *bpp = 0;
3876 break;
3877 }
3878}
3879EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003880
3881/**
3882 * drm_format_num_planes - get the number of planes for format
3883 * @format: pixel format (DRM_FORMAT_*)
3884 *
3885 * RETURNS:
3886 * The number of planes used by the specified pixel format.
3887 */
3888int drm_format_num_planes(uint32_t format)
3889{
3890 switch (format) {
3891 case DRM_FORMAT_YUV410:
3892 case DRM_FORMAT_YVU410:
3893 case DRM_FORMAT_YUV411:
3894 case DRM_FORMAT_YVU411:
3895 case DRM_FORMAT_YUV420:
3896 case DRM_FORMAT_YVU420:
3897 case DRM_FORMAT_YUV422:
3898 case DRM_FORMAT_YVU422:
3899 case DRM_FORMAT_YUV444:
3900 case DRM_FORMAT_YVU444:
3901 return 3;
3902 case DRM_FORMAT_NV12:
3903 case DRM_FORMAT_NV21:
3904 case DRM_FORMAT_NV16:
3905 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003906 case DRM_FORMAT_NV24:
3907 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003908 return 2;
3909 default:
3910 return 1;
3911 }
3912}
3913EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003914
3915/**
3916 * drm_format_plane_cpp - determine the bytes per pixel value
3917 * @format: pixel format (DRM_FORMAT_*)
3918 * @plane: plane index
3919 *
3920 * RETURNS:
3921 * The bytes per pixel value for the specified plane.
3922 */
3923int drm_format_plane_cpp(uint32_t format, int plane)
3924{
3925 unsigned int depth;
3926 int bpp;
3927
3928 if (plane >= drm_format_num_planes(format))
3929 return 0;
3930
3931 switch (format) {
3932 case DRM_FORMAT_YUYV:
3933 case DRM_FORMAT_YVYU:
3934 case DRM_FORMAT_UYVY:
3935 case DRM_FORMAT_VYUY:
3936 return 2;
3937 case DRM_FORMAT_NV12:
3938 case DRM_FORMAT_NV21:
3939 case DRM_FORMAT_NV16:
3940 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003941 case DRM_FORMAT_NV24:
3942 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003943 return plane ? 2 : 1;
3944 case DRM_FORMAT_YUV410:
3945 case DRM_FORMAT_YVU410:
3946 case DRM_FORMAT_YUV411:
3947 case DRM_FORMAT_YVU411:
3948 case DRM_FORMAT_YUV420:
3949 case DRM_FORMAT_YVU420:
3950 case DRM_FORMAT_YUV422:
3951 case DRM_FORMAT_YVU422:
3952 case DRM_FORMAT_YUV444:
3953 case DRM_FORMAT_YVU444:
3954 return 1;
3955 default:
3956 drm_fb_get_bpp_depth(format, &depth, &bpp);
3957 return bpp >> 3;
3958 }
3959}
3960EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003961
3962/**
3963 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3964 * @format: pixel format (DRM_FORMAT_*)
3965 *
3966 * RETURNS:
3967 * The horizontal chroma subsampling factor for the
3968 * specified pixel format.
3969 */
3970int drm_format_horz_chroma_subsampling(uint32_t format)
3971{
3972 switch (format) {
3973 case DRM_FORMAT_YUV411:
3974 case DRM_FORMAT_YVU411:
3975 case DRM_FORMAT_YUV410:
3976 case DRM_FORMAT_YVU410:
3977 return 4;
3978 case DRM_FORMAT_YUYV:
3979 case DRM_FORMAT_YVYU:
3980 case DRM_FORMAT_UYVY:
3981 case DRM_FORMAT_VYUY:
3982 case DRM_FORMAT_NV12:
3983 case DRM_FORMAT_NV21:
3984 case DRM_FORMAT_NV16:
3985 case DRM_FORMAT_NV61:
3986 case DRM_FORMAT_YUV422:
3987 case DRM_FORMAT_YVU422:
3988 case DRM_FORMAT_YUV420:
3989 case DRM_FORMAT_YVU420:
3990 return 2;
3991 default:
3992 return 1;
3993 }
3994}
3995EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3996
3997/**
3998 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3999 * @format: pixel format (DRM_FORMAT_*)
4000 *
4001 * RETURNS:
4002 * The vertical chroma subsampling factor for the
4003 * specified pixel format.
4004 */
4005int drm_format_vert_chroma_subsampling(uint32_t format)
4006{
4007 switch (format) {
4008 case DRM_FORMAT_YUV410:
4009 case DRM_FORMAT_YVU410:
4010 return 4;
4011 case DRM_FORMAT_YUV420:
4012 case DRM_FORMAT_YVU420:
4013 case DRM_FORMAT_NV12:
4014 case DRM_FORMAT_NV21:
4015 return 2;
4016 default:
4017 return 1;
4018 }
4019}
4020EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);