blob: f2ccda85309f648e839d0b9c4284b112e047e91f [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}
71EXPORT_SYMBOL(drm_modeset_unlock_all);
72
Dave Airlief453ba02008-11-07 14:05:41 -080073/* Avoid boilerplate. I'm tired of typing. */
74#define DRM_ENUM_NAME_FN(fnname, list) \
75 char *fnname(int val) \
76 { \
77 int i; \
78 for (i = 0; i < ARRAY_SIZE(list); i++) { \
79 if (list[i].type == val) \
80 return list[i].name; \
81 } \
82 return "(unknown)"; \
83 }
84
85/*
86 * Global properties
87 */
88static struct drm_prop_enum_list drm_dpms_enum_list[] =
89{ { DRM_MODE_DPMS_ON, "On" },
90 { DRM_MODE_DPMS_STANDBY, "Standby" },
91 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
92 { DRM_MODE_DPMS_OFF, "Off" }
93};
94
95DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
96
97/*
98 * Optional properties
99 */
100static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
101{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700102 { DRM_MODE_SCALE_NONE, "None" },
103 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
104 { DRM_MODE_SCALE_CENTER, "Center" },
105 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800106};
107
108static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
109{
110 { DRM_MODE_DITHERING_OFF, "Off" },
111 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000112 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800113};
114
115/*
116 * Non-global properties, but "required" for certain connectors.
117 */
118static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
119{
120 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
122 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
123};
124
125DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
126
127static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
128{
129 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
131 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
132};
133
134DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
135 drm_dvi_i_subconnector_enum_list)
136
137static struct drm_prop_enum_list drm_tv_select_enum_list[] =
138{
139 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
140 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
141 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
142 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200143 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800144};
145
146DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
147
148static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
149{
150 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
151 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
152 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
153 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200154 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800155};
156
157DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
158 drm_tv_subconnector_enum_list)
159
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000160static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
161 { DRM_MODE_DIRTY_OFF, "Off" },
162 { DRM_MODE_DIRTY_ON, "On" },
163 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
164};
165
166DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
167 drm_dirty_info_enum_list)
168
Dave Airlief453ba02008-11-07 14:05:41 -0800169struct drm_conn_prop_enum_list {
170 int type;
171 char *name;
172 int count;
173};
174
175/*
176 * Connector and encoder types.
177 */
178static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
179{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
180 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
181 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
182 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
183 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
184 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
185 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
186 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
187 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500188 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
189 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
190 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
191 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200192 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500193 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200194 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800195};
196
197static struct drm_prop_enum_list drm_encoder_enum_list[] =
198{ { DRM_MODE_ENCODER_NONE, "None" },
199 { DRM_MODE_ENCODER_DAC, "DAC" },
200 { DRM_MODE_ENCODER_TMDS, "TMDS" },
201 { DRM_MODE_ENCODER_LVDS, "LVDS" },
202 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200203 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800204};
205
206char *drm_get_encoder_name(struct drm_encoder *encoder)
207{
208 static char buf[32];
209
210 snprintf(buf, 32, "%s-%d",
211 drm_encoder_enum_list[encoder->encoder_type].name,
212 encoder->base.id);
213 return buf;
214}
Dave Airlie13a81952009-09-07 15:45:33 +1000215EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800216
217char *drm_get_connector_name(struct drm_connector *connector)
218{
219 static char buf[32];
220
221 snprintf(buf, 32, "%s-%d",
222 drm_connector_enum_list[connector->connector_type].name,
223 connector->connector_type_id);
224 return buf;
225}
226EXPORT_SYMBOL(drm_get_connector_name);
227
228char *drm_get_connector_status_name(enum drm_connector_status status)
229{
230 if (status == connector_status_connected)
231 return "connected";
232 else if (status == connector_status_disconnected)
233 return "disconnected";
234 else
235 return "unknown";
236}
237
238/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100239 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800240 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100241 * @obj: object pointer, used to generate unique ID
242 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800243 *
Dave Airlief453ba02008-11-07 14:05:41 -0800244 * Create a unique identifier based on @ptr in @dev's identifier space. Used
245 * for tracking modes, CRTCs and connectors.
246 *
247 * RETURNS:
248 * New unique (relative to other objects in @dev) integer identifier for the
249 * object.
250 */
251static int drm_mode_object_get(struct drm_device *dev,
252 struct drm_mode_object *obj, uint32_t obj_type)
253{
254 int new_id = 0;
255 int ret;
256
Dave Airlief453ba02008-11-07 14:05:41 -0800257again:
258 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
259 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200260 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800261 }
262
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800264 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100265
266 if (!ret) {
267 /*
268 * Set up the object linking under the protection of the idr
269 * lock so that other users can't see inconsistent state.
270 */
271 obj->id = new_id;
272 obj->type = obj_type;
273 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000274 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100275
Dave Airlief453ba02008-11-07 14:05:41 -0800276 if (ret == -EAGAIN)
277 goto again;
278
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100279 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800280}
281
282/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100283 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800284 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100285 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800286 *
Dave Airlief453ba02008-11-07 14:05:41 -0800287 * Free @id from @dev's unique identifier pool.
288 */
289static void drm_mode_object_put(struct drm_device *dev,
290 struct drm_mode_object *object)
291{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000292 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800293 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000294 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800295}
296
Daniel Vetter786b99e2012-12-02 21:53:40 +0100297/**
298 * drm_mode_object_find - look up a drm object with static lifetime
299 * @dev: drm device
300 * @id: id of the mode object
301 * @type: type of the mode object
302 *
303 * Note that framebuffers cannot be looked up with this functions - since those
304 * are reference counted, they need special treatment.
305 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200306struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
307 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800308{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000309 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800310
Daniel Vetter786b99e2012-12-02 21:53:40 +0100311 /* Framebuffers are reference counted and need their own lookup
312 * function.*/
313 WARN_ON(type == DRM_MODE_OBJECT_FB);
314
Jesse Barnesad2563c2009-01-19 17:21:45 +1000315 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800316 obj = idr_find(&dev->mode_config.crtc_idr, id);
317 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000318 obj = NULL;
319 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800320
321 return obj;
322}
323EXPORT_SYMBOL(drm_mode_object_find);
324
325/**
Dave Airlief453ba02008-11-07 14:05:41 -0800326 * drm_framebuffer_init - initialize a framebuffer
327 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100328 * @fb: framebuffer to be initialized
329 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800330 *
Dave Airlief453ba02008-11-07 14:05:41 -0800331 * Allocates an ID for the framebuffer's parent mode object, sets its mode
332 * functions & device file and adds it to the master fd list.
333 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100334 * IMPORTANT:
335 * This functions publishes the fb and makes it available for concurrent access
336 * by other users. Which means by this point the fb _must_ be fully set up -
337 * since all the fb attributes are invariant over its lifetime, no further
338 * locking but only correct reference counting is required.
339 *
Dave Airlief453ba02008-11-07 14:05:41 -0800340 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200341 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800342 */
343int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
344 const struct drm_framebuffer_funcs *funcs)
345{
346 int ret;
347
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100348 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000349 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100350 INIT_LIST_HEAD(&fb->filp_head);
351 fb->dev = dev;
352 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000353
Dave Airlief453ba02008-11-07 14:05:41 -0800354 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200355 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100356 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800357
Dave Airlief453ba02008-11-07 14:05:41 -0800358 dev->mode_config.num_fb++;
359 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100360out:
361 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800362
363 return 0;
364}
365EXPORT_SYMBOL(drm_framebuffer_init);
366
Rob Clarkf7eff602012-09-05 21:48:38 +0000367static void drm_framebuffer_free(struct kref *kref)
368{
369 struct drm_framebuffer *fb =
370 container_of(kref, struct drm_framebuffer, refcount);
371 fb->funcs->destroy(fb);
372}
373
374/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100375 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
376 * @dev: drm device
377 * @id: id of the fb object
378 *
379 * If successful, this grabs an additional reference to the framebuffer -
380 * callers need to make sure to eventually unreference the returned framebuffer
381 * again.
382 */
383struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
384 uint32_t id)
385{
386 struct drm_mode_object *obj = NULL;
387 struct drm_framebuffer *fb;
388
389 mutex_lock(&dev->mode_config.fb_lock);
390
391 mutex_lock(&dev->mode_config.idr_mutex);
392 obj = idr_find(&dev->mode_config.crtc_idr, id);
393 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
394 fb = NULL;
395 else
396 fb = obj_to_fb(obj);
397 mutex_unlock(&dev->mode_config.idr_mutex);
398
399 if (fb)
400 kref_get(&fb->refcount);
401
402 mutex_unlock(&dev->mode_config.fb_lock);
403
404 return fb;
405}
406EXPORT_SYMBOL(drm_framebuffer_lookup);
407
408/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000409 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100410 * @fb: framebuffer to unref
411 *
412 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000413 */
414void drm_framebuffer_unreference(struct drm_framebuffer *fb)
415{
Rob Clarkf7eff602012-09-05 21:48:38 +0000416 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000417 kref_put(&fb->refcount, drm_framebuffer_free);
418}
419EXPORT_SYMBOL(drm_framebuffer_unreference);
420
421/**
422 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100423 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000424 */
425void drm_framebuffer_reference(struct drm_framebuffer *fb)
426{
427 DRM_DEBUG("FB ID: %d\n", fb->base.id);
428 kref_get(&fb->refcount);
429}
430EXPORT_SYMBOL(drm_framebuffer_reference);
431
Dave Airlief453ba02008-11-07 14:05:41 -0800432/**
433 * drm_framebuffer_cleanup - remove a framebuffer object
434 * @fb: framebuffer to remove
435 *
Dave Airlief453ba02008-11-07 14:05:41 -0800436 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
437 * it, setting it to NULL.
438 */
439void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
440{
441 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100442
Rob Clarkf7eff602012-09-05 21:48:38 +0000443 /*
444 * This could be moved to drm_framebuffer_remove(), but for
445 * debugging is nice to keep around the list of fb's that are
446 * no longer associated w/ a drm_file but are not unreferenced
447 * yet. (i915 and omapdrm have debugfs files which will show
448 * this.)
449 */
450 drm_mode_object_put(dev, &fb->base);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100451 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000452 list_del(&fb->head);
453 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100454 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000455}
456EXPORT_SYMBOL(drm_framebuffer_cleanup);
457
458/**
459 * drm_framebuffer_remove - remove and unreference a framebuffer object
460 * @fb: framebuffer to remove
461 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000462 * Scans all the CRTCs and planes in @dev's mode_config. If they're
463 * using @fb, removes it, setting it to NULL.
464 */
465void drm_framebuffer_remove(struct drm_framebuffer *fb)
466{
467 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800468 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800469 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000470 struct drm_mode_set set;
471 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800472
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100473 WARN_ON(!drm_modeset_is_locked(dev));
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100474 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100475
Dave Airlief453ba02008-11-07 14:05:41 -0800476 /* remove from any CRTC */
477 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000478 if (crtc->fb == fb) {
479 /* should turn off the crtc */
480 memset(&set, 0, sizeof(struct drm_mode_set));
481 set.crtc = crtc;
482 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100483 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000484 if (ret)
485 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
486 }
Dave Airlief453ba02008-11-07 14:05:41 -0800487 }
488
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800489 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
490 if (plane->fb == fb) {
491 /* should turn off the crtc */
492 ret = plane->funcs->disable_plane(plane);
493 if (ret)
494 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600495 /* disconnect the plane from the fb and crtc: */
496 plane->fb = NULL;
497 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800498 }
499 }
500
Rob Clarkf7eff602012-09-05 21:48:38 +0000501 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800502}
Rob Clarkf7eff602012-09-05 21:48:38 +0000503EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800504
505/**
506 * drm_crtc_init - Initialise a new CRTC object
507 * @dev: DRM device
508 * @crtc: CRTC object to init
509 * @funcs: callbacks for the new CRTC
510 *
Dave Airlief453ba02008-11-07 14:05:41 -0800511 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200512 *
513 * RETURNS:
514 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800515 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200516int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800517 const struct drm_crtc_funcs *funcs)
518{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200519 int ret;
520
Dave Airlief453ba02008-11-07 14:05:41 -0800521 crtc->dev = dev;
522 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000523 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800524
Daniel Vetter84849902012-12-02 00:28:11 +0100525 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100526 mutex_init(&crtc->mutex);
527 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200528
529 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
530 if (ret)
531 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800532
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300533 crtc->base.properties = &crtc->properties;
534
Dave Airlief453ba02008-11-07 14:05:41 -0800535 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
536 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200537
538 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100539 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200540
541 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800542}
543EXPORT_SYMBOL(drm_crtc_init);
544
545/**
546 * drm_crtc_cleanup - Cleans up the core crtc usage.
547 * @crtc: CRTC to cleanup
548 *
Dave Airlief453ba02008-11-07 14:05:41 -0800549 * Cleanup @crtc. Removes from drm modesetting space
550 * does NOT free object, caller does that.
551 */
552void drm_crtc_cleanup(struct drm_crtc *crtc)
553{
554 struct drm_device *dev = crtc->dev;
555
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000556 kfree(crtc->gamma_store);
557 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800558
559 drm_mode_object_put(dev, &crtc->base);
560 list_del(&crtc->head);
561 dev->mode_config.num_crtc--;
562}
563EXPORT_SYMBOL(drm_crtc_cleanup);
564
565/**
566 * drm_mode_probed_add - add a mode to a connector's probed mode list
567 * @connector: connector the new mode
568 * @mode: mode data
569 *
Dave Airlief453ba02008-11-07 14:05:41 -0800570 * Add @mode to @connector's mode list for later use.
571 */
572void drm_mode_probed_add(struct drm_connector *connector,
573 struct drm_display_mode *mode)
574{
575 list_add(&mode->head, &connector->probed_modes);
576}
577EXPORT_SYMBOL(drm_mode_probed_add);
578
579/**
580 * drm_mode_remove - remove and free a mode
581 * @connector: connector list to modify
582 * @mode: mode to remove
583 *
Dave Airlief453ba02008-11-07 14:05:41 -0800584 * Remove @mode from @connector's mode list, then free it.
585 */
586void drm_mode_remove(struct drm_connector *connector,
587 struct drm_display_mode *mode)
588{
589 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100590 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800591}
592EXPORT_SYMBOL(drm_mode_remove);
593
594/**
595 * drm_connector_init - Init a preallocated connector
596 * @dev: DRM device
597 * @connector: the connector to init
598 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100599 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800600 *
Dave Airlief453ba02008-11-07 14:05:41 -0800601 * Initialises a preallocated connector. Connectors should be
602 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200603 *
604 * RETURNS:
605 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800606 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200607int drm_connector_init(struct drm_device *dev,
608 struct drm_connector *connector,
609 const struct drm_connector_funcs *funcs,
610 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800611{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200612 int ret;
613
Daniel Vetter84849902012-12-02 00:28:11 +0100614 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800615
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200616 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
617 if (ret)
618 goto out;
619
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300620 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800621 connector->dev = dev;
622 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800623 connector->connector_type = connector_type;
624 connector->connector_type_id =
625 ++drm_connector_enum_list[connector_type].count; /* TODO */
626 INIT_LIST_HEAD(&connector->user_modes);
627 INIT_LIST_HEAD(&connector->probed_modes);
628 INIT_LIST_HEAD(&connector->modes);
629 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000630 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800631
632 list_add_tail(&connector->head, &dev->mode_config.connector_list);
633 dev->mode_config.num_connector++;
634
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200635 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500636 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200637 dev->mode_config.edid_property,
638 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800639
Rob Clark58495562012-10-11 20:50:56 -0500640 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800641 dev->mode_config.dpms_property, 0);
642
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200643 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100644 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200645
646 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800647}
648EXPORT_SYMBOL(drm_connector_init);
649
650/**
651 * drm_connector_cleanup - cleans up an initialised connector
652 * @connector: connector to cleanup
653 *
Dave Airlief453ba02008-11-07 14:05:41 -0800654 * Cleans up the connector but doesn't free the object.
655 */
656void drm_connector_cleanup(struct drm_connector *connector)
657{
658 struct drm_device *dev = connector->dev;
659 struct drm_display_mode *mode, *t;
660
661 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
662 drm_mode_remove(connector, mode);
663
664 list_for_each_entry_safe(mode, t, &connector->modes, head)
665 drm_mode_remove(connector, mode);
666
667 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
668 drm_mode_remove(connector, mode);
669
Dave Airlief453ba02008-11-07 14:05:41 -0800670 drm_mode_object_put(dev, &connector->base);
671 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000672 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800673}
674EXPORT_SYMBOL(drm_connector_cleanup);
675
Dave Airliecbc7e222012-02-20 14:16:40 +0000676void drm_connector_unplug_all(struct drm_device *dev)
677{
678 struct drm_connector *connector;
679
680 /* taking the mode config mutex ends up in a clash with sysfs */
681 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
682 drm_sysfs_connector_remove(connector);
683
684}
685EXPORT_SYMBOL(drm_connector_unplug_all);
686
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200687int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000688 struct drm_encoder *encoder,
689 const struct drm_encoder_funcs *funcs,
690 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800691{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200692 int ret;
693
Daniel Vetter84849902012-12-02 00:28:11 +0100694 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800695
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200696 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
697 if (ret)
698 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800699
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200700 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800701 encoder->encoder_type = encoder_type;
702 encoder->funcs = funcs;
703
704 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
705 dev->mode_config.num_encoder++;
706
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200707 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100708 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200709
710 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800711}
712EXPORT_SYMBOL(drm_encoder_init);
713
714void drm_encoder_cleanup(struct drm_encoder *encoder)
715{
716 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100717 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800718 drm_mode_object_put(dev, &encoder->base);
719 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000720 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100721 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800722}
723EXPORT_SYMBOL(drm_encoder_cleanup);
724
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800725int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
726 unsigned long possible_crtcs,
727 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600728 const uint32_t *formats, uint32_t format_count,
729 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800730{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200731 int ret;
732
Daniel Vetter84849902012-12-02 00:28:11 +0100733 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800734
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200735 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
736 if (ret)
737 goto out;
738
Rob Clark4d939142012-05-17 02:23:27 -0600739 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800740 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800741 plane->funcs = funcs;
742 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
743 GFP_KERNEL);
744 if (!plane->format_types) {
745 DRM_DEBUG_KMS("out of memory when allocating plane\n");
746 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200747 ret = -ENOMEM;
748 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800749 }
750
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800751 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800752 plane->format_count = format_count;
753 plane->possible_crtcs = possible_crtcs;
754
Rob Clark0a7eb242011-12-13 20:19:36 -0600755 /* private planes are not exposed to userspace, but depending on
756 * display hardware, might be convenient to allow sharing programming
757 * for the scanout engine with the crtc implementation.
758 */
759 if (!priv) {
760 list_add_tail(&plane->head, &dev->mode_config.plane_list);
761 dev->mode_config.num_plane++;
762 } else {
763 INIT_LIST_HEAD(&plane->head);
764 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800765
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200766 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100767 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800768
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200769 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800770}
771EXPORT_SYMBOL(drm_plane_init);
772
773void drm_plane_cleanup(struct drm_plane *plane)
774{
775 struct drm_device *dev = plane->dev;
776
Daniel Vetter84849902012-12-02 00:28:11 +0100777 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800778 kfree(plane->format_types);
779 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600780 /* if not added to a list, it must be a private plane */
781 if (!list_empty(&plane->head)) {
782 list_del(&plane->head);
783 dev->mode_config.num_plane--;
784 }
Daniel Vetter84849902012-12-02 00:28:11 +0100785 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800786}
787EXPORT_SYMBOL(drm_plane_cleanup);
788
Dave Airlief453ba02008-11-07 14:05:41 -0800789/**
790 * drm_mode_create - create a new display mode
791 * @dev: DRM device
792 *
Dave Airlief453ba02008-11-07 14:05:41 -0800793 * Create a new drm_display_mode, give it an ID, and return it.
794 *
795 * RETURNS:
796 * Pointer to new mode on success, NULL on error.
797 */
798struct drm_display_mode *drm_mode_create(struct drm_device *dev)
799{
800 struct drm_display_mode *nmode;
801
802 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
803 if (!nmode)
804 return NULL;
805
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200806 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
807 kfree(nmode);
808 return NULL;
809 }
810
Dave Airlief453ba02008-11-07 14:05:41 -0800811 return nmode;
812}
813EXPORT_SYMBOL(drm_mode_create);
814
815/**
816 * drm_mode_destroy - remove a mode
817 * @dev: DRM device
818 * @mode: mode to remove
819 *
Dave Airlief453ba02008-11-07 14:05:41 -0800820 * Free @mode's unique identifier, then free it.
821 */
822void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
823{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200824 if (!mode)
825 return;
826
Dave Airlief453ba02008-11-07 14:05:41 -0800827 drm_mode_object_put(dev, &mode->base);
828
829 kfree(mode);
830}
831EXPORT_SYMBOL(drm_mode_destroy);
832
833static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
834{
835 struct drm_property *edid;
836 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800837
838 /*
839 * Standard properties (apply to all connectors)
840 */
841 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
842 DRM_MODE_PROP_IMMUTABLE,
843 "EDID", 0);
844 dev->mode_config.edid_property = edid;
845
Sascha Hauer4a67d392012-02-06 10:58:17 +0100846 dpms = drm_property_create_enum(dev, 0,
847 "DPMS", drm_dpms_enum_list,
848 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800849 dev->mode_config.dpms_property = dpms;
850
851 return 0;
852}
853
854/**
855 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
856 * @dev: DRM device
857 *
858 * Called by a driver the first time a DVI-I connector is made.
859 */
860int drm_mode_create_dvi_i_properties(struct drm_device *dev)
861{
862 struct drm_property *dvi_i_selector;
863 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800864
865 if (dev->mode_config.dvi_i_select_subconnector_property)
866 return 0;
867
868 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100869 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800870 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100871 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800872 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800873 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
874
Sascha Hauer4a67d392012-02-06 10:58:17 +0100875 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800876 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100877 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800878 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800879 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
880
881 return 0;
882}
883EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
884
885/**
886 * drm_create_tv_properties - create TV specific connector properties
887 * @dev: DRM device
888 * @num_modes: number of different TV formats (modes) supported
889 * @modes: array of pointers to strings containing name of each format
890 *
891 * Called by a driver's TV initialization routine, this function creates
892 * the TV specific connector properties for a given device. Caller is
893 * responsible for allocating a list of format names and passing them to
894 * this routine.
895 */
896int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
897 char *modes[])
898{
899 struct drm_property *tv_selector;
900 struct drm_property *tv_subconnector;
901 int i;
902
903 if (dev->mode_config.tv_select_subconnector_property)
904 return 0;
905
906 /*
907 * Basic connector properties
908 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100909 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800910 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100911 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800912 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800913 dev->mode_config.tv_select_subconnector_property = tv_selector;
914
915 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100916 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
917 "subconnector",
918 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800919 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800920 dev->mode_config.tv_subconnector_property = tv_subconnector;
921
922 /*
923 * Other, TV specific properties: margins & TV modes.
924 */
925 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100926 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800927
928 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100929 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800930
931 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100932 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800933
934 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100935 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800936
937 dev->mode_config.tv_mode_property =
938 drm_property_create(dev, DRM_MODE_PROP_ENUM,
939 "mode", num_modes);
940 for (i = 0; i < num_modes; i++)
941 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
942 i, modes[i]);
943
Francisco Jerezb6b79022009-08-02 04:19:20 +0200944 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100945 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200946
947 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100948 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200949
950 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100951 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200952
Francisco Jereza75f0232009-08-12 02:30:10 +0200953 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100954 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200955
956 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100957 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200958
959 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100960 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200961
Dave Airlief453ba02008-11-07 14:05:41 -0800962 return 0;
963}
964EXPORT_SYMBOL(drm_mode_create_tv_properties);
965
966/**
967 * drm_mode_create_scaling_mode_property - create scaling mode property
968 * @dev: DRM device
969 *
970 * Called by a driver the first time it's needed, must be attached to desired
971 * connectors.
972 */
973int drm_mode_create_scaling_mode_property(struct drm_device *dev)
974{
975 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800976
977 if (dev->mode_config.scaling_mode_property)
978 return 0;
979
980 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100981 drm_property_create_enum(dev, 0, "scaling mode",
982 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800983 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800984
985 dev->mode_config.scaling_mode_property = scaling_mode;
986
987 return 0;
988}
989EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
990
991/**
992 * drm_mode_create_dithering_property - create dithering 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_dithering_property(struct drm_device *dev)
999{
1000 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001001
1002 if (dev->mode_config.dithering_mode_property)
1003 return 0;
1004
1005 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001006 drm_property_create_enum(dev, 0, "dithering",
1007 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001008 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001009 dev->mode_config.dithering_mode_property = dithering_mode;
1010
1011 return 0;
1012}
1013EXPORT_SYMBOL(drm_mode_create_dithering_property);
1014
1015/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001016 * drm_mode_create_dirty_property - create dirty property
1017 * @dev: DRM device
1018 *
1019 * Called by a driver the first time it's needed, must be attached to desired
1020 * connectors.
1021 */
1022int drm_mode_create_dirty_info_property(struct drm_device *dev)
1023{
1024 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001025
1026 if (dev->mode_config.dirty_info_property)
1027 return 0;
1028
1029 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001030 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001031 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001032 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001033 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001034 dev->mode_config.dirty_info_property = dirty_info;
1035
1036 return 0;
1037}
1038EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1039
1040/**
Dave Airlief453ba02008-11-07 14:05:41 -08001041 * drm_mode_config_init - initialize DRM mode_configuration structure
1042 * @dev: DRM device
1043 *
Dave Airlief453ba02008-11-07 14:05:41 -08001044 * Initialize @dev's mode_config structure, used for tracking the graphics
1045 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001046 *
1047 * Since this initializes the modeset locks, no locking is possible. Which is no
1048 * problem, since this should happen single threaded at init time. It is the
1049 * driver's problem to ensure this guarantee.
1050 *
Dave Airlief453ba02008-11-07 14:05:41 -08001051 */
1052void drm_mode_config_init(struct drm_device *dev)
1053{
1054 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001055 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001056 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001057 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001058 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1059 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1060 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1061 INIT_LIST_HEAD(&dev->mode_config.property_list);
1062 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001063 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001064 idr_init(&dev->mode_config.crtc_idr);
1065
Daniel Vetter84849902012-12-02 00:28:11 +01001066 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001067 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001068 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001069
1070 /* Just to be sure */
1071 dev->mode_config.num_fb = 0;
1072 dev->mode_config.num_connector = 0;
1073 dev->mode_config.num_crtc = 0;
1074 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001075}
1076EXPORT_SYMBOL(drm_mode_config_init);
1077
1078int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1079{
1080 uint32_t total_objects = 0;
1081
1082 total_objects += dev->mode_config.num_crtc;
1083 total_objects += dev->mode_config.num_connector;
1084 total_objects += dev->mode_config.num_encoder;
1085
Dave Airlief453ba02008-11-07 14:05:41 -08001086 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1087 if (!group->id_list)
1088 return -ENOMEM;
1089
1090 group->num_crtcs = 0;
1091 group->num_connectors = 0;
1092 group->num_encoders = 0;
1093 return 0;
1094}
1095
1096int drm_mode_group_init_legacy_group(struct drm_device *dev,
1097 struct drm_mode_group *group)
1098{
1099 struct drm_crtc *crtc;
1100 struct drm_encoder *encoder;
1101 struct drm_connector *connector;
1102 int ret;
1103
1104 if ((ret = drm_mode_group_init(dev, group)))
1105 return ret;
1106
1107 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1108 group->id_list[group->num_crtcs++] = crtc->base.id;
1109
1110 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1111 group->id_list[group->num_crtcs + group->num_encoders++] =
1112 encoder->base.id;
1113
1114 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1115 group->id_list[group->num_crtcs + group->num_encoders +
1116 group->num_connectors++] = connector->base.id;
1117
1118 return 0;
1119}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001120EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001121
1122/**
1123 * drm_mode_config_cleanup - free up DRM mode_config info
1124 * @dev: DRM device
1125 *
Dave Airlief453ba02008-11-07 14:05:41 -08001126 * Free up all the connectors and CRTCs associated with this DRM device, then
1127 * free up the framebuffers and associated buffer objects.
1128 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001129 * Note that since this /should/ happen single-threaded at driver/device
1130 * teardown time, no locking is required. It's the driver's job to ensure that
1131 * this guarantee actually holds true.
1132 *
Dave Airlief453ba02008-11-07 14:05:41 -08001133 * FIXME: cleanup any dangling user buffer objects too
1134 */
1135void drm_mode_config_cleanup(struct drm_device *dev)
1136{
1137 struct drm_connector *connector, *ot;
1138 struct drm_crtc *crtc, *ct;
1139 struct drm_encoder *encoder, *enct;
1140 struct drm_framebuffer *fb, *fbt;
1141 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001142 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001143
1144 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1145 head) {
1146 encoder->funcs->destroy(encoder);
1147 }
1148
1149 list_for_each_entry_safe(connector, ot,
1150 &dev->mode_config.connector_list, head) {
1151 connector->funcs->destroy(connector);
1152 }
1153
1154 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1155 head) {
1156 drm_property_destroy(dev, property);
1157 }
1158
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001159 /* Single-threaded teardown context, so it's not requied to grab the
1160 * fb_lock to protect against concurrent fb_list access. Contrary, it
1161 * would actually deadlock with the drm_framebuffer_cleanup function. */
Dave Airlief453ba02008-11-07 14:05:41 -08001162 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001163 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001164 }
1165
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001166 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1167 head) {
1168 plane->funcs->destroy(plane);
1169 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001170
Chris Wilson31840092012-09-17 09:38:03 +00001171 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1172 crtc->funcs->destroy(crtc);
1173 }
1174
Sascha Hauer59ce0622012-02-01 11:38:20 +01001175 idr_remove_all(&dev->mode_config.crtc_idr);
1176 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001177}
1178EXPORT_SYMBOL(drm_mode_config_cleanup);
1179
Dave Airlief453ba02008-11-07 14:05:41 -08001180/**
1181 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1182 * @out: drm_mode_modeinfo struct to return to the user
1183 * @in: drm_display_mode to use
1184 *
Dave Airlief453ba02008-11-07 14:05:41 -08001185 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1186 * the user.
1187 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001188static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1189 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001190{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001191 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1192 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1193 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1194 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1195 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1196 "timing values too large for mode info\n");
1197
Dave Airlief453ba02008-11-07 14:05:41 -08001198 out->clock = in->clock;
1199 out->hdisplay = in->hdisplay;
1200 out->hsync_start = in->hsync_start;
1201 out->hsync_end = in->hsync_end;
1202 out->htotal = in->htotal;
1203 out->hskew = in->hskew;
1204 out->vdisplay = in->vdisplay;
1205 out->vsync_start = in->vsync_start;
1206 out->vsync_end = in->vsync_end;
1207 out->vtotal = in->vtotal;
1208 out->vscan = in->vscan;
1209 out->vrefresh = in->vrefresh;
1210 out->flags = in->flags;
1211 out->type = in->type;
1212 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1213 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1214}
1215
1216/**
1217 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1218 * @out: drm_display_mode to return to the user
1219 * @in: drm_mode_modeinfo to use
1220 *
Dave Airlief453ba02008-11-07 14:05:41 -08001221 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1222 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001223 *
1224 * RETURNS:
1225 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001226 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001227static int drm_crtc_convert_umode(struct drm_display_mode *out,
1228 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001229{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001230 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1231 return -ERANGE;
1232
Dave Airlief453ba02008-11-07 14:05:41 -08001233 out->clock = in->clock;
1234 out->hdisplay = in->hdisplay;
1235 out->hsync_start = in->hsync_start;
1236 out->hsync_end = in->hsync_end;
1237 out->htotal = in->htotal;
1238 out->hskew = in->hskew;
1239 out->vdisplay = in->vdisplay;
1240 out->vsync_start = in->vsync_start;
1241 out->vsync_end = in->vsync_end;
1242 out->vtotal = in->vtotal;
1243 out->vscan = in->vscan;
1244 out->vrefresh = in->vrefresh;
1245 out->flags = in->flags;
1246 out->type = in->type;
1247 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1248 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001249
1250 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001251}
1252
1253/**
1254 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001255 * @dev: drm device for the ioctl
1256 * @data: data pointer for the ioctl
1257 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001258 *
Dave Airlief453ba02008-11-07 14:05:41 -08001259 * Construct a set of configuration description structures and return
1260 * them to the user, including CRTC, connector and framebuffer configuration.
1261 *
1262 * Called by the user via ioctl.
1263 *
1264 * RETURNS:
1265 * Zero on success, errno on failure.
1266 */
1267int drm_mode_getresources(struct drm_device *dev, void *data,
1268 struct drm_file *file_priv)
1269{
1270 struct drm_mode_card_res *card_res = data;
1271 struct list_head *lh;
1272 struct drm_framebuffer *fb;
1273 struct drm_connector *connector;
1274 struct drm_crtc *crtc;
1275 struct drm_encoder *encoder;
1276 int ret = 0;
1277 int connector_count = 0;
1278 int crtc_count = 0;
1279 int fb_count = 0;
1280 int encoder_count = 0;
1281 int copied = 0, i;
1282 uint32_t __user *fb_id;
1283 uint32_t __user *crtc_id;
1284 uint32_t __user *connector_id;
1285 uint32_t __user *encoder_id;
1286 struct drm_mode_group *mode_group;
1287
Dave Airliefb3b06c2011-02-08 13:55:21 +10001288 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1289 return -EINVAL;
1290
Dave Airlief453ba02008-11-07 14:05:41 -08001291
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001292 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001293 /*
1294 * For the non-control nodes we need to limit the list of resources
1295 * by IDs in the group list for this node
1296 */
1297 list_for_each(lh, &file_priv->fbs)
1298 fb_count++;
1299
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001300 /* handle this in 4 parts */
1301 /* FBs */
1302 if (card_res->count_fbs >= fb_count) {
1303 copied = 0;
1304 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1305 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1306 if (put_user(fb->base.id, fb_id + copied)) {
1307 mutex_unlock(&file_priv->fbs_lock);
1308 return -EFAULT;
1309 }
1310 copied++;
1311 }
1312 }
1313 card_res->count_fbs = fb_count;
1314 mutex_unlock(&file_priv->fbs_lock);
1315
1316 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001317 mode_group = &file_priv->master->minor->mode_group;
1318 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1319
1320 list_for_each(lh, &dev->mode_config.crtc_list)
1321 crtc_count++;
1322
1323 list_for_each(lh, &dev->mode_config.connector_list)
1324 connector_count++;
1325
1326 list_for_each(lh, &dev->mode_config.encoder_list)
1327 encoder_count++;
1328 } else {
1329
1330 crtc_count = mode_group->num_crtcs;
1331 connector_count = mode_group->num_connectors;
1332 encoder_count = mode_group->num_encoders;
1333 }
1334
1335 card_res->max_height = dev->mode_config.max_height;
1336 card_res->min_height = dev->mode_config.min_height;
1337 card_res->max_width = dev->mode_config.max_width;
1338 card_res->min_width = dev->mode_config.min_width;
1339
Dave Airlief453ba02008-11-07 14:05:41 -08001340 /* CRTCs */
1341 if (card_res->count_crtcs >= crtc_count) {
1342 copied = 0;
1343 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1344 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1345 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1346 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001347 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001348 if (put_user(crtc->base.id, crtc_id + copied)) {
1349 ret = -EFAULT;
1350 goto out;
1351 }
1352 copied++;
1353 }
1354 } else {
1355 for (i = 0; i < mode_group->num_crtcs; i++) {
1356 if (put_user(mode_group->id_list[i],
1357 crtc_id + copied)) {
1358 ret = -EFAULT;
1359 goto out;
1360 }
1361 copied++;
1362 }
1363 }
1364 }
1365 card_res->count_crtcs = crtc_count;
1366
1367 /* Encoders */
1368 if (card_res->count_encoders >= encoder_count) {
1369 copied = 0;
1370 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1371 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1372 list_for_each_entry(encoder,
1373 &dev->mode_config.encoder_list,
1374 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001375 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1376 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001377 if (put_user(encoder->base.id, encoder_id +
1378 copied)) {
1379 ret = -EFAULT;
1380 goto out;
1381 }
1382 copied++;
1383 }
1384 } else {
1385 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1386 if (put_user(mode_group->id_list[i],
1387 encoder_id + copied)) {
1388 ret = -EFAULT;
1389 goto out;
1390 }
1391 copied++;
1392 }
1393
1394 }
1395 }
1396 card_res->count_encoders = encoder_count;
1397
1398 /* Connectors */
1399 if (card_res->count_connectors >= connector_count) {
1400 copied = 0;
1401 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1402 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1403 list_for_each_entry(connector,
1404 &dev->mode_config.connector_list,
1405 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001406 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1407 connector->base.id,
1408 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001409 if (put_user(connector->base.id,
1410 connector_id + copied)) {
1411 ret = -EFAULT;
1412 goto out;
1413 }
1414 copied++;
1415 }
1416 } else {
1417 int start = mode_group->num_crtcs +
1418 mode_group->num_encoders;
1419 for (i = start; i < start + mode_group->num_connectors; i++) {
1420 if (put_user(mode_group->id_list[i],
1421 connector_id + copied)) {
1422 ret = -EFAULT;
1423 goto out;
1424 }
1425 copied++;
1426 }
1427 }
1428 }
1429 card_res->count_connectors = connector_count;
1430
Jerome Glisse94401062010-07-15 15:43:25 -04001431 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001432 card_res->count_connectors, card_res->count_encoders);
1433
1434out:
Daniel Vetter84849902012-12-02 00:28:11 +01001435 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001436 return ret;
1437}
1438
1439/**
1440 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001441 * @dev: drm device for the ioctl
1442 * @data: data pointer for the ioctl
1443 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001444 *
Dave Airlief453ba02008-11-07 14:05:41 -08001445 * Construct a CRTC configuration structure to return to the user.
1446 *
1447 * Called by the user via ioctl.
1448 *
1449 * RETURNS:
1450 * Zero on success, errno on failure.
1451 */
1452int drm_mode_getcrtc(struct drm_device *dev,
1453 void *data, struct drm_file *file_priv)
1454{
1455 struct drm_mode_crtc *crtc_resp = data;
1456 struct drm_crtc *crtc;
1457 struct drm_mode_object *obj;
1458 int ret = 0;
1459
Dave Airliefb3b06c2011-02-08 13:55:21 +10001460 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1461 return -EINVAL;
1462
Daniel Vetter84849902012-12-02 00:28:11 +01001463 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001464
1465 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1466 DRM_MODE_OBJECT_CRTC);
1467 if (!obj) {
1468 ret = -EINVAL;
1469 goto out;
1470 }
1471 crtc = obj_to_crtc(obj);
1472
1473 crtc_resp->x = crtc->x;
1474 crtc_resp->y = crtc->y;
1475 crtc_resp->gamma_size = crtc->gamma_size;
1476 if (crtc->fb)
1477 crtc_resp->fb_id = crtc->fb->base.id;
1478 else
1479 crtc_resp->fb_id = 0;
1480
1481 if (crtc->enabled) {
1482
1483 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1484 crtc_resp->mode_valid = 1;
1485
1486 } else {
1487 crtc_resp->mode_valid = 0;
1488 }
1489
1490out:
Daniel Vetter84849902012-12-02 00:28:11 +01001491 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001492 return ret;
1493}
1494
1495/**
1496 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001497 * @dev: drm device for the ioctl
1498 * @data: data pointer for the ioctl
1499 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001500 *
Dave Airlief453ba02008-11-07 14:05:41 -08001501 * Construct a connector configuration structure to return to the user.
1502 *
1503 * Called by the user via ioctl.
1504 *
1505 * RETURNS:
1506 * Zero on success, errno on failure.
1507 */
1508int drm_mode_getconnector(struct drm_device *dev, void *data,
1509 struct drm_file *file_priv)
1510{
1511 struct drm_mode_get_connector *out_resp = data;
1512 struct drm_mode_object *obj;
1513 struct drm_connector *connector;
1514 struct drm_display_mode *mode;
1515 int mode_count = 0;
1516 int props_count = 0;
1517 int encoders_count = 0;
1518 int ret = 0;
1519 int copied = 0;
1520 int i;
1521 struct drm_mode_modeinfo u_mode;
1522 struct drm_mode_modeinfo __user *mode_ptr;
1523 uint32_t __user *prop_ptr;
1524 uint64_t __user *prop_values;
1525 uint32_t __user *encoder_ptr;
1526
Dave Airliefb3b06c2011-02-08 13:55:21 +10001527 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1528 return -EINVAL;
1529
Dave Airlief453ba02008-11-07 14:05:41 -08001530 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1531
Jerome Glisse94401062010-07-15 15:43:25 -04001532 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001533
Daniel Vetter84849902012-12-02 00:28:11 +01001534 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001535
1536 obj = drm_mode_object_find(dev, out_resp->connector_id,
1537 DRM_MODE_OBJECT_CONNECTOR);
1538 if (!obj) {
1539 ret = -EINVAL;
1540 goto out;
1541 }
1542 connector = obj_to_connector(obj);
1543
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001544 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001545
1546 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1547 if (connector->encoder_ids[i] != 0) {
1548 encoders_count++;
1549 }
1550 }
1551
1552 if (out_resp->count_modes == 0) {
1553 connector->funcs->fill_modes(connector,
1554 dev->mode_config.max_width,
1555 dev->mode_config.max_height);
1556 }
1557
1558 /* delayed so we get modes regardless of pre-fill_modes state */
1559 list_for_each_entry(mode, &connector->modes, head)
1560 mode_count++;
1561
1562 out_resp->connector_id = connector->base.id;
1563 out_resp->connector_type = connector->connector_type;
1564 out_resp->connector_type_id = connector->connector_type_id;
1565 out_resp->mm_width = connector->display_info.width_mm;
1566 out_resp->mm_height = connector->display_info.height_mm;
1567 out_resp->subpixel = connector->display_info.subpixel_order;
1568 out_resp->connection = connector->status;
1569 if (connector->encoder)
1570 out_resp->encoder_id = connector->encoder->base.id;
1571 else
1572 out_resp->encoder_id = 0;
1573
1574 /*
1575 * This ioctl is called twice, once to determine how much space is
1576 * needed, and the 2nd time to fill it.
1577 */
1578 if ((out_resp->count_modes >= mode_count) && mode_count) {
1579 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001580 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001581 list_for_each_entry(mode, &connector->modes, head) {
1582 drm_crtc_convert_to_umode(&u_mode, mode);
1583 if (copy_to_user(mode_ptr + copied,
1584 &u_mode, sizeof(u_mode))) {
1585 ret = -EFAULT;
1586 goto out;
1587 }
1588 copied++;
1589 }
1590 }
1591 out_resp->count_modes = mode_count;
1592
1593 if ((out_resp->count_props >= props_count) && props_count) {
1594 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001595 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1596 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001597 for (i = 0; i < connector->properties.count; i++) {
1598 if (put_user(connector->properties.ids[i],
1599 prop_ptr + copied)) {
1600 ret = -EFAULT;
1601 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001602 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001603
1604 if (put_user(connector->properties.values[i],
1605 prop_values + copied)) {
1606 ret = -EFAULT;
1607 goto out;
1608 }
1609 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001610 }
1611 }
1612 out_resp->count_props = props_count;
1613
1614 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1615 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001616 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001617 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1618 if (connector->encoder_ids[i] != 0) {
1619 if (put_user(connector->encoder_ids[i],
1620 encoder_ptr + copied)) {
1621 ret = -EFAULT;
1622 goto out;
1623 }
1624 copied++;
1625 }
1626 }
1627 }
1628 out_resp->count_encoders = encoders_count;
1629
1630out:
Daniel Vetter84849902012-12-02 00:28:11 +01001631 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001632 return ret;
1633}
1634
1635int drm_mode_getencoder(struct drm_device *dev, void *data,
1636 struct drm_file *file_priv)
1637{
1638 struct drm_mode_get_encoder *enc_resp = data;
1639 struct drm_mode_object *obj;
1640 struct drm_encoder *encoder;
1641 int ret = 0;
1642
Dave Airliefb3b06c2011-02-08 13:55:21 +10001643 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1644 return -EINVAL;
1645
Daniel Vetter84849902012-12-02 00:28:11 +01001646 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001647 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1648 DRM_MODE_OBJECT_ENCODER);
1649 if (!obj) {
1650 ret = -EINVAL;
1651 goto out;
1652 }
1653 encoder = obj_to_encoder(obj);
1654
1655 if (encoder->crtc)
1656 enc_resp->crtc_id = encoder->crtc->base.id;
1657 else
1658 enc_resp->crtc_id = 0;
1659 enc_resp->encoder_type = encoder->encoder_type;
1660 enc_resp->encoder_id = encoder->base.id;
1661 enc_resp->possible_crtcs = encoder->possible_crtcs;
1662 enc_resp->possible_clones = encoder->possible_clones;
1663
1664out:
Daniel Vetter84849902012-12-02 00:28:11 +01001665 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001666 return ret;
1667}
1668
1669/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001670 * drm_mode_getplane_res - get plane info
1671 * @dev: DRM device
1672 * @data: ioctl data
1673 * @file_priv: DRM file info
1674 *
1675 * Return an plane count and set of IDs.
1676 */
1677int drm_mode_getplane_res(struct drm_device *dev, void *data,
1678 struct drm_file *file_priv)
1679{
1680 struct drm_mode_get_plane_res *plane_resp = data;
1681 struct drm_mode_config *config;
1682 struct drm_plane *plane;
1683 uint32_t __user *plane_ptr;
1684 int copied = 0, ret = 0;
1685
1686 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1687 return -EINVAL;
1688
Daniel Vetter84849902012-12-02 00:28:11 +01001689 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001690 config = &dev->mode_config;
1691
1692 /*
1693 * This ioctl is called twice, once to determine how much space is
1694 * needed, and the 2nd time to fill it.
1695 */
1696 if (config->num_plane &&
1697 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001698 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001699
1700 list_for_each_entry(plane, &config->plane_list, head) {
1701 if (put_user(plane->base.id, plane_ptr + copied)) {
1702 ret = -EFAULT;
1703 goto out;
1704 }
1705 copied++;
1706 }
1707 }
1708 plane_resp->count_planes = config->num_plane;
1709
1710out:
Daniel Vetter84849902012-12-02 00:28:11 +01001711 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001712 return ret;
1713}
1714
1715/**
1716 * drm_mode_getplane - get plane info
1717 * @dev: DRM device
1718 * @data: ioctl data
1719 * @file_priv: DRM file info
1720 *
1721 * Return plane info, including formats supported, gamma size, any
1722 * current fb, etc.
1723 */
1724int drm_mode_getplane(struct drm_device *dev, void *data,
1725 struct drm_file *file_priv)
1726{
1727 struct drm_mode_get_plane *plane_resp = data;
1728 struct drm_mode_object *obj;
1729 struct drm_plane *plane;
1730 uint32_t __user *format_ptr;
1731 int ret = 0;
1732
1733 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1734 return -EINVAL;
1735
Daniel Vetter84849902012-12-02 00:28:11 +01001736 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001737 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1738 DRM_MODE_OBJECT_PLANE);
1739 if (!obj) {
1740 ret = -ENOENT;
1741 goto out;
1742 }
1743 plane = obj_to_plane(obj);
1744
1745 if (plane->crtc)
1746 plane_resp->crtc_id = plane->crtc->base.id;
1747 else
1748 plane_resp->crtc_id = 0;
1749
1750 if (plane->fb)
1751 plane_resp->fb_id = plane->fb->base.id;
1752 else
1753 plane_resp->fb_id = 0;
1754
1755 plane_resp->plane_id = plane->base.id;
1756 plane_resp->possible_crtcs = plane->possible_crtcs;
1757 plane_resp->gamma_size = plane->gamma_size;
1758
1759 /*
1760 * This ioctl is called twice, once to determine how much space is
1761 * needed, and the 2nd time to fill it.
1762 */
1763 if (plane->format_count &&
1764 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001765 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001766 if (copy_to_user(format_ptr,
1767 plane->format_types,
1768 sizeof(uint32_t) * plane->format_count)) {
1769 ret = -EFAULT;
1770 goto out;
1771 }
1772 }
1773 plane_resp->count_format_types = plane->format_count;
1774
1775out:
Daniel Vetter84849902012-12-02 00:28:11 +01001776 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001777 return ret;
1778}
1779
1780/**
1781 * drm_mode_setplane - set up or tear down an plane
1782 * @dev: DRM device
1783 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001784 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001785 *
1786 * Set plane info, including placement, fb, scaling, and other factors.
1787 * Or pass a NULL fb to disable.
1788 */
1789int drm_mode_setplane(struct drm_device *dev, void *data,
1790 struct drm_file *file_priv)
1791{
1792 struct drm_mode_set_plane *plane_req = data;
1793 struct drm_mode_object *obj;
1794 struct drm_plane *plane;
1795 struct drm_crtc *crtc;
1796 struct drm_framebuffer *fb;
1797 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001798 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001799 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001800
1801 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1802 return -EINVAL;
1803
Daniel Vetter84849902012-12-02 00:28:11 +01001804 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001805
1806 /*
1807 * First, find the plane, crtc, and fb objects. If not available,
1808 * we don't bother to call the driver.
1809 */
1810 obj = drm_mode_object_find(dev, plane_req->plane_id,
1811 DRM_MODE_OBJECT_PLANE);
1812 if (!obj) {
1813 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1814 plane_req->plane_id);
1815 ret = -ENOENT;
1816 goto out;
1817 }
1818 plane = obj_to_plane(obj);
1819
1820 /* No fb means shut it down */
1821 if (!plane_req->fb_id) {
1822 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001823 plane->crtc = NULL;
1824 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001825 goto out;
1826 }
1827
1828 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1829 DRM_MODE_OBJECT_CRTC);
1830 if (!obj) {
1831 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1832 plane_req->crtc_id);
1833 ret = -ENOENT;
1834 goto out;
1835 }
1836 crtc = obj_to_crtc(obj);
1837
Daniel Vetter786b99e2012-12-02 21:53:40 +01001838 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1839 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001840 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1841 plane_req->fb_id);
1842 ret = -ENOENT;
1843 goto out;
1844 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01001845 /* fb is protect by the mode_config lock, so drop the ref immediately */
1846 drm_framebuffer_unreference(fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001847
Ville Syrjälä62443be2011-12-20 00:06:47 +02001848 /* Check whether this plane supports the fb pixel format. */
1849 for (i = 0; i < plane->format_count; i++)
1850 if (fb->pixel_format == plane->format_types[i])
1851 break;
1852 if (i == plane->format_count) {
1853 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1854 ret = -EINVAL;
1855 goto out;
1856 }
1857
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001858 fb_width = fb->width << 16;
1859 fb_height = fb->height << 16;
1860
1861 /* Make sure source coordinates are inside the fb. */
1862 if (plane_req->src_w > fb_width ||
1863 plane_req->src_x > fb_width - plane_req->src_w ||
1864 plane_req->src_h > fb_height ||
1865 plane_req->src_y > fb_height - plane_req->src_h) {
1866 DRM_DEBUG_KMS("Invalid source coordinates "
1867 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1868 plane_req->src_w >> 16,
1869 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1870 plane_req->src_h >> 16,
1871 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1872 plane_req->src_x >> 16,
1873 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1874 plane_req->src_y >> 16,
1875 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1876 ret = -ENOSPC;
1877 goto out;
1878 }
1879
Ville Syrjälä687a0402011-12-20 00:06:45 +02001880 /* Give drivers some help against integer overflows */
1881 if (plane_req->crtc_w > INT_MAX ||
1882 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1883 plane_req->crtc_h > INT_MAX ||
1884 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1885 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1886 plane_req->crtc_w, plane_req->crtc_h,
1887 plane_req->crtc_x, plane_req->crtc_y);
1888 ret = -ERANGE;
1889 goto out;
1890 }
1891
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001892 ret = plane->funcs->update_plane(plane, crtc, fb,
1893 plane_req->crtc_x, plane_req->crtc_y,
1894 plane_req->crtc_w, plane_req->crtc_h,
1895 plane_req->src_x, plane_req->src_y,
1896 plane_req->src_w, plane_req->src_h);
1897 if (!ret) {
1898 plane->crtc = crtc;
1899 plane->fb = fb;
1900 }
1901
1902out:
Daniel Vetter84849902012-12-02 00:28:11 +01001903 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904
1905 return ret;
1906}
1907
1908/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001909 * drm_mode_set_config_internal - helper to call ->set_config
1910 * @set: modeset config to set
1911 *
1912 * This is a little helper to wrap internal calls to the ->set_config driver
1913 * interface. The only thing it adds is correct refcounting dance.
1914 */
1915int drm_mode_set_config_internal(struct drm_mode_set *set)
1916{
1917 struct drm_crtc *crtc = set->crtc;
1918
1919 return crtc->funcs->set_config(set);
1920}
1921EXPORT_SYMBOL(drm_mode_set_config_internal);
1922
1923/**
Dave Airlief453ba02008-11-07 14:05:41 -08001924 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001925 * @dev: drm device for the ioctl
1926 * @data: data pointer for the ioctl
1927 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001928 *
Dave Airlief453ba02008-11-07 14:05:41 -08001929 * Build a new CRTC configuration based on user request.
1930 *
1931 * Called by the user via ioctl.
1932 *
1933 * RETURNS:
1934 * Zero on success, errno on failure.
1935 */
1936int drm_mode_setcrtc(struct drm_device *dev, void *data,
1937 struct drm_file *file_priv)
1938{
1939 struct drm_mode_config *config = &dev->mode_config;
1940 struct drm_mode_crtc *crtc_req = data;
1941 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001942 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001943 struct drm_connector **connector_set = NULL, *connector;
1944 struct drm_framebuffer *fb = NULL;
1945 struct drm_display_mode *mode = NULL;
1946 struct drm_mode_set set;
1947 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001948 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001949 int i;
1950
Dave Airliefb3b06c2011-02-08 13:55:21 +10001951 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1952 return -EINVAL;
1953
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001954 /* For some reason crtc x/y offsets are signed internally. */
1955 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1956 return -ERANGE;
1957
Daniel Vetter84849902012-12-02 00:28:11 +01001958 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001959 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1960 DRM_MODE_OBJECT_CRTC);
1961 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001962 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001963 ret = -EINVAL;
1964 goto out;
1965 }
1966 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001967 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001968
1969 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001970 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001971 /* If we have a mode we need a framebuffer. */
1972 /* If we pass -1, set the mode with the currently bound fb */
1973 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001974 if (!crtc->fb) {
1975 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1976 ret = -EINVAL;
1977 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001978 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001979 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001980 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01001981 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1982 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001983 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1984 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001985 ret = -EINVAL;
1986 goto out;
1987 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01001988 /* fb is protect by the mode_config lock, so drop the
1989 * ref immediately */
1990 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 }
1992
1993 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001994 if (!mode) {
1995 ret = -ENOMEM;
1996 goto out;
1997 }
1998
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001999 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2000 if (ret) {
2001 DRM_DEBUG_KMS("Invalid mode\n");
2002 goto out;
2003 }
2004
Dave Airlief453ba02008-11-07 14:05:41 -08002005 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002006
Rob Clark7c80e122012-09-04 16:35:56 +00002007 hdisplay = mode->hdisplay;
2008 vdisplay = mode->vdisplay;
2009
2010 if (crtc->invert_dimensions)
2011 swap(hdisplay, vdisplay);
2012
2013 if (hdisplay > fb->width ||
2014 vdisplay > fb->height ||
2015 crtc_req->x > fb->width - hdisplay ||
2016 crtc_req->y > fb->height - vdisplay) {
2017 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2018 fb->width, fb->height,
2019 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2020 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002021 ret = -ENOSPC;
2022 goto out;
2023 }
Dave Airlief453ba02008-11-07 14:05:41 -08002024 }
2025
2026 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002027 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002028 ret = -EINVAL;
2029 goto out;
2030 }
2031
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002032 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002033 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002034 crtc_req->count_connectors);
2035 ret = -EINVAL;
2036 goto out;
2037 }
2038
2039 if (crtc_req->count_connectors > 0) {
2040 u32 out_id;
2041
2042 /* Avoid unbounded kernel memory allocation */
2043 if (crtc_req->count_connectors > config->num_connector) {
2044 ret = -EINVAL;
2045 goto out;
2046 }
2047
2048 connector_set = kmalloc(crtc_req->count_connectors *
2049 sizeof(struct drm_connector *),
2050 GFP_KERNEL);
2051 if (!connector_set) {
2052 ret = -ENOMEM;
2053 goto out;
2054 }
2055
2056 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002057 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002058 if (get_user(out_id, &set_connectors_ptr[i])) {
2059 ret = -EFAULT;
2060 goto out;
2061 }
2062
2063 obj = drm_mode_object_find(dev, out_id,
2064 DRM_MODE_OBJECT_CONNECTOR);
2065 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002066 DRM_DEBUG_KMS("Connector id %d unknown\n",
2067 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002068 ret = -EINVAL;
2069 goto out;
2070 }
2071 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002072 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2073 connector->base.id,
2074 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002075
2076 connector_set[i] = connector;
2077 }
2078 }
2079
2080 set.crtc = crtc;
2081 set.x = crtc_req->x;
2082 set.y = crtc_req->y;
2083 set.mode = mode;
2084 set.connectors = connector_set;
2085 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002086 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002087 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002088
2089out:
2090 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002091 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002092 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002093 return ret;
2094}
2095
2096int drm_mode_cursor_ioctl(struct drm_device *dev,
2097 void *data, struct drm_file *file_priv)
2098{
2099 struct drm_mode_cursor *req = data;
2100 struct drm_mode_object *obj;
2101 struct drm_crtc *crtc;
2102 int ret = 0;
2103
Dave Airliefb3b06c2011-02-08 13:55:21 +10002104 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2105 return -EINVAL;
2106
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002107 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002108 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002109
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002110 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002111 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002112 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002113 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002114 }
2115 crtc = obj_to_crtc(obj);
2116
Daniel Vetterdac35662012-12-02 15:24:10 +01002117 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002118 if (req->flags & DRM_MODE_CURSOR_BO) {
2119 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002120 ret = -ENXIO;
2121 goto out;
2122 }
2123 /* Turns off the cursor if handle is 0 */
2124 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2125 req->width, req->height);
2126 }
2127
2128 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2129 if (crtc->funcs->cursor_move) {
2130 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2131 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002132 ret = -EFAULT;
2133 goto out;
2134 }
2135 }
2136out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002137 mutex_unlock(&crtc->mutex);
2138
Dave Airlief453ba02008-11-07 14:05:41 -08002139 return ret;
2140}
2141
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002142/* Original addfb only supported RGB formats, so figure out which one */
2143uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2144{
2145 uint32_t fmt;
2146
2147 switch (bpp) {
2148 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002149 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002150 break;
2151 case 16:
2152 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002153 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002154 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002155 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002156 break;
2157 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002158 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002159 break;
2160 case 32:
2161 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002162 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002163 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002164 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002165 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002166 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002167 break;
2168 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002169 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2170 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002171 break;
2172 }
2173
2174 return fmt;
2175}
2176EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2177
Dave Airlief453ba02008-11-07 14:05:41 -08002178/**
2179 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002180 * @dev: drm device for the ioctl
2181 * @data: data pointer for the ioctl
2182 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002183 *
Dave Airlief453ba02008-11-07 14:05:41 -08002184 * Add a new FB to the specified CRTC, given a user request.
2185 *
2186 * Called by the user via ioctl.
2187 *
2188 * RETURNS:
2189 * Zero on success, errno on failure.
2190 */
2191int drm_mode_addfb(struct drm_device *dev,
2192 void *data, struct drm_file *file_priv)
2193{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002194 struct drm_mode_fb_cmd *or = data;
2195 struct drm_mode_fb_cmd2 r = {};
2196 struct drm_mode_config *config = &dev->mode_config;
2197 struct drm_framebuffer *fb;
2198 int ret = 0;
2199
2200 /* Use new struct with format internally */
2201 r.fb_id = or->fb_id;
2202 r.width = or->width;
2203 r.height = or->height;
2204 r.pitches[0] = or->pitch;
2205 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2206 r.handles[0] = or->handle;
2207
2208 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2209 return -EINVAL;
2210
Jesse Barnesacb4b992011-11-07 12:03:23 -08002211 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002212 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002213
2214 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002215 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002216
Daniel Vetter84849902012-12-02 00:28:11 +01002217 drm_modeset_lock_all(dev);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002218
2219 /* TODO check buffer is sufficiently large */
2220 /* TODO setup destructor callback */
2221
2222 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2223 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002224 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002225 drm_modeset_unlock_all(dev);
2226 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002227 }
2228
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002229 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002230 or->fb_id = fb->base.id;
2231 list_add(&fb->filp_head, &file_priv->fbs);
2232 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002233 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002234 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002235
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002236 return ret;
2237}
2238
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002239static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002240{
2241 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2242
2243 switch (format) {
2244 case DRM_FORMAT_C8:
2245 case DRM_FORMAT_RGB332:
2246 case DRM_FORMAT_BGR233:
2247 case DRM_FORMAT_XRGB4444:
2248 case DRM_FORMAT_XBGR4444:
2249 case DRM_FORMAT_RGBX4444:
2250 case DRM_FORMAT_BGRX4444:
2251 case DRM_FORMAT_ARGB4444:
2252 case DRM_FORMAT_ABGR4444:
2253 case DRM_FORMAT_RGBA4444:
2254 case DRM_FORMAT_BGRA4444:
2255 case DRM_FORMAT_XRGB1555:
2256 case DRM_FORMAT_XBGR1555:
2257 case DRM_FORMAT_RGBX5551:
2258 case DRM_FORMAT_BGRX5551:
2259 case DRM_FORMAT_ARGB1555:
2260 case DRM_FORMAT_ABGR1555:
2261 case DRM_FORMAT_RGBA5551:
2262 case DRM_FORMAT_BGRA5551:
2263 case DRM_FORMAT_RGB565:
2264 case DRM_FORMAT_BGR565:
2265 case DRM_FORMAT_RGB888:
2266 case DRM_FORMAT_BGR888:
2267 case DRM_FORMAT_XRGB8888:
2268 case DRM_FORMAT_XBGR8888:
2269 case DRM_FORMAT_RGBX8888:
2270 case DRM_FORMAT_BGRX8888:
2271 case DRM_FORMAT_ARGB8888:
2272 case DRM_FORMAT_ABGR8888:
2273 case DRM_FORMAT_RGBA8888:
2274 case DRM_FORMAT_BGRA8888:
2275 case DRM_FORMAT_XRGB2101010:
2276 case DRM_FORMAT_XBGR2101010:
2277 case DRM_FORMAT_RGBX1010102:
2278 case DRM_FORMAT_BGRX1010102:
2279 case DRM_FORMAT_ARGB2101010:
2280 case DRM_FORMAT_ABGR2101010:
2281 case DRM_FORMAT_RGBA1010102:
2282 case DRM_FORMAT_BGRA1010102:
2283 case DRM_FORMAT_YUYV:
2284 case DRM_FORMAT_YVYU:
2285 case DRM_FORMAT_UYVY:
2286 case DRM_FORMAT_VYUY:
2287 case DRM_FORMAT_AYUV:
2288 case DRM_FORMAT_NV12:
2289 case DRM_FORMAT_NV21:
2290 case DRM_FORMAT_NV16:
2291 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002292 case DRM_FORMAT_NV24:
2293 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002294 case DRM_FORMAT_YUV410:
2295 case DRM_FORMAT_YVU410:
2296 case DRM_FORMAT_YUV411:
2297 case DRM_FORMAT_YVU411:
2298 case DRM_FORMAT_YUV420:
2299 case DRM_FORMAT_YVU420:
2300 case DRM_FORMAT_YUV422:
2301 case DRM_FORMAT_YVU422:
2302 case DRM_FORMAT_YUV444:
2303 case DRM_FORMAT_YVU444:
2304 return 0;
2305 default:
2306 return -EINVAL;
2307 }
2308}
2309
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002310static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002311{
2312 int ret, hsub, vsub, num_planes, i;
2313
2314 ret = format_check(r);
2315 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002316 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002317 return ret;
2318 }
2319
2320 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2321 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2322 num_planes = drm_format_num_planes(r->pixel_format);
2323
2324 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002325 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002326 return -EINVAL;
2327 }
2328
2329 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002330 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002331 return -EINVAL;
2332 }
2333
2334 for (i = 0; i < num_planes; i++) {
2335 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002336 unsigned int height = r->height / (i != 0 ? vsub : 1);
2337 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002338
2339 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002340 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002341 return -EINVAL;
2342 }
2343
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002344 if ((uint64_t) width * cpp > UINT_MAX)
2345 return -ERANGE;
2346
2347 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2348 return -ERANGE;
2349
2350 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002351 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002352 return -EINVAL;
2353 }
2354 }
2355
2356 return 0;
2357}
2358
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002359/**
2360 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002361 * @dev: drm device for the ioctl
2362 * @data: data pointer for the ioctl
2363 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002364 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002365 * Add a new FB to the specified CRTC, given a user request with format.
2366 *
2367 * Called by the user via ioctl.
2368 *
2369 * RETURNS:
2370 * Zero on success, errno on failure.
2371 */
2372int drm_mode_addfb2(struct drm_device *dev,
2373 void *data, struct drm_file *file_priv)
2374{
2375 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002376 struct drm_mode_config *config = &dev->mode_config;
2377 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002378 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002379
Dave Airliefb3b06c2011-02-08 13:55:21 +10002380 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2381 return -EINVAL;
2382
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002383 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2384 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2385 return -EINVAL;
2386 }
2387
Dave Airlief453ba02008-11-07 14:05:41 -08002388 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002389 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002390 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002391 return -EINVAL;
2392 }
2393 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002394 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002395 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002396 return -EINVAL;
2397 }
2398
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002399 ret = framebuffer_check(r);
2400 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002401 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002402
Daniel Vetter84849902012-12-02 00:28:11 +01002403 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002404
Dave Airlief453ba02008-11-07 14:05:41 -08002405 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002406 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002407 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002408 drm_modeset_unlock_all(dev);
2409 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002410 }
2411
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002412 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002413 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002414 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002415 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002416 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002417
Daniel Vetter84849902012-12-02 00:28:11 +01002418 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002419
Dave Airlief453ba02008-11-07 14:05:41 -08002420 return ret;
2421}
2422
2423/**
2424 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002425 * @dev: drm device for the ioctl
2426 * @data: data pointer for the ioctl
2427 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002428 *
Dave Airlief453ba02008-11-07 14:05:41 -08002429 * Remove the FB specified by the user.
2430 *
2431 * Called by the user via ioctl.
2432 *
2433 * RETURNS:
2434 * Zero on success, errno on failure.
2435 */
2436int drm_mode_rmfb(struct drm_device *dev,
2437 void *data, struct drm_file *file_priv)
2438{
Dave Airlief453ba02008-11-07 14:05:41 -08002439 struct drm_framebuffer *fb = NULL;
2440 struct drm_framebuffer *fbl = NULL;
2441 uint32_t *id = data;
2442 int ret = 0;
2443 int found = 0;
2444
Dave Airliefb3b06c2011-02-08 13:55:21 +10002445 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2446 return -EINVAL;
2447
Daniel Vetter84849902012-12-02 00:28:11 +01002448 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002449 fb = drm_framebuffer_lookup(dev, *id);
2450 if (!fb) {
Dave Airlief453ba02008-11-07 14:05:41 -08002451 ret = -EINVAL;
2452 goto out;
2453 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002454 /* fb is protect by the mode_config lock, so drop the ref immediately */
2455 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002456
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002457 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002458 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2459 if (fb == fbl)
2460 found = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002461 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002462 ret = -EINVAL;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002463 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002464 goto out;
2465 }
2466
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002467 list_del_init(&fb->filp_head);
2468 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002469
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002470 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002471out:
Daniel Vetter84849902012-12-02 00:28:11 +01002472 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002473
Dave Airlief453ba02008-11-07 14:05:41 -08002474 return ret;
2475}
2476
2477/**
2478 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002479 * @dev: drm device for the ioctl
2480 * @data: data pointer for the ioctl
2481 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002482 *
Dave Airlief453ba02008-11-07 14:05:41 -08002483 * Lookup the FB given its ID and return info about it.
2484 *
2485 * Called by the user via ioctl.
2486 *
2487 * RETURNS:
2488 * Zero on success, errno on failure.
2489 */
2490int drm_mode_getfb(struct drm_device *dev,
2491 void *data, struct drm_file *file_priv)
2492{
2493 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002494 struct drm_framebuffer *fb;
2495 int ret = 0;
2496
Dave Airliefb3b06c2011-02-08 13:55:21 +10002497 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2498 return -EINVAL;
2499
Daniel Vetter84849902012-12-02 00:28:11 +01002500 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002501 fb = drm_framebuffer_lookup(dev, r->fb_id);
2502 if (!fb) {
Dave Airlief453ba02008-11-07 14:05:41 -08002503 ret = -EINVAL;
2504 goto out;
2505 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002506 /* fb is protect by the mode_config lock, so drop the ref immediately */
2507 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002508
2509 r->height = fb->height;
2510 r->width = fb->width;
2511 r->depth = fb->depth;
2512 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002513 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002514 if (fb->funcs->create_handle)
2515 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2516 else
2517 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002518
2519out:
Daniel Vetter84849902012-12-02 00:28:11 +01002520 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002521 return ret;
2522}
2523
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002524int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2525 void *data, struct drm_file *file_priv)
2526{
2527 struct drm_clip_rect __user *clips_ptr;
2528 struct drm_clip_rect *clips = NULL;
2529 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002530 struct drm_framebuffer *fb;
2531 unsigned flags;
2532 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002533 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002534
Dave Airliefb3b06c2011-02-08 13:55:21 +10002535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2536 return -EINVAL;
2537
Daniel Vetter84849902012-12-02 00:28:11 +01002538 drm_modeset_lock_all(dev);
Daniel Vetter786b99e2012-12-02 21:53:40 +01002539 fb = drm_framebuffer_lookup(dev, r->fb_id);
2540 if (!fb) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002541 ret = -EINVAL;
2542 goto out_err1;
2543 }
Daniel Vetter786b99e2012-12-02 21:53:40 +01002544 /* fb is protect by the mode_config lock, so drop the ref immediately */
2545 drm_framebuffer_unreference(fb);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002546
2547 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002548 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002549
2550 if (!num_clips != !clips_ptr) {
2551 ret = -EINVAL;
2552 goto out_err1;
2553 }
2554
2555 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2556
2557 /* If userspace annotates copy, clips must come in pairs */
2558 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2559 ret = -EINVAL;
2560 goto out_err1;
2561 }
2562
2563 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002564 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2565 ret = -EINVAL;
2566 goto out_err1;
2567 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002568 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2569 if (!clips) {
2570 ret = -ENOMEM;
2571 goto out_err1;
2572 }
2573
2574 ret = copy_from_user(clips, clips_ptr,
2575 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002576 if (ret) {
2577 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002578 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002579 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002580 }
2581
2582 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002583 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2584 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002585 } else {
2586 ret = -ENOSYS;
2587 goto out_err2;
2588 }
2589
2590out_err2:
2591 kfree(clips);
2592out_err1:
Daniel Vetter84849902012-12-02 00:28:11 +01002593 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002594 return ret;
2595}
2596
2597
Dave Airlief453ba02008-11-07 14:05:41 -08002598/**
2599 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002600 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002601 *
Dave Airlief453ba02008-11-07 14:05:41 -08002602 * Destroy all the FBs associated with @filp.
2603 *
2604 * Called by the user via ioctl.
2605 *
2606 * RETURNS:
2607 * Zero on success, errno on failure.
2608 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002609void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002610{
Dave Airlief453ba02008-11-07 14:05:41 -08002611 struct drm_device *dev = priv->minor->dev;
2612 struct drm_framebuffer *fb, *tfb;
2613
Daniel Vetter84849902012-12-02 00:28:11 +01002614 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002615 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002616 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002617 list_del_init(&fb->filp_head);
Rob Clarkf7eff602012-09-05 21:48:38 +00002618 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002619 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002620 mutex_unlock(&priv->fbs_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01002621 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002622}
2623
2624/**
2625 * drm_mode_attachmode - add a mode to the user mode list
2626 * @dev: DRM device
2627 * @connector: connector to add the mode to
2628 * @mode: mode to add
2629 *
2630 * Add @mode to @connector's user mode list.
2631 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002632static void drm_mode_attachmode(struct drm_device *dev,
2633 struct drm_connector *connector,
2634 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002635{
Dave Airlief453ba02008-11-07 14:05:41 -08002636 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002637}
2638
2639int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002640 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002641{
2642 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002643 int ret = 0;
2644 struct drm_display_mode *dup_mode, *next;
2645 LIST_HEAD(list);
2646
Dave Airlief453ba02008-11-07 14:05:41 -08002647 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2648 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002649 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002650 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002651 dup_mode = drm_mode_duplicate(dev, mode);
2652 if (!dup_mode) {
2653 ret = -ENOMEM;
2654 goto out;
2655 }
2656 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002657 }
2658 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002659
2660 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2661 if (!connector->encoder)
2662 continue;
2663 if (connector->encoder->crtc == crtc)
2664 list_move_tail(list.next, &connector->user_modes);
2665 }
2666
2667 WARN_ON(!list_empty(&list));
2668
2669 out:
2670 list_for_each_entry_safe(dup_mode, next, &list, head)
2671 drm_mode_destroy(dev, dup_mode);
2672
2673 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002674}
2675EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2676
2677static int drm_mode_detachmode(struct drm_device *dev,
2678 struct drm_connector *connector,
2679 struct drm_display_mode *mode)
2680{
2681 int found = 0;
2682 int ret = 0;
2683 struct drm_display_mode *match_mode, *t;
2684
2685 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2686 if (drm_mode_equal(match_mode, mode)) {
2687 list_del(&match_mode->head);
2688 drm_mode_destroy(dev, match_mode);
2689 found = 1;
2690 break;
2691 }
2692 }
2693
2694 if (!found)
2695 ret = -EINVAL;
2696
2697 return ret;
2698}
2699
2700int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2701{
2702 struct drm_connector *connector;
2703
2704 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2705 drm_mode_detachmode(dev, connector, mode);
2706 }
2707 return 0;
2708}
2709EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2710
2711/**
2712 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002713 * @dev: drm device for the ioctl
2714 * @data: data pointer for the ioctl
2715 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002716 *
2717 * This attaches a user specified mode to an connector.
2718 * Called by the user via ioctl.
2719 *
2720 * RETURNS:
2721 * Zero on success, errno on failure.
2722 */
2723int drm_mode_attachmode_ioctl(struct drm_device *dev,
2724 void *data, struct drm_file *file_priv)
2725{
2726 struct drm_mode_mode_cmd *mode_cmd = data;
2727 struct drm_connector *connector;
2728 struct drm_display_mode *mode;
2729 struct drm_mode_object *obj;
2730 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002731 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002732
Dave Airliefb3b06c2011-02-08 13:55:21 +10002733 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2734 return -EINVAL;
2735
Daniel Vetter84849902012-12-02 00:28:11 +01002736 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002737
2738 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2739 if (!obj) {
2740 ret = -EINVAL;
2741 goto out;
2742 }
2743 connector = obj_to_connector(obj);
2744
2745 mode = drm_mode_create(dev);
2746 if (!mode) {
2747 ret = -ENOMEM;
2748 goto out;
2749 }
2750
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002751 ret = drm_crtc_convert_umode(mode, umode);
2752 if (ret) {
2753 DRM_DEBUG_KMS("Invalid mode\n");
2754 drm_mode_destroy(dev, mode);
2755 goto out;
2756 }
Dave Airlief453ba02008-11-07 14:05:41 -08002757
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002758 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002759out:
Daniel Vetter84849902012-12-02 00:28:11 +01002760 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002761 return ret;
2762}
2763
2764
2765/**
2766 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002767 * @dev: drm device for the ioctl
2768 * @data: data pointer for the ioctl
2769 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002770 *
2771 * Called by the user via ioctl.
2772 *
2773 * RETURNS:
2774 * Zero on success, errno on failure.
2775 */
2776int drm_mode_detachmode_ioctl(struct drm_device *dev,
2777 void *data, struct drm_file *file_priv)
2778{
2779 struct drm_mode_object *obj;
2780 struct drm_mode_mode_cmd *mode_cmd = data;
2781 struct drm_connector *connector;
2782 struct drm_display_mode mode;
2783 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002784 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002785
Dave Airliefb3b06c2011-02-08 13:55:21 +10002786 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2787 return -EINVAL;
2788
Daniel Vetter84849902012-12-02 00:28:11 +01002789 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002790
2791 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2792 if (!obj) {
2793 ret = -EINVAL;
2794 goto out;
2795 }
2796 connector = obj_to_connector(obj);
2797
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002798 ret = drm_crtc_convert_umode(&mode, umode);
2799 if (ret) {
2800 DRM_DEBUG_KMS("Invalid mode\n");
2801 goto out;
2802 }
2803
Dave Airlief453ba02008-11-07 14:05:41 -08002804 ret = drm_mode_detachmode(dev, connector, &mode);
2805out:
Daniel Vetter84849902012-12-02 00:28:11 +01002806 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002807 return ret;
2808}
2809
2810struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2811 const char *name, int num_values)
2812{
2813 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002814 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002815
2816 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2817 if (!property)
2818 return NULL;
2819
2820 if (num_values) {
2821 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2822 if (!property->values)
2823 goto fail;
2824 }
2825
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002826 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2827 if (ret)
2828 goto fail;
2829
Dave Airlief453ba02008-11-07 14:05:41 -08002830 property->flags = flags;
2831 property->num_values = num_values;
2832 INIT_LIST_HEAD(&property->enum_blob_list);
2833
Vinson Lee471dd2e2011-11-10 11:55:40 -08002834 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002835 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002836 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2837 }
Dave Airlief453ba02008-11-07 14:05:41 -08002838
2839 list_add_tail(&property->head, &dev->mode_config.property_list);
2840 return property;
2841fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002842 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002843 kfree(property);
2844 return NULL;
2845}
2846EXPORT_SYMBOL(drm_property_create);
2847
Sascha Hauer4a67d392012-02-06 10:58:17 +01002848struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2849 const char *name,
2850 const struct drm_prop_enum_list *props,
2851 int num_values)
2852{
2853 struct drm_property *property;
2854 int i, ret;
2855
2856 flags |= DRM_MODE_PROP_ENUM;
2857
2858 property = drm_property_create(dev, flags, name, num_values);
2859 if (!property)
2860 return NULL;
2861
2862 for (i = 0; i < num_values; i++) {
2863 ret = drm_property_add_enum(property, i,
2864 props[i].type,
2865 props[i].name);
2866 if (ret) {
2867 drm_property_destroy(dev, property);
2868 return NULL;
2869 }
2870 }
2871
2872 return property;
2873}
2874EXPORT_SYMBOL(drm_property_create_enum);
2875
Rob Clark49e27542012-05-17 02:23:26 -06002876struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2877 int flags, const char *name,
2878 const struct drm_prop_enum_list *props,
2879 int num_values)
2880{
2881 struct drm_property *property;
2882 int i, ret;
2883
2884 flags |= DRM_MODE_PROP_BITMASK;
2885
2886 property = drm_property_create(dev, flags, name, num_values);
2887 if (!property)
2888 return NULL;
2889
2890 for (i = 0; i < num_values; i++) {
2891 ret = drm_property_add_enum(property, i,
2892 props[i].type,
2893 props[i].name);
2894 if (ret) {
2895 drm_property_destroy(dev, property);
2896 return NULL;
2897 }
2898 }
2899
2900 return property;
2901}
2902EXPORT_SYMBOL(drm_property_create_bitmask);
2903
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002904struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2905 const char *name,
2906 uint64_t min, uint64_t max)
2907{
2908 struct drm_property *property;
2909
2910 flags |= DRM_MODE_PROP_RANGE;
2911
2912 property = drm_property_create(dev, flags, name, 2);
2913 if (!property)
2914 return NULL;
2915
2916 property->values[0] = min;
2917 property->values[1] = max;
2918
2919 return property;
2920}
2921EXPORT_SYMBOL(drm_property_create_range);
2922
Dave Airlief453ba02008-11-07 14:05:41 -08002923int drm_property_add_enum(struct drm_property *property, int index,
2924 uint64_t value, const char *name)
2925{
2926 struct drm_property_enum *prop_enum;
2927
Rob Clark49e27542012-05-17 02:23:26 -06002928 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2929 return -EINVAL;
2930
2931 /*
2932 * Bitmask enum properties have the additional constraint of values
2933 * from 0 to 63
2934 */
2935 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002936 return -EINVAL;
2937
2938 if (!list_empty(&property->enum_blob_list)) {
2939 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2940 if (prop_enum->value == value) {
2941 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2942 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2943 return 0;
2944 }
2945 }
2946 }
2947
2948 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2949 if (!prop_enum)
2950 return -ENOMEM;
2951
2952 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2953 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2954 prop_enum->value = value;
2955
2956 property->values[index] = value;
2957 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2958 return 0;
2959}
2960EXPORT_SYMBOL(drm_property_add_enum);
2961
2962void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2963{
2964 struct drm_property_enum *prop_enum, *pt;
2965
2966 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2967 list_del(&prop_enum->head);
2968 kfree(prop_enum);
2969 }
2970
2971 if (property->num_values)
2972 kfree(property->values);
2973 drm_mode_object_put(dev, &property->base);
2974 list_del(&property->head);
2975 kfree(property);
2976}
2977EXPORT_SYMBOL(drm_property_destroy);
2978
Paulo Zanonic5431882012-05-15 18:09:02 -03002979void drm_object_attach_property(struct drm_mode_object *obj,
2980 struct drm_property *property,
2981 uint64_t init_val)
2982{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002983 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002984
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002985 if (count == DRM_OBJECT_MAX_PROPERTY) {
2986 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2987 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2988 "you see this message on the same object type.\n",
2989 obj->type);
2990 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002991 }
2992
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002993 obj->properties->ids[count] = property->base.id;
2994 obj->properties->values[count] = init_val;
2995 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002996}
2997EXPORT_SYMBOL(drm_object_attach_property);
2998
2999int drm_object_property_set_value(struct drm_mode_object *obj,
3000 struct drm_property *property, uint64_t val)
3001{
3002 int i;
3003
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003004 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003005 if (obj->properties->ids[i] == property->base.id) {
3006 obj->properties->values[i] = val;
3007 return 0;
3008 }
3009 }
3010
3011 return -EINVAL;
3012}
3013EXPORT_SYMBOL(drm_object_property_set_value);
3014
3015int drm_object_property_get_value(struct drm_mode_object *obj,
3016 struct drm_property *property, uint64_t *val)
3017{
3018 int i;
3019
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003020 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003021 if (obj->properties->ids[i] == property->base.id) {
3022 *val = obj->properties->values[i];
3023 return 0;
3024 }
3025 }
3026
3027 return -EINVAL;
3028}
3029EXPORT_SYMBOL(drm_object_property_get_value);
3030
Dave Airlief453ba02008-11-07 14:05:41 -08003031int drm_mode_getproperty_ioctl(struct drm_device *dev,
3032 void *data, struct drm_file *file_priv)
3033{
3034 struct drm_mode_object *obj;
3035 struct drm_mode_get_property *out_resp = data;
3036 struct drm_property *property;
3037 int enum_count = 0;
3038 int blob_count = 0;
3039 int value_count = 0;
3040 int ret = 0, i;
3041 int copied;
3042 struct drm_property_enum *prop_enum;
3043 struct drm_mode_property_enum __user *enum_ptr;
3044 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003045 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003046 uint64_t __user *values_ptr;
3047 uint32_t __user *blob_length_ptr;
3048
Dave Airliefb3b06c2011-02-08 13:55:21 +10003049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3050 return -EINVAL;
3051
Daniel Vetter84849902012-12-02 00:28:11 +01003052 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003053 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3054 if (!obj) {
3055 ret = -EINVAL;
3056 goto done;
3057 }
3058 property = obj_to_property(obj);
3059
Rob Clark49e27542012-05-17 02:23:26 -06003060 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003061 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3062 enum_count++;
3063 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3064 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3065 blob_count++;
3066 }
3067
3068 value_count = property->num_values;
3069
3070 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3071 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3072 out_resp->flags = property->flags;
3073
3074 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003075 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003076 for (i = 0; i < value_count; i++) {
3077 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3078 ret = -EFAULT;
3079 goto done;
3080 }
3081 }
3082 }
3083 out_resp->count_values = value_count;
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 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3087 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003088 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003089 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3090
3091 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3092 ret = -EFAULT;
3093 goto done;
3094 }
3095
3096 if (copy_to_user(&enum_ptr[copied].name,
3097 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3098 ret = -EFAULT;
3099 goto done;
3100 }
3101 copied++;
3102 }
3103 }
3104 out_resp->count_enum_blobs = enum_count;
3105 }
3106
3107 if (property->flags & DRM_MODE_PROP_BLOB) {
3108 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3109 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003110 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3111 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003112
3113 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3114 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3115 ret = -EFAULT;
3116 goto done;
3117 }
3118
3119 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3120 ret = -EFAULT;
3121 goto done;
3122 }
3123
3124 copied++;
3125 }
3126 }
3127 out_resp->count_enum_blobs = blob_count;
3128 }
3129done:
Daniel Vetter84849902012-12-02 00:28:11 +01003130 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003131 return ret;
3132}
3133
3134static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3135 void *data)
3136{
3137 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003138 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003139
3140 if (!length || !data)
3141 return NULL;
3142
3143 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3144 if (!blob)
3145 return NULL;
3146
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003147 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3148 if (ret) {
3149 kfree(blob);
3150 return NULL;
3151 }
3152
Dave Airlief453ba02008-11-07 14:05:41 -08003153 blob->length = length;
3154
3155 memcpy(blob->data, data, length);
3156
Dave Airlief453ba02008-11-07 14:05:41 -08003157 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3158 return blob;
3159}
3160
3161static void drm_property_destroy_blob(struct drm_device *dev,
3162 struct drm_property_blob *blob)
3163{
3164 drm_mode_object_put(dev, &blob->base);
3165 list_del(&blob->head);
3166 kfree(blob);
3167}
3168
3169int drm_mode_getblob_ioctl(struct drm_device *dev,
3170 void *data, struct drm_file *file_priv)
3171{
3172 struct drm_mode_object *obj;
3173 struct drm_mode_get_blob *out_resp = data;
3174 struct drm_property_blob *blob;
3175 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003176 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003177
Dave Airliefb3b06c2011-02-08 13:55:21 +10003178 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3179 return -EINVAL;
3180
Daniel Vetter84849902012-12-02 00:28:11 +01003181 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003182 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3183 if (!obj) {
3184 ret = -EINVAL;
3185 goto done;
3186 }
3187 blob = obj_to_blob(obj);
3188
3189 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003190 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003191 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3192 ret = -EFAULT;
3193 goto done;
3194 }
3195 }
3196 out_resp->length = blob->length;
3197
3198done:
Daniel Vetter84849902012-12-02 00:28:11 +01003199 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003200 return ret;
3201}
3202
3203int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3204 struct edid *edid)
3205{
3206 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003207 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003208
3209 if (connector->edid_blob_ptr)
3210 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3211
3212 /* Delete edid, when there is none. */
3213 if (!edid) {
3214 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003215 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003216 return ret;
3217 }
3218
Adam Jackson7466f4c2010-03-29 21:43:23 +00003219 size = EDID_LENGTH * (1 + edid->extensions);
3220 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3221 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003222 if (!connector->edid_blob_ptr)
3223 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003224
Rob Clark58495562012-10-11 20:50:56 -05003225 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003226 dev->mode_config.edid_property,
3227 connector->edid_blob_ptr->base.id);
3228
3229 return ret;
3230}
3231EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3232
Paulo Zanoni26a34812012-05-15 18:08:59 -03003233static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003234 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003235{
3236 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3237 return false;
3238 if (property->flags & DRM_MODE_PROP_RANGE) {
3239 if (value < property->values[0] || value > property->values[1])
3240 return false;
3241 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003242 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3243 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003244 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003245 for (i = 0; i < property->num_values; i++)
3246 valid_mask |= (1ULL << property->values[i]);
3247 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003248 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3249 /* Only the driver knows */
3250 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003251 } else {
3252 int i;
3253 for (i = 0; i < property->num_values; i++)
3254 if (property->values[i] == value)
3255 return true;
3256 return false;
3257 }
3258}
3259
Dave Airlief453ba02008-11-07 14:05:41 -08003260int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3261 void *data, struct drm_file *file_priv)
3262{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003263 struct drm_mode_connector_set_property *conn_set_prop = data;
3264 struct drm_mode_obj_set_property obj_set_prop = {
3265 .value = conn_set_prop->value,
3266 .prop_id = conn_set_prop->prop_id,
3267 .obj_id = conn_set_prop->connector_id,
3268 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3269 };
Dave Airlief453ba02008-11-07 14:05:41 -08003270
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003271 /* It does all the locking and checking we need */
3272 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003273}
3274
Paulo Zanonic5431882012-05-15 18:09:02 -03003275static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3276 struct drm_property *property,
3277 uint64_t value)
3278{
3279 int ret = -EINVAL;
3280 struct drm_connector *connector = obj_to_connector(obj);
3281
3282 /* Do DPMS ourselves */
3283 if (property == connector->dev->mode_config.dpms_property) {
3284 if (connector->funcs->dpms)
3285 (*connector->funcs->dpms)(connector, (int)value);
3286 ret = 0;
3287 } else if (connector->funcs->set_property)
3288 ret = connector->funcs->set_property(connector, property, value);
3289
3290 /* store the property value if successful */
3291 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003292 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003293 return ret;
3294}
3295
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003296static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3297 struct drm_property *property,
3298 uint64_t value)
3299{
3300 int ret = -EINVAL;
3301 struct drm_crtc *crtc = obj_to_crtc(obj);
3302
3303 if (crtc->funcs->set_property)
3304 ret = crtc->funcs->set_property(crtc, property, value);
3305 if (!ret)
3306 drm_object_property_set_value(obj, property, value);
3307
3308 return ret;
3309}
3310
Rob Clark4d939142012-05-17 02:23:27 -06003311static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3312 struct drm_property *property,
3313 uint64_t value)
3314{
3315 int ret = -EINVAL;
3316 struct drm_plane *plane = obj_to_plane(obj);
3317
3318 if (plane->funcs->set_property)
3319 ret = plane->funcs->set_property(plane, property, value);
3320 if (!ret)
3321 drm_object_property_set_value(obj, property, value);
3322
3323 return ret;
3324}
3325
Paulo Zanonic5431882012-05-15 18:09:02 -03003326int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3327 struct drm_file *file_priv)
3328{
3329 struct drm_mode_obj_get_properties *arg = data;
3330 struct drm_mode_object *obj;
3331 int ret = 0;
3332 int i;
3333 int copied = 0;
3334 int props_count = 0;
3335 uint32_t __user *props_ptr;
3336 uint64_t __user *prop_values_ptr;
3337
3338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3339 return -EINVAL;
3340
Daniel Vetter84849902012-12-02 00:28:11 +01003341 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003342
3343 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3344 if (!obj) {
3345 ret = -EINVAL;
3346 goto out;
3347 }
3348 if (!obj->properties) {
3349 ret = -EINVAL;
3350 goto out;
3351 }
3352
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003353 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003354
3355 /* This ioctl is called twice, once to determine how much space is
3356 * needed, and the 2nd time to fill it. */
3357 if ((arg->count_props >= props_count) && props_count) {
3358 copied = 0;
3359 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3360 prop_values_ptr = (uint64_t __user *)(unsigned long)
3361 (arg->prop_values_ptr);
3362 for (i = 0; i < props_count; i++) {
3363 if (put_user(obj->properties->ids[i],
3364 props_ptr + copied)) {
3365 ret = -EFAULT;
3366 goto out;
3367 }
3368 if (put_user(obj->properties->values[i],
3369 prop_values_ptr + copied)) {
3370 ret = -EFAULT;
3371 goto out;
3372 }
3373 copied++;
3374 }
3375 }
3376 arg->count_props = props_count;
3377out:
Daniel Vetter84849902012-12-02 00:28:11 +01003378 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003379 return ret;
3380}
3381
3382int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3383 struct drm_file *file_priv)
3384{
3385 struct drm_mode_obj_set_property *arg = data;
3386 struct drm_mode_object *arg_obj;
3387 struct drm_mode_object *prop_obj;
3388 struct drm_property *property;
3389 int ret = -EINVAL;
3390 int i;
3391
3392 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3393 return -EINVAL;
3394
Daniel Vetter84849902012-12-02 00:28:11 +01003395 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003396
3397 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3398 if (!arg_obj)
3399 goto out;
3400 if (!arg_obj->properties)
3401 goto out;
3402
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003403 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003404 if (arg_obj->properties->ids[i] == arg->prop_id)
3405 break;
3406
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003407 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003408 goto out;
3409
3410 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3411 DRM_MODE_OBJECT_PROPERTY);
3412 if (!prop_obj)
3413 goto out;
3414 property = obj_to_property(prop_obj);
3415
3416 if (!drm_property_change_is_valid(property, arg->value))
3417 goto out;
3418
3419 switch (arg_obj->type) {
3420 case DRM_MODE_OBJECT_CONNECTOR:
3421 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3422 arg->value);
3423 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003424 case DRM_MODE_OBJECT_CRTC:
3425 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3426 break;
Rob Clark4d939142012-05-17 02:23:27 -06003427 case DRM_MODE_OBJECT_PLANE:
3428 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3429 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003430 }
3431
3432out:
Daniel Vetter84849902012-12-02 00:28:11 +01003433 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003434 return ret;
3435}
3436
Dave Airlief453ba02008-11-07 14:05:41 -08003437int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3438 struct drm_encoder *encoder)
3439{
3440 int i;
3441
3442 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3443 if (connector->encoder_ids[i] == 0) {
3444 connector->encoder_ids[i] = encoder->base.id;
3445 return 0;
3446 }
3447 }
3448 return -ENOMEM;
3449}
3450EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3451
3452void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3453 struct drm_encoder *encoder)
3454{
3455 int i;
3456 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3457 if (connector->encoder_ids[i] == encoder->base.id) {
3458 connector->encoder_ids[i] = 0;
3459 if (connector->encoder == encoder)
3460 connector->encoder = NULL;
3461 break;
3462 }
3463 }
3464}
3465EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3466
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003467int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003468 int gamma_size)
3469{
3470 crtc->gamma_size = gamma_size;
3471
3472 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3473 if (!crtc->gamma_store) {
3474 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003475 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003476 }
3477
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003478 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003479}
3480EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3481
3482int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3483 void *data, struct drm_file *file_priv)
3484{
3485 struct drm_mode_crtc_lut *crtc_lut = data;
3486 struct drm_mode_object *obj;
3487 struct drm_crtc *crtc;
3488 void *r_base, *g_base, *b_base;
3489 int size;
3490 int ret = 0;
3491
Dave Airliefb3b06c2011-02-08 13:55:21 +10003492 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3493 return -EINVAL;
3494
Daniel Vetter84849902012-12-02 00:28:11 +01003495 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003496 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3497 if (!obj) {
3498 ret = -EINVAL;
3499 goto out;
3500 }
3501 crtc = obj_to_crtc(obj);
3502
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003503 if (crtc->funcs->gamma_set == NULL) {
3504 ret = -ENOSYS;
3505 goto out;
3506 }
3507
Dave Airlief453ba02008-11-07 14:05:41 -08003508 /* memcpy into gamma store */
3509 if (crtc_lut->gamma_size != crtc->gamma_size) {
3510 ret = -EINVAL;
3511 goto out;
3512 }
3513
3514 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3515 r_base = crtc->gamma_store;
3516 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3517 ret = -EFAULT;
3518 goto out;
3519 }
3520
3521 g_base = r_base + size;
3522 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3523 ret = -EFAULT;
3524 goto out;
3525 }
3526
3527 b_base = g_base + size;
3528 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3529 ret = -EFAULT;
3530 goto out;
3531 }
3532
James Simmons72034252010-08-03 01:33:19 +01003533 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003534
3535out:
Daniel Vetter84849902012-12-02 00:28:11 +01003536 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003537 return ret;
3538
3539}
3540
3541int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3542 void *data, struct drm_file *file_priv)
3543{
3544 struct drm_mode_crtc_lut *crtc_lut = data;
3545 struct drm_mode_object *obj;
3546 struct drm_crtc *crtc;
3547 void *r_base, *g_base, *b_base;
3548 int size;
3549 int ret = 0;
3550
Dave Airliefb3b06c2011-02-08 13:55:21 +10003551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3552 return -EINVAL;
3553
Daniel Vetter84849902012-12-02 00:28:11 +01003554 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003555 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3556 if (!obj) {
3557 ret = -EINVAL;
3558 goto out;
3559 }
3560 crtc = obj_to_crtc(obj);
3561
3562 /* memcpy into gamma store */
3563 if (crtc_lut->gamma_size != crtc->gamma_size) {
3564 ret = -EINVAL;
3565 goto out;
3566 }
3567
3568 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3569 r_base = crtc->gamma_store;
3570 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3571 ret = -EFAULT;
3572 goto out;
3573 }
3574
3575 g_base = r_base + size;
3576 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3577 ret = -EFAULT;
3578 goto out;
3579 }
3580
3581 b_base = g_base + size;
3582 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3583 ret = -EFAULT;
3584 goto out;
3585 }
3586out:
Daniel Vetter84849902012-12-02 00:28:11 +01003587 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003588 return ret;
3589}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003590
3591int drm_mode_page_flip_ioctl(struct drm_device *dev,
3592 void *data, struct drm_file *file_priv)
3593{
3594 struct drm_mode_crtc_page_flip *page_flip = data;
3595 struct drm_mode_object *obj;
3596 struct drm_crtc *crtc;
3597 struct drm_framebuffer *fb;
3598 struct drm_pending_vblank_event *e = NULL;
3599 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003600 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003601 int ret = -EINVAL;
3602
3603 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3604 page_flip->reserved != 0)
3605 return -EINVAL;
3606
Daniel Vetter84849902012-12-02 00:28:11 +01003607 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003608 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3609 if (!obj)
3610 goto out;
3611 crtc = obj_to_crtc(obj);
3612
Chris Wilson90c1efd2010-07-17 20:23:26 +01003613 if (crtc->fb == NULL) {
3614 /* The framebuffer is currently unbound, presumably
3615 * due to a hotplug event, that userspace has not
3616 * yet discovered.
3617 */
3618 ret = -EBUSY;
3619 goto out;
3620 }
3621
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003622 if (crtc->funcs->page_flip == NULL)
3623 goto out;
3624
Daniel Vetter786b99e2012-12-02 21:53:40 +01003625 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3626 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003627 goto out;
Daniel Vetter786b99e2012-12-02 21:53:40 +01003628 /* fb is protect by the mode_config lock, so drop the ref immediately */
3629 drm_framebuffer_unreference(fb);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003630
Rob Clark7c80e122012-09-04 16:35:56 +00003631 hdisplay = crtc->mode.hdisplay;
3632 vdisplay = crtc->mode.vdisplay;
3633
3634 if (crtc->invert_dimensions)
3635 swap(hdisplay, vdisplay);
3636
3637 if (hdisplay > fb->width ||
3638 vdisplay > fb->height ||
3639 crtc->x > fb->width - hdisplay ||
3640 crtc->y > fb->height - vdisplay) {
3641 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3642 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3643 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003644 ret = -ENOSPC;
3645 goto out;
3646 }
3647
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003648 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3649 ret = -ENOMEM;
3650 spin_lock_irqsave(&dev->event_lock, flags);
3651 if (file_priv->event_space < sizeof e->event) {
3652 spin_unlock_irqrestore(&dev->event_lock, flags);
3653 goto out;
3654 }
3655 file_priv->event_space -= sizeof e->event;
3656 spin_unlock_irqrestore(&dev->event_lock, flags);
3657
3658 e = kzalloc(sizeof *e, GFP_KERNEL);
3659 if (e == NULL) {
3660 spin_lock_irqsave(&dev->event_lock, flags);
3661 file_priv->event_space += sizeof e->event;
3662 spin_unlock_irqrestore(&dev->event_lock, flags);
3663 goto out;
3664 }
3665
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003666 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003667 e->event.base.length = sizeof e->event;
3668 e->event.user_data = page_flip->user_data;
3669 e->base.event = &e->event.base;
3670 e->base.file_priv = file_priv;
3671 e->base.destroy =
3672 (void (*) (struct drm_pending_event *)) kfree;
3673 }
3674
3675 ret = crtc->funcs->page_flip(crtc, fb, e);
3676 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003677 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3678 spin_lock_irqsave(&dev->event_lock, flags);
3679 file_priv->event_space += sizeof e->event;
3680 spin_unlock_irqrestore(&dev->event_lock, flags);
3681 kfree(e);
3682 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003683 }
3684
3685out:
Daniel Vetter84849902012-12-02 00:28:11 +01003686 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003687 return ret;
3688}
Chris Wilsoneb033552011-01-24 15:11:08 +00003689
3690void drm_mode_config_reset(struct drm_device *dev)
3691{
3692 struct drm_crtc *crtc;
3693 struct drm_encoder *encoder;
3694 struct drm_connector *connector;
3695
3696 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3697 if (crtc->funcs->reset)
3698 crtc->funcs->reset(crtc);
3699
3700 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3701 if (encoder->funcs->reset)
3702 encoder->funcs->reset(encoder);
3703
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003704 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3705 connector->status = connector_status_unknown;
3706
Chris Wilsoneb033552011-01-24 15:11:08 +00003707 if (connector->funcs->reset)
3708 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003709 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003710}
3711EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003712
3713int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3714 void *data, struct drm_file *file_priv)
3715{
3716 struct drm_mode_create_dumb *args = data;
3717
3718 if (!dev->driver->dumb_create)
3719 return -ENOSYS;
3720 return dev->driver->dumb_create(file_priv, dev, args);
3721}
3722
3723int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3724 void *data, struct drm_file *file_priv)
3725{
3726 struct drm_mode_map_dumb *args = data;
3727
3728 /* call driver ioctl to get mmap offset */
3729 if (!dev->driver->dumb_map_offset)
3730 return -ENOSYS;
3731
3732 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3733}
3734
3735int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3736 void *data, struct drm_file *file_priv)
3737{
3738 struct drm_mode_destroy_dumb *args = data;
3739
3740 if (!dev->driver->dumb_destroy)
3741 return -ENOSYS;
3742
3743 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3744}
Dave Airlie248dbc22011-11-29 20:02:54 +00003745
3746/*
3747 * Just need to support RGB formats here for compat with code that doesn't
3748 * use pixel formats directly yet.
3749 */
3750void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3751 int *bpp)
3752{
3753 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003754 case DRM_FORMAT_RGB332:
3755 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003756 *depth = 8;
3757 *bpp = 8;
3758 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003759 case DRM_FORMAT_XRGB1555:
3760 case DRM_FORMAT_XBGR1555:
3761 case DRM_FORMAT_RGBX5551:
3762 case DRM_FORMAT_BGRX5551:
3763 case DRM_FORMAT_ARGB1555:
3764 case DRM_FORMAT_ABGR1555:
3765 case DRM_FORMAT_RGBA5551:
3766 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003767 *depth = 15;
3768 *bpp = 16;
3769 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003770 case DRM_FORMAT_RGB565:
3771 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003772 *depth = 16;
3773 *bpp = 16;
3774 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003775 case DRM_FORMAT_RGB888:
3776 case DRM_FORMAT_BGR888:
3777 *depth = 24;
3778 *bpp = 24;
3779 break;
3780 case DRM_FORMAT_XRGB8888:
3781 case DRM_FORMAT_XBGR8888:
3782 case DRM_FORMAT_RGBX8888:
3783 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003784 *depth = 24;
3785 *bpp = 32;
3786 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003787 case DRM_FORMAT_XRGB2101010:
3788 case DRM_FORMAT_XBGR2101010:
3789 case DRM_FORMAT_RGBX1010102:
3790 case DRM_FORMAT_BGRX1010102:
3791 case DRM_FORMAT_ARGB2101010:
3792 case DRM_FORMAT_ABGR2101010:
3793 case DRM_FORMAT_RGBA1010102:
3794 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003795 *depth = 30;
3796 *bpp = 32;
3797 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003798 case DRM_FORMAT_ARGB8888:
3799 case DRM_FORMAT_ABGR8888:
3800 case DRM_FORMAT_RGBA8888:
3801 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003802 *depth = 32;
3803 *bpp = 32;
3804 break;
3805 default:
3806 DRM_DEBUG_KMS("unsupported pixel format\n");
3807 *depth = 0;
3808 *bpp = 0;
3809 break;
3810 }
3811}
3812EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003813
3814/**
3815 * drm_format_num_planes - get the number of planes for format
3816 * @format: pixel format (DRM_FORMAT_*)
3817 *
3818 * RETURNS:
3819 * The number of planes used by the specified pixel format.
3820 */
3821int drm_format_num_planes(uint32_t format)
3822{
3823 switch (format) {
3824 case DRM_FORMAT_YUV410:
3825 case DRM_FORMAT_YVU410:
3826 case DRM_FORMAT_YUV411:
3827 case DRM_FORMAT_YVU411:
3828 case DRM_FORMAT_YUV420:
3829 case DRM_FORMAT_YVU420:
3830 case DRM_FORMAT_YUV422:
3831 case DRM_FORMAT_YVU422:
3832 case DRM_FORMAT_YUV444:
3833 case DRM_FORMAT_YVU444:
3834 return 3;
3835 case DRM_FORMAT_NV12:
3836 case DRM_FORMAT_NV21:
3837 case DRM_FORMAT_NV16:
3838 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003839 case DRM_FORMAT_NV24:
3840 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003841 return 2;
3842 default:
3843 return 1;
3844 }
3845}
3846EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003847
3848/**
3849 * drm_format_plane_cpp - determine the bytes per pixel value
3850 * @format: pixel format (DRM_FORMAT_*)
3851 * @plane: plane index
3852 *
3853 * RETURNS:
3854 * The bytes per pixel value for the specified plane.
3855 */
3856int drm_format_plane_cpp(uint32_t format, int plane)
3857{
3858 unsigned int depth;
3859 int bpp;
3860
3861 if (plane >= drm_format_num_planes(format))
3862 return 0;
3863
3864 switch (format) {
3865 case DRM_FORMAT_YUYV:
3866 case DRM_FORMAT_YVYU:
3867 case DRM_FORMAT_UYVY:
3868 case DRM_FORMAT_VYUY:
3869 return 2;
3870 case DRM_FORMAT_NV12:
3871 case DRM_FORMAT_NV21:
3872 case DRM_FORMAT_NV16:
3873 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003874 case DRM_FORMAT_NV24:
3875 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003876 return plane ? 2 : 1;
3877 case DRM_FORMAT_YUV410:
3878 case DRM_FORMAT_YVU410:
3879 case DRM_FORMAT_YUV411:
3880 case DRM_FORMAT_YVU411:
3881 case DRM_FORMAT_YUV420:
3882 case DRM_FORMAT_YVU420:
3883 case DRM_FORMAT_YUV422:
3884 case DRM_FORMAT_YVU422:
3885 case DRM_FORMAT_YUV444:
3886 case DRM_FORMAT_YVU444:
3887 return 1;
3888 default:
3889 drm_fb_get_bpp_depth(format, &depth, &bpp);
3890 return bpp >> 3;
3891 }
3892}
3893EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003894
3895/**
3896 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3897 * @format: pixel format (DRM_FORMAT_*)
3898 *
3899 * RETURNS:
3900 * The horizontal chroma subsampling factor for the
3901 * specified pixel format.
3902 */
3903int drm_format_horz_chroma_subsampling(uint32_t format)
3904{
3905 switch (format) {
3906 case DRM_FORMAT_YUV411:
3907 case DRM_FORMAT_YVU411:
3908 case DRM_FORMAT_YUV410:
3909 case DRM_FORMAT_YVU410:
3910 return 4;
3911 case DRM_FORMAT_YUYV:
3912 case DRM_FORMAT_YVYU:
3913 case DRM_FORMAT_UYVY:
3914 case DRM_FORMAT_VYUY:
3915 case DRM_FORMAT_NV12:
3916 case DRM_FORMAT_NV21:
3917 case DRM_FORMAT_NV16:
3918 case DRM_FORMAT_NV61:
3919 case DRM_FORMAT_YUV422:
3920 case DRM_FORMAT_YVU422:
3921 case DRM_FORMAT_YUV420:
3922 case DRM_FORMAT_YVU420:
3923 return 2;
3924 default:
3925 return 1;
3926 }
3927}
3928EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3929
3930/**
3931 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3932 * @format: pixel format (DRM_FORMAT_*)
3933 *
3934 * RETURNS:
3935 * The vertical chroma subsampling factor for the
3936 * specified pixel format.
3937 */
3938int drm_format_vert_chroma_subsampling(uint32_t format)
3939{
3940 switch (format) {
3941 case DRM_FORMAT_YUV410:
3942 case DRM_FORMAT_YVU410:
3943 return 4;
3944 case DRM_FORMAT_YUV420:
3945 case DRM_FORMAT_YVU420:
3946 case DRM_FORMAT_NV12:
3947 case DRM_FORMAT_NV21:
3948 return 2;
3949 default:
3950 return 1;
3951 }
3952}
3953EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);