blob: cba8c8bb789c868a81c58b0eac5767a1ab47ae40 [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
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002274 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2275 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002276 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002277 drm_modeset_unlock_all(dev);
2278 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002279 }
2280
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002281 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002282 or->fb_id = fb->base.id;
2283 list_add(&fb->filp_head, &file_priv->fbs);
2284 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002285 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002286
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002287 return ret;
2288}
2289
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002290static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002291{
2292 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2293
2294 switch (format) {
2295 case DRM_FORMAT_C8:
2296 case DRM_FORMAT_RGB332:
2297 case DRM_FORMAT_BGR233:
2298 case DRM_FORMAT_XRGB4444:
2299 case DRM_FORMAT_XBGR4444:
2300 case DRM_FORMAT_RGBX4444:
2301 case DRM_FORMAT_BGRX4444:
2302 case DRM_FORMAT_ARGB4444:
2303 case DRM_FORMAT_ABGR4444:
2304 case DRM_FORMAT_RGBA4444:
2305 case DRM_FORMAT_BGRA4444:
2306 case DRM_FORMAT_XRGB1555:
2307 case DRM_FORMAT_XBGR1555:
2308 case DRM_FORMAT_RGBX5551:
2309 case DRM_FORMAT_BGRX5551:
2310 case DRM_FORMAT_ARGB1555:
2311 case DRM_FORMAT_ABGR1555:
2312 case DRM_FORMAT_RGBA5551:
2313 case DRM_FORMAT_BGRA5551:
2314 case DRM_FORMAT_RGB565:
2315 case DRM_FORMAT_BGR565:
2316 case DRM_FORMAT_RGB888:
2317 case DRM_FORMAT_BGR888:
2318 case DRM_FORMAT_XRGB8888:
2319 case DRM_FORMAT_XBGR8888:
2320 case DRM_FORMAT_RGBX8888:
2321 case DRM_FORMAT_BGRX8888:
2322 case DRM_FORMAT_ARGB8888:
2323 case DRM_FORMAT_ABGR8888:
2324 case DRM_FORMAT_RGBA8888:
2325 case DRM_FORMAT_BGRA8888:
2326 case DRM_FORMAT_XRGB2101010:
2327 case DRM_FORMAT_XBGR2101010:
2328 case DRM_FORMAT_RGBX1010102:
2329 case DRM_FORMAT_BGRX1010102:
2330 case DRM_FORMAT_ARGB2101010:
2331 case DRM_FORMAT_ABGR2101010:
2332 case DRM_FORMAT_RGBA1010102:
2333 case DRM_FORMAT_BGRA1010102:
2334 case DRM_FORMAT_YUYV:
2335 case DRM_FORMAT_YVYU:
2336 case DRM_FORMAT_UYVY:
2337 case DRM_FORMAT_VYUY:
2338 case DRM_FORMAT_AYUV:
2339 case DRM_FORMAT_NV12:
2340 case DRM_FORMAT_NV21:
2341 case DRM_FORMAT_NV16:
2342 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002343 case DRM_FORMAT_NV24:
2344 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002345 case DRM_FORMAT_YUV410:
2346 case DRM_FORMAT_YVU410:
2347 case DRM_FORMAT_YUV411:
2348 case DRM_FORMAT_YVU411:
2349 case DRM_FORMAT_YUV420:
2350 case DRM_FORMAT_YVU420:
2351 case DRM_FORMAT_YUV422:
2352 case DRM_FORMAT_YVU422:
2353 case DRM_FORMAT_YUV444:
2354 case DRM_FORMAT_YVU444:
2355 return 0;
2356 default:
2357 return -EINVAL;
2358 }
2359}
2360
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002361static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002362{
2363 int ret, hsub, vsub, num_planes, i;
2364
2365 ret = format_check(r);
2366 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002367 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002368 return ret;
2369 }
2370
2371 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2372 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2373 num_planes = drm_format_num_planes(r->pixel_format);
2374
2375 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002376 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002377 return -EINVAL;
2378 }
2379
2380 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002381 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002382 return -EINVAL;
2383 }
2384
2385 for (i = 0; i < num_planes; i++) {
2386 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002387 unsigned int height = r->height / (i != 0 ? vsub : 1);
2388 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002389
2390 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002391 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002392 return -EINVAL;
2393 }
2394
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002395 if ((uint64_t) width * cpp > UINT_MAX)
2396 return -ERANGE;
2397
2398 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2399 return -ERANGE;
2400
2401 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002402 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002403 return -EINVAL;
2404 }
2405 }
2406
2407 return 0;
2408}
2409
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002410/**
2411 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002412 * @dev: drm device for the ioctl
2413 * @data: data pointer for the ioctl
2414 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002415 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002416 * Add a new FB to the specified CRTC, given a user request with format.
2417 *
2418 * Called by the user via ioctl.
2419 *
2420 * RETURNS:
2421 * Zero on success, errno on failure.
2422 */
2423int drm_mode_addfb2(struct drm_device *dev,
2424 void *data, struct drm_file *file_priv)
2425{
2426 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002427 struct drm_mode_config *config = &dev->mode_config;
2428 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002429 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002430
Dave Airliefb3b06c2011-02-08 13:55:21 +10002431 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2432 return -EINVAL;
2433
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002434 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2435 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2436 return -EINVAL;
2437 }
2438
Dave Airlief453ba02008-11-07 14:05:41 -08002439 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002440 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002441 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002442 return -EINVAL;
2443 }
2444 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002445 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002446 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002447 return -EINVAL;
2448 }
2449
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002450 ret = framebuffer_check(r);
2451 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002452 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002453
Dave Airlief453ba02008-11-07 14:05:41 -08002454 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002455 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002456 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002457 drm_modeset_unlock_all(dev);
2458 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002459 }
2460
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002461 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002462 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002463 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002464 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002465 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002466
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002467
Dave Airlief453ba02008-11-07 14:05:41 -08002468 return ret;
2469}
2470
2471/**
2472 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002473 * @dev: drm device for the ioctl
2474 * @data: data pointer for the ioctl
2475 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002476 *
Dave Airlief453ba02008-11-07 14:05:41 -08002477 * Remove the FB specified by the user.
2478 *
2479 * Called by the user via ioctl.
2480 *
2481 * RETURNS:
2482 * Zero on success, errno on failure.
2483 */
2484int drm_mode_rmfb(struct drm_device *dev,
2485 void *data, struct drm_file *file_priv)
2486{
Dave Airlief453ba02008-11-07 14:05:41 -08002487 struct drm_framebuffer *fb = NULL;
2488 struct drm_framebuffer *fbl = NULL;
2489 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002490 int found = 0;
2491
Dave Airliefb3b06c2011-02-08 13:55:21 +10002492 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2493 return -EINVAL;
2494
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002495 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002496 mutex_lock(&dev->mode_config.fb_lock);
2497 fb = __drm_framebuffer_lookup(dev, *id);
2498 if (!fb)
2499 goto fail_lookup;
2500
Dave Airlief453ba02008-11-07 14:05:41 -08002501 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2502 if (fb == fbl)
2503 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002504 if (!found)
2505 goto fail_lookup;
2506
2507 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2508 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002509
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002510 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002511 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002512 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002513
Daniel Vetter2b677e82012-12-10 21:16:05 +01002514 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002515 drm_framebuffer_remove(fb);
Daniel Vetter84849902012-12-02 00:28:11 +01002516 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002517
Daniel Vetter2b677e82012-12-10 21:16:05 +01002518 return 0;
2519
2520fail_lookup:
2521 mutex_unlock(&dev->mode_config.fb_lock);
2522 mutex_unlock(&file_priv->fbs_lock);
2523
2524 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002525}
2526
2527/**
2528 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002529 * @dev: drm device for the ioctl
2530 * @data: data pointer for the ioctl
2531 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002532 *
Dave Airlief453ba02008-11-07 14:05:41 -08002533 * Lookup the FB given its ID and return info about it.
2534 *
2535 * Called by the user via ioctl.
2536 *
2537 * RETURNS:
2538 * Zero on success, errno on failure.
2539 */
2540int drm_mode_getfb(struct drm_device *dev,
2541 void *data, struct drm_file *file_priv)
2542{
2543 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002544 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002545 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002546
Dave Airliefb3b06c2011-02-08 13:55:21 +10002547 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2548 return -EINVAL;
2549
Daniel Vetter786b99e2012-12-02 21:53:40 +01002550 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002551 if (!fb)
2552 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002553
2554 r->height = fb->height;
2555 r->width = fb->width;
2556 r->depth = fb->depth;
2557 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002558 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002559 if (fb->funcs->create_handle)
2560 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2561 else
2562 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002563
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002564 drm_framebuffer_unreference(fb);
2565
Dave Airlief453ba02008-11-07 14:05:41 -08002566 return ret;
2567}
2568
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002569int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2570 void *data, struct drm_file *file_priv)
2571{
2572 struct drm_clip_rect __user *clips_ptr;
2573 struct drm_clip_rect *clips = NULL;
2574 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002575 struct drm_framebuffer *fb;
2576 unsigned flags;
2577 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002578 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002579
Dave Airliefb3b06c2011-02-08 13:55:21 +10002580 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2581 return -EINVAL;
2582
Daniel Vetter84849902012-12-02 00:28:11 +01002583 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002584 fb = drm_framebuffer_lookup(dev, r->fb_id);
2585 if (!fb) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002586 ret = -EINVAL;
2587 goto out_err1;
2588 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002589 /* fb is protect by the mode_config lock, so drop the ref immediately */
2590 drm_framebuffer_unreference(fb);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002591
2592 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002593 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002594
2595 if (!num_clips != !clips_ptr) {
2596 ret = -EINVAL;
2597 goto out_err1;
2598 }
2599
2600 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2601
2602 /* If userspace annotates copy, clips must come in pairs */
2603 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2604 ret = -EINVAL;
2605 goto out_err1;
2606 }
2607
2608 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002609 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2610 ret = -EINVAL;
2611 goto out_err1;
2612 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002613 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2614 if (!clips) {
2615 ret = -ENOMEM;
2616 goto out_err1;
2617 }
2618
2619 ret = copy_from_user(clips, clips_ptr,
2620 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002621 if (ret) {
2622 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002623 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002624 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002625 }
2626
2627 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002628 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2629 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002630 } else {
2631 ret = -ENOSYS;
2632 goto out_err2;
2633 }
2634
2635out_err2:
2636 kfree(clips);
2637out_err1:
Daniel Vetter84849902012-12-02 00:28:11 +01002638 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002639 return ret;
2640}
2641
2642
Dave Airlief453ba02008-11-07 14:05:41 -08002643/**
2644 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002645 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002646 *
Dave Airlief453ba02008-11-07 14:05:41 -08002647 * Destroy all the FBs associated with @filp.
2648 *
2649 * Called by the user via ioctl.
2650 *
2651 * RETURNS:
2652 * Zero on success, errno on failure.
2653 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002654void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002655{
Dave Airlief453ba02008-11-07 14:05:41 -08002656 struct drm_device *dev = priv->minor->dev;
2657 struct drm_framebuffer *fb, *tfb;
2658
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002659 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002660 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002661
2662 mutex_lock(&dev->mode_config.fb_lock);
2663 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2664 __drm_framebuffer_unregister(dev, fb);
2665 mutex_unlock(&dev->mode_config.fb_lock);
2666
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002667 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002668
2669 /* This will also drop the fpriv->fbs reference. */
Daniel Vetter7d331592012-12-10 23:44:22 +01002670 drm_modeset_lock_all(dev);
Rob Clarkf7eff602012-09-05 21:48:38 +00002671 drm_framebuffer_remove(fb);
Daniel Vetter7d331592012-12-10 23:44:22 +01002672 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002673 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002674 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002675}
2676
2677/**
2678 * drm_mode_attachmode - add a mode to the user mode list
2679 * @dev: DRM device
2680 * @connector: connector to add the mode to
2681 * @mode: mode to add
2682 *
2683 * Add @mode to @connector's user mode list.
2684 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002685static void drm_mode_attachmode(struct drm_device *dev,
2686 struct drm_connector *connector,
2687 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002688{
Dave Airlief453ba02008-11-07 14:05:41 -08002689 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002690}
2691
2692int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002693 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002694{
2695 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002696 int ret = 0;
2697 struct drm_display_mode *dup_mode, *next;
2698 LIST_HEAD(list);
2699
Dave Airlief453ba02008-11-07 14:05:41 -08002700 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2701 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002702 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002703 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002704 dup_mode = drm_mode_duplicate(dev, mode);
2705 if (!dup_mode) {
2706 ret = -ENOMEM;
2707 goto out;
2708 }
2709 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002710 }
2711 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002712
2713 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2714 if (!connector->encoder)
2715 continue;
2716 if (connector->encoder->crtc == crtc)
2717 list_move_tail(list.next, &connector->user_modes);
2718 }
2719
2720 WARN_ON(!list_empty(&list));
2721
2722 out:
2723 list_for_each_entry_safe(dup_mode, next, &list, head)
2724 drm_mode_destroy(dev, dup_mode);
2725
2726 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002727}
2728EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2729
2730static int drm_mode_detachmode(struct drm_device *dev,
2731 struct drm_connector *connector,
2732 struct drm_display_mode *mode)
2733{
2734 int found = 0;
2735 int ret = 0;
2736 struct drm_display_mode *match_mode, *t;
2737
2738 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2739 if (drm_mode_equal(match_mode, mode)) {
2740 list_del(&match_mode->head);
2741 drm_mode_destroy(dev, match_mode);
2742 found = 1;
2743 break;
2744 }
2745 }
2746
2747 if (!found)
2748 ret = -EINVAL;
2749
2750 return ret;
2751}
2752
2753int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2754{
2755 struct drm_connector *connector;
2756
2757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2758 drm_mode_detachmode(dev, connector, mode);
2759 }
2760 return 0;
2761}
2762EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2763
2764/**
2765 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002766 * @dev: drm device for the ioctl
2767 * @data: data pointer for the ioctl
2768 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002769 *
2770 * This attaches a user specified mode to an connector.
2771 * Called by the user via ioctl.
2772 *
2773 * RETURNS:
2774 * Zero on success, errno on failure.
2775 */
2776int drm_mode_attachmode_ioctl(struct drm_device *dev,
2777 void *data, struct drm_file *file_priv)
2778{
2779 struct drm_mode_mode_cmd *mode_cmd = data;
2780 struct drm_connector *connector;
2781 struct drm_display_mode *mode;
2782 struct drm_mode_object *obj;
2783 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002784 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002785
Dave Airliefb3b06c2011-02-08 13:55:21 +10002786 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2787 return -EINVAL;
2788
Daniel Vetter84849902012-12-02 00:28:11 +01002789 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002790
2791 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2792 if (!obj) {
2793 ret = -EINVAL;
2794 goto out;
2795 }
2796 connector = obj_to_connector(obj);
2797
2798 mode = drm_mode_create(dev);
2799 if (!mode) {
2800 ret = -ENOMEM;
2801 goto out;
2802 }
2803
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002804 ret = drm_crtc_convert_umode(mode, umode);
2805 if (ret) {
2806 DRM_DEBUG_KMS("Invalid mode\n");
2807 drm_mode_destroy(dev, mode);
2808 goto out;
2809 }
Dave Airlief453ba02008-11-07 14:05:41 -08002810
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002811 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002812out:
Daniel Vetter84849902012-12-02 00:28:11 +01002813 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002814 return ret;
2815}
2816
2817
2818/**
2819 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002820 * @dev: drm device for the ioctl
2821 * @data: data pointer for the ioctl
2822 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002823 *
2824 * Called by the user via ioctl.
2825 *
2826 * RETURNS:
2827 * Zero on success, errno on failure.
2828 */
2829int drm_mode_detachmode_ioctl(struct drm_device *dev,
2830 void *data, struct drm_file *file_priv)
2831{
2832 struct drm_mode_object *obj;
2833 struct drm_mode_mode_cmd *mode_cmd = data;
2834 struct drm_connector *connector;
2835 struct drm_display_mode mode;
2836 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002837 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002838
Dave Airliefb3b06c2011-02-08 13:55:21 +10002839 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2840 return -EINVAL;
2841
Daniel Vetter84849902012-12-02 00:28:11 +01002842 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002843
2844 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2845 if (!obj) {
2846 ret = -EINVAL;
2847 goto out;
2848 }
2849 connector = obj_to_connector(obj);
2850
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002851 ret = drm_crtc_convert_umode(&mode, umode);
2852 if (ret) {
2853 DRM_DEBUG_KMS("Invalid mode\n");
2854 goto out;
2855 }
2856
Dave Airlief453ba02008-11-07 14:05:41 -08002857 ret = drm_mode_detachmode(dev, connector, &mode);
2858out:
Daniel Vetter84849902012-12-02 00:28:11 +01002859 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002860 return ret;
2861}
2862
2863struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2864 const char *name, int num_values)
2865{
2866 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002867 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002868
2869 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2870 if (!property)
2871 return NULL;
2872
2873 if (num_values) {
2874 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2875 if (!property->values)
2876 goto fail;
2877 }
2878
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002879 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2880 if (ret)
2881 goto fail;
2882
Dave Airlief453ba02008-11-07 14:05:41 -08002883 property->flags = flags;
2884 property->num_values = num_values;
2885 INIT_LIST_HEAD(&property->enum_blob_list);
2886
Vinson Lee471dd2e2011-11-10 11:55:40 -08002887 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002888 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002889 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2890 }
Dave Airlief453ba02008-11-07 14:05:41 -08002891
2892 list_add_tail(&property->head, &dev->mode_config.property_list);
2893 return property;
2894fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002895 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002896 kfree(property);
2897 return NULL;
2898}
2899EXPORT_SYMBOL(drm_property_create);
2900
Sascha Hauer4a67d392012-02-06 10:58:17 +01002901struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2902 const char *name,
2903 const struct drm_prop_enum_list *props,
2904 int num_values)
2905{
2906 struct drm_property *property;
2907 int i, ret;
2908
2909 flags |= DRM_MODE_PROP_ENUM;
2910
2911 property = drm_property_create(dev, flags, name, num_values);
2912 if (!property)
2913 return NULL;
2914
2915 for (i = 0; i < num_values; i++) {
2916 ret = drm_property_add_enum(property, i,
2917 props[i].type,
2918 props[i].name);
2919 if (ret) {
2920 drm_property_destroy(dev, property);
2921 return NULL;
2922 }
2923 }
2924
2925 return property;
2926}
2927EXPORT_SYMBOL(drm_property_create_enum);
2928
Rob Clark49e27542012-05-17 02:23:26 -06002929struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2930 int flags, const char *name,
2931 const struct drm_prop_enum_list *props,
2932 int num_values)
2933{
2934 struct drm_property *property;
2935 int i, ret;
2936
2937 flags |= DRM_MODE_PROP_BITMASK;
2938
2939 property = drm_property_create(dev, flags, name, num_values);
2940 if (!property)
2941 return NULL;
2942
2943 for (i = 0; i < num_values; i++) {
2944 ret = drm_property_add_enum(property, i,
2945 props[i].type,
2946 props[i].name);
2947 if (ret) {
2948 drm_property_destroy(dev, property);
2949 return NULL;
2950 }
2951 }
2952
2953 return property;
2954}
2955EXPORT_SYMBOL(drm_property_create_bitmask);
2956
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002957struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2958 const char *name,
2959 uint64_t min, uint64_t max)
2960{
2961 struct drm_property *property;
2962
2963 flags |= DRM_MODE_PROP_RANGE;
2964
2965 property = drm_property_create(dev, flags, name, 2);
2966 if (!property)
2967 return NULL;
2968
2969 property->values[0] = min;
2970 property->values[1] = max;
2971
2972 return property;
2973}
2974EXPORT_SYMBOL(drm_property_create_range);
2975
Dave Airlief453ba02008-11-07 14:05:41 -08002976int drm_property_add_enum(struct drm_property *property, int index,
2977 uint64_t value, const char *name)
2978{
2979 struct drm_property_enum *prop_enum;
2980
Rob Clark49e27542012-05-17 02:23:26 -06002981 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2982 return -EINVAL;
2983
2984 /*
2985 * Bitmask enum properties have the additional constraint of values
2986 * from 0 to 63
2987 */
2988 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002989 return -EINVAL;
2990
2991 if (!list_empty(&property->enum_blob_list)) {
2992 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2993 if (prop_enum->value == value) {
2994 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2995 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2996 return 0;
2997 }
2998 }
2999 }
3000
3001 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3002 if (!prop_enum)
3003 return -ENOMEM;
3004
3005 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3006 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3007 prop_enum->value = value;
3008
3009 property->values[index] = value;
3010 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3011 return 0;
3012}
3013EXPORT_SYMBOL(drm_property_add_enum);
3014
3015void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3016{
3017 struct drm_property_enum *prop_enum, *pt;
3018
3019 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3020 list_del(&prop_enum->head);
3021 kfree(prop_enum);
3022 }
3023
3024 if (property->num_values)
3025 kfree(property->values);
3026 drm_mode_object_put(dev, &property->base);
3027 list_del(&property->head);
3028 kfree(property);
3029}
3030EXPORT_SYMBOL(drm_property_destroy);
3031
Paulo Zanonic5431882012-05-15 18:09:02 -03003032void drm_object_attach_property(struct drm_mode_object *obj,
3033 struct drm_property *property,
3034 uint64_t init_val)
3035{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003036 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003037
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003038 if (count == DRM_OBJECT_MAX_PROPERTY) {
3039 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3040 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3041 "you see this message on the same object type.\n",
3042 obj->type);
3043 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003044 }
3045
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003046 obj->properties->ids[count] = property->base.id;
3047 obj->properties->values[count] = init_val;
3048 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003049}
3050EXPORT_SYMBOL(drm_object_attach_property);
3051
3052int drm_object_property_set_value(struct drm_mode_object *obj,
3053 struct drm_property *property, uint64_t val)
3054{
3055 int i;
3056
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003057 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003058 if (obj->properties->ids[i] == property->base.id) {
3059 obj->properties->values[i] = val;
3060 return 0;
3061 }
3062 }
3063
3064 return -EINVAL;
3065}
3066EXPORT_SYMBOL(drm_object_property_set_value);
3067
3068int drm_object_property_get_value(struct drm_mode_object *obj,
3069 struct drm_property *property, uint64_t *val)
3070{
3071 int i;
3072
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003073 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003074 if (obj->properties->ids[i] == property->base.id) {
3075 *val = obj->properties->values[i];
3076 return 0;
3077 }
3078 }
3079
3080 return -EINVAL;
3081}
3082EXPORT_SYMBOL(drm_object_property_get_value);
3083
Dave Airlief453ba02008-11-07 14:05:41 -08003084int drm_mode_getproperty_ioctl(struct drm_device *dev,
3085 void *data, struct drm_file *file_priv)
3086{
3087 struct drm_mode_object *obj;
3088 struct drm_mode_get_property *out_resp = data;
3089 struct drm_property *property;
3090 int enum_count = 0;
3091 int blob_count = 0;
3092 int value_count = 0;
3093 int ret = 0, i;
3094 int copied;
3095 struct drm_property_enum *prop_enum;
3096 struct drm_mode_property_enum __user *enum_ptr;
3097 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003098 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003099 uint64_t __user *values_ptr;
3100 uint32_t __user *blob_length_ptr;
3101
Dave Airliefb3b06c2011-02-08 13:55:21 +10003102 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3103 return -EINVAL;
3104
Daniel Vetter84849902012-12-02 00:28:11 +01003105 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003106 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3107 if (!obj) {
3108 ret = -EINVAL;
3109 goto done;
3110 }
3111 property = obj_to_property(obj);
3112
Rob Clark49e27542012-05-17 02:23:26 -06003113 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003114 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3115 enum_count++;
3116 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3117 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3118 blob_count++;
3119 }
3120
3121 value_count = property->num_values;
3122
3123 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3124 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3125 out_resp->flags = property->flags;
3126
3127 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003128 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003129 for (i = 0; i < value_count; i++) {
3130 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3131 ret = -EFAULT;
3132 goto done;
3133 }
3134 }
3135 }
3136 out_resp->count_values = value_count;
3137
Rob Clark49e27542012-05-17 02:23:26 -06003138 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003139 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3140 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003141 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003142 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3143
3144 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3145 ret = -EFAULT;
3146 goto done;
3147 }
3148
3149 if (copy_to_user(&enum_ptr[copied].name,
3150 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3151 ret = -EFAULT;
3152 goto done;
3153 }
3154 copied++;
3155 }
3156 }
3157 out_resp->count_enum_blobs = enum_count;
3158 }
3159
3160 if (property->flags & DRM_MODE_PROP_BLOB) {
3161 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3162 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003163 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3164 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003165
3166 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3167 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3168 ret = -EFAULT;
3169 goto done;
3170 }
3171
3172 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3173 ret = -EFAULT;
3174 goto done;
3175 }
3176
3177 copied++;
3178 }
3179 }
3180 out_resp->count_enum_blobs = blob_count;
3181 }
3182done:
Daniel Vetter84849902012-12-02 00:28:11 +01003183 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003184 return ret;
3185}
3186
3187static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3188 void *data)
3189{
3190 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003191 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003192
3193 if (!length || !data)
3194 return NULL;
3195
3196 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3197 if (!blob)
3198 return NULL;
3199
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003200 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3201 if (ret) {
3202 kfree(blob);
3203 return NULL;
3204 }
3205
Dave Airlief453ba02008-11-07 14:05:41 -08003206 blob->length = length;
3207
3208 memcpy(blob->data, data, length);
3209
Dave Airlief453ba02008-11-07 14:05:41 -08003210 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3211 return blob;
3212}
3213
3214static void drm_property_destroy_blob(struct drm_device *dev,
3215 struct drm_property_blob *blob)
3216{
3217 drm_mode_object_put(dev, &blob->base);
3218 list_del(&blob->head);
3219 kfree(blob);
3220}
3221
3222int drm_mode_getblob_ioctl(struct drm_device *dev,
3223 void *data, struct drm_file *file_priv)
3224{
3225 struct drm_mode_object *obj;
3226 struct drm_mode_get_blob *out_resp = data;
3227 struct drm_property_blob *blob;
3228 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003229 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003230
Dave Airliefb3b06c2011-02-08 13:55:21 +10003231 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3232 return -EINVAL;
3233
Daniel Vetter84849902012-12-02 00:28:11 +01003234 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003235 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3236 if (!obj) {
3237 ret = -EINVAL;
3238 goto done;
3239 }
3240 blob = obj_to_blob(obj);
3241
3242 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003243 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003244 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3245 ret = -EFAULT;
3246 goto done;
3247 }
3248 }
3249 out_resp->length = blob->length;
3250
3251done:
Daniel Vetter84849902012-12-02 00:28:11 +01003252 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003253 return ret;
3254}
3255
3256int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3257 struct edid *edid)
3258{
3259 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003260 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003261
3262 if (connector->edid_blob_ptr)
3263 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3264
3265 /* Delete edid, when there is none. */
3266 if (!edid) {
3267 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003268 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003269 return ret;
3270 }
3271
Adam Jackson7466f4c2010-03-29 21:43:23 +00003272 size = EDID_LENGTH * (1 + edid->extensions);
3273 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3274 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003275 if (!connector->edid_blob_ptr)
3276 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003277
Rob Clark58495562012-10-11 20:50:56 -05003278 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003279 dev->mode_config.edid_property,
3280 connector->edid_blob_ptr->base.id);
3281
3282 return ret;
3283}
3284EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3285
Paulo Zanoni26a34812012-05-15 18:08:59 -03003286static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003287 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003288{
3289 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3290 return false;
3291 if (property->flags & DRM_MODE_PROP_RANGE) {
3292 if (value < property->values[0] || value > property->values[1])
3293 return false;
3294 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003295 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3296 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003297 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003298 for (i = 0; i < property->num_values; i++)
3299 valid_mask |= (1ULL << property->values[i]);
3300 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003301 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3302 /* Only the driver knows */
3303 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003304 } else {
3305 int i;
3306 for (i = 0; i < property->num_values; i++)
3307 if (property->values[i] == value)
3308 return true;
3309 return false;
3310 }
3311}
3312
Dave Airlief453ba02008-11-07 14:05:41 -08003313int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3314 void *data, struct drm_file *file_priv)
3315{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003316 struct drm_mode_connector_set_property *conn_set_prop = data;
3317 struct drm_mode_obj_set_property obj_set_prop = {
3318 .value = conn_set_prop->value,
3319 .prop_id = conn_set_prop->prop_id,
3320 .obj_id = conn_set_prop->connector_id,
3321 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3322 };
Dave Airlief453ba02008-11-07 14:05:41 -08003323
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003324 /* It does all the locking and checking we need */
3325 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003326}
3327
Paulo Zanonic5431882012-05-15 18:09:02 -03003328static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3329 struct drm_property *property,
3330 uint64_t value)
3331{
3332 int ret = -EINVAL;
3333 struct drm_connector *connector = obj_to_connector(obj);
3334
3335 /* Do DPMS ourselves */
3336 if (property == connector->dev->mode_config.dpms_property) {
3337 if (connector->funcs->dpms)
3338 (*connector->funcs->dpms)(connector, (int)value);
3339 ret = 0;
3340 } else if (connector->funcs->set_property)
3341 ret = connector->funcs->set_property(connector, property, value);
3342
3343 /* store the property value if successful */
3344 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003345 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003346 return ret;
3347}
3348
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003349static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3350 struct drm_property *property,
3351 uint64_t value)
3352{
3353 int ret = -EINVAL;
3354 struct drm_crtc *crtc = obj_to_crtc(obj);
3355
3356 if (crtc->funcs->set_property)
3357 ret = crtc->funcs->set_property(crtc, property, value);
3358 if (!ret)
3359 drm_object_property_set_value(obj, property, value);
3360
3361 return ret;
3362}
3363
Rob Clark4d939142012-05-17 02:23:27 -06003364static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3365 struct drm_property *property,
3366 uint64_t value)
3367{
3368 int ret = -EINVAL;
3369 struct drm_plane *plane = obj_to_plane(obj);
3370
3371 if (plane->funcs->set_property)
3372 ret = plane->funcs->set_property(plane, property, value);
3373 if (!ret)
3374 drm_object_property_set_value(obj, property, value);
3375
3376 return ret;
3377}
3378
Paulo Zanonic5431882012-05-15 18:09:02 -03003379int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3380 struct drm_file *file_priv)
3381{
3382 struct drm_mode_obj_get_properties *arg = data;
3383 struct drm_mode_object *obj;
3384 int ret = 0;
3385 int i;
3386 int copied = 0;
3387 int props_count = 0;
3388 uint32_t __user *props_ptr;
3389 uint64_t __user *prop_values_ptr;
3390
3391 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3392 return -EINVAL;
3393
Daniel Vetter84849902012-12-02 00:28:11 +01003394 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003395
3396 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3397 if (!obj) {
3398 ret = -EINVAL;
3399 goto out;
3400 }
3401 if (!obj->properties) {
3402 ret = -EINVAL;
3403 goto out;
3404 }
3405
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003406 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003407
3408 /* This ioctl is called twice, once to determine how much space is
3409 * needed, and the 2nd time to fill it. */
3410 if ((arg->count_props >= props_count) && props_count) {
3411 copied = 0;
3412 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3413 prop_values_ptr = (uint64_t __user *)(unsigned long)
3414 (arg->prop_values_ptr);
3415 for (i = 0; i < props_count; i++) {
3416 if (put_user(obj->properties->ids[i],
3417 props_ptr + copied)) {
3418 ret = -EFAULT;
3419 goto out;
3420 }
3421 if (put_user(obj->properties->values[i],
3422 prop_values_ptr + copied)) {
3423 ret = -EFAULT;
3424 goto out;
3425 }
3426 copied++;
3427 }
3428 }
3429 arg->count_props = props_count;
3430out:
Daniel Vetter84849902012-12-02 00:28:11 +01003431 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003432 return ret;
3433}
3434
3435int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3436 struct drm_file *file_priv)
3437{
3438 struct drm_mode_obj_set_property *arg = data;
3439 struct drm_mode_object *arg_obj;
3440 struct drm_mode_object *prop_obj;
3441 struct drm_property *property;
3442 int ret = -EINVAL;
3443 int i;
3444
3445 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3446 return -EINVAL;
3447
Daniel Vetter84849902012-12-02 00:28:11 +01003448 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003449
3450 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3451 if (!arg_obj)
3452 goto out;
3453 if (!arg_obj->properties)
3454 goto out;
3455
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003456 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003457 if (arg_obj->properties->ids[i] == arg->prop_id)
3458 break;
3459
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003460 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003461 goto out;
3462
3463 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3464 DRM_MODE_OBJECT_PROPERTY);
3465 if (!prop_obj)
3466 goto out;
3467 property = obj_to_property(prop_obj);
3468
3469 if (!drm_property_change_is_valid(property, arg->value))
3470 goto out;
3471
3472 switch (arg_obj->type) {
3473 case DRM_MODE_OBJECT_CONNECTOR:
3474 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3475 arg->value);
3476 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003477 case DRM_MODE_OBJECT_CRTC:
3478 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3479 break;
Rob Clark4d939142012-05-17 02:23:27 -06003480 case DRM_MODE_OBJECT_PLANE:
3481 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3482 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003483 }
3484
3485out:
Daniel Vetter84849902012-12-02 00:28:11 +01003486 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003487 return ret;
3488}
3489
Dave Airlief453ba02008-11-07 14:05:41 -08003490int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3491 struct drm_encoder *encoder)
3492{
3493 int i;
3494
3495 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3496 if (connector->encoder_ids[i] == 0) {
3497 connector->encoder_ids[i] = encoder->base.id;
3498 return 0;
3499 }
3500 }
3501 return -ENOMEM;
3502}
3503EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3504
3505void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3506 struct drm_encoder *encoder)
3507{
3508 int i;
3509 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3510 if (connector->encoder_ids[i] == encoder->base.id) {
3511 connector->encoder_ids[i] = 0;
3512 if (connector->encoder == encoder)
3513 connector->encoder = NULL;
3514 break;
3515 }
3516 }
3517}
3518EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3519
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003520int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003521 int gamma_size)
3522{
3523 crtc->gamma_size = gamma_size;
3524
3525 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3526 if (!crtc->gamma_store) {
3527 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003528 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003529 }
3530
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003531 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003532}
3533EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3534
3535int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3536 void *data, struct drm_file *file_priv)
3537{
3538 struct drm_mode_crtc_lut *crtc_lut = data;
3539 struct drm_mode_object *obj;
3540 struct drm_crtc *crtc;
3541 void *r_base, *g_base, *b_base;
3542 int size;
3543 int ret = 0;
3544
Dave Airliefb3b06c2011-02-08 13:55:21 +10003545 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3546 return -EINVAL;
3547
Daniel Vetter84849902012-12-02 00:28:11 +01003548 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003549 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3550 if (!obj) {
3551 ret = -EINVAL;
3552 goto out;
3553 }
3554 crtc = obj_to_crtc(obj);
3555
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003556 if (crtc->funcs->gamma_set == NULL) {
3557 ret = -ENOSYS;
3558 goto out;
3559 }
3560
Dave Airlief453ba02008-11-07 14:05:41 -08003561 /* memcpy into gamma store */
3562 if (crtc_lut->gamma_size != crtc->gamma_size) {
3563 ret = -EINVAL;
3564 goto out;
3565 }
3566
3567 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3568 r_base = crtc->gamma_store;
3569 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3570 ret = -EFAULT;
3571 goto out;
3572 }
3573
3574 g_base = r_base + size;
3575 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3576 ret = -EFAULT;
3577 goto out;
3578 }
3579
3580 b_base = g_base + size;
3581 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3582 ret = -EFAULT;
3583 goto out;
3584 }
3585
James Simmons72034252010-08-03 01:33:19 +01003586 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003587
3588out:
Daniel Vetter84849902012-12-02 00:28:11 +01003589 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003590 return ret;
3591
3592}
3593
3594int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3595 void *data, struct drm_file *file_priv)
3596{
3597 struct drm_mode_crtc_lut *crtc_lut = data;
3598 struct drm_mode_object *obj;
3599 struct drm_crtc *crtc;
3600 void *r_base, *g_base, *b_base;
3601 int size;
3602 int ret = 0;
3603
Dave Airliefb3b06c2011-02-08 13:55:21 +10003604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3605 return -EINVAL;
3606
Daniel Vetter84849902012-12-02 00:28:11 +01003607 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003608 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3609 if (!obj) {
3610 ret = -EINVAL;
3611 goto out;
3612 }
3613 crtc = obj_to_crtc(obj);
3614
3615 /* memcpy into gamma store */
3616 if (crtc_lut->gamma_size != crtc->gamma_size) {
3617 ret = -EINVAL;
3618 goto out;
3619 }
3620
3621 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3622 r_base = crtc->gamma_store;
3623 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3624 ret = -EFAULT;
3625 goto out;
3626 }
3627
3628 g_base = r_base + size;
3629 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3630 ret = -EFAULT;
3631 goto out;
3632 }
3633
3634 b_base = g_base + size;
3635 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3636 ret = -EFAULT;
3637 goto out;
3638 }
3639out:
Daniel Vetter84849902012-12-02 00:28:11 +01003640 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003641 return ret;
3642}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003643
3644int drm_mode_page_flip_ioctl(struct drm_device *dev,
3645 void *data, struct drm_file *file_priv)
3646{
3647 struct drm_mode_crtc_page_flip *page_flip = data;
3648 struct drm_mode_object *obj;
3649 struct drm_crtc *crtc;
3650 struct drm_framebuffer *fb;
3651 struct drm_pending_vblank_event *e = NULL;
3652 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003653 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003654 int ret = -EINVAL;
3655
3656 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3657 page_flip->reserved != 0)
3658 return -EINVAL;
3659
Daniel Vetter84849902012-12-02 00:28:11 +01003660 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003661 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3662 if (!obj)
3663 goto out;
3664 crtc = obj_to_crtc(obj);
3665
Chris Wilson90c1efd2010-07-17 20:23:26 +01003666 if (crtc->fb == NULL) {
3667 /* The framebuffer is currently unbound, presumably
3668 * due to a hotplug event, that userspace has not
3669 * yet discovered.
3670 */
3671 ret = -EBUSY;
3672 goto out;
3673 }
3674
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003675 if (crtc->funcs->page_flip == NULL)
3676 goto out;
3677
Daniel Vetter786b99e2012-12-02 21:53:40 +01003678 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3679 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003680 goto out;
Daniel Vetter786b99e2012-12-02 21:53:40 +01003681 /* fb is protect by the mode_config lock, so drop the ref immediately */
3682 drm_framebuffer_unreference(fb);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003683
Rob Clark7c80e122012-09-04 16:35:56 +00003684 hdisplay = crtc->mode.hdisplay;
3685 vdisplay = crtc->mode.vdisplay;
3686
3687 if (crtc->invert_dimensions)
3688 swap(hdisplay, vdisplay);
3689
3690 if (hdisplay > fb->width ||
3691 vdisplay > fb->height ||
3692 crtc->x > fb->width - hdisplay ||
3693 crtc->y > fb->height - vdisplay) {
3694 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3695 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3696 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003697 ret = -ENOSPC;
3698 goto out;
3699 }
3700
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003701 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3702 ret = -ENOMEM;
3703 spin_lock_irqsave(&dev->event_lock, flags);
3704 if (file_priv->event_space < sizeof e->event) {
3705 spin_unlock_irqrestore(&dev->event_lock, flags);
3706 goto out;
3707 }
3708 file_priv->event_space -= sizeof e->event;
3709 spin_unlock_irqrestore(&dev->event_lock, flags);
3710
3711 e = kzalloc(sizeof *e, GFP_KERNEL);
3712 if (e == NULL) {
3713 spin_lock_irqsave(&dev->event_lock, flags);
3714 file_priv->event_space += sizeof e->event;
3715 spin_unlock_irqrestore(&dev->event_lock, flags);
3716 goto out;
3717 }
3718
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003719 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003720 e->event.base.length = sizeof e->event;
3721 e->event.user_data = page_flip->user_data;
3722 e->base.event = &e->event.base;
3723 e->base.file_priv = file_priv;
3724 e->base.destroy =
3725 (void (*) (struct drm_pending_event *)) kfree;
3726 }
3727
3728 ret = crtc->funcs->page_flip(crtc, fb, e);
3729 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003730 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3731 spin_lock_irqsave(&dev->event_lock, flags);
3732 file_priv->event_space += sizeof e->event;
3733 spin_unlock_irqrestore(&dev->event_lock, flags);
3734 kfree(e);
3735 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003736 }
3737
3738out:
Daniel Vetter84849902012-12-02 00:28:11 +01003739 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003740 return ret;
3741}
Chris Wilsoneb033552011-01-24 15:11:08 +00003742
3743void drm_mode_config_reset(struct drm_device *dev)
3744{
3745 struct drm_crtc *crtc;
3746 struct drm_encoder *encoder;
3747 struct drm_connector *connector;
3748
3749 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3750 if (crtc->funcs->reset)
3751 crtc->funcs->reset(crtc);
3752
3753 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3754 if (encoder->funcs->reset)
3755 encoder->funcs->reset(encoder);
3756
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3758 connector->status = connector_status_unknown;
3759
Chris Wilsoneb033552011-01-24 15:11:08 +00003760 if (connector->funcs->reset)
3761 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003762 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003763}
3764EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003765
3766int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3767 void *data, struct drm_file *file_priv)
3768{
3769 struct drm_mode_create_dumb *args = data;
3770
3771 if (!dev->driver->dumb_create)
3772 return -ENOSYS;
3773 return dev->driver->dumb_create(file_priv, dev, args);
3774}
3775
3776int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3777 void *data, struct drm_file *file_priv)
3778{
3779 struct drm_mode_map_dumb *args = data;
3780
3781 /* call driver ioctl to get mmap offset */
3782 if (!dev->driver->dumb_map_offset)
3783 return -ENOSYS;
3784
3785 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3786}
3787
3788int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3789 void *data, struct drm_file *file_priv)
3790{
3791 struct drm_mode_destroy_dumb *args = data;
3792
3793 if (!dev->driver->dumb_destroy)
3794 return -ENOSYS;
3795
3796 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3797}
Dave Airlie248dbc22011-11-29 20:02:54 +00003798
3799/*
3800 * Just need to support RGB formats here for compat with code that doesn't
3801 * use pixel formats directly yet.
3802 */
3803void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3804 int *bpp)
3805{
3806 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003807 case DRM_FORMAT_RGB332:
3808 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003809 *depth = 8;
3810 *bpp = 8;
3811 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003812 case DRM_FORMAT_XRGB1555:
3813 case DRM_FORMAT_XBGR1555:
3814 case DRM_FORMAT_RGBX5551:
3815 case DRM_FORMAT_BGRX5551:
3816 case DRM_FORMAT_ARGB1555:
3817 case DRM_FORMAT_ABGR1555:
3818 case DRM_FORMAT_RGBA5551:
3819 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003820 *depth = 15;
3821 *bpp = 16;
3822 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003823 case DRM_FORMAT_RGB565:
3824 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003825 *depth = 16;
3826 *bpp = 16;
3827 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003828 case DRM_FORMAT_RGB888:
3829 case DRM_FORMAT_BGR888:
3830 *depth = 24;
3831 *bpp = 24;
3832 break;
3833 case DRM_FORMAT_XRGB8888:
3834 case DRM_FORMAT_XBGR8888:
3835 case DRM_FORMAT_RGBX8888:
3836 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003837 *depth = 24;
3838 *bpp = 32;
3839 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003840 case DRM_FORMAT_XRGB2101010:
3841 case DRM_FORMAT_XBGR2101010:
3842 case DRM_FORMAT_RGBX1010102:
3843 case DRM_FORMAT_BGRX1010102:
3844 case DRM_FORMAT_ARGB2101010:
3845 case DRM_FORMAT_ABGR2101010:
3846 case DRM_FORMAT_RGBA1010102:
3847 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003848 *depth = 30;
3849 *bpp = 32;
3850 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003851 case DRM_FORMAT_ARGB8888:
3852 case DRM_FORMAT_ABGR8888:
3853 case DRM_FORMAT_RGBA8888:
3854 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003855 *depth = 32;
3856 *bpp = 32;
3857 break;
3858 default:
3859 DRM_DEBUG_KMS("unsupported pixel format\n");
3860 *depth = 0;
3861 *bpp = 0;
3862 break;
3863 }
3864}
3865EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003866
3867/**
3868 * drm_format_num_planes - get the number of planes for format
3869 * @format: pixel format (DRM_FORMAT_*)
3870 *
3871 * RETURNS:
3872 * The number of planes used by the specified pixel format.
3873 */
3874int drm_format_num_planes(uint32_t format)
3875{
3876 switch (format) {
3877 case DRM_FORMAT_YUV410:
3878 case DRM_FORMAT_YVU410:
3879 case DRM_FORMAT_YUV411:
3880 case DRM_FORMAT_YVU411:
3881 case DRM_FORMAT_YUV420:
3882 case DRM_FORMAT_YVU420:
3883 case DRM_FORMAT_YUV422:
3884 case DRM_FORMAT_YVU422:
3885 case DRM_FORMAT_YUV444:
3886 case DRM_FORMAT_YVU444:
3887 return 3;
3888 case DRM_FORMAT_NV12:
3889 case DRM_FORMAT_NV21:
3890 case DRM_FORMAT_NV16:
3891 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003892 case DRM_FORMAT_NV24:
3893 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003894 return 2;
3895 default:
3896 return 1;
3897 }
3898}
3899EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003900
3901/**
3902 * drm_format_plane_cpp - determine the bytes per pixel value
3903 * @format: pixel format (DRM_FORMAT_*)
3904 * @plane: plane index
3905 *
3906 * RETURNS:
3907 * The bytes per pixel value for the specified plane.
3908 */
3909int drm_format_plane_cpp(uint32_t format, int plane)
3910{
3911 unsigned int depth;
3912 int bpp;
3913
3914 if (plane >= drm_format_num_planes(format))
3915 return 0;
3916
3917 switch (format) {
3918 case DRM_FORMAT_YUYV:
3919 case DRM_FORMAT_YVYU:
3920 case DRM_FORMAT_UYVY:
3921 case DRM_FORMAT_VYUY:
3922 return 2;
3923 case DRM_FORMAT_NV12:
3924 case DRM_FORMAT_NV21:
3925 case DRM_FORMAT_NV16:
3926 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003927 case DRM_FORMAT_NV24:
3928 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003929 return plane ? 2 : 1;
3930 case DRM_FORMAT_YUV410:
3931 case DRM_FORMAT_YVU410:
3932 case DRM_FORMAT_YUV411:
3933 case DRM_FORMAT_YVU411:
3934 case DRM_FORMAT_YUV420:
3935 case DRM_FORMAT_YVU420:
3936 case DRM_FORMAT_YUV422:
3937 case DRM_FORMAT_YVU422:
3938 case DRM_FORMAT_YUV444:
3939 case DRM_FORMAT_YVU444:
3940 return 1;
3941 default:
3942 drm_fb_get_bpp_depth(format, &depth, &bpp);
3943 return bpp >> 3;
3944 }
3945}
3946EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003947
3948/**
3949 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3950 * @format: pixel format (DRM_FORMAT_*)
3951 *
3952 * RETURNS:
3953 * The horizontal chroma subsampling factor for the
3954 * specified pixel format.
3955 */
3956int drm_format_horz_chroma_subsampling(uint32_t format)
3957{
3958 switch (format) {
3959 case DRM_FORMAT_YUV411:
3960 case DRM_FORMAT_YVU411:
3961 case DRM_FORMAT_YUV410:
3962 case DRM_FORMAT_YVU410:
3963 return 4;
3964 case DRM_FORMAT_YUYV:
3965 case DRM_FORMAT_YVYU:
3966 case DRM_FORMAT_UYVY:
3967 case DRM_FORMAT_VYUY:
3968 case DRM_FORMAT_NV12:
3969 case DRM_FORMAT_NV21:
3970 case DRM_FORMAT_NV16:
3971 case DRM_FORMAT_NV61:
3972 case DRM_FORMAT_YUV422:
3973 case DRM_FORMAT_YVU422:
3974 case DRM_FORMAT_YUV420:
3975 case DRM_FORMAT_YVU420:
3976 return 2;
3977 default:
3978 return 1;
3979 }
3980}
3981EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3982
3983/**
3984 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3985 * @format: pixel format (DRM_FORMAT_*)
3986 *
3987 * RETURNS:
3988 * The vertical chroma subsampling factor for the
3989 * specified pixel format.
3990 */
3991int drm_format_vert_chroma_subsampling(uint32_t format)
3992{
3993 switch (format) {
3994 case DRM_FORMAT_YUV410:
3995 case DRM_FORMAT_YVU410:
3996 return 4;
3997 case DRM_FORMAT_YUV420:
3998 case DRM_FORMAT_YVU420:
3999 case DRM_FORMAT_NV12:
4000 case DRM_FORMAT_NV21:
4001 return 2;
4002 default:
4003 return 1;
4004 }
4005}
4006EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);