blob: 3eddfabeba9622ae608a86ff534de049c5b638ec [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
Dave Airlief453ba02008-11-07 14:05:41 -0800359 dev->mode_config.num_fb++;
360 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100361out:
362 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800363
364 return 0;
365}
366EXPORT_SYMBOL(drm_framebuffer_init);
367
Rob Clarkf7eff602012-09-05 21:48:38 +0000368static void drm_framebuffer_free(struct kref *kref)
369{
370 struct drm_framebuffer *fb =
371 container_of(kref, struct drm_framebuffer, refcount);
372 fb->funcs->destroy(fb);
373}
374
375/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100376 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
377 * @dev: drm device
378 * @id: id of the fb object
379 *
380 * If successful, this grabs an additional reference to the framebuffer -
381 * callers need to make sure to eventually unreference the returned framebuffer
382 * again.
383 */
384struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
385 uint32_t id)
386{
387 struct drm_mode_object *obj = NULL;
388 struct drm_framebuffer *fb;
389
390 mutex_lock(&dev->mode_config.fb_lock);
391
392 mutex_lock(&dev->mode_config.idr_mutex);
393 obj = idr_find(&dev->mode_config.crtc_idr, id);
394 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
395 fb = NULL;
396 else
397 fb = obj_to_fb(obj);
398 mutex_unlock(&dev->mode_config.idr_mutex);
399
400 if (fb)
401 kref_get(&fb->refcount);
402
403 mutex_unlock(&dev->mode_config.fb_lock);
404
405 return fb;
406}
407EXPORT_SYMBOL(drm_framebuffer_lookup);
408
409/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000410 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100411 * @fb: framebuffer to unref
412 *
413 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000414 */
415void drm_framebuffer_unreference(struct drm_framebuffer *fb)
416{
Rob Clarkf7eff602012-09-05 21:48:38 +0000417 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000418 kref_put(&fb->refcount, drm_framebuffer_free);
419}
420EXPORT_SYMBOL(drm_framebuffer_unreference);
421
422/**
423 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100424 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000425 */
426void drm_framebuffer_reference(struct drm_framebuffer *fb)
427{
428 DRM_DEBUG("FB ID: %d\n", fb->base.id);
429 kref_get(&fb->refcount);
430}
431EXPORT_SYMBOL(drm_framebuffer_reference);
432
Dave Airlief453ba02008-11-07 14:05:41 -0800433/**
Daniel Vetter36206362012-12-10 20:42:17 +0100434 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
435 * @fb: fb to unregister
436 *
437 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
438 * those used for fbdev. Note that the caller must hold a reference of it's own,
439 * i.e. the object may not be destroyed through this call (since it'll lead to a
440 * locking inversion).
441 */
442void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
443{
444}
445EXPORT_SYMBOL(drm_framebuffer_unregister_private);
446
447/**
Dave Airlief453ba02008-11-07 14:05:41 -0800448 * drm_framebuffer_cleanup - remove a framebuffer object
449 * @fb: framebuffer to remove
450 *
Daniel Vetter36206362012-12-10 20:42:17 +0100451 * Cleanup references to a user-created framebuffer. This function is intended
452 * to be used from the drivers ->destroy callback.
453 *
454 * Note that this function does not remove the fb from active usuage - if it is
455 * still used anywhere, hilarity can ensue since userspace could call getfb on
456 * the id and get back -EINVAL. Obviously no concern at driver unload time.
457 *
458 * Also, the framebuffer will not be removed from the lookup idr - for
459 * user-created framebuffers this will happen in in the rmfb ioctl. For
460 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
461 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800462 */
463void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
464{
465 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100466
Rob Clarkf7eff602012-09-05 21:48:38 +0000467 /*
468 * This could be moved to drm_framebuffer_remove(), but for
469 * debugging is nice to keep around the list of fb's that are
470 * no longer associated w/ a drm_file but are not unreferenced
471 * yet. (i915 and omapdrm have debugfs files which will show
472 * this.)
473 */
474 drm_mode_object_put(dev, &fb->base);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100475 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000476 list_del(&fb->head);
477 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100478 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000479}
480EXPORT_SYMBOL(drm_framebuffer_cleanup);
481
482/**
483 * drm_framebuffer_remove - remove and unreference a framebuffer object
484 * @fb: framebuffer to remove
485 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000486 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100487 * using @fb, removes it, setting it to NULL. Then drops the reference to the
488 * passed-in framebuffer.
Rob Clarkf7eff602012-09-05 21:48:38 +0000489 */
490void drm_framebuffer_remove(struct drm_framebuffer *fb)
491{
492 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800493 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800494 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000495 struct drm_mode_set set;
496 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800497
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100498 WARN_ON(!drm_modeset_is_locked(dev));
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100499 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100500
Dave Airlief453ba02008-11-07 14:05:41 -0800501 /* remove from any CRTC */
502 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000503 if (crtc->fb == fb) {
504 /* should turn off the crtc */
505 memset(&set, 0, sizeof(struct drm_mode_set));
506 set.crtc = crtc;
507 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100508 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000509 if (ret)
510 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
511 }
Dave Airlief453ba02008-11-07 14:05:41 -0800512 }
513
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800514 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
515 if (plane->fb == fb) {
516 /* should turn off the crtc */
517 ret = plane->funcs->disable_plane(plane);
518 if (ret)
519 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600520 /* disconnect the plane from the fb and crtc: */
521 plane->fb = NULL;
522 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800523 }
524 }
525
Rob Clarkf7eff602012-09-05 21:48:38 +0000526 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800527}
Rob Clarkf7eff602012-09-05 21:48:38 +0000528EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800529
530/**
531 * drm_crtc_init - Initialise a new CRTC object
532 * @dev: DRM device
533 * @crtc: CRTC object to init
534 * @funcs: callbacks for the new CRTC
535 *
Dave Airlief453ba02008-11-07 14:05:41 -0800536 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200537 *
538 * RETURNS:
539 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800540 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200541int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800542 const struct drm_crtc_funcs *funcs)
543{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200544 int ret;
545
Dave Airlief453ba02008-11-07 14:05:41 -0800546 crtc->dev = dev;
547 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000548 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800549
Daniel Vetter84849902012-12-02 00:28:11 +0100550 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100551 mutex_init(&crtc->mutex);
552 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200553
554 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
555 if (ret)
556 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800557
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300558 crtc->base.properties = &crtc->properties;
559
Dave Airlief453ba02008-11-07 14:05:41 -0800560 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
561 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200562
563 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100564 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200565
566 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800567}
568EXPORT_SYMBOL(drm_crtc_init);
569
570/**
571 * drm_crtc_cleanup - Cleans up the core crtc usage.
572 * @crtc: CRTC to cleanup
573 *
Dave Airlief453ba02008-11-07 14:05:41 -0800574 * Cleanup @crtc. Removes from drm modesetting space
575 * does NOT free object, caller does that.
576 */
577void drm_crtc_cleanup(struct drm_crtc *crtc)
578{
579 struct drm_device *dev = crtc->dev;
580
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000581 kfree(crtc->gamma_store);
582 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800583
584 drm_mode_object_put(dev, &crtc->base);
585 list_del(&crtc->head);
586 dev->mode_config.num_crtc--;
587}
588EXPORT_SYMBOL(drm_crtc_cleanup);
589
590/**
591 * drm_mode_probed_add - add a mode to a connector's probed mode list
592 * @connector: connector the new mode
593 * @mode: mode data
594 *
Dave Airlief453ba02008-11-07 14:05:41 -0800595 * Add @mode to @connector's mode list for later use.
596 */
597void drm_mode_probed_add(struct drm_connector *connector,
598 struct drm_display_mode *mode)
599{
600 list_add(&mode->head, &connector->probed_modes);
601}
602EXPORT_SYMBOL(drm_mode_probed_add);
603
604/**
605 * drm_mode_remove - remove and free a mode
606 * @connector: connector list to modify
607 * @mode: mode to remove
608 *
Dave Airlief453ba02008-11-07 14:05:41 -0800609 * Remove @mode from @connector's mode list, then free it.
610 */
611void drm_mode_remove(struct drm_connector *connector,
612 struct drm_display_mode *mode)
613{
614 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100615 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800616}
617EXPORT_SYMBOL(drm_mode_remove);
618
619/**
620 * drm_connector_init - Init a preallocated connector
621 * @dev: DRM device
622 * @connector: the connector to init
623 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100624 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800625 *
Dave Airlief453ba02008-11-07 14:05:41 -0800626 * Initialises a preallocated connector. Connectors should be
627 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628 *
629 * RETURNS:
630 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800631 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632int drm_connector_init(struct drm_device *dev,
633 struct drm_connector *connector,
634 const struct drm_connector_funcs *funcs,
635 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800636{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200637 int ret;
638
Daniel Vetter84849902012-12-02 00:28:11 +0100639 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800640
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200641 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
642 if (ret)
643 goto out;
644
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300645 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800646 connector->dev = dev;
647 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800648 connector->connector_type = connector_type;
649 connector->connector_type_id =
650 ++drm_connector_enum_list[connector_type].count; /* TODO */
651 INIT_LIST_HEAD(&connector->user_modes);
652 INIT_LIST_HEAD(&connector->probed_modes);
653 INIT_LIST_HEAD(&connector->modes);
654 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000655 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800656
657 list_add_tail(&connector->head, &dev->mode_config.connector_list);
658 dev->mode_config.num_connector++;
659
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200660 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500661 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200662 dev->mode_config.edid_property,
663 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800664
Rob Clark58495562012-10-11 20:50:56 -0500665 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800666 dev->mode_config.dpms_property, 0);
667
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100669 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670
671 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800672}
673EXPORT_SYMBOL(drm_connector_init);
674
675/**
676 * drm_connector_cleanup - cleans up an initialised connector
677 * @connector: connector to cleanup
678 *
Dave Airlief453ba02008-11-07 14:05:41 -0800679 * Cleans up the connector but doesn't free the object.
680 */
681void drm_connector_cleanup(struct drm_connector *connector)
682{
683 struct drm_device *dev = connector->dev;
684 struct drm_display_mode *mode, *t;
685
686 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
687 drm_mode_remove(connector, mode);
688
689 list_for_each_entry_safe(mode, t, &connector->modes, head)
690 drm_mode_remove(connector, mode);
691
692 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
693 drm_mode_remove(connector, mode);
694
Dave Airlief453ba02008-11-07 14:05:41 -0800695 drm_mode_object_put(dev, &connector->base);
696 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000697 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800698}
699EXPORT_SYMBOL(drm_connector_cleanup);
700
Dave Airliecbc7e222012-02-20 14:16:40 +0000701void drm_connector_unplug_all(struct drm_device *dev)
702{
703 struct drm_connector *connector;
704
705 /* taking the mode config mutex ends up in a clash with sysfs */
706 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
707 drm_sysfs_connector_remove(connector);
708
709}
710EXPORT_SYMBOL(drm_connector_unplug_all);
711
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200712int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000713 struct drm_encoder *encoder,
714 const struct drm_encoder_funcs *funcs,
715 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800716{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200717 int ret;
718
Daniel Vetter84849902012-12-02 00:28:11 +0100719 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800720
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200721 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
722 if (ret)
723 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800724
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200725 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800726 encoder->encoder_type = encoder_type;
727 encoder->funcs = funcs;
728
729 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
730 dev->mode_config.num_encoder++;
731
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200732 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100733 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200734
735 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800736}
737EXPORT_SYMBOL(drm_encoder_init);
738
739void drm_encoder_cleanup(struct drm_encoder *encoder)
740{
741 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100742 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800743 drm_mode_object_put(dev, &encoder->base);
744 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000745 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100746 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800747}
748EXPORT_SYMBOL(drm_encoder_cleanup);
749
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800750int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
751 unsigned long possible_crtcs,
752 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600753 const uint32_t *formats, uint32_t format_count,
754 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800755{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200756 int ret;
757
Daniel Vetter84849902012-12-02 00:28:11 +0100758 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800759
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
761 if (ret)
762 goto out;
763
Rob Clark4d939142012-05-17 02:23:27 -0600764 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800765 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800766 plane->funcs = funcs;
767 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
768 GFP_KERNEL);
769 if (!plane->format_types) {
770 DRM_DEBUG_KMS("out of memory when allocating plane\n");
771 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200772 ret = -ENOMEM;
773 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800774 }
775
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800776 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800777 plane->format_count = format_count;
778 plane->possible_crtcs = possible_crtcs;
779
Rob Clark0a7eb242011-12-13 20:19:36 -0600780 /* private planes are not exposed to userspace, but depending on
781 * display hardware, might be convenient to allow sharing programming
782 * for the scanout engine with the crtc implementation.
783 */
784 if (!priv) {
785 list_add_tail(&plane->head, &dev->mode_config.plane_list);
786 dev->mode_config.num_plane++;
787 } else {
788 INIT_LIST_HEAD(&plane->head);
789 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800790
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200791 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100792 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800793
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800795}
796EXPORT_SYMBOL(drm_plane_init);
797
798void drm_plane_cleanup(struct drm_plane *plane)
799{
800 struct drm_device *dev = plane->dev;
801
Daniel Vetter84849902012-12-02 00:28:11 +0100802 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800803 kfree(plane->format_types);
804 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600805 /* if not added to a list, it must be a private plane */
806 if (!list_empty(&plane->head)) {
807 list_del(&plane->head);
808 dev->mode_config.num_plane--;
809 }
Daniel Vetter84849902012-12-02 00:28:11 +0100810 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800811}
812EXPORT_SYMBOL(drm_plane_cleanup);
813
Dave Airlief453ba02008-11-07 14:05:41 -0800814/**
815 * drm_mode_create - create a new display mode
816 * @dev: DRM device
817 *
Dave Airlief453ba02008-11-07 14:05:41 -0800818 * Create a new drm_display_mode, give it an ID, and return it.
819 *
820 * RETURNS:
821 * Pointer to new mode on success, NULL on error.
822 */
823struct drm_display_mode *drm_mode_create(struct drm_device *dev)
824{
825 struct drm_display_mode *nmode;
826
827 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
828 if (!nmode)
829 return NULL;
830
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200831 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
832 kfree(nmode);
833 return NULL;
834 }
835
Dave Airlief453ba02008-11-07 14:05:41 -0800836 return nmode;
837}
838EXPORT_SYMBOL(drm_mode_create);
839
840/**
841 * drm_mode_destroy - remove a mode
842 * @dev: DRM device
843 * @mode: mode to remove
844 *
Dave Airlief453ba02008-11-07 14:05:41 -0800845 * Free @mode's unique identifier, then free it.
846 */
847void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
848{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200849 if (!mode)
850 return;
851
Dave Airlief453ba02008-11-07 14:05:41 -0800852 drm_mode_object_put(dev, &mode->base);
853
854 kfree(mode);
855}
856EXPORT_SYMBOL(drm_mode_destroy);
857
858static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
859{
860 struct drm_property *edid;
861 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800862
863 /*
864 * Standard properties (apply to all connectors)
865 */
866 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
867 DRM_MODE_PROP_IMMUTABLE,
868 "EDID", 0);
869 dev->mode_config.edid_property = edid;
870
Sascha Hauer4a67d392012-02-06 10:58:17 +0100871 dpms = drm_property_create_enum(dev, 0,
872 "DPMS", drm_dpms_enum_list,
873 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800874 dev->mode_config.dpms_property = dpms;
875
876 return 0;
877}
878
879/**
880 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
881 * @dev: DRM device
882 *
883 * Called by a driver the first time a DVI-I connector is made.
884 */
885int drm_mode_create_dvi_i_properties(struct drm_device *dev)
886{
887 struct drm_property *dvi_i_selector;
888 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800889
890 if (dev->mode_config.dvi_i_select_subconnector_property)
891 return 0;
892
893 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100894 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800895 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100896 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800897 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800898 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
899
Sascha Hauer4a67d392012-02-06 10:58:17 +0100900 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800901 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100902 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800903 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800904 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
905
906 return 0;
907}
908EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
909
910/**
911 * drm_create_tv_properties - create TV specific connector properties
912 * @dev: DRM device
913 * @num_modes: number of different TV formats (modes) supported
914 * @modes: array of pointers to strings containing name of each format
915 *
916 * Called by a driver's TV initialization routine, this function creates
917 * the TV specific connector properties for a given device. Caller is
918 * responsible for allocating a list of format names and passing them to
919 * this routine.
920 */
921int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
922 char *modes[])
923{
924 struct drm_property *tv_selector;
925 struct drm_property *tv_subconnector;
926 int i;
927
928 if (dev->mode_config.tv_select_subconnector_property)
929 return 0;
930
931 /*
932 * Basic connector properties
933 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100934 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800935 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100936 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800937 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800938 dev->mode_config.tv_select_subconnector_property = tv_selector;
939
940 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100941 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
942 "subconnector",
943 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800944 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800945 dev->mode_config.tv_subconnector_property = tv_subconnector;
946
947 /*
948 * Other, TV specific properties: margins & TV modes.
949 */
950 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100951 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800952
953 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100954 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800955
956 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100957 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800958
959 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100960 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800961
962 dev->mode_config.tv_mode_property =
963 drm_property_create(dev, DRM_MODE_PROP_ENUM,
964 "mode", num_modes);
965 for (i = 0; i < num_modes; i++)
966 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
967 i, modes[i]);
968
Francisco Jerezb6b79022009-08-02 04:19:20 +0200969 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100970 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200971
972 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100973 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200974
975 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100976 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200977
Francisco Jereza75f0232009-08-12 02:30:10 +0200978 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100979 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200980
981 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100982 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200983
984 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100985 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200986
Dave Airlief453ba02008-11-07 14:05:41 -0800987 return 0;
988}
989EXPORT_SYMBOL(drm_mode_create_tv_properties);
990
991/**
992 * drm_mode_create_scaling_mode_property - create scaling mode property
993 * @dev: DRM device
994 *
995 * Called by a driver the first time it's needed, must be attached to desired
996 * connectors.
997 */
998int drm_mode_create_scaling_mode_property(struct drm_device *dev)
999{
1000 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001001
1002 if (dev->mode_config.scaling_mode_property)
1003 return 0;
1004
1005 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001006 drm_property_create_enum(dev, 0, "scaling mode",
1007 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001008 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001009
1010 dev->mode_config.scaling_mode_property = scaling_mode;
1011
1012 return 0;
1013}
1014EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1015
1016/**
1017 * drm_mode_create_dithering_property - create dithering property
1018 * @dev: DRM device
1019 *
1020 * Called by a driver the first time it's needed, must be attached to desired
1021 * connectors.
1022 */
1023int drm_mode_create_dithering_property(struct drm_device *dev)
1024{
1025 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001026
1027 if (dev->mode_config.dithering_mode_property)
1028 return 0;
1029
1030 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001031 drm_property_create_enum(dev, 0, "dithering",
1032 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001033 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001034 dev->mode_config.dithering_mode_property = dithering_mode;
1035
1036 return 0;
1037}
1038EXPORT_SYMBOL(drm_mode_create_dithering_property);
1039
1040/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001041 * drm_mode_create_dirty_property - create dirty property
1042 * @dev: DRM device
1043 *
1044 * Called by a driver the first time it's needed, must be attached to desired
1045 * connectors.
1046 */
1047int drm_mode_create_dirty_info_property(struct drm_device *dev)
1048{
1049 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001050
1051 if (dev->mode_config.dirty_info_property)
1052 return 0;
1053
1054 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001055 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001056 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001057 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001058 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001059 dev->mode_config.dirty_info_property = dirty_info;
1060
1061 return 0;
1062}
1063EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1064
1065/**
Dave Airlief453ba02008-11-07 14:05:41 -08001066 * drm_mode_config_init - initialize DRM mode_configuration structure
1067 * @dev: DRM device
1068 *
Dave Airlief453ba02008-11-07 14:05:41 -08001069 * Initialize @dev's mode_config structure, used for tracking the graphics
1070 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001071 *
1072 * Since this initializes the modeset locks, no locking is possible. Which is no
1073 * problem, since this should happen single threaded at init time. It is the
1074 * driver's problem to ensure this guarantee.
1075 *
Dave Airlief453ba02008-11-07 14:05:41 -08001076 */
1077void drm_mode_config_init(struct drm_device *dev)
1078{
1079 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001080 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001081 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001082 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001083 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1084 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1085 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1086 INIT_LIST_HEAD(&dev->mode_config.property_list);
1087 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001088 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001089 idr_init(&dev->mode_config.crtc_idr);
1090
Daniel Vetter84849902012-12-02 00:28:11 +01001091 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001092 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001093 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001094
1095 /* Just to be sure */
1096 dev->mode_config.num_fb = 0;
1097 dev->mode_config.num_connector = 0;
1098 dev->mode_config.num_crtc = 0;
1099 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001100}
1101EXPORT_SYMBOL(drm_mode_config_init);
1102
1103int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1104{
1105 uint32_t total_objects = 0;
1106
1107 total_objects += dev->mode_config.num_crtc;
1108 total_objects += dev->mode_config.num_connector;
1109 total_objects += dev->mode_config.num_encoder;
1110
Dave Airlief453ba02008-11-07 14:05:41 -08001111 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1112 if (!group->id_list)
1113 return -ENOMEM;
1114
1115 group->num_crtcs = 0;
1116 group->num_connectors = 0;
1117 group->num_encoders = 0;
1118 return 0;
1119}
1120
1121int drm_mode_group_init_legacy_group(struct drm_device *dev,
1122 struct drm_mode_group *group)
1123{
1124 struct drm_crtc *crtc;
1125 struct drm_encoder *encoder;
1126 struct drm_connector *connector;
1127 int ret;
1128
1129 if ((ret = drm_mode_group_init(dev, group)))
1130 return ret;
1131
1132 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1133 group->id_list[group->num_crtcs++] = crtc->base.id;
1134
1135 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1136 group->id_list[group->num_crtcs + group->num_encoders++] =
1137 encoder->base.id;
1138
1139 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1140 group->id_list[group->num_crtcs + group->num_encoders +
1141 group->num_connectors++] = connector->base.id;
1142
1143 return 0;
1144}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001145EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001146
1147/**
1148 * drm_mode_config_cleanup - free up DRM mode_config info
1149 * @dev: DRM device
1150 *
Dave Airlief453ba02008-11-07 14:05:41 -08001151 * Free up all the connectors and CRTCs associated with this DRM device, then
1152 * free up the framebuffers and associated buffer objects.
1153 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001154 * Note that since this /should/ happen single-threaded at driver/device
1155 * teardown time, no locking is required. It's the driver's job to ensure that
1156 * this guarantee actually holds true.
1157 *
Dave Airlief453ba02008-11-07 14:05:41 -08001158 * FIXME: cleanup any dangling user buffer objects too
1159 */
1160void drm_mode_config_cleanup(struct drm_device *dev)
1161{
1162 struct drm_connector *connector, *ot;
1163 struct drm_crtc *crtc, *ct;
1164 struct drm_encoder *encoder, *enct;
1165 struct drm_framebuffer *fb, *fbt;
1166 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001167 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001168
1169 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1170 head) {
1171 encoder->funcs->destroy(encoder);
1172 }
1173
1174 list_for_each_entry_safe(connector, ot,
1175 &dev->mode_config.connector_list, head) {
1176 connector->funcs->destroy(connector);
1177 }
1178
1179 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1180 head) {
1181 drm_property_destroy(dev, property);
1182 }
1183
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001184 /* Single-threaded teardown context, so it's not requied to grab the
1185 * fb_lock to protect against concurrent fb_list access. Contrary, it
1186 * would actually deadlock with the drm_framebuffer_cleanup function. */
Dave Airlief453ba02008-11-07 14:05:41 -08001187 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001188 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001189 }
1190
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001191 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1192 head) {
1193 plane->funcs->destroy(plane);
1194 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001195
Chris Wilson31840092012-09-17 09:38:03 +00001196 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1197 crtc->funcs->destroy(crtc);
1198 }
1199
Sascha Hauer59ce0622012-02-01 11:38:20 +01001200 idr_remove_all(&dev->mode_config.crtc_idr);
1201 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001202}
1203EXPORT_SYMBOL(drm_mode_config_cleanup);
1204
Dave Airlief453ba02008-11-07 14:05:41 -08001205/**
1206 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1207 * @out: drm_mode_modeinfo struct to return to the user
1208 * @in: drm_display_mode to use
1209 *
Dave Airlief453ba02008-11-07 14:05:41 -08001210 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1211 * the user.
1212 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001213static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1214 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001215{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001216 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1217 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1218 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1219 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1220 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1221 "timing values too large for mode info\n");
1222
Dave Airlief453ba02008-11-07 14:05:41 -08001223 out->clock = in->clock;
1224 out->hdisplay = in->hdisplay;
1225 out->hsync_start = in->hsync_start;
1226 out->hsync_end = in->hsync_end;
1227 out->htotal = in->htotal;
1228 out->hskew = in->hskew;
1229 out->vdisplay = in->vdisplay;
1230 out->vsync_start = in->vsync_start;
1231 out->vsync_end = in->vsync_end;
1232 out->vtotal = in->vtotal;
1233 out->vscan = in->vscan;
1234 out->vrefresh = in->vrefresh;
1235 out->flags = in->flags;
1236 out->type = in->type;
1237 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1238 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1239}
1240
1241/**
1242 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1243 * @out: drm_display_mode to return to the user
1244 * @in: drm_mode_modeinfo to use
1245 *
Dave Airlief453ba02008-11-07 14:05:41 -08001246 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1247 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001248 *
1249 * RETURNS:
1250 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001251 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001252static int drm_crtc_convert_umode(struct drm_display_mode *out,
1253 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001254{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001255 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1256 return -ERANGE;
1257
Dave Airlief453ba02008-11-07 14:05:41 -08001258 out->clock = in->clock;
1259 out->hdisplay = in->hdisplay;
1260 out->hsync_start = in->hsync_start;
1261 out->hsync_end = in->hsync_end;
1262 out->htotal = in->htotal;
1263 out->hskew = in->hskew;
1264 out->vdisplay = in->vdisplay;
1265 out->vsync_start = in->vsync_start;
1266 out->vsync_end = in->vsync_end;
1267 out->vtotal = in->vtotal;
1268 out->vscan = in->vscan;
1269 out->vrefresh = in->vrefresh;
1270 out->flags = in->flags;
1271 out->type = in->type;
1272 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1273 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001274
1275 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001276}
1277
1278/**
1279 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001280 * @dev: drm device for the ioctl
1281 * @data: data pointer for the ioctl
1282 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001283 *
Dave Airlief453ba02008-11-07 14:05:41 -08001284 * Construct a set of configuration description structures and return
1285 * them to the user, including CRTC, connector and framebuffer configuration.
1286 *
1287 * Called by the user via ioctl.
1288 *
1289 * RETURNS:
1290 * Zero on success, errno on failure.
1291 */
1292int drm_mode_getresources(struct drm_device *dev, void *data,
1293 struct drm_file *file_priv)
1294{
1295 struct drm_mode_card_res *card_res = data;
1296 struct list_head *lh;
1297 struct drm_framebuffer *fb;
1298 struct drm_connector *connector;
1299 struct drm_crtc *crtc;
1300 struct drm_encoder *encoder;
1301 int ret = 0;
1302 int connector_count = 0;
1303 int crtc_count = 0;
1304 int fb_count = 0;
1305 int encoder_count = 0;
1306 int copied = 0, i;
1307 uint32_t __user *fb_id;
1308 uint32_t __user *crtc_id;
1309 uint32_t __user *connector_id;
1310 uint32_t __user *encoder_id;
1311 struct drm_mode_group *mode_group;
1312
Dave Airliefb3b06c2011-02-08 13:55:21 +10001313 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1314 return -EINVAL;
1315
Dave Airlief453ba02008-11-07 14:05:41 -08001316
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001317 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001318 /*
1319 * For the non-control nodes we need to limit the list of resources
1320 * by IDs in the group list for this node
1321 */
1322 list_for_each(lh, &file_priv->fbs)
1323 fb_count++;
1324
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001325 /* handle this in 4 parts */
1326 /* FBs */
1327 if (card_res->count_fbs >= fb_count) {
1328 copied = 0;
1329 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1330 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1331 if (put_user(fb->base.id, fb_id + copied)) {
1332 mutex_unlock(&file_priv->fbs_lock);
1333 return -EFAULT;
1334 }
1335 copied++;
1336 }
1337 }
1338 card_res->count_fbs = fb_count;
1339 mutex_unlock(&file_priv->fbs_lock);
1340
1341 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001342 mode_group = &file_priv->master->minor->mode_group;
1343 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1344
1345 list_for_each(lh, &dev->mode_config.crtc_list)
1346 crtc_count++;
1347
1348 list_for_each(lh, &dev->mode_config.connector_list)
1349 connector_count++;
1350
1351 list_for_each(lh, &dev->mode_config.encoder_list)
1352 encoder_count++;
1353 } else {
1354
1355 crtc_count = mode_group->num_crtcs;
1356 connector_count = mode_group->num_connectors;
1357 encoder_count = mode_group->num_encoders;
1358 }
1359
1360 card_res->max_height = dev->mode_config.max_height;
1361 card_res->min_height = dev->mode_config.min_height;
1362 card_res->max_width = dev->mode_config.max_width;
1363 card_res->min_width = dev->mode_config.min_width;
1364
Dave Airlief453ba02008-11-07 14:05:41 -08001365 /* CRTCs */
1366 if (card_res->count_crtcs >= crtc_count) {
1367 copied = 0;
1368 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1369 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1370 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1371 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001372 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001373 if (put_user(crtc->base.id, crtc_id + copied)) {
1374 ret = -EFAULT;
1375 goto out;
1376 }
1377 copied++;
1378 }
1379 } else {
1380 for (i = 0; i < mode_group->num_crtcs; i++) {
1381 if (put_user(mode_group->id_list[i],
1382 crtc_id + copied)) {
1383 ret = -EFAULT;
1384 goto out;
1385 }
1386 copied++;
1387 }
1388 }
1389 }
1390 card_res->count_crtcs = crtc_count;
1391
1392 /* Encoders */
1393 if (card_res->count_encoders >= encoder_count) {
1394 copied = 0;
1395 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1396 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1397 list_for_each_entry(encoder,
1398 &dev->mode_config.encoder_list,
1399 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001400 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1401 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001402 if (put_user(encoder->base.id, encoder_id +
1403 copied)) {
1404 ret = -EFAULT;
1405 goto out;
1406 }
1407 copied++;
1408 }
1409 } else {
1410 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1411 if (put_user(mode_group->id_list[i],
1412 encoder_id + copied)) {
1413 ret = -EFAULT;
1414 goto out;
1415 }
1416 copied++;
1417 }
1418
1419 }
1420 }
1421 card_res->count_encoders = encoder_count;
1422
1423 /* Connectors */
1424 if (card_res->count_connectors >= connector_count) {
1425 copied = 0;
1426 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1427 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1428 list_for_each_entry(connector,
1429 &dev->mode_config.connector_list,
1430 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001431 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1432 connector->base.id,
1433 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001434 if (put_user(connector->base.id,
1435 connector_id + copied)) {
1436 ret = -EFAULT;
1437 goto out;
1438 }
1439 copied++;
1440 }
1441 } else {
1442 int start = mode_group->num_crtcs +
1443 mode_group->num_encoders;
1444 for (i = start; i < start + mode_group->num_connectors; i++) {
1445 if (put_user(mode_group->id_list[i],
1446 connector_id + copied)) {
1447 ret = -EFAULT;
1448 goto out;
1449 }
1450 copied++;
1451 }
1452 }
1453 }
1454 card_res->count_connectors = connector_count;
1455
Jerome Glisse94401062010-07-15 15:43:25 -04001456 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001457 card_res->count_connectors, card_res->count_encoders);
1458
1459out:
Daniel Vetter84849902012-12-02 00:28:11 +01001460 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001461 return ret;
1462}
1463
1464/**
1465 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001466 * @dev: drm device for the ioctl
1467 * @data: data pointer for the ioctl
1468 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001469 *
Dave Airlief453ba02008-11-07 14:05:41 -08001470 * Construct a CRTC configuration structure to return to the user.
1471 *
1472 * Called by the user via ioctl.
1473 *
1474 * RETURNS:
1475 * Zero on success, errno on failure.
1476 */
1477int drm_mode_getcrtc(struct drm_device *dev,
1478 void *data, struct drm_file *file_priv)
1479{
1480 struct drm_mode_crtc *crtc_resp = data;
1481 struct drm_crtc *crtc;
1482 struct drm_mode_object *obj;
1483 int ret = 0;
1484
Dave Airliefb3b06c2011-02-08 13:55:21 +10001485 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1486 return -EINVAL;
1487
Daniel Vetter84849902012-12-02 00:28:11 +01001488 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001489
1490 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1491 DRM_MODE_OBJECT_CRTC);
1492 if (!obj) {
1493 ret = -EINVAL;
1494 goto out;
1495 }
1496 crtc = obj_to_crtc(obj);
1497
1498 crtc_resp->x = crtc->x;
1499 crtc_resp->y = crtc->y;
1500 crtc_resp->gamma_size = crtc->gamma_size;
1501 if (crtc->fb)
1502 crtc_resp->fb_id = crtc->fb->base.id;
1503 else
1504 crtc_resp->fb_id = 0;
1505
1506 if (crtc->enabled) {
1507
1508 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1509 crtc_resp->mode_valid = 1;
1510
1511 } else {
1512 crtc_resp->mode_valid = 0;
1513 }
1514
1515out:
Daniel Vetter84849902012-12-02 00:28:11 +01001516 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001517 return ret;
1518}
1519
1520/**
1521 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001522 * @dev: drm device for the ioctl
1523 * @data: data pointer for the ioctl
1524 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001525 *
Dave Airlief453ba02008-11-07 14:05:41 -08001526 * Construct a connector configuration structure to return to the user.
1527 *
1528 * Called by the user via ioctl.
1529 *
1530 * RETURNS:
1531 * Zero on success, errno on failure.
1532 */
1533int drm_mode_getconnector(struct drm_device *dev, void *data,
1534 struct drm_file *file_priv)
1535{
1536 struct drm_mode_get_connector *out_resp = data;
1537 struct drm_mode_object *obj;
1538 struct drm_connector *connector;
1539 struct drm_display_mode *mode;
1540 int mode_count = 0;
1541 int props_count = 0;
1542 int encoders_count = 0;
1543 int ret = 0;
1544 int copied = 0;
1545 int i;
1546 struct drm_mode_modeinfo u_mode;
1547 struct drm_mode_modeinfo __user *mode_ptr;
1548 uint32_t __user *prop_ptr;
1549 uint64_t __user *prop_values;
1550 uint32_t __user *encoder_ptr;
1551
Dave Airliefb3b06c2011-02-08 13:55:21 +10001552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1553 return -EINVAL;
1554
Dave Airlief453ba02008-11-07 14:05:41 -08001555 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1556
Jerome Glisse94401062010-07-15 15:43:25 -04001557 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001558
Daniel Vetter84849902012-12-02 00:28:11 +01001559 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001560
1561 obj = drm_mode_object_find(dev, out_resp->connector_id,
1562 DRM_MODE_OBJECT_CONNECTOR);
1563 if (!obj) {
1564 ret = -EINVAL;
1565 goto out;
1566 }
1567 connector = obj_to_connector(obj);
1568
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001569 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001570
1571 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1572 if (connector->encoder_ids[i] != 0) {
1573 encoders_count++;
1574 }
1575 }
1576
1577 if (out_resp->count_modes == 0) {
1578 connector->funcs->fill_modes(connector,
1579 dev->mode_config.max_width,
1580 dev->mode_config.max_height);
1581 }
1582
1583 /* delayed so we get modes regardless of pre-fill_modes state */
1584 list_for_each_entry(mode, &connector->modes, head)
1585 mode_count++;
1586
1587 out_resp->connector_id = connector->base.id;
1588 out_resp->connector_type = connector->connector_type;
1589 out_resp->connector_type_id = connector->connector_type_id;
1590 out_resp->mm_width = connector->display_info.width_mm;
1591 out_resp->mm_height = connector->display_info.height_mm;
1592 out_resp->subpixel = connector->display_info.subpixel_order;
1593 out_resp->connection = connector->status;
1594 if (connector->encoder)
1595 out_resp->encoder_id = connector->encoder->base.id;
1596 else
1597 out_resp->encoder_id = 0;
1598
1599 /*
1600 * This ioctl is called twice, once to determine how much space is
1601 * needed, and the 2nd time to fill it.
1602 */
1603 if ((out_resp->count_modes >= mode_count) && mode_count) {
1604 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001605 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001606 list_for_each_entry(mode, &connector->modes, head) {
1607 drm_crtc_convert_to_umode(&u_mode, mode);
1608 if (copy_to_user(mode_ptr + copied,
1609 &u_mode, sizeof(u_mode))) {
1610 ret = -EFAULT;
1611 goto out;
1612 }
1613 copied++;
1614 }
1615 }
1616 out_resp->count_modes = mode_count;
1617
1618 if ((out_resp->count_props >= props_count) && props_count) {
1619 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001620 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1621 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001622 for (i = 0; i < connector->properties.count; i++) {
1623 if (put_user(connector->properties.ids[i],
1624 prop_ptr + copied)) {
1625 ret = -EFAULT;
1626 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001627 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001628
1629 if (put_user(connector->properties.values[i],
1630 prop_values + copied)) {
1631 ret = -EFAULT;
1632 goto out;
1633 }
1634 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001635 }
1636 }
1637 out_resp->count_props = props_count;
1638
1639 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1640 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001641 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001642 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1643 if (connector->encoder_ids[i] != 0) {
1644 if (put_user(connector->encoder_ids[i],
1645 encoder_ptr + copied)) {
1646 ret = -EFAULT;
1647 goto out;
1648 }
1649 copied++;
1650 }
1651 }
1652 }
1653 out_resp->count_encoders = encoders_count;
1654
1655out:
Daniel Vetter84849902012-12-02 00:28:11 +01001656 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001657 return ret;
1658}
1659
1660int drm_mode_getencoder(struct drm_device *dev, void *data,
1661 struct drm_file *file_priv)
1662{
1663 struct drm_mode_get_encoder *enc_resp = data;
1664 struct drm_mode_object *obj;
1665 struct drm_encoder *encoder;
1666 int ret = 0;
1667
Dave Airliefb3b06c2011-02-08 13:55:21 +10001668 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1669 return -EINVAL;
1670
Daniel Vetter84849902012-12-02 00:28:11 +01001671 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001672 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1673 DRM_MODE_OBJECT_ENCODER);
1674 if (!obj) {
1675 ret = -EINVAL;
1676 goto out;
1677 }
1678 encoder = obj_to_encoder(obj);
1679
1680 if (encoder->crtc)
1681 enc_resp->crtc_id = encoder->crtc->base.id;
1682 else
1683 enc_resp->crtc_id = 0;
1684 enc_resp->encoder_type = encoder->encoder_type;
1685 enc_resp->encoder_id = encoder->base.id;
1686 enc_resp->possible_crtcs = encoder->possible_crtcs;
1687 enc_resp->possible_clones = encoder->possible_clones;
1688
1689out:
Daniel Vetter84849902012-12-02 00:28:11 +01001690 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001691 return ret;
1692}
1693
1694/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001695 * drm_mode_getplane_res - get plane info
1696 * @dev: DRM device
1697 * @data: ioctl data
1698 * @file_priv: DRM file info
1699 *
1700 * Return an plane count and set of IDs.
1701 */
1702int drm_mode_getplane_res(struct drm_device *dev, void *data,
1703 struct drm_file *file_priv)
1704{
1705 struct drm_mode_get_plane_res *plane_resp = data;
1706 struct drm_mode_config *config;
1707 struct drm_plane *plane;
1708 uint32_t __user *plane_ptr;
1709 int copied = 0, ret = 0;
1710
1711 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1712 return -EINVAL;
1713
Daniel Vetter84849902012-12-02 00:28:11 +01001714 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001715 config = &dev->mode_config;
1716
1717 /*
1718 * This ioctl is called twice, once to determine how much space is
1719 * needed, and the 2nd time to fill it.
1720 */
1721 if (config->num_plane &&
1722 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001723 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001724
1725 list_for_each_entry(plane, &config->plane_list, head) {
1726 if (put_user(plane->base.id, plane_ptr + copied)) {
1727 ret = -EFAULT;
1728 goto out;
1729 }
1730 copied++;
1731 }
1732 }
1733 plane_resp->count_planes = config->num_plane;
1734
1735out:
Daniel Vetter84849902012-12-02 00:28:11 +01001736 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001737 return ret;
1738}
1739
1740/**
1741 * drm_mode_getplane - get plane info
1742 * @dev: DRM device
1743 * @data: ioctl data
1744 * @file_priv: DRM file info
1745 *
1746 * Return plane info, including formats supported, gamma size, any
1747 * current fb, etc.
1748 */
1749int drm_mode_getplane(struct drm_device *dev, void *data,
1750 struct drm_file *file_priv)
1751{
1752 struct drm_mode_get_plane *plane_resp = data;
1753 struct drm_mode_object *obj;
1754 struct drm_plane *plane;
1755 uint32_t __user *format_ptr;
1756 int ret = 0;
1757
1758 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1759 return -EINVAL;
1760
Daniel Vetter84849902012-12-02 00:28:11 +01001761 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001762 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1763 DRM_MODE_OBJECT_PLANE);
1764 if (!obj) {
1765 ret = -ENOENT;
1766 goto out;
1767 }
1768 plane = obj_to_plane(obj);
1769
1770 if (plane->crtc)
1771 plane_resp->crtc_id = plane->crtc->base.id;
1772 else
1773 plane_resp->crtc_id = 0;
1774
1775 if (plane->fb)
1776 plane_resp->fb_id = plane->fb->base.id;
1777 else
1778 plane_resp->fb_id = 0;
1779
1780 plane_resp->plane_id = plane->base.id;
1781 plane_resp->possible_crtcs = plane->possible_crtcs;
1782 plane_resp->gamma_size = plane->gamma_size;
1783
1784 /*
1785 * This ioctl is called twice, once to determine how much space is
1786 * needed, and the 2nd time to fill it.
1787 */
1788 if (plane->format_count &&
1789 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001790 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001791 if (copy_to_user(format_ptr,
1792 plane->format_types,
1793 sizeof(uint32_t) * plane->format_count)) {
1794 ret = -EFAULT;
1795 goto out;
1796 }
1797 }
1798 plane_resp->count_format_types = plane->format_count;
1799
1800out:
Daniel Vetter84849902012-12-02 00:28:11 +01001801 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001802 return ret;
1803}
1804
1805/**
1806 * drm_mode_setplane - set up or tear down an plane
1807 * @dev: DRM device
1808 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001809 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001810 *
1811 * Set plane info, including placement, fb, scaling, and other factors.
1812 * Or pass a NULL fb to disable.
1813 */
1814int drm_mode_setplane(struct drm_device *dev, void *data,
1815 struct drm_file *file_priv)
1816{
1817 struct drm_mode_set_plane *plane_req = data;
1818 struct drm_mode_object *obj;
1819 struct drm_plane *plane;
1820 struct drm_crtc *crtc;
1821 struct drm_framebuffer *fb;
1822 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001823 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001824 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001825
1826 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1827 return -EINVAL;
1828
Daniel Vetter84849902012-12-02 00:28:11 +01001829 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830
1831 /*
1832 * First, find the plane, crtc, and fb objects. If not available,
1833 * we don't bother to call the driver.
1834 */
1835 obj = drm_mode_object_find(dev, plane_req->plane_id,
1836 DRM_MODE_OBJECT_PLANE);
1837 if (!obj) {
1838 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1839 plane_req->plane_id);
1840 ret = -ENOENT;
1841 goto out;
1842 }
1843 plane = obj_to_plane(obj);
1844
1845 /* No fb means shut it down */
1846 if (!plane_req->fb_id) {
1847 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001848 plane->crtc = NULL;
1849 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001850 goto out;
1851 }
1852
1853 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1854 DRM_MODE_OBJECT_CRTC);
1855 if (!obj) {
1856 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1857 plane_req->crtc_id);
1858 ret = -ENOENT;
1859 goto out;
1860 }
1861 crtc = obj_to_crtc(obj);
1862
Daniel Vetter786b99e2012-12-02 21:53:40 +01001863 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1864 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001865 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1866 plane_req->fb_id);
1867 ret = -ENOENT;
1868 goto out;
1869 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01001870 /* fb is protect by the mode_config lock, so drop the ref immediately */
1871 drm_framebuffer_unreference(fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001872
Ville Syrjälä62443be2011-12-20 00:06:47 +02001873 /* Check whether this plane supports the fb pixel format. */
1874 for (i = 0; i < plane->format_count; i++)
1875 if (fb->pixel_format == plane->format_types[i])
1876 break;
1877 if (i == plane->format_count) {
1878 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1879 ret = -EINVAL;
1880 goto out;
1881 }
1882
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001883 fb_width = fb->width << 16;
1884 fb_height = fb->height << 16;
1885
1886 /* Make sure source coordinates are inside the fb. */
1887 if (plane_req->src_w > fb_width ||
1888 plane_req->src_x > fb_width - plane_req->src_w ||
1889 plane_req->src_h > fb_height ||
1890 plane_req->src_y > fb_height - plane_req->src_h) {
1891 DRM_DEBUG_KMS("Invalid source coordinates "
1892 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1893 plane_req->src_w >> 16,
1894 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1895 plane_req->src_h >> 16,
1896 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1897 plane_req->src_x >> 16,
1898 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1899 plane_req->src_y >> 16,
1900 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1901 ret = -ENOSPC;
1902 goto out;
1903 }
1904
Ville Syrjälä687a0402011-12-20 00:06:45 +02001905 /* Give drivers some help against integer overflows */
1906 if (plane_req->crtc_w > INT_MAX ||
1907 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1908 plane_req->crtc_h > INT_MAX ||
1909 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1910 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1911 plane_req->crtc_w, plane_req->crtc_h,
1912 plane_req->crtc_x, plane_req->crtc_y);
1913 ret = -ERANGE;
1914 goto out;
1915 }
1916
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001917 ret = plane->funcs->update_plane(plane, crtc, fb,
1918 plane_req->crtc_x, plane_req->crtc_y,
1919 plane_req->crtc_w, plane_req->crtc_h,
1920 plane_req->src_x, plane_req->src_y,
1921 plane_req->src_w, plane_req->src_h);
1922 if (!ret) {
1923 plane->crtc = crtc;
1924 plane->fb = fb;
1925 }
1926
1927out:
Daniel Vetter84849902012-12-02 00:28:11 +01001928 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001929
1930 return ret;
1931}
1932
1933/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001934 * drm_mode_set_config_internal - helper to call ->set_config
1935 * @set: modeset config to set
1936 *
1937 * This is a little helper to wrap internal calls to the ->set_config driver
1938 * interface. The only thing it adds is correct refcounting dance.
1939 */
1940int drm_mode_set_config_internal(struct drm_mode_set *set)
1941{
1942 struct drm_crtc *crtc = set->crtc;
1943
1944 return crtc->funcs->set_config(set);
1945}
1946EXPORT_SYMBOL(drm_mode_set_config_internal);
1947
1948/**
Dave Airlief453ba02008-11-07 14:05:41 -08001949 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001950 * @dev: drm device for the ioctl
1951 * @data: data pointer for the ioctl
1952 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001953 *
Dave Airlief453ba02008-11-07 14:05:41 -08001954 * Build a new CRTC configuration based on user request.
1955 *
1956 * Called by the user via ioctl.
1957 *
1958 * RETURNS:
1959 * Zero on success, errno on failure.
1960 */
1961int drm_mode_setcrtc(struct drm_device *dev, void *data,
1962 struct drm_file *file_priv)
1963{
1964 struct drm_mode_config *config = &dev->mode_config;
1965 struct drm_mode_crtc *crtc_req = data;
1966 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001967 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001968 struct drm_connector **connector_set = NULL, *connector;
1969 struct drm_framebuffer *fb = NULL;
1970 struct drm_display_mode *mode = NULL;
1971 struct drm_mode_set set;
1972 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001973 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001974 int i;
1975
Dave Airliefb3b06c2011-02-08 13:55:21 +10001976 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1977 return -EINVAL;
1978
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001979 /* For some reason crtc x/y offsets are signed internally. */
1980 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1981 return -ERANGE;
1982
Daniel Vetter84849902012-12-02 00:28:11 +01001983 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001984 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1985 DRM_MODE_OBJECT_CRTC);
1986 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001987 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001988 ret = -EINVAL;
1989 goto out;
1990 }
1991 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001992 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001993
1994 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001995 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001996 /* If we have a mode we need a framebuffer. */
1997 /* If we pass -1, set the mode with the currently bound fb */
1998 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001999 if (!crtc->fb) {
2000 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2001 ret = -EINVAL;
2002 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002003 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002004 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002005 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002006 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2007 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002008 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2009 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002010 ret = -EINVAL;
2011 goto out;
2012 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002013 /* fb is protect by the mode_config lock, so drop the
2014 * ref immediately */
2015 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002016 }
2017
2018 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002019 if (!mode) {
2020 ret = -ENOMEM;
2021 goto out;
2022 }
2023
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002024 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2025 if (ret) {
2026 DRM_DEBUG_KMS("Invalid mode\n");
2027 goto out;
2028 }
2029
Dave Airlief453ba02008-11-07 14:05:41 -08002030 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002031
Rob Clark7c80e122012-09-04 16:35:56 +00002032 hdisplay = mode->hdisplay;
2033 vdisplay = mode->vdisplay;
2034
2035 if (crtc->invert_dimensions)
2036 swap(hdisplay, vdisplay);
2037
2038 if (hdisplay > fb->width ||
2039 vdisplay > fb->height ||
2040 crtc_req->x > fb->width - hdisplay ||
2041 crtc_req->y > fb->height - vdisplay) {
2042 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2043 fb->width, fb->height,
2044 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2045 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002046 ret = -ENOSPC;
2047 goto out;
2048 }
Dave Airlief453ba02008-11-07 14:05:41 -08002049 }
2050
2051 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002052 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002053 ret = -EINVAL;
2054 goto out;
2055 }
2056
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002057 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002058 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002059 crtc_req->count_connectors);
2060 ret = -EINVAL;
2061 goto out;
2062 }
2063
2064 if (crtc_req->count_connectors > 0) {
2065 u32 out_id;
2066
2067 /* Avoid unbounded kernel memory allocation */
2068 if (crtc_req->count_connectors > config->num_connector) {
2069 ret = -EINVAL;
2070 goto out;
2071 }
2072
2073 connector_set = kmalloc(crtc_req->count_connectors *
2074 sizeof(struct drm_connector *),
2075 GFP_KERNEL);
2076 if (!connector_set) {
2077 ret = -ENOMEM;
2078 goto out;
2079 }
2080
2081 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002082 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002083 if (get_user(out_id, &set_connectors_ptr[i])) {
2084 ret = -EFAULT;
2085 goto out;
2086 }
2087
2088 obj = drm_mode_object_find(dev, out_id,
2089 DRM_MODE_OBJECT_CONNECTOR);
2090 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002091 DRM_DEBUG_KMS("Connector id %d unknown\n",
2092 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002093 ret = -EINVAL;
2094 goto out;
2095 }
2096 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002097 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2098 connector->base.id,
2099 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002100
2101 connector_set[i] = connector;
2102 }
2103 }
2104
2105 set.crtc = crtc;
2106 set.x = crtc_req->x;
2107 set.y = crtc_req->y;
2108 set.mode = mode;
2109 set.connectors = connector_set;
2110 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002111 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002112 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002113
2114out:
2115 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002116 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002117 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002118 return ret;
2119}
2120
2121int drm_mode_cursor_ioctl(struct drm_device *dev,
2122 void *data, struct drm_file *file_priv)
2123{
2124 struct drm_mode_cursor *req = data;
2125 struct drm_mode_object *obj;
2126 struct drm_crtc *crtc;
2127 int ret = 0;
2128
Dave Airliefb3b06c2011-02-08 13:55:21 +10002129 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2130 return -EINVAL;
2131
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002132 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002133 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002134
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002135 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002136 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002137 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002138 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002139 }
2140 crtc = obj_to_crtc(obj);
2141
Daniel Vetterdac35662012-12-02 15:24:10 +01002142 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002143 if (req->flags & DRM_MODE_CURSOR_BO) {
2144 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002145 ret = -ENXIO;
2146 goto out;
2147 }
2148 /* Turns off the cursor if handle is 0 */
2149 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2150 req->width, req->height);
2151 }
2152
2153 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2154 if (crtc->funcs->cursor_move) {
2155 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2156 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002157 ret = -EFAULT;
2158 goto out;
2159 }
2160 }
2161out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002162 mutex_unlock(&crtc->mutex);
2163
Dave Airlief453ba02008-11-07 14:05:41 -08002164 return ret;
2165}
2166
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002167/* Original addfb only supported RGB formats, so figure out which one */
2168uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2169{
2170 uint32_t fmt;
2171
2172 switch (bpp) {
2173 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002174 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002175 break;
2176 case 16:
2177 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002178 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002179 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002180 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002181 break;
2182 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002183 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002184 break;
2185 case 32:
2186 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002187 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002188 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002189 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002190 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002191 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002192 break;
2193 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002194 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2195 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002196 break;
2197 }
2198
2199 return fmt;
2200}
2201EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2202
Dave Airlief453ba02008-11-07 14:05:41 -08002203/**
2204 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002205 * @dev: drm device for the ioctl
2206 * @data: data pointer for the ioctl
2207 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002208 *
Dave Airlief453ba02008-11-07 14:05:41 -08002209 * Add a new FB to the specified CRTC, given a user request.
2210 *
2211 * Called by the user via ioctl.
2212 *
2213 * RETURNS:
2214 * Zero on success, errno on failure.
2215 */
2216int drm_mode_addfb(struct drm_device *dev,
2217 void *data, struct drm_file *file_priv)
2218{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002219 struct drm_mode_fb_cmd *or = data;
2220 struct drm_mode_fb_cmd2 r = {};
2221 struct drm_mode_config *config = &dev->mode_config;
2222 struct drm_framebuffer *fb;
2223 int ret = 0;
2224
2225 /* Use new struct with format internally */
2226 r.fb_id = or->fb_id;
2227 r.width = or->width;
2228 r.height = or->height;
2229 r.pitches[0] = or->pitch;
2230 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2231 r.handles[0] = or->handle;
2232
2233 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2234 return -EINVAL;
2235
Jesse Barnesacb4b992011-11-07 12:03:23 -08002236 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002237 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002238
2239 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002240 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002241
Daniel Vetter84849902012-12-02 00:28:11 +01002242 drm_modeset_lock_all(dev);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002243
2244 /* TODO check buffer is sufficiently large */
2245 /* TODO setup destructor callback */
2246
2247 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2248 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002249 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002250 drm_modeset_unlock_all(dev);
2251 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002252 }
2253
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002254 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002255 or->fb_id = fb->base.id;
2256 list_add(&fb->filp_head, &file_priv->fbs);
2257 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002258 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002259 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002260
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002261 return ret;
2262}
2263
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002264static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002265{
2266 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2267
2268 switch (format) {
2269 case DRM_FORMAT_C8:
2270 case DRM_FORMAT_RGB332:
2271 case DRM_FORMAT_BGR233:
2272 case DRM_FORMAT_XRGB4444:
2273 case DRM_FORMAT_XBGR4444:
2274 case DRM_FORMAT_RGBX4444:
2275 case DRM_FORMAT_BGRX4444:
2276 case DRM_FORMAT_ARGB4444:
2277 case DRM_FORMAT_ABGR4444:
2278 case DRM_FORMAT_RGBA4444:
2279 case DRM_FORMAT_BGRA4444:
2280 case DRM_FORMAT_XRGB1555:
2281 case DRM_FORMAT_XBGR1555:
2282 case DRM_FORMAT_RGBX5551:
2283 case DRM_FORMAT_BGRX5551:
2284 case DRM_FORMAT_ARGB1555:
2285 case DRM_FORMAT_ABGR1555:
2286 case DRM_FORMAT_RGBA5551:
2287 case DRM_FORMAT_BGRA5551:
2288 case DRM_FORMAT_RGB565:
2289 case DRM_FORMAT_BGR565:
2290 case DRM_FORMAT_RGB888:
2291 case DRM_FORMAT_BGR888:
2292 case DRM_FORMAT_XRGB8888:
2293 case DRM_FORMAT_XBGR8888:
2294 case DRM_FORMAT_RGBX8888:
2295 case DRM_FORMAT_BGRX8888:
2296 case DRM_FORMAT_ARGB8888:
2297 case DRM_FORMAT_ABGR8888:
2298 case DRM_FORMAT_RGBA8888:
2299 case DRM_FORMAT_BGRA8888:
2300 case DRM_FORMAT_XRGB2101010:
2301 case DRM_FORMAT_XBGR2101010:
2302 case DRM_FORMAT_RGBX1010102:
2303 case DRM_FORMAT_BGRX1010102:
2304 case DRM_FORMAT_ARGB2101010:
2305 case DRM_FORMAT_ABGR2101010:
2306 case DRM_FORMAT_RGBA1010102:
2307 case DRM_FORMAT_BGRA1010102:
2308 case DRM_FORMAT_YUYV:
2309 case DRM_FORMAT_YVYU:
2310 case DRM_FORMAT_UYVY:
2311 case DRM_FORMAT_VYUY:
2312 case DRM_FORMAT_AYUV:
2313 case DRM_FORMAT_NV12:
2314 case DRM_FORMAT_NV21:
2315 case DRM_FORMAT_NV16:
2316 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002317 case DRM_FORMAT_NV24:
2318 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002319 case DRM_FORMAT_YUV410:
2320 case DRM_FORMAT_YVU410:
2321 case DRM_FORMAT_YUV411:
2322 case DRM_FORMAT_YVU411:
2323 case DRM_FORMAT_YUV420:
2324 case DRM_FORMAT_YVU420:
2325 case DRM_FORMAT_YUV422:
2326 case DRM_FORMAT_YVU422:
2327 case DRM_FORMAT_YUV444:
2328 case DRM_FORMAT_YVU444:
2329 return 0;
2330 default:
2331 return -EINVAL;
2332 }
2333}
2334
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002335static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002336{
2337 int ret, hsub, vsub, num_planes, i;
2338
2339 ret = format_check(r);
2340 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002341 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002342 return ret;
2343 }
2344
2345 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2346 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2347 num_planes = drm_format_num_planes(r->pixel_format);
2348
2349 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002350 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002351 return -EINVAL;
2352 }
2353
2354 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002355 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002356 return -EINVAL;
2357 }
2358
2359 for (i = 0; i < num_planes; i++) {
2360 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002361 unsigned int height = r->height / (i != 0 ? vsub : 1);
2362 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002363
2364 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002365 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002366 return -EINVAL;
2367 }
2368
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002369 if ((uint64_t) width * cpp > UINT_MAX)
2370 return -ERANGE;
2371
2372 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2373 return -ERANGE;
2374
2375 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002376 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002377 return -EINVAL;
2378 }
2379 }
2380
2381 return 0;
2382}
2383
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002384/**
2385 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002386 * @dev: drm device for the ioctl
2387 * @data: data pointer for the ioctl
2388 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002389 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002390 * Add a new FB to the specified CRTC, given a user request with format.
2391 *
2392 * Called by the user via ioctl.
2393 *
2394 * RETURNS:
2395 * Zero on success, errno on failure.
2396 */
2397int drm_mode_addfb2(struct drm_device *dev,
2398 void *data, struct drm_file *file_priv)
2399{
2400 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002401 struct drm_mode_config *config = &dev->mode_config;
2402 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002403 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002404
Dave Airliefb3b06c2011-02-08 13:55:21 +10002405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2406 return -EINVAL;
2407
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002408 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2409 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2410 return -EINVAL;
2411 }
2412
Dave Airlief453ba02008-11-07 14:05:41 -08002413 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002414 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002415 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002416 return -EINVAL;
2417 }
2418 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002419 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002420 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002421 return -EINVAL;
2422 }
2423
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002424 ret = framebuffer_check(r);
2425 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002426 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002427
Daniel Vetter84849902012-12-02 00:28:11 +01002428 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002429
Dave Airlief453ba02008-11-07 14:05:41 -08002430 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002431 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002432 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002433 drm_modeset_unlock_all(dev);
2434 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002435 }
2436
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002437 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002438 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002439 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002440 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002441 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002442
Daniel Vetter84849902012-12-02 00:28:11 +01002443 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002444
Dave Airlief453ba02008-11-07 14:05:41 -08002445 return ret;
2446}
2447
2448/**
2449 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002450 * @dev: drm device for the ioctl
2451 * @data: data pointer for the ioctl
2452 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002453 *
Dave Airlief453ba02008-11-07 14:05:41 -08002454 * Remove the FB specified by the user.
2455 *
2456 * Called by the user via ioctl.
2457 *
2458 * RETURNS:
2459 * Zero on success, errno on failure.
2460 */
2461int drm_mode_rmfb(struct drm_device *dev,
2462 void *data, struct drm_file *file_priv)
2463{
Dave Airlief453ba02008-11-07 14:05:41 -08002464 struct drm_framebuffer *fb = NULL;
2465 struct drm_framebuffer *fbl = NULL;
2466 uint32_t *id = data;
2467 int ret = 0;
2468 int found = 0;
2469
Dave Airliefb3b06c2011-02-08 13:55:21 +10002470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2471 return -EINVAL;
2472
Daniel Vetter84849902012-12-02 00:28:11 +01002473 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002474 fb = drm_framebuffer_lookup(dev, *id);
2475 if (!fb) {
Dave Airlief453ba02008-11-07 14:05:41 -08002476 ret = -EINVAL;
2477 goto out;
2478 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002479 /* fb is protect by the mode_config lock, so drop the ref immediately */
2480 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002481
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002482 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002483 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2484 if (fb == fbl)
2485 found = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002486 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002487 ret = -EINVAL;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002488 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002489 goto out;
2490 }
2491
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002492 list_del_init(&fb->filp_head);
2493 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002494
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002495 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002496out:
Daniel Vetter84849902012-12-02 00:28:11 +01002497 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002498
Dave Airlief453ba02008-11-07 14:05:41 -08002499 return ret;
2500}
2501
2502/**
2503 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002504 * @dev: drm device for the ioctl
2505 * @data: data pointer for the ioctl
2506 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002507 *
Dave Airlief453ba02008-11-07 14:05:41 -08002508 * Lookup the FB given its ID and return info about it.
2509 *
2510 * Called by the user via ioctl.
2511 *
2512 * RETURNS:
2513 * Zero on success, errno on failure.
2514 */
2515int drm_mode_getfb(struct drm_device *dev,
2516 void *data, struct drm_file *file_priv)
2517{
2518 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002519 struct drm_framebuffer *fb;
2520 int ret = 0;
2521
Dave Airliefb3b06c2011-02-08 13:55:21 +10002522 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2523 return -EINVAL;
2524
Daniel Vetter84849902012-12-02 00:28:11 +01002525 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002526 fb = drm_framebuffer_lookup(dev, r->fb_id);
2527 if (!fb) {
Dave Airlief453ba02008-11-07 14:05:41 -08002528 ret = -EINVAL;
2529 goto out;
2530 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002531 /* fb is protect by the mode_config lock, so drop the ref immediately */
2532 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002533
2534 r->height = fb->height;
2535 r->width = fb->width;
2536 r->depth = fb->depth;
2537 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002538 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002539 if (fb->funcs->create_handle)
2540 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2541 else
2542 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002543
2544out:
Daniel Vetter84849902012-12-02 00:28:11 +01002545 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002546 return ret;
2547}
2548
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002549int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2550 void *data, struct drm_file *file_priv)
2551{
2552 struct drm_clip_rect __user *clips_ptr;
2553 struct drm_clip_rect *clips = NULL;
2554 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002555 struct drm_framebuffer *fb;
2556 unsigned flags;
2557 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002558 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002559
Dave Airliefb3b06c2011-02-08 13:55:21 +10002560 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2561 return -EINVAL;
2562
Daniel Vetter84849902012-12-02 00:28:11 +01002563 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002564 fb = drm_framebuffer_lookup(dev, r->fb_id);
2565 if (!fb) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002566 ret = -EINVAL;
2567 goto out_err1;
2568 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002569 /* fb is protect by the mode_config lock, so drop the ref immediately */
2570 drm_framebuffer_unreference(fb);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002571
2572 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002573 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002574
2575 if (!num_clips != !clips_ptr) {
2576 ret = -EINVAL;
2577 goto out_err1;
2578 }
2579
2580 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2581
2582 /* If userspace annotates copy, clips must come in pairs */
2583 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2584 ret = -EINVAL;
2585 goto out_err1;
2586 }
2587
2588 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002589 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2590 ret = -EINVAL;
2591 goto out_err1;
2592 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002593 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2594 if (!clips) {
2595 ret = -ENOMEM;
2596 goto out_err1;
2597 }
2598
2599 ret = copy_from_user(clips, clips_ptr,
2600 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002601 if (ret) {
2602 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002603 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002604 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002605 }
2606
2607 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002608 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2609 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002610 } else {
2611 ret = -ENOSYS;
2612 goto out_err2;
2613 }
2614
2615out_err2:
2616 kfree(clips);
2617out_err1:
Daniel Vetter84849902012-12-02 00:28:11 +01002618 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002619 return ret;
2620}
2621
2622
Dave Airlief453ba02008-11-07 14:05:41 -08002623/**
2624 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002625 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002626 *
Dave Airlief453ba02008-11-07 14:05:41 -08002627 * Destroy all the FBs associated with @filp.
2628 *
2629 * Called by the user via ioctl.
2630 *
2631 * RETURNS:
2632 * Zero on success, errno on failure.
2633 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002634void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002635{
Dave Airlief453ba02008-11-07 14:05:41 -08002636 struct drm_device *dev = priv->minor->dev;
2637 struct drm_framebuffer *fb, *tfb;
2638
Daniel Vetter84849902012-12-02 00:28:11 +01002639 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002640 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002641 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002642 list_del_init(&fb->filp_head);
Rob Clarkf7eff602012-09-05 21:48:38 +00002643 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002644 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002645 mutex_unlock(&priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002646 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002647}
2648
2649/**
2650 * drm_mode_attachmode - add a mode to the user mode list
2651 * @dev: DRM device
2652 * @connector: connector to add the mode to
2653 * @mode: mode to add
2654 *
2655 * Add @mode to @connector's user mode list.
2656 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002657static void drm_mode_attachmode(struct drm_device *dev,
2658 struct drm_connector *connector,
2659 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002660{
Dave Airlief453ba02008-11-07 14:05:41 -08002661 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002662}
2663
2664int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002665 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002666{
2667 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002668 int ret = 0;
2669 struct drm_display_mode *dup_mode, *next;
2670 LIST_HEAD(list);
2671
Dave Airlief453ba02008-11-07 14:05:41 -08002672 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2673 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002674 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002675 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002676 dup_mode = drm_mode_duplicate(dev, mode);
2677 if (!dup_mode) {
2678 ret = -ENOMEM;
2679 goto out;
2680 }
2681 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002682 }
2683 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002684
2685 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2686 if (!connector->encoder)
2687 continue;
2688 if (connector->encoder->crtc == crtc)
2689 list_move_tail(list.next, &connector->user_modes);
2690 }
2691
2692 WARN_ON(!list_empty(&list));
2693
2694 out:
2695 list_for_each_entry_safe(dup_mode, next, &list, head)
2696 drm_mode_destroy(dev, dup_mode);
2697
2698 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002699}
2700EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2701
2702static int drm_mode_detachmode(struct drm_device *dev,
2703 struct drm_connector *connector,
2704 struct drm_display_mode *mode)
2705{
2706 int found = 0;
2707 int ret = 0;
2708 struct drm_display_mode *match_mode, *t;
2709
2710 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2711 if (drm_mode_equal(match_mode, mode)) {
2712 list_del(&match_mode->head);
2713 drm_mode_destroy(dev, match_mode);
2714 found = 1;
2715 break;
2716 }
2717 }
2718
2719 if (!found)
2720 ret = -EINVAL;
2721
2722 return ret;
2723}
2724
2725int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2726{
2727 struct drm_connector *connector;
2728
2729 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2730 drm_mode_detachmode(dev, connector, mode);
2731 }
2732 return 0;
2733}
2734EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2735
2736/**
2737 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002738 * @dev: drm device for the ioctl
2739 * @data: data pointer for the ioctl
2740 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002741 *
2742 * This attaches a user specified mode to an connector.
2743 * Called by the user via ioctl.
2744 *
2745 * RETURNS:
2746 * Zero on success, errno on failure.
2747 */
2748int drm_mode_attachmode_ioctl(struct drm_device *dev,
2749 void *data, struct drm_file *file_priv)
2750{
2751 struct drm_mode_mode_cmd *mode_cmd = data;
2752 struct drm_connector *connector;
2753 struct drm_display_mode *mode;
2754 struct drm_mode_object *obj;
2755 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002756 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002757
Dave Airliefb3b06c2011-02-08 13:55:21 +10002758 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2759 return -EINVAL;
2760
Daniel Vetter84849902012-12-02 00:28:11 +01002761 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002762
2763 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2764 if (!obj) {
2765 ret = -EINVAL;
2766 goto out;
2767 }
2768 connector = obj_to_connector(obj);
2769
2770 mode = drm_mode_create(dev);
2771 if (!mode) {
2772 ret = -ENOMEM;
2773 goto out;
2774 }
2775
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002776 ret = drm_crtc_convert_umode(mode, umode);
2777 if (ret) {
2778 DRM_DEBUG_KMS("Invalid mode\n");
2779 drm_mode_destroy(dev, mode);
2780 goto out;
2781 }
Dave Airlief453ba02008-11-07 14:05:41 -08002782
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002783 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002784out:
Daniel Vetter84849902012-12-02 00:28:11 +01002785 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002786 return ret;
2787}
2788
2789
2790/**
2791 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002792 * @dev: drm device for the ioctl
2793 * @data: data pointer for the ioctl
2794 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002795 *
2796 * Called by the user via ioctl.
2797 *
2798 * RETURNS:
2799 * Zero on success, errno on failure.
2800 */
2801int drm_mode_detachmode_ioctl(struct drm_device *dev,
2802 void *data, struct drm_file *file_priv)
2803{
2804 struct drm_mode_object *obj;
2805 struct drm_mode_mode_cmd *mode_cmd = data;
2806 struct drm_connector *connector;
2807 struct drm_display_mode mode;
2808 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002809 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002810
Dave Airliefb3b06c2011-02-08 13:55:21 +10002811 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2812 return -EINVAL;
2813
Daniel Vetter84849902012-12-02 00:28:11 +01002814 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002815
2816 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2817 if (!obj) {
2818 ret = -EINVAL;
2819 goto out;
2820 }
2821 connector = obj_to_connector(obj);
2822
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002823 ret = drm_crtc_convert_umode(&mode, umode);
2824 if (ret) {
2825 DRM_DEBUG_KMS("Invalid mode\n");
2826 goto out;
2827 }
2828
Dave Airlief453ba02008-11-07 14:05:41 -08002829 ret = drm_mode_detachmode(dev, connector, &mode);
2830out:
Daniel Vetter84849902012-12-02 00:28:11 +01002831 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002832 return ret;
2833}
2834
2835struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2836 const char *name, int num_values)
2837{
2838 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002839 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002840
2841 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2842 if (!property)
2843 return NULL;
2844
2845 if (num_values) {
2846 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2847 if (!property->values)
2848 goto fail;
2849 }
2850
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002851 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2852 if (ret)
2853 goto fail;
2854
Dave Airlief453ba02008-11-07 14:05:41 -08002855 property->flags = flags;
2856 property->num_values = num_values;
2857 INIT_LIST_HEAD(&property->enum_blob_list);
2858
Vinson Lee471dd2e2011-11-10 11:55:40 -08002859 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002860 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002861 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2862 }
Dave Airlief453ba02008-11-07 14:05:41 -08002863
2864 list_add_tail(&property->head, &dev->mode_config.property_list);
2865 return property;
2866fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002867 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002868 kfree(property);
2869 return NULL;
2870}
2871EXPORT_SYMBOL(drm_property_create);
2872
Sascha Hauer4a67d392012-02-06 10:58:17 +01002873struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2874 const char *name,
2875 const struct drm_prop_enum_list *props,
2876 int num_values)
2877{
2878 struct drm_property *property;
2879 int i, ret;
2880
2881 flags |= DRM_MODE_PROP_ENUM;
2882
2883 property = drm_property_create(dev, flags, name, num_values);
2884 if (!property)
2885 return NULL;
2886
2887 for (i = 0; i < num_values; i++) {
2888 ret = drm_property_add_enum(property, i,
2889 props[i].type,
2890 props[i].name);
2891 if (ret) {
2892 drm_property_destroy(dev, property);
2893 return NULL;
2894 }
2895 }
2896
2897 return property;
2898}
2899EXPORT_SYMBOL(drm_property_create_enum);
2900
Rob Clark49e27542012-05-17 02:23:26 -06002901struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2902 int flags, 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_BITMASK;
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_bitmask);
2928
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002929struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2930 const char *name,
2931 uint64_t min, uint64_t max)
2932{
2933 struct drm_property *property;
2934
2935 flags |= DRM_MODE_PROP_RANGE;
2936
2937 property = drm_property_create(dev, flags, name, 2);
2938 if (!property)
2939 return NULL;
2940
2941 property->values[0] = min;
2942 property->values[1] = max;
2943
2944 return property;
2945}
2946EXPORT_SYMBOL(drm_property_create_range);
2947
Dave Airlief453ba02008-11-07 14:05:41 -08002948int drm_property_add_enum(struct drm_property *property, int index,
2949 uint64_t value, const char *name)
2950{
2951 struct drm_property_enum *prop_enum;
2952
Rob Clark49e27542012-05-17 02:23:26 -06002953 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2954 return -EINVAL;
2955
2956 /*
2957 * Bitmask enum properties have the additional constraint of values
2958 * from 0 to 63
2959 */
2960 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002961 return -EINVAL;
2962
2963 if (!list_empty(&property->enum_blob_list)) {
2964 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2965 if (prop_enum->value == value) {
2966 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2967 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2968 return 0;
2969 }
2970 }
2971 }
2972
2973 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2974 if (!prop_enum)
2975 return -ENOMEM;
2976
2977 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2978 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2979 prop_enum->value = value;
2980
2981 property->values[index] = value;
2982 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2983 return 0;
2984}
2985EXPORT_SYMBOL(drm_property_add_enum);
2986
2987void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2988{
2989 struct drm_property_enum *prop_enum, *pt;
2990
2991 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2992 list_del(&prop_enum->head);
2993 kfree(prop_enum);
2994 }
2995
2996 if (property->num_values)
2997 kfree(property->values);
2998 drm_mode_object_put(dev, &property->base);
2999 list_del(&property->head);
3000 kfree(property);
3001}
3002EXPORT_SYMBOL(drm_property_destroy);
3003
Paulo Zanonic5431882012-05-15 18:09:02 -03003004void drm_object_attach_property(struct drm_mode_object *obj,
3005 struct drm_property *property,
3006 uint64_t init_val)
3007{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003008 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003009
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003010 if (count == DRM_OBJECT_MAX_PROPERTY) {
3011 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3012 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3013 "you see this message on the same object type.\n",
3014 obj->type);
3015 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003016 }
3017
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003018 obj->properties->ids[count] = property->base.id;
3019 obj->properties->values[count] = init_val;
3020 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003021}
3022EXPORT_SYMBOL(drm_object_attach_property);
3023
3024int drm_object_property_set_value(struct drm_mode_object *obj,
3025 struct drm_property *property, uint64_t val)
3026{
3027 int i;
3028
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003029 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003030 if (obj->properties->ids[i] == property->base.id) {
3031 obj->properties->values[i] = val;
3032 return 0;
3033 }
3034 }
3035
3036 return -EINVAL;
3037}
3038EXPORT_SYMBOL(drm_object_property_set_value);
3039
3040int drm_object_property_get_value(struct drm_mode_object *obj,
3041 struct drm_property *property, uint64_t *val)
3042{
3043 int i;
3044
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003045 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003046 if (obj->properties->ids[i] == property->base.id) {
3047 *val = obj->properties->values[i];
3048 return 0;
3049 }
3050 }
3051
3052 return -EINVAL;
3053}
3054EXPORT_SYMBOL(drm_object_property_get_value);
3055
Dave Airlief453ba02008-11-07 14:05:41 -08003056int drm_mode_getproperty_ioctl(struct drm_device *dev,
3057 void *data, struct drm_file *file_priv)
3058{
3059 struct drm_mode_object *obj;
3060 struct drm_mode_get_property *out_resp = data;
3061 struct drm_property *property;
3062 int enum_count = 0;
3063 int blob_count = 0;
3064 int value_count = 0;
3065 int ret = 0, i;
3066 int copied;
3067 struct drm_property_enum *prop_enum;
3068 struct drm_mode_property_enum __user *enum_ptr;
3069 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003070 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003071 uint64_t __user *values_ptr;
3072 uint32_t __user *blob_length_ptr;
3073
Dave Airliefb3b06c2011-02-08 13:55:21 +10003074 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3075 return -EINVAL;
3076
Daniel Vetter84849902012-12-02 00:28:11 +01003077 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003078 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3079 if (!obj) {
3080 ret = -EINVAL;
3081 goto done;
3082 }
3083 property = obj_to_property(obj);
3084
Rob Clark49e27542012-05-17 02:23:26 -06003085 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003086 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3087 enum_count++;
3088 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3089 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3090 blob_count++;
3091 }
3092
3093 value_count = property->num_values;
3094
3095 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3096 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3097 out_resp->flags = property->flags;
3098
3099 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003100 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003101 for (i = 0; i < value_count; i++) {
3102 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3103 ret = -EFAULT;
3104 goto done;
3105 }
3106 }
3107 }
3108 out_resp->count_values = value_count;
3109
Rob Clark49e27542012-05-17 02:23:26 -06003110 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003111 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3112 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003113 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003114 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3115
3116 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3117 ret = -EFAULT;
3118 goto done;
3119 }
3120
3121 if (copy_to_user(&enum_ptr[copied].name,
3122 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3123 ret = -EFAULT;
3124 goto done;
3125 }
3126 copied++;
3127 }
3128 }
3129 out_resp->count_enum_blobs = enum_count;
3130 }
3131
3132 if (property->flags & DRM_MODE_PROP_BLOB) {
3133 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3134 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003135 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3136 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003137
3138 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3139 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3140 ret = -EFAULT;
3141 goto done;
3142 }
3143
3144 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3145 ret = -EFAULT;
3146 goto done;
3147 }
3148
3149 copied++;
3150 }
3151 }
3152 out_resp->count_enum_blobs = blob_count;
3153 }
3154done:
Daniel Vetter84849902012-12-02 00:28:11 +01003155 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003156 return ret;
3157}
3158
3159static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3160 void *data)
3161{
3162 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003163 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003164
3165 if (!length || !data)
3166 return NULL;
3167
3168 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3169 if (!blob)
3170 return NULL;
3171
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003172 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3173 if (ret) {
3174 kfree(blob);
3175 return NULL;
3176 }
3177
Dave Airlief453ba02008-11-07 14:05:41 -08003178 blob->length = length;
3179
3180 memcpy(blob->data, data, length);
3181
Dave Airlief453ba02008-11-07 14:05:41 -08003182 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3183 return blob;
3184}
3185
3186static void drm_property_destroy_blob(struct drm_device *dev,
3187 struct drm_property_blob *blob)
3188{
3189 drm_mode_object_put(dev, &blob->base);
3190 list_del(&blob->head);
3191 kfree(blob);
3192}
3193
3194int drm_mode_getblob_ioctl(struct drm_device *dev,
3195 void *data, struct drm_file *file_priv)
3196{
3197 struct drm_mode_object *obj;
3198 struct drm_mode_get_blob *out_resp = data;
3199 struct drm_property_blob *blob;
3200 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003201 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003202
Dave Airliefb3b06c2011-02-08 13:55:21 +10003203 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3204 return -EINVAL;
3205
Daniel Vetter84849902012-12-02 00:28:11 +01003206 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003207 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3208 if (!obj) {
3209 ret = -EINVAL;
3210 goto done;
3211 }
3212 blob = obj_to_blob(obj);
3213
3214 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003215 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003216 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3217 ret = -EFAULT;
3218 goto done;
3219 }
3220 }
3221 out_resp->length = blob->length;
3222
3223done:
Daniel Vetter84849902012-12-02 00:28:11 +01003224 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003225 return ret;
3226}
3227
3228int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3229 struct edid *edid)
3230{
3231 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003232 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003233
3234 if (connector->edid_blob_ptr)
3235 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3236
3237 /* Delete edid, when there is none. */
3238 if (!edid) {
3239 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003240 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003241 return ret;
3242 }
3243
Adam Jackson7466f4c2010-03-29 21:43:23 +00003244 size = EDID_LENGTH * (1 + edid->extensions);
3245 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3246 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003247 if (!connector->edid_blob_ptr)
3248 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003249
Rob Clark58495562012-10-11 20:50:56 -05003250 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003251 dev->mode_config.edid_property,
3252 connector->edid_blob_ptr->base.id);
3253
3254 return ret;
3255}
3256EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3257
Paulo Zanoni26a34812012-05-15 18:08:59 -03003258static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003259 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003260{
3261 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3262 return false;
3263 if (property->flags & DRM_MODE_PROP_RANGE) {
3264 if (value < property->values[0] || value > property->values[1])
3265 return false;
3266 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003267 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3268 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003269 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003270 for (i = 0; i < property->num_values; i++)
3271 valid_mask |= (1ULL << property->values[i]);
3272 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003273 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3274 /* Only the driver knows */
3275 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003276 } else {
3277 int i;
3278 for (i = 0; i < property->num_values; i++)
3279 if (property->values[i] == value)
3280 return true;
3281 return false;
3282 }
3283}
3284
Dave Airlief453ba02008-11-07 14:05:41 -08003285int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3286 void *data, struct drm_file *file_priv)
3287{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003288 struct drm_mode_connector_set_property *conn_set_prop = data;
3289 struct drm_mode_obj_set_property obj_set_prop = {
3290 .value = conn_set_prop->value,
3291 .prop_id = conn_set_prop->prop_id,
3292 .obj_id = conn_set_prop->connector_id,
3293 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3294 };
Dave Airlief453ba02008-11-07 14:05:41 -08003295
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003296 /* It does all the locking and checking we need */
3297 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003298}
3299
Paulo Zanonic5431882012-05-15 18:09:02 -03003300static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3301 struct drm_property *property,
3302 uint64_t value)
3303{
3304 int ret = -EINVAL;
3305 struct drm_connector *connector = obj_to_connector(obj);
3306
3307 /* Do DPMS ourselves */
3308 if (property == connector->dev->mode_config.dpms_property) {
3309 if (connector->funcs->dpms)
3310 (*connector->funcs->dpms)(connector, (int)value);
3311 ret = 0;
3312 } else if (connector->funcs->set_property)
3313 ret = connector->funcs->set_property(connector, property, value);
3314
3315 /* store the property value if successful */
3316 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003317 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003318 return ret;
3319}
3320
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003321static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3322 struct drm_property *property,
3323 uint64_t value)
3324{
3325 int ret = -EINVAL;
3326 struct drm_crtc *crtc = obj_to_crtc(obj);
3327
3328 if (crtc->funcs->set_property)
3329 ret = crtc->funcs->set_property(crtc, property, value);
3330 if (!ret)
3331 drm_object_property_set_value(obj, property, value);
3332
3333 return ret;
3334}
3335
Rob Clark4d939142012-05-17 02:23:27 -06003336static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3337 struct drm_property *property,
3338 uint64_t value)
3339{
3340 int ret = -EINVAL;
3341 struct drm_plane *plane = obj_to_plane(obj);
3342
3343 if (plane->funcs->set_property)
3344 ret = plane->funcs->set_property(plane, property, value);
3345 if (!ret)
3346 drm_object_property_set_value(obj, property, value);
3347
3348 return ret;
3349}
3350
Paulo Zanonic5431882012-05-15 18:09:02 -03003351int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3352 struct drm_file *file_priv)
3353{
3354 struct drm_mode_obj_get_properties *arg = data;
3355 struct drm_mode_object *obj;
3356 int ret = 0;
3357 int i;
3358 int copied = 0;
3359 int props_count = 0;
3360 uint32_t __user *props_ptr;
3361 uint64_t __user *prop_values_ptr;
3362
3363 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3364 return -EINVAL;
3365
Daniel Vetter84849902012-12-02 00:28:11 +01003366 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003367
3368 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3369 if (!obj) {
3370 ret = -EINVAL;
3371 goto out;
3372 }
3373 if (!obj->properties) {
3374 ret = -EINVAL;
3375 goto out;
3376 }
3377
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003378 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003379
3380 /* This ioctl is called twice, once to determine how much space is
3381 * needed, and the 2nd time to fill it. */
3382 if ((arg->count_props >= props_count) && props_count) {
3383 copied = 0;
3384 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3385 prop_values_ptr = (uint64_t __user *)(unsigned long)
3386 (arg->prop_values_ptr);
3387 for (i = 0; i < props_count; i++) {
3388 if (put_user(obj->properties->ids[i],
3389 props_ptr + copied)) {
3390 ret = -EFAULT;
3391 goto out;
3392 }
3393 if (put_user(obj->properties->values[i],
3394 prop_values_ptr + copied)) {
3395 ret = -EFAULT;
3396 goto out;
3397 }
3398 copied++;
3399 }
3400 }
3401 arg->count_props = props_count;
3402out:
Daniel Vetter84849902012-12-02 00:28:11 +01003403 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003404 return ret;
3405}
3406
3407int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3408 struct drm_file *file_priv)
3409{
3410 struct drm_mode_obj_set_property *arg = data;
3411 struct drm_mode_object *arg_obj;
3412 struct drm_mode_object *prop_obj;
3413 struct drm_property *property;
3414 int ret = -EINVAL;
3415 int i;
3416
3417 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3418 return -EINVAL;
3419
Daniel Vetter84849902012-12-02 00:28:11 +01003420 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003421
3422 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3423 if (!arg_obj)
3424 goto out;
3425 if (!arg_obj->properties)
3426 goto out;
3427
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003428 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003429 if (arg_obj->properties->ids[i] == arg->prop_id)
3430 break;
3431
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003432 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003433 goto out;
3434
3435 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3436 DRM_MODE_OBJECT_PROPERTY);
3437 if (!prop_obj)
3438 goto out;
3439 property = obj_to_property(prop_obj);
3440
3441 if (!drm_property_change_is_valid(property, arg->value))
3442 goto out;
3443
3444 switch (arg_obj->type) {
3445 case DRM_MODE_OBJECT_CONNECTOR:
3446 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3447 arg->value);
3448 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003449 case DRM_MODE_OBJECT_CRTC:
3450 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3451 break;
Rob Clark4d939142012-05-17 02:23:27 -06003452 case DRM_MODE_OBJECT_PLANE:
3453 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3454 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003455 }
3456
3457out:
Daniel Vetter84849902012-12-02 00:28:11 +01003458 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003459 return ret;
3460}
3461
Dave Airlief453ba02008-11-07 14:05:41 -08003462int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3463 struct drm_encoder *encoder)
3464{
3465 int i;
3466
3467 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3468 if (connector->encoder_ids[i] == 0) {
3469 connector->encoder_ids[i] = encoder->base.id;
3470 return 0;
3471 }
3472 }
3473 return -ENOMEM;
3474}
3475EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3476
3477void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3478 struct drm_encoder *encoder)
3479{
3480 int i;
3481 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3482 if (connector->encoder_ids[i] == encoder->base.id) {
3483 connector->encoder_ids[i] = 0;
3484 if (connector->encoder == encoder)
3485 connector->encoder = NULL;
3486 break;
3487 }
3488 }
3489}
3490EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3491
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003492int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003493 int gamma_size)
3494{
3495 crtc->gamma_size = gamma_size;
3496
3497 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3498 if (!crtc->gamma_store) {
3499 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003500 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003501 }
3502
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003503 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003504}
3505EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3506
3507int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3508 void *data, struct drm_file *file_priv)
3509{
3510 struct drm_mode_crtc_lut *crtc_lut = data;
3511 struct drm_mode_object *obj;
3512 struct drm_crtc *crtc;
3513 void *r_base, *g_base, *b_base;
3514 int size;
3515 int ret = 0;
3516
Dave Airliefb3b06c2011-02-08 13:55:21 +10003517 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3518 return -EINVAL;
3519
Daniel Vetter84849902012-12-02 00:28:11 +01003520 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003521 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3522 if (!obj) {
3523 ret = -EINVAL;
3524 goto out;
3525 }
3526 crtc = obj_to_crtc(obj);
3527
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003528 if (crtc->funcs->gamma_set == NULL) {
3529 ret = -ENOSYS;
3530 goto out;
3531 }
3532
Dave Airlief453ba02008-11-07 14:05:41 -08003533 /* memcpy into gamma store */
3534 if (crtc_lut->gamma_size != crtc->gamma_size) {
3535 ret = -EINVAL;
3536 goto out;
3537 }
3538
3539 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3540 r_base = crtc->gamma_store;
3541 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3542 ret = -EFAULT;
3543 goto out;
3544 }
3545
3546 g_base = r_base + size;
3547 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3548 ret = -EFAULT;
3549 goto out;
3550 }
3551
3552 b_base = g_base + size;
3553 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3554 ret = -EFAULT;
3555 goto out;
3556 }
3557
James Simmons72034252010-08-03 01:33:19 +01003558 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003559
3560out:
Daniel Vetter84849902012-12-02 00:28:11 +01003561 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003562 return ret;
3563
3564}
3565
3566int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3567 void *data, struct drm_file *file_priv)
3568{
3569 struct drm_mode_crtc_lut *crtc_lut = data;
3570 struct drm_mode_object *obj;
3571 struct drm_crtc *crtc;
3572 void *r_base, *g_base, *b_base;
3573 int size;
3574 int ret = 0;
3575
Dave Airliefb3b06c2011-02-08 13:55:21 +10003576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3577 return -EINVAL;
3578
Daniel Vetter84849902012-12-02 00:28:11 +01003579 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003580 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3581 if (!obj) {
3582 ret = -EINVAL;
3583 goto out;
3584 }
3585 crtc = obj_to_crtc(obj);
3586
3587 /* memcpy into gamma store */
3588 if (crtc_lut->gamma_size != crtc->gamma_size) {
3589 ret = -EINVAL;
3590 goto out;
3591 }
3592
3593 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3594 r_base = crtc->gamma_store;
3595 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3596 ret = -EFAULT;
3597 goto out;
3598 }
3599
3600 g_base = r_base + size;
3601 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3602 ret = -EFAULT;
3603 goto out;
3604 }
3605
3606 b_base = g_base + size;
3607 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3608 ret = -EFAULT;
3609 goto out;
3610 }
3611out:
Daniel Vetter84849902012-12-02 00:28:11 +01003612 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003613 return ret;
3614}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003615
3616int drm_mode_page_flip_ioctl(struct drm_device *dev,
3617 void *data, struct drm_file *file_priv)
3618{
3619 struct drm_mode_crtc_page_flip *page_flip = data;
3620 struct drm_mode_object *obj;
3621 struct drm_crtc *crtc;
3622 struct drm_framebuffer *fb;
3623 struct drm_pending_vblank_event *e = NULL;
3624 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003625 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003626 int ret = -EINVAL;
3627
3628 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3629 page_flip->reserved != 0)
3630 return -EINVAL;
3631
Daniel Vetter84849902012-12-02 00:28:11 +01003632 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003633 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3634 if (!obj)
3635 goto out;
3636 crtc = obj_to_crtc(obj);
3637
Chris Wilson90c1efd2010-07-17 20:23:26 +01003638 if (crtc->fb == NULL) {
3639 /* The framebuffer is currently unbound, presumably
3640 * due to a hotplug event, that userspace has not
3641 * yet discovered.
3642 */
3643 ret = -EBUSY;
3644 goto out;
3645 }
3646
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003647 if (crtc->funcs->page_flip == NULL)
3648 goto out;
3649
Daniel Vetter786b99e2012-12-02 21:53:40 +01003650 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3651 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003652 goto out;
Daniel Vetter786b99e2012-12-02 21:53:40 +01003653 /* fb is protect by the mode_config lock, so drop the ref immediately */
3654 drm_framebuffer_unreference(fb);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003655
Rob Clark7c80e122012-09-04 16:35:56 +00003656 hdisplay = crtc->mode.hdisplay;
3657 vdisplay = crtc->mode.vdisplay;
3658
3659 if (crtc->invert_dimensions)
3660 swap(hdisplay, vdisplay);
3661
3662 if (hdisplay > fb->width ||
3663 vdisplay > fb->height ||
3664 crtc->x > fb->width - hdisplay ||
3665 crtc->y > fb->height - vdisplay) {
3666 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3667 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3668 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003669 ret = -ENOSPC;
3670 goto out;
3671 }
3672
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003673 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3674 ret = -ENOMEM;
3675 spin_lock_irqsave(&dev->event_lock, flags);
3676 if (file_priv->event_space < sizeof e->event) {
3677 spin_unlock_irqrestore(&dev->event_lock, flags);
3678 goto out;
3679 }
3680 file_priv->event_space -= sizeof e->event;
3681 spin_unlock_irqrestore(&dev->event_lock, flags);
3682
3683 e = kzalloc(sizeof *e, GFP_KERNEL);
3684 if (e == NULL) {
3685 spin_lock_irqsave(&dev->event_lock, flags);
3686 file_priv->event_space += sizeof e->event;
3687 spin_unlock_irqrestore(&dev->event_lock, flags);
3688 goto out;
3689 }
3690
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003691 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003692 e->event.base.length = sizeof e->event;
3693 e->event.user_data = page_flip->user_data;
3694 e->base.event = &e->event.base;
3695 e->base.file_priv = file_priv;
3696 e->base.destroy =
3697 (void (*) (struct drm_pending_event *)) kfree;
3698 }
3699
3700 ret = crtc->funcs->page_flip(crtc, fb, e);
3701 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003702 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3703 spin_lock_irqsave(&dev->event_lock, flags);
3704 file_priv->event_space += sizeof e->event;
3705 spin_unlock_irqrestore(&dev->event_lock, flags);
3706 kfree(e);
3707 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003708 }
3709
3710out:
Daniel Vetter84849902012-12-02 00:28:11 +01003711 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003712 return ret;
3713}
Chris Wilsoneb033552011-01-24 15:11:08 +00003714
3715void drm_mode_config_reset(struct drm_device *dev)
3716{
3717 struct drm_crtc *crtc;
3718 struct drm_encoder *encoder;
3719 struct drm_connector *connector;
3720
3721 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3722 if (crtc->funcs->reset)
3723 crtc->funcs->reset(crtc);
3724
3725 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3726 if (encoder->funcs->reset)
3727 encoder->funcs->reset(encoder);
3728
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003729 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3730 connector->status = connector_status_unknown;
3731
Chris Wilsoneb033552011-01-24 15:11:08 +00003732 if (connector->funcs->reset)
3733 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003734 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003735}
3736EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003737
3738int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3739 void *data, struct drm_file *file_priv)
3740{
3741 struct drm_mode_create_dumb *args = data;
3742
3743 if (!dev->driver->dumb_create)
3744 return -ENOSYS;
3745 return dev->driver->dumb_create(file_priv, dev, args);
3746}
3747
3748int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3749 void *data, struct drm_file *file_priv)
3750{
3751 struct drm_mode_map_dumb *args = data;
3752
3753 /* call driver ioctl to get mmap offset */
3754 if (!dev->driver->dumb_map_offset)
3755 return -ENOSYS;
3756
3757 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3758}
3759
3760int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3761 void *data, struct drm_file *file_priv)
3762{
3763 struct drm_mode_destroy_dumb *args = data;
3764
3765 if (!dev->driver->dumb_destroy)
3766 return -ENOSYS;
3767
3768 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3769}
Dave Airlie248dbc22011-11-29 20:02:54 +00003770
3771/*
3772 * Just need to support RGB formats here for compat with code that doesn't
3773 * use pixel formats directly yet.
3774 */
3775void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3776 int *bpp)
3777{
3778 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003779 case DRM_FORMAT_RGB332:
3780 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003781 *depth = 8;
3782 *bpp = 8;
3783 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003784 case DRM_FORMAT_XRGB1555:
3785 case DRM_FORMAT_XBGR1555:
3786 case DRM_FORMAT_RGBX5551:
3787 case DRM_FORMAT_BGRX5551:
3788 case DRM_FORMAT_ARGB1555:
3789 case DRM_FORMAT_ABGR1555:
3790 case DRM_FORMAT_RGBA5551:
3791 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003792 *depth = 15;
3793 *bpp = 16;
3794 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003795 case DRM_FORMAT_RGB565:
3796 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003797 *depth = 16;
3798 *bpp = 16;
3799 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003800 case DRM_FORMAT_RGB888:
3801 case DRM_FORMAT_BGR888:
3802 *depth = 24;
3803 *bpp = 24;
3804 break;
3805 case DRM_FORMAT_XRGB8888:
3806 case DRM_FORMAT_XBGR8888:
3807 case DRM_FORMAT_RGBX8888:
3808 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003809 *depth = 24;
3810 *bpp = 32;
3811 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003812 case DRM_FORMAT_XRGB2101010:
3813 case DRM_FORMAT_XBGR2101010:
3814 case DRM_FORMAT_RGBX1010102:
3815 case DRM_FORMAT_BGRX1010102:
3816 case DRM_FORMAT_ARGB2101010:
3817 case DRM_FORMAT_ABGR2101010:
3818 case DRM_FORMAT_RGBA1010102:
3819 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003820 *depth = 30;
3821 *bpp = 32;
3822 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003823 case DRM_FORMAT_ARGB8888:
3824 case DRM_FORMAT_ABGR8888:
3825 case DRM_FORMAT_RGBA8888:
3826 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003827 *depth = 32;
3828 *bpp = 32;
3829 break;
3830 default:
3831 DRM_DEBUG_KMS("unsupported pixel format\n");
3832 *depth = 0;
3833 *bpp = 0;
3834 break;
3835 }
3836}
3837EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003838
3839/**
3840 * drm_format_num_planes - get the number of planes for format
3841 * @format: pixel format (DRM_FORMAT_*)
3842 *
3843 * RETURNS:
3844 * The number of planes used by the specified pixel format.
3845 */
3846int drm_format_num_planes(uint32_t format)
3847{
3848 switch (format) {
3849 case DRM_FORMAT_YUV410:
3850 case DRM_FORMAT_YVU410:
3851 case DRM_FORMAT_YUV411:
3852 case DRM_FORMAT_YVU411:
3853 case DRM_FORMAT_YUV420:
3854 case DRM_FORMAT_YVU420:
3855 case DRM_FORMAT_YUV422:
3856 case DRM_FORMAT_YVU422:
3857 case DRM_FORMAT_YUV444:
3858 case DRM_FORMAT_YVU444:
3859 return 3;
3860 case DRM_FORMAT_NV12:
3861 case DRM_FORMAT_NV21:
3862 case DRM_FORMAT_NV16:
3863 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003864 case DRM_FORMAT_NV24:
3865 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003866 return 2;
3867 default:
3868 return 1;
3869 }
3870}
3871EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003872
3873/**
3874 * drm_format_plane_cpp - determine the bytes per pixel value
3875 * @format: pixel format (DRM_FORMAT_*)
3876 * @plane: plane index
3877 *
3878 * RETURNS:
3879 * The bytes per pixel value for the specified plane.
3880 */
3881int drm_format_plane_cpp(uint32_t format, int plane)
3882{
3883 unsigned int depth;
3884 int bpp;
3885
3886 if (plane >= drm_format_num_planes(format))
3887 return 0;
3888
3889 switch (format) {
3890 case DRM_FORMAT_YUYV:
3891 case DRM_FORMAT_YVYU:
3892 case DRM_FORMAT_UYVY:
3893 case DRM_FORMAT_VYUY:
3894 return 2;
3895 case DRM_FORMAT_NV12:
3896 case DRM_FORMAT_NV21:
3897 case DRM_FORMAT_NV16:
3898 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003899 case DRM_FORMAT_NV24:
3900 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003901 return plane ? 2 : 1;
3902 case DRM_FORMAT_YUV410:
3903 case DRM_FORMAT_YVU410:
3904 case DRM_FORMAT_YUV411:
3905 case DRM_FORMAT_YVU411:
3906 case DRM_FORMAT_YUV420:
3907 case DRM_FORMAT_YVU420:
3908 case DRM_FORMAT_YUV422:
3909 case DRM_FORMAT_YVU422:
3910 case DRM_FORMAT_YUV444:
3911 case DRM_FORMAT_YVU444:
3912 return 1;
3913 default:
3914 drm_fb_get_bpp_depth(format, &depth, &bpp);
3915 return bpp >> 3;
3916 }
3917}
3918EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003919
3920/**
3921 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3922 * @format: pixel format (DRM_FORMAT_*)
3923 *
3924 * RETURNS:
3925 * The horizontal chroma subsampling factor for the
3926 * specified pixel format.
3927 */
3928int drm_format_horz_chroma_subsampling(uint32_t format)
3929{
3930 switch (format) {
3931 case DRM_FORMAT_YUV411:
3932 case DRM_FORMAT_YVU411:
3933 case DRM_FORMAT_YUV410:
3934 case DRM_FORMAT_YVU410:
3935 return 4;
3936 case DRM_FORMAT_YUYV:
3937 case DRM_FORMAT_YVYU:
3938 case DRM_FORMAT_UYVY:
3939 case DRM_FORMAT_VYUY:
3940 case DRM_FORMAT_NV12:
3941 case DRM_FORMAT_NV21:
3942 case DRM_FORMAT_NV16:
3943 case DRM_FORMAT_NV61:
3944 case DRM_FORMAT_YUV422:
3945 case DRM_FORMAT_YVU422:
3946 case DRM_FORMAT_YUV420:
3947 case DRM_FORMAT_YVU420:
3948 return 2;
3949 default:
3950 return 1;
3951 }
3952}
3953EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3954
3955/**
3956 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3957 * @format: pixel format (DRM_FORMAT_*)
3958 *
3959 * RETURNS:
3960 * The vertical chroma subsampling factor for the
3961 * specified pixel format.
3962 */
3963int drm_format_vert_chroma_subsampling(uint32_t format)
3964{
3965 switch (format) {
3966 case DRM_FORMAT_YUV410:
3967 case DRM_FORMAT_YVU410:
3968 return 4;
3969 case DRM_FORMAT_YUV420:
3970 case DRM_FORMAT_YVU420:
3971 case DRM_FORMAT_NV12:
3972 case DRM_FORMAT_NV21:
3973 return 2;
3974 default:
3975 return 1;
3976 }
3977}
3978EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);