blob: f17077307c6513ede7fc401a0742881c7b03d600 [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
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010073/**
74 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
75 * @dev: device
76 */
77void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
78{
79 struct drm_crtc *crtc;
80
81 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
82 WARN_ON(!mutex_is_locked(&crtc->mutex));
83
84 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
85}
86EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
87
Dave Airlief453ba02008-11-07 14:05:41 -080088/* Avoid boilerplate. I'm tired of typing. */
89#define DRM_ENUM_NAME_FN(fnname, list) \
90 char *fnname(int val) \
91 { \
92 int i; \
93 for (i = 0; i < ARRAY_SIZE(list); i++) { \
94 if (list[i].type == val) \
95 return list[i].name; \
96 } \
97 return "(unknown)"; \
98 }
99
100/*
101 * Global properties
102 */
103static struct drm_prop_enum_list drm_dpms_enum_list[] =
104{ { DRM_MODE_DPMS_ON, "On" },
105 { DRM_MODE_DPMS_STANDBY, "Standby" },
106 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
107 { DRM_MODE_DPMS_OFF, "Off" }
108};
109
110DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
111
112/*
113 * Optional properties
114 */
115static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
116{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700117 { DRM_MODE_SCALE_NONE, "None" },
118 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
119 { DRM_MODE_SCALE_CENTER, "Center" },
120 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800121};
122
123static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
124{
125 { DRM_MODE_DITHERING_OFF, "Off" },
126 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000127 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
130/*
131 * Non-global properties, but "required" for certain connectors.
132 */
133static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
134{
135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
138};
139
140DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
141
142static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
143{
144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
147};
148
149DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
150 drm_dvi_i_subconnector_enum_list)
151
152static struct drm_prop_enum_list drm_tv_select_enum_list[] =
153{
154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800159};
160
161DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
162
163static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
164{
165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800170};
171
172DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
173 drm_tv_subconnector_enum_list)
174
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000175static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
176 { DRM_MODE_DIRTY_OFF, "Off" },
177 { DRM_MODE_DIRTY_ON, "On" },
178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
179};
180
181DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
182 drm_dirty_info_enum_list)
183
Dave Airlief453ba02008-11-07 14:05:41 -0800184struct drm_conn_prop_enum_list {
185 int type;
186 char *name;
187 int count;
188};
189
190/*
191 * Connector and encoder types.
192 */
193static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
194{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
195 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
196 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
197 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
198 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
199 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
200 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
201 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
202 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500203 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
204 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
205 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
206 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200207 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500208 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200209 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800210};
211
212static struct drm_prop_enum_list drm_encoder_enum_list[] =
213{ { DRM_MODE_ENCODER_NONE, "None" },
214 { DRM_MODE_ENCODER_DAC, "DAC" },
215 { DRM_MODE_ENCODER_TMDS, "TMDS" },
216 { DRM_MODE_ENCODER_LVDS, "LVDS" },
217 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200218 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800219};
220
221char *drm_get_encoder_name(struct drm_encoder *encoder)
222{
223 static char buf[32];
224
225 snprintf(buf, 32, "%s-%d",
226 drm_encoder_enum_list[encoder->encoder_type].name,
227 encoder->base.id);
228 return buf;
229}
Dave Airlie13a81952009-09-07 15:45:33 +1000230EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800231
232char *drm_get_connector_name(struct drm_connector *connector)
233{
234 static char buf[32];
235
236 snprintf(buf, 32, "%s-%d",
237 drm_connector_enum_list[connector->connector_type].name,
238 connector->connector_type_id);
239 return buf;
240}
241EXPORT_SYMBOL(drm_get_connector_name);
242
243char *drm_get_connector_status_name(enum drm_connector_status status)
244{
245 if (status == connector_status_connected)
246 return "connected";
247 else if (status == connector_status_disconnected)
248 return "disconnected";
249 else
250 return "unknown";
251}
252
253/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100254 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800255 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100256 * @obj: object pointer, used to generate unique ID
257 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800258 *
Dave Airlief453ba02008-11-07 14:05:41 -0800259 * Create a unique identifier based on @ptr in @dev's identifier space. Used
260 * for tracking modes, CRTCs and connectors.
261 *
262 * RETURNS:
263 * New unique (relative to other objects in @dev) integer identifier for the
264 * object.
265 */
266static int drm_mode_object_get(struct drm_device *dev,
267 struct drm_mode_object *obj, uint32_t obj_type)
268{
269 int new_id = 0;
270 int ret;
271
Dave Airlief453ba02008-11-07 14:05:41 -0800272again:
273 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
274 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200275 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800276 }
277
Jesse Barnesad2563c2009-01-19 17:21:45 +1000278 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800279 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100280
281 if (!ret) {
282 /*
283 * Set up the object linking under the protection of the idr
284 * lock so that other users can't see inconsistent state.
285 */
286 obj->id = new_id;
287 obj->type = obj_type;
288 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000289 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100290
Dave Airlief453ba02008-11-07 14:05:41 -0800291 if (ret == -EAGAIN)
292 goto again;
293
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100294 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800295}
296
297/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100298 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800299 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100300 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800301 *
Dave Airlief453ba02008-11-07 14:05:41 -0800302 * Free @id from @dev's unique identifier pool.
303 */
304static void drm_mode_object_put(struct drm_device *dev,
305 struct drm_mode_object *object)
306{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000307 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800308 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000309 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800310}
311
Daniel Vetter786b99e2012-12-02 21:53:40 +0100312/**
313 * drm_mode_object_find - look up a drm object with static lifetime
314 * @dev: drm device
315 * @id: id of the mode object
316 * @type: type of the mode object
317 *
318 * Note that framebuffers cannot be looked up with this functions - since those
319 * are reference counted, they need special treatment.
320 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200321struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
322 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800323{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000324 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800325
Daniel Vetter786b99e2012-12-02 21:53:40 +0100326 /* Framebuffers are reference counted and need their own lookup
327 * function.*/
328 WARN_ON(type == DRM_MODE_OBJECT_FB);
329
Jesse Barnesad2563c2009-01-19 17:21:45 +1000330 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800331 obj = idr_find(&dev->mode_config.crtc_idr, id);
332 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000333 obj = NULL;
334 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800335
336 return obj;
337}
338EXPORT_SYMBOL(drm_mode_object_find);
339
340/**
Dave Airlief453ba02008-11-07 14:05:41 -0800341 * drm_framebuffer_init - initialize a framebuffer
342 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100343 * @fb: framebuffer to be initialized
344 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800345 *
Dave Airlief453ba02008-11-07 14:05:41 -0800346 * Allocates an ID for the framebuffer's parent mode object, sets its mode
347 * functions & device file and adds it to the master fd list.
348 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100349 * IMPORTANT:
350 * This functions publishes the fb and makes it available for concurrent access
351 * by other users. Which means by this point the fb _must_ be fully set up -
352 * since all the fb attributes are invariant over its lifetime, no further
353 * locking but only correct reference counting is required.
354 *
Dave Airlief453ba02008-11-07 14:05:41 -0800355 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200356 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800357 */
358int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
359 const struct drm_framebuffer_funcs *funcs)
360{
361 int ret;
362
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100363 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000364 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100365 INIT_LIST_HEAD(&fb->filp_head);
366 fb->dev = dev;
367 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000368
Dave Airlief453ba02008-11-07 14:05:41 -0800369 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200370 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100371 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800372
Daniel Vetter2b677e82012-12-10 21:16:05 +0100373 /* Grab the idr reference. */
374 drm_framebuffer_reference(fb);
375
Dave Airlief453ba02008-11-07 14:05:41 -0800376 dev->mode_config.num_fb++;
377 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100378out:
379 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800380
381 return 0;
382}
383EXPORT_SYMBOL(drm_framebuffer_init);
384
Rob Clarkf7eff602012-09-05 21:48:38 +0000385static void drm_framebuffer_free(struct kref *kref)
386{
387 struct drm_framebuffer *fb =
388 container_of(kref, struct drm_framebuffer, refcount);
389 fb->funcs->destroy(fb);
390}
391
Daniel Vetter2b677e82012-12-10 21:16:05 +0100392static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
393 uint32_t id)
394{
395 struct drm_mode_object *obj = NULL;
396 struct drm_framebuffer *fb;
397
398 mutex_lock(&dev->mode_config.idr_mutex);
399 obj = idr_find(&dev->mode_config.crtc_idr, id);
400 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
401 fb = NULL;
402 else
403 fb = obj_to_fb(obj);
404 mutex_unlock(&dev->mode_config.idr_mutex);
405
406 return fb;
407}
408
Rob Clarkf7eff602012-09-05 21:48:38 +0000409/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100410 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
411 * @dev: drm device
412 * @id: id of the fb object
413 *
414 * If successful, this grabs an additional reference to the framebuffer -
415 * callers need to make sure to eventually unreference the returned framebuffer
416 * again.
417 */
418struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
419 uint32_t id)
420{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100421 struct drm_framebuffer *fb;
422
423 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100424 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100425 if (fb)
426 kref_get(&fb->refcount);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100427 mutex_unlock(&dev->mode_config.fb_lock);
428
429 return fb;
430}
431EXPORT_SYMBOL(drm_framebuffer_lookup);
432
433/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000434 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100435 * @fb: framebuffer to unref
436 *
437 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000438 */
439void drm_framebuffer_unreference(struct drm_framebuffer *fb)
440{
Rob Clarkf7eff602012-09-05 21:48:38 +0000441 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000442 kref_put(&fb->refcount, drm_framebuffer_free);
443}
444EXPORT_SYMBOL(drm_framebuffer_unreference);
445
446/**
447 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100448 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000449 */
450void drm_framebuffer_reference(struct drm_framebuffer *fb)
451{
452 DRM_DEBUG("FB ID: %d\n", fb->base.id);
453 kref_get(&fb->refcount);
454}
455EXPORT_SYMBOL(drm_framebuffer_reference);
456
Daniel Vetter2b677e82012-12-10 21:16:05 +0100457static void drm_framebuffer_free_bug(struct kref *kref)
458{
459 BUG();
460}
461
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100462static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
463{
464 DRM_DEBUG("FB ID: %d\n", fb->base.id);
465 kref_put(&fb->refcount, drm_framebuffer_free_bug);
466}
467
Daniel Vetter2b677e82012-12-10 21:16:05 +0100468/* dev->mode_config.fb_lock must be held! */
469static void __drm_framebuffer_unregister(struct drm_device *dev,
470 struct drm_framebuffer *fb)
471{
472 mutex_lock(&dev->mode_config.idr_mutex);
473 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
474 mutex_unlock(&dev->mode_config.idr_mutex);
475
476 fb->base.id = 0;
477
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100478 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100479}
480
Dave Airlief453ba02008-11-07 14:05:41 -0800481/**
Daniel Vetter36206362012-12-10 20:42:17 +0100482 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
483 * @fb: fb to unregister
484 *
485 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
486 * those used for fbdev. Note that the caller must hold a reference of it's own,
487 * i.e. the object may not be destroyed through this call (since it'll lead to a
488 * locking inversion).
489 */
490void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
491{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100492 struct drm_device *dev = fb->dev;
493
494 mutex_lock(&dev->mode_config.fb_lock);
495 /* Mark fb as reaped and drop idr ref. */
496 __drm_framebuffer_unregister(dev, fb);
497 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100498}
499EXPORT_SYMBOL(drm_framebuffer_unregister_private);
500
501/**
Dave Airlief453ba02008-11-07 14:05:41 -0800502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
504 *
Daniel Vetter36206362012-12-10 20:42:17 +0100505 * Cleanup references to a user-created framebuffer. This function is intended
506 * to be used from the drivers ->destroy callback.
507 *
508 * Note that this function does not remove the fb from active usuage - if it is
509 * still used anywhere, hilarity can ensue since userspace could call getfb on
510 * the id and get back -EINVAL. Obviously no concern at driver unload time.
511 *
512 * Also, the framebuffer will not be removed from the lookup idr - for
513 * user-created framebuffers this will happen in in the rmfb ioctl. For
514 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
515 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800516 */
517void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
518{
519 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100520
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100521 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000522 list_del(&fb->head);
523 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100524 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000525}
526EXPORT_SYMBOL(drm_framebuffer_cleanup);
527
528/**
529 * drm_framebuffer_remove - remove and unreference a framebuffer object
530 * @fb: framebuffer to remove
531 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000532 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100533 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100534 * passed-in framebuffer. Might take the modeset locks.
535 *
536 * Note that this function optimizes the cleanup away if the caller holds the
537 * last reference to the framebuffer. It is also guaranteed to not take the
538 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000539 */
540void drm_framebuffer_remove(struct drm_framebuffer *fb)
541{
542 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800543 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800544 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000545 struct drm_mode_set set;
546 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800547
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100548 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100549
Daniel Vetterb62584e2012-12-11 16:51:35 +0100550 /*
551 * drm ABI mandates that we remove any deleted framebuffers from active
552 * useage. But since most sane clients only remove framebuffers they no
553 * longer need, try to optimize this away.
554 *
555 * Since we're holding a reference ourselves, observing a refcount of 1
556 * means that we're the last holder and can skip it. Also, the refcount
557 * can never increase from 1 again, so we don't need any barriers or
558 * locks.
559 *
560 * Note that userspace could try to race with use and instate a new
561 * usage _after_ we've cleared all current ones. End result will be an
562 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
563 * in this manner.
564 */
565 if (atomic_read(&fb->refcount.refcount) > 1) {
566 drm_modeset_lock_all(dev);
567 /* remove from any CRTC */
568 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
569 if (crtc->fb == fb) {
570 /* should turn off the crtc */
571 memset(&set, 0, sizeof(struct drm_mode_set));
572 set.crtc = crtc;
573 set.fb = NULL;
574 ret = drm_mode_set_config_internal(&set);
575 if (ret)
576 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
577 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000578 }
Dave Airlief453ba02008-11-07 14:05:41 -0800579
Daniel Vetterb62584e2012-12-11 16:51:35 +0100580 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
581 if (plane->fb == fb) {
582 /* should turn off the crtc */
583 ret = plane->funcs->disable_plane(plane);
584 if (ret)
585 DRM_ERROR("failed to disable plane with busy fb\n");
586 /* disconnect the plane from the fb and crtc: */
587 __drm_framebuffer_unreference(plane->fb);
588 plane->fb = NULL;
589 plane->crtc = NULL;
590 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800591 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100592 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800593 }
594
Rob Clarkf7eff602012-09-05 21:48:38 +0000595 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800596}
Rob Clarkf7eff602012-09-05 21:48:38 +0000597EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800598
599/**
600 * drm_crtc_init - Initialise a new CRTC object
601 * @dev: DRM device
602 * @crtc: CRTC object to init
603 * @funcs: callbacks for the new CRTC
604 *
Dave Airlief453ba02008-11-07 14:05:41 -0800605 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200606 *
607 * RETURNS:
608 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800609 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200610int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800611 const struct drm_crtc_funcs *funcs)
612{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200613 int ret;
614
Dave Airlief453ba02008-11-07 14:05:41 -0800615 crtc->dev = dev;
616 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000617 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800618
Daniel Vetter84849902012-12-02 00:28:11 +0100619 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100620 mutex_init(&crtc->mutex);
621 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200622
623 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
624 if (ret)
625 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800626
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300627 crtc->base.properties = &crtc->properties;
628
Dave Airlief453ba02008-11-07 14:05:41 -0800629 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
630 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200631
632 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100633 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200634
635 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800636}
637EXPORT_SYMBOL(drm_crtc_init);
638
639/**
640 * drm_crtc_cleanup - Cleans up the core crtc usage.
641 * @crtc: CRTC to cleanup
642 *
Dave Airlief453ba02008-11-07 14:05:41 -0800643 * Cleanup @crtc. Removes from drm modesetting space
644 * does NOT free object, caller does that.
645 */
646void drm_crtc_cleanup(struct drm_crtc *crtc)
647{
648 struct drm_device *dev = crtc->dev;
649
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000650 kfree(crtc->gamma_store);
651 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800652
653 drm_mode_object_put(dev, &crtc->base);
654 list_del(&crtc->head);
655 dev->mode_config.num_crtc--;
656}
657EXPORT_SYMBOL(drm_crtc_cleanup);
658
659/**
660 * drm_mode_probed_add - add a mode to a connector's probed mode list
661 * @connector: connector the new mode
662 * @mode: mode data
663 *
Dave Airlief453ba02008-11-07 14:05:41 -0800664 * Add @mode to @connector's mode list for later use.
665 */
666void drm_mode_probed_add(struct drm_connector *connector,
667 struct drm_display_mode *mode)
668{
669 list_add(&mode->head, &connector->probed_modes);
670}
671EXPORT_SYMBOL(drm_mode_probed_add);
672
673/**
674 * drm_mode_remove - remove and free a mode
675 * @connector: connector list to modify
676 * @mode: mode to remove
677 *
Dave Airlief453ba02008-11-07 14:05:41 -0800678 * Remove @mode from @connector's mode list, then free it.
679 */
680void drm_mode_remove(struct drm_connector *connector,
681 struct drm_display_mode *mode)
682{
683 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100684 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800685}
686EXPORT_SYMBOL(drm_mode_remove);
687
688/**
689 * drm_connector_init - Init a preallocated connector
690 * @dev: DRM device
691 * @connector: the connector to init
692 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100693 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800694 *
Dave Airlief453ba02008-11-07 14:05:41 -0800695 * Initialises a preallocated connector. Connectors should be
696 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200697 *
698 * RETURNS:
699 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800700 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200701int drm_connector_init(struct drm_device *dev,
702 struct drm_connector *connector,
703 const struct drm_connector_funcs *funcs,
704 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800705{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200706 int ret;
707
Daniel Vetter84849902012-12-02 00:28:11 +0100708 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800709
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200710 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
711 if (ret)
712 goto out;
713
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300714 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800715 connector->dev = dev;
716 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800717 connector->connector_type = connector_type;
718 connector->connector_type_id =
719 ++drm_connector_enum_list[connector_type].count; /* TODO */
720 INIT_LIST_HEAD(&connector->user_modes);
721 INIT_LIST_HEAD(&connector->probed_modes);
722 INIT_LIST_HEAD(&connector->modes);
723 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000724 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800725
726 list_add_tail(&connector->head, &dev->mode_config.connector_list);
727 dev->mode_config.num_connector++;
728
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200729 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500730 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200731 dev->mode_config.edid_property,
732 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800733
Rob Clark58495562012-10-11 20:50:56 -0500734 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800735 dev->mode_config.dpms_property, 0);
736
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200737 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100738 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200739
740 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800741}
742EXPORT_SYMBOL(drm_connector_init);
743
744/**
745 * drm_connector_cleanup - cleans up an initialised connector
746 * @connector: connector to cleanup
747 *
Dave Airlief453ba02008-11-07 14:05:41 -0800748 * Cleans up the connector but doesn't free the object.
749 */
750void drm_connector_cleanup(struct drm_connector *connector)
751{
752 struct drm_device *dev = connector->dev;
753 struct drm_display_mode *mode, *t;
754
755 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
756 drm_mode_remove(connector, mode);
757
758 list_for_each_entry_safe(mode, t, &connector->modes, head)
759 drm_mode_remove(connector, mode);
760
761 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
762 drm_mode_remove(connector, mode);
763
Dave Airlief453ba02008-11-07 14:05:41 -0800764 drm_mode_object_put(dev, &connector->base);
765 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000766 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800767}
768EXPORT_SYMBOL(drm_connector_cleanup);
769
Dave Airliecbc7e222012-02-20 14:16:40 +0000770void drm_connector_unplug_all(struct drm_device *dev)
771{
772 struct drm_connector *connector;
773
774 /* taking the mode config mutex ends up in a clash with sysfs */
775 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
776 drm_sysfs_connector_remove(connector);
777
778}
779EXPORT_SYMBOL(drm_connector_unplug_all);
780
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200781int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000782 struct drm_encoder *encoder,
783 const struct drm_encoder_funcs *funcs,
784 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800785{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200786 int ret;
787
Daniel Vetter84849902012-12-02 00:28:11 +0100788 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800789
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200790 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
791 if (ret)
792 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800793
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800795 encoder->encoder_type = encoder_type;
796 encoder->funcs = funcs;
797
798 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
799 dev->mode_config.num_encoder++;
800
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200801 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100802 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200803
804 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800805}
806EXPORT_SYMBOL(drm_encoder_init);
807
808void drm_encoder_cleanup(struct drm_encoder *encoder)
809{
810 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100811 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800812 drm_mode_object_put(dev, &encoder->base);
813 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000814 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100815 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800816}
817EXPORT_SYMBOL(drm_encoder_cleanup);
818
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800819int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
820 unsigned long possible_crtcs,
821 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600822 const uint32_t *formats, uint32_t format_count,
823 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800824{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200825 int ret;
826
Daniel Vetter84849902012-12-02 00:28:11 +0100827 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800828
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200829 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
830 if (ret)
831 goto out;
832
Rob Clark4d939142012-05-17 02:23:27 -0600833 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800834 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800835 plane->funcs = funcs;
836 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
837 GFP_KERNEL);
838 if (!plane->format_types) {
839 DRM_DEBUG_KMS("out of memory when allocating plane\n");
840 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200841 ret = -ENOMEM;
842 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800843 }
844
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800845 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800846 plane->format_count = format_count;
847 plane->possible_crtcs = possible_crtcs;
848
Rob Clark0a7eb242011-12-13 20:19:36 -0600849 /* private planes are not exposed to userspace, but depending on
850 * display hardware, might be convenient to allow sharing programming
851 * for the scanout engine with the crtc implementation.
852 */
853 if (!priv) {
854 list_add_tail(&plane->head, &dev->mode_config.plane_list);
855 dev->mode_config.num_plane++;
856 } else {
857 INIT_LIST_HEAD(&plane->head);
858 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800859
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200860 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100861 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800862
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200863 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800864}
865EXPORT_SYMBOL(drm_plane_init);
866
867void drm_plane_cleanup(struct drm_plane *plane)
868{
869 struct drm_device *dev = plane->dev;
870
Daniel Vetter84849902012-12-02 00:28:11 +0100871 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800872 kfree(plane->format_types);
873 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600874 /* if not added to a list, it must be a private plane */
875 if (!list_empty(&plane->head)) {
876 list_del(&plane->head);
877 dev->mode_config.num_plane--;
878 }
Daniel Vetter84849902012-12-02 00:28:11 +0100879 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800880}
881EXPORT_SYMBOL(drm_plane_cleanup);
882
Dave Airlief453ba02008-11-07 14:05:41 -0800883/**
884 * drm_mode_create - create a new display mode
885 * @dev: DRM device
886 *
Dave Airlief453ba02008-11-07 14:05:41 -0800887 * Create a new drm_display_mode, give it an ID, and return it.
888 *
889 * RETURNS:
890 * Pointer to new mode on success, NULL on error.
891 */
892struct drm_display_mode *drm_mode_create(struct drm_device *dev)
893{
894 struct drm_display_mode *nmode;
895
896 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
897 if (!nmode)
898 return NULL;
899
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200900 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
901 kfree(nmode);
902 return NULL;
903 }
904
Dave Airlief453ba02008-11-07 14:05:41 -0800905 return nmode;
906}
907EXPORT_SYMBOL(drm_mode_create);
908
909/**
910 * drm_mode_destroy - remove a mode
911 * @dev: DRM device
912 * @mode: mode to remove
913 *
Dave Airlief453ba02008-11-07 14:05:41 -0800914 * Free @mode's unique identifier, then free it.
915 */
916void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
917{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200918 if (!mode)
919 return;
920
Dave Airlief453ba02008-11-07 14:05:41 -0800921 drm_mode_object_put(dev, &mode->base);
922
923 kfree(mode);
924}
925EXPORT_SYMBOL(drm_mode_destroy);
926
927static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
928{
929 struct drm_property *edid;
930 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800931
932 /*
933 * Standard properties (apply to all connectors)
934 */
935 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
936 DRM_MODE_PROP_IMMUTABLE,
937 "EDID", 0);
938 dev->mode_config.edid_property = edid;
939
Sascha Hauer4a67d392012-02-06 10:58:17 +0100940 dpms = drm_property_create_enum(dev, 0,
941 "DPMS", drm_dpms_enum_list,
942 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800943 dev->mode_config.dpms_property = dpms;
944
945 return 0;
946}
947
948/**
949 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
950 * @dev: DRM device
951 *
952 * Called by a driver the first time a DVI-I connector is made.
953 */
954int drm_mode_create_dvi_i_properties(struct drm_device *dev)
955{
956 struct drm_property *dvi_i_selector;
957 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800958
959 if (dev->mode_config.dvi_i_select_subconnector_property)
960 return 0;
961
962 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100963 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800964 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100965 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800966 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800967 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
968
Sascha Hauer4a67d392012-02-06 10:58:17 +0100969 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800970 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100971 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800972 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800973 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
974
975 return 0;
976}
977EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
978
979/**
980 * drm_create_tv_properties - create TV specific connector properties
981 * @dev: DRM device
982 * @num_modes: number of different TV formats (modes) supported
983 * @modes: array of pointers to strings containing name of each format
984 *
985 * Called by a driver's TV initialization routine, this function creates
986 * the TV specific connector properties for a given device. Caller is
987 * responsible for allocating a list of format names and passing them to
988 * this routine.
989 */
990int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
991 char *modes[])
992{
993 struct drm_property *tv_selector;
994 struct drm_property *tv_subconnector;
995 int i;
996
997 if (dev->mode_config.tv_select_subconnector_property)
998 return 0;
999
1000 /*
1001 * Basic connector properties
1002 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001003 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001004 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001005 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001006 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001007 dev->mode_config.tv_select_subconnector_property = tv_selector;
1008
1009 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001010 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1011 "subconnector",
1012 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001013 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001014 dev->mode_config.tv_subconnector_property = tv_subconnector;
1015
1016 /*
1017 * Other, TV specific properties: margins & TV modes.
1018 */
1019 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001020 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001021
1022 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001023 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001024
1025 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001026 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001027
1028 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001029 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001030
1031 dev->mode_config.tv_mode_property =
1032 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1033 "mode", num_modes);
1034 for (i = 0; i < num_modes; i++)
1035 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1036 i, modes[i]);
1037
Francisco Jerezb6b79022009-08-02 04:19:20 +02001038 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001039 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001040
1041 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001042 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001043
1044 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001045 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001046
Francisco Jereza75f0232009-08-12 02:30:10 +02001047 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001048 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001049
1050 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001051 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001052
1053 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001054 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001055
Dave Airlief453ba02008-11-07 14:05:41 -08001056 return 0;
1057}
1058EXPORT_SYMBOL(drm_mode_create_tv_properties);
1059
1060/**
1061 * drm_mode_create_scaling_mode_property - create scaling mode property
1062 * @dev: DRM device
1063 *
1064 * Called by a driver the first time it's needed, must be attached to desired
1065 * connectors.
1066 */
1067int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1068{
1069 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001070
1071 if (dev->mode_config.scaling_mode_property)
1072 return 0;
1073
1074 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001075 drm_property_create_enum(dev, 0, "scaling mode",
1076 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001077 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001078
1079 dev->mode_config.scaling_mode_property = scaling_mode;
1080
1081 return 0;
1082}
1083EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1084
1085/**
1086 * drm_mode_create_dithering_property - create dithering property
1087 * @dev: DRM device
1088 *
1089 * Called by a driver the first time it's needed, must be attached to desired
1090 * connectors.
1091 */
1092int drm_mode_create_dithering_property(struct drm_device *dev)
1093{
1094 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001095
1096 if (dev->mode_config.dithering_mode_property)
1097 return 0;
1098
1099 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001100 drm_property_create_enum(dev, 0, "dithering",
1101 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001102 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001103 dev->mode_config.dithering_mode_property = dithering_mode;
1104
1105 return 0;
1106}
1107EXPORT_SYMBOL(drm_mode_create_dithering_property);
1108
1109/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001110 * drm_mode_create_dirty_property - create dirty property
1111 * @dev: DRM device
1112 *
1113 * Called by a driver the first time it's needed, must be attached to desired
1114 * connectors.
1115 */
1116int drm_mode_create_dirty_info_property(struct drm_device *dev)
1117{
1118 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001119
1120 if (dev->mode_config.dirty_info_property)
1121 return 0;
1122
1123 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001124 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001125 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001126 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001127 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001128 dev->mode_config.dirty_info_property = dirty_info;
1129
1130 return 0;
1131}
1132EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1133
1134/**
Dave Airlief453ba02008-11-07 14:05:41 -08001135 * drm_mode_config_init - initialize DRM mode_configuration structure
1136 * @dev: DRM device
1137 *
Dave Airlief453ba02008-11-07 14:05:41 -08001138 * Initialize @dev's mode_config structure, used for tracking the graphics
1139 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001140 *
1141 * Since this initializes the modeset locks, no locking is possible. Which is no
1142 * problem, since this should happen single threaded at init time. It is the
1143 * driver's problem to ensure this guarantee.
1144 *
Dave Airlief453ba02008-11-07 14:05:41 -08001145 */
1146void drm_mode_config_init(struct drm_device *dev)
1147{
1148 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001149 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001150 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001151 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001152 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1153 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1154 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1155 INIT_LIST_HEAD(&dev->mode_config.property_list);
1156 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001157 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001158 idr_init(&dev->mode_config.crtc_idr);
1159
Daniel Vetter84849902012-12-02 00:28:11 +01001160 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001161 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001162 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001163
1164 /* Just to be sure */
1165 dev->mode_config.num_fb = 0;
1166 dev->mode_config.num_connector = 0;
1167 dev->mode_config.num_crtc = 0;
1168 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001169}
1170EXPORT_SYMBOL(drm_mode_config_init);
1171
1172int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1173{
1174 uint32_t total_objects = 0;
1175
1176 total_objects += dev->mode_config.num_crtc;
1177 total_objects += dev->mode_config.num_connector;
1178 total_objects += dev->mode_config.num_encoder;
1179
Dave Airlief453ba02008-11-07 14:05:41 -08001180 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1181 if (!group->id_list)
1182 return -ENOMEM;
1183
1184 group->num_crtcs = 0;
1185 group->num_connectors = 0;
1186 group->num_encoders = 0;
1187 return 0;
1188}
1189
1190int drm_mode_group_init_legacy_group(struct drm_device *dev,
1191 struct drm_mode_group *group)
1192{
1193 struct drm_crtc *crtc;
1194 struct drm_encoder *encoder;
1195 struct drm_connector *connector;
1196 int ret;
1197
1198 if ((ret = drm_mode_group_init(dev, group)))
1199 return ret;
1200
1201 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1202 group->id_list[group->num_crtcs++] = crtc->base.id;
1203
1204 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1205 group->id_list[group->num_crtcs + group->num_encoders++] =
1206 encoder->base.id;
1207
1208 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1209 group->id_list[group->num_crtcs + group->num_encoders +
1210 group->num_connectors++] = connector->base.id;
1211
1212 return 0;
1213}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001214EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001215
1216/**
1217 * drm_mode_config_cleanup - free up DRM mode_config info
1218 * @dev: DRM device
1219 *
Dave Airlief453ba02008-11-07 14:05:41 -08001220 * Free up all the connectors and CRTCs associated with this DRM device, then
1221 * free up the framebuffers and associated buffer objects.
1222 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001223 * Note that since this /should/ happen single-threaded at driver/device
1224 * teardown time, no locking is required. It's the driver's job to ensure that
1225 * this guarantee actually holds true.
1226 *
Dave Airlief453ba02008-11-07 14:05:41 -08001227 * FIXME: cleanup any dangling user buffer objects too
1228 */
1229void drm_mode_config_cleanup(struct drm_device *dev)
1230{
1231 struct drm_connector *connector, *ot;
1232 struct drm_crtc *crtc, *ct;
1233 struct drm_encoder *encoder, *enct;
1234 struct drm_framebuffer *fb, *fbt;
1235 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001236 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001237
1238 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1239 head) {
1240 encoder->funcs->destroy(encoder);
1241 }
1242
1243 list_for_each_entry_safe(connector, ot,
1244 &dev->mode_config.connector_list, head) {
1245 connector->funcs->destroy(connector);
1246 }
1247
1248 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1249 head) {
1250 drm_property_destroy(dev, property);
1251 }
1252
Daniel Vetter2b677e82012-12-10 21:16:05 +01001253 /*
1254 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001255 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001256 * would actually deadlock with the drm_framebuffer_cleanup function.
1257 *
1258 * Also, if there are any framebuffers left, that's a driver leak now,
1259 * so politely WARN about this.
1260 */
1261 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001262 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001263 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001264 }
1265
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001266 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1267 head) {
1268 plane->funcs->destroy(plane);
1269 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001270
Chris Wilson31840092012-09-17 09:38:03 +00001271 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1272 crtc->funcs->destroy(crtc);
1273 }
1274
Sascha Hauer59ce0622012-02-01 11:38:20 +01001275 idr_remove_all(&dev->mode_config.crtc_idr);
1276 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001277}
1278EXPORT_SYMBOL(drm_mode_config_cleanup);
1279
Dave Airlief453ba02008-11-07 14:05:41 -08001280/**
1281 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1282 * @out: drm_mode_modeinfo struct to return to the user
1283 * @in: drm_display_mode to use
1284 *
Dave Airlief453ba02008-11-07 14:05:41 -08001285 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1286 * the user.
1287 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001288static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1289 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001290{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001291 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1292 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1293 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1294 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1295 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1296 "timing values too large for mode info\n");
1297
Dave Airlief453ba02008-11-07 14:05:41 -08001298 out->clock = in->clock;
1299 out->hdisplay = in->hdisplay;
1300 out->hsync_start = in->hsync_start;
1301 out->hsync_end = in->hsync_end;
1302 out->htotal = in->htotal;
1303 out->hskew = in->hskew;
1304 out->vdisplay = in->vdisplay;
1305 out->vsync_start = in->vsync_start;
1306 out->vsync_end = in->vsync_end;
1307 out->vtotal = in->vtotal;
1308 out->vscan = in->vscan;
1309 out->vrefresh = in->vrefresh;
1310 out->flags = in->flags;
1311 out->type = in->type;
1312 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1313 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1314}
1315
1316/**
1317 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1318 * @out: drm_display_mode to return to the user
1319 * @in: drm_mode_modeinfo to use
1320 *
Dave Airlief453ba02008-11-07 14:05:41 -08001321 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1322 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001323 *
1324 * RETURNS:
1325 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001326 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001327static int drm_crtc_convert_umode(struct drm_display_mode *out,
1328 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001329{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001330 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1331 return -ERANGE;
1332
Dave Airlief453ba02008-11-07 14:05:41 -08001333 out->clock = in->clock;
1334 out->hdisplay = in->hdisplay;
1335 out->hsync_start = in->hsync_start;
1336 out->hsync_end = in->hsync_end;
1337 out->htotal = in->htotal;
1338 out->hskew = in->hskew;
1339 out->vdisplay = in->vdisplay;
1340 out->vsync_start = in->vsync_start;
1341 out->vsync_end = in->vsync_end;
1342 out->vtotal = in->vtotal;
1343 out->vscan = in->vscan;
1344 out->vrefresh = in->vrefresh;
1345 out->flags = in->flags;
1346 out->type = in->type;
1347 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1348 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001349
1350 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001351}
1352
1353/**
1354 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001355 * @dev: drm device for the ioctl
1356 * @data: data pointer for the ioctl
1357 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001358 *
Dave Airlief453ba02008-11-07 14:05:41 -08001359 * Construct a set of configuration description structures and return
1360 * them to the user, including CRTC, connector and framebuffer configuration.
1361 *
1362 * Called by the user via ioctl.
1363 *
1364 * RETURNS:
1365 * Zero on success, errno on failure.
1366 */
1367int drm_mode_getresources(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
1369{
1370 struct drm_mode_card_res *card_res = data;
1371 struct list_head *lh;
1372 struct drm_framebuffer *fb;
1373 struct drm_connector *connector;
1374 struct drm_crtc *crtc;
1375 struct drm_encoder *encoder;
1376 int ret = 0;
1377 int connector_count = 0;
1378 int crtc_count = 0;
1379 int fb_count = 0;
1380 int encoder_count = 0;
1381 int copied = 0, i;
1382 uint32_t __user *fb_id;
1383 uint32_t __user *crtc_id;
1384 uint32_t __user *connector_id;
1385 uint32_t __user *encoder_id;
1386 struct drm_mode_group *mode_group;
1387
Dave Airliefb3b06c2011-02-08 13:55:21 +10001388 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1389 return -EINVAL;
1390
Dave Airlief453ba02008-11-07 14:05:41 -08001391
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001392 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001393 /*
1394 * For the non-control nodes we need to limit the list of resources
1395 * by IDs in the group list for this node
1396 */
1397 list_for_each(lh, &file_priv->fbs)
1398 fb_count++;
1399
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001400 /* handle this in 4 parts */
1401 /* FBs */
1402 if (card_res->count_fbs >= fb_count) {
1403 copied = 0;
1404 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1405 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1406 if (put_user(fb->base.id, fb_id + copied)) {
1407 mutex_unlock(&file_priv->fbs_lock);
1408 return -EFAULT;
1409 }
1410 copied++;
1411 }
1412 }
1413 card_res->count_fbs = fb_count;
1414 mutex_unlock(&file_priv->fbs_lock);
1415
1416 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001417 mode_group = &file_priv->master->minor->mode_group;
1418 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1419
1420 list_for_each(lh, &dev->mode_config.crtc_list)
1421 crtc_count++;
1422
1423 list_for_each(lh, &dev->mode_config.connector_list)
1424 connector_count++;
1425
1426 list_for_each(lh, &dev->mode_config.encoder_list)
1427 encoder_count++;
1428 } else {
1429
1430 crtc_count = mode_group->num_crtcs;
1431 connector_count = mode_group->num_connectors;
1432 encoder_count = mode_group->num_encoders;
1433 }
1434
1435 card_res->max_height = dev->mode_config.max_height;
1436 card_res->min_height = dev->mode_config.min_height;
1437 card_res->max_width = dev->mode_config.max_width;
1438 card_res->min_width = dev->mode_config.min_width;
1439
Dave Airlief453ba02008-11-07 14:05:41 -08001440 /* CRTCs */
1441 if (card_res->count_crtcs >= crtc_count) {
1442 copied = 0;
1443 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1444 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1445 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1446 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001447 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001448 if (put_user(crtc->base.id, crtc_id + copied)) {
1449 ret = -EFAULT;
1450 goto out;
1451 }
1452 copied++;
1453 }
1454 } else {
1455 for (i = 0; i < mode_group->num_crtcs; i++) {
1456 if (put_user(mode_group->id_list[i],
1457 crtc_id + copied)) {
1458 ret = -EFAULT;
1459 goto out;
1460 }
1461 copied++;
1462 }
1463 }
1464 }
1465 card_res->count_crtcs = crtc_count;
1466
1467 /* Encoders */
1468 if (card_res->count_encoders >= encoder_count) {
1469 copied = 0;
1470 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1471 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1472 list_for_each_entry(encoder,
1473 &dev->mode_config.encoder_list,
1474 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001475 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1476 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001477 if (put_user(encoder->base.id, encoder_id +
1478 copied)) {
1479 ret = -EFAULT;
1480 goto out;
1481 }
1482 copied++;
1483 }
1484 } else {
1485 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1486 if (put_user(mode_group->id_list[i],
1487 encoder_id + copied)) {
1488 ret = -EFAULT;
1489 goto out;
1490 }
1491 copied++;
1492 }
1493
1494 }
1495 }
1496 card_res->count_encoders = encoder_count;
1497
1498 /* Connectors */
1499 if (card_res->count_connectors >= connector_count) {
1500 copied = 0;
1501 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1502 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1503 list_for_each_entry(connector,
1504 &dev->mode_config.connector_list,
1505 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001506 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1507 connector->base.id,
1508 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001509 if (put_user(connector->base.id,
1510 connector_id + copied)) {
1511 ret = -EFAULT;
1512 goto out;
1513 }
1514 copied++;
1515 }
1516 } else {
1517 int start = mode_group->num_crtcs +
1518 mode_group->num_encoders;
1519 for (i = start; i < start + mode_group->num_connectors; i++) {
1520 if (put_user(mode_group->id_list[i],
1521 connector_id + copied)) {
1522 ret = -EFAULT;
1523 goto out;
1524 }
1525 copied++;
1526 }
1527 }
1528 }
1529 card_res->count_connectors = connector_count;
1530
Jerome Glisse94401062010-07-15 15:43:25 -04001531 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001532 card_res->count_connectors, card_res->count_encoders);
1533
1534out:
Daniel Vetter84849902012-12-02 00:28:11 +01001535 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001536 return ret;
1537}
1538
1539/**
1540 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001541 * @dev: drm device for the ioctl
1542 * @data: data pointer for the ioctl
1543 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001544 *
Dave Airlief453ba02008-11-07 14:05:41 -08001545 * Construct a CRTC configuration structure to return to the user.
1546 *
1547 * Called by the user via ioctl.
1548 *
1549 * RETURNS:
1550 * Zero on success, errno on failure.
1551 */
1552int drm_mode_getcrtc(struct drm_device *dev,
1553 void *data, struct drm_file *file_priv)
1554{
1555 struct drm_mode_crtc *crtc_resp = data;
1556 struct drm_crtc *crtc;
1557 struct drm_mode_object *obj;
1558 int ret = 0;
1559
Dave Airliefb3b06c2011-02-08 13:55:21 +10001560 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1561 return -EINVAL;
1562
Daniel Vetter84849902012-12-02 00:28:11 +01001563 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001564
1565 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1566 DRM_MODE_OBJECT_CRTC);
1567 if (!obj) {
1568 ret = -EINVAL;
1569 goto out;
1570 }
1571 crtc = obj_to_crtc(obj);
1572
1573 crtc_resp->x = crtc->x;
1574 crtc_resp->y = crtc->y;
1575 crtc_resp->gamma_size = crtc->gamma_size;
1576 if (crtc->fb)
1577 crtc_resp->fb_id = crtc->fb->base.id;
1578 else
1579 crtc_resp->fb_id = 0;
1580
1581 if (crtc->enabled) {
1582
1583 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1584 crtc_resp->mode_valid = 1;
1585
1586 } else {
1587 crtc_resp->mode_valid = 0;
1588 }
1589
1590out:
Daniel Vetter84849902012-12-02 00:28:11 +01001591 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001592 return ret;
1593}
1594
1595/**
1596 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001597 * @dev: drm device for the ioctl
1598 * @data: data pointer for the ioctl
1599 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001600 *
Dave Airlief453ba02008-11-07 14:05:41 -08001601 * Construct a connector configuration structure to return to the user.
1602 *
1603 * Called by the user via ioctl.
1604 *
1605 * RETURNS:
1606 * Zero on success, errno on failure.
1607 */
1608int drm_mode_getconnector(struct drm_device *dev, void *data,
1609 struct drm_file *file_priv)
1610{
1611 struct drm_mode_get_connector *out_resp = data;
1612 struct drm_mode_object *obj;
1613 struct drm_connector *connector;
1614 struct drm_display_mode *mode;
1615 int mode_count = 0;
1616 int props_count = 0;
1617 int encoders_count = 0;
1618 int ret = 0;
1619 int copied = 0;
1620 int i;
1621 struct drm_mode_modeinfo u_mode;
1622 struct drm_mode_modeinfo __user *mode_ptr;
1623 uint32_t __user *prop_ptr;
1624 uint64_t __user *prop_values;
1625 uint32_t __user *encoder_ptr;
1626
Dave Airliefb3b06c2011-02-08 13:55:21 +10001627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1628 return -EINVAL;
1629
Dave Airlief453ba02008-11-07 14:05:41 -08001630 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1631
Jerome Glisse94401062010-07-15 15:43:25 -04001632 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001633
Daniel Vetter7b240562012-12-12 00:35:33 +01001634 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001635
1636 obj = drm_mode_object_find(dev, out_resp->connector_id,
1637 DRM_MODE_OBJECT_CONNECTOR);
1638 if (!obj) {
1639 ret = -EINVAL;
1640 goto out;
1641 }
1642 connector = obj_to_connector(obj);
1643
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001644 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001645
1646 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1647 if (connector->encoder_ids[i] != 0) {
1648 encoders_count++;
1649 }
1650 }
1651
1652 if (out_resp->count_modes == 0) {
1653 connector->funcs->fill_modes(connector,
1654 dev->mode_config.max_width,
1655 dev->mode_config.max_height);
1656 }
1657
1658 /* delayed so we get modes regardless of pre-fill_modes state */
1659 list_for_each_entry(mode, &connector->modes, head)
1660 mode_count++;
1661
1662 out_resp->connector_id = connector->base.id;
1663 out_resp->connector_type = connector->connector_type;
1664 out_resp->connector_type_id = connector->connector_type_id;
1665 out_resp->mm_width = connector->display_info.width_mm;
1666 out_resp->mm_height = connector->display_info.height_mm;
1667 out_resp->subpixel = connector->display_info.subpixel_order;
1668 out_resp->connection = connector->status;
1669 if (connector->encoder)
1670 out_resp->encoder_id = connector->encoder->base.id;
1671 else
1672 out_resp->encoder_id = 0;
1673
1674 /*
1675 * This ioctl is called twice, once to determine how much space is
1676 * needed, and the 2nd time to fill it.
1677 */
1678 if ((out_resp->count_modes >= mode_count) && mode_count) {
1679 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001680 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001681 list_for_each_entry(mode, &connector->modes, head) {
1682 drm_crtc_convert_to_umode(&u_mode, mode);
1683 if (copy_to_user(mode_ptr + copied,
1684 &u_mode, sizeof(u_mode))) {
1685 ret = -EFAULT;
1686 goto out;
1687 }
1688 copied++;
1689 }
1690 }
1691 out_resp->count_modes = mode_count;
1692
1693 if ((out_resp->count_props >= props_count) && props_count) {
1694 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001695 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1696 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001697 for (i = 0; i < connector->properties.count; i++) {
1698 if (put_user(connector->properties.ids[i],
1699 prop_ptr + copied)) {
1700 ret = -EFAULT;
1701 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001702 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001703
1704 if (put_user(connector->properties.values[i],
1705 prop_values + copied)) {
1706 ret = -EFAULT;
1707 goto out;
1708 }
1709 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001710 }
1711 }
1712 out_resp->count_props = props_count;
1713
1714 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1715 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001716 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001717 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1718 if (connector->encoder_ids[i] != 0) {
1719 if (put_user(connector->encoder_ids[i],
1720 encoder_ptr + copied)) {
1721 ret = -EFAULT;
1722 goto out;
1723 }
1724 copied++;
1725 }
1726 }
1727 }
1728 out_resp->count_encoders = encoders_count;
1729
1730out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001731 mutex_unlock(&dev->mode_config.mutex);
1732
Dave Airlief453ba02008-11-07 14:05:41 -08001733 return ret;
1734}
1735
1736int drm_mode_getencoder(struct drm_device *dev, void *data,
1737 struct drm_file *file_priv)
1738{
1739 struct drm_mode_get_encoder *enc_resp = data;
1740 struct drm_mode_object *obj;
1741 struct drm_encoder *encoder;
1742 int ret = 0;
1743
Dave Airliefb3b06c2011-02-08 13:55:21 +10001744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1745 return -EINVAL;
1746
Daniel Vetter84849902012-12-02 00:28:11 +01001747 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001748 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1749 DRM_MODE_OBJECT_ENCODER);
1750 if (!obj) {
1751 ret = -EINVAL;
1752 goto out;
1753 }
1754 encoder = obj_to_encoder(obj);
1755
1756 if (encoder->crtc)
1757 enc_resp->crtc_id = encoder->crtc->base.id;
1758 else
1759 enc_resp->crtc_id = 0;
1760 enc_resp->encoder_type = encoder->encoder_type;
1761 enc_resp->encoder_id = encoder->base.id;
1762 enc_resp->possible_crtcs = encoder->possible_crtcs;
1763 enc_resp->possible_clones = encoder->possible_clones;
1764
1765out:
Daniel Vetter84849902012-12-02 00:28:11 +01001766 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001767 return ret;
1768}
1769
1770/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001771 * drm_mode_getplane_res - get plane info
1772 * @dev: DRM device
1773 * @data: ioctl data
1774 * @file_priv: DRM file info
1775 *
1776 * Return an plane count and set of IDs.
1777 */
1778int drm_mode_getplane_res(struct drm_device *dev, void *data,
1779 struct drm_file *file_priv)
1780{
1781 struct drm_mode_get_plane_res *plane_resp = data;
1782 struct drm_mode_config *config;
1783 struct drm_plane *plane;
1784 uint32_t __user *plane_ptr;
1785 int copied = 0, ret = 0;
1786
1787 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1788 return -EINVAL;
1789
Daniel Vetter84849902012-12-02 00:28:11 +01001790 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001791 config = &dev->mode_config;
1792
1793 /*
1794 * This ioctl is called twice, once to determine how much space is
1795 * needed, and the 2nd time to fill it.
1796 */
1797 if (config->num_plane &&
1798 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001799 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001800
1801 list_for_each_entry(plane, &config->plane_list, head) {
1802 if (put_user(plane->base.id, plane_ptr + copied)) {
1803 ret = -EFAULT;
1804 goto out;
1805 }
1806 copied++;
1807 }
1808 }
1809 plane_resp->count_planes = config->num_plane;
1810
1811out:
Daniel Vetter84849902012-12-02 00:28:11 +01001812 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001813 return ret;
1814}
1815
1816/**
1817 * drm_mode_getplane - get plane info
1818 * @dev: DRM device
1819 * @data: ioctl data
1820 * @file_priv: DRM file info
1821 *
1822 * Return plane info, including formats supported, gamma size, any
1823 * current fb, etc.
1824 */
1825int drm_mode_getplane(struct drm_device *dev, void *data,
1826 struct drm_file *file_priv)
1827{
1828 struct drm_mode_get_plane *plane_resp = data;
1829 struct drm_mode_object *obj;
1830 struct drm_plane *plane;
1831 uint32_t __user *format_ptr;
1832 int ret = 0;
1833
1834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1835 return -EINVAL;
1836
Daniel Vetter84849902012-12-02 00:28:11 +01001837 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001838 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1839 DRM_MODE_OBJECT_PLANE);
1840 if (!obj) {
1841 ret = -ENOENT;
1842 goto out;
1843 }
1844 plane = obj_to_plane(obj);
1845
1846 if (plane->crtc)
1847 plane_resp->crtc_id = plane->crtc->base.id;
1848 else
1849 plane_resp->crtc_id = 0;
1850
1851 if (plane->fb)
1852 plane_resp->fb_id = plane->fb->base.id;
1853 else
1854 plane_resp->fb_id = 0;
1855
1856 plane_resp->plane_id = plane->base.id;
1857 plane_resp->possible_crtcs = plane->possible_crtcs;
1858 plane_resp->gamma_size = plane->gamma_size;
1859
1860 /*
1861 * This ioctl is called twice, once to determine how much space is
1862 * needed, and the 2nd time to fill it.
1863 */
1864 if (plane->format_count &&
1865 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001866 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001867 if (copy_to_user(format_ptr,
1868 plane->format_types,
1869 sizeof(uint32_t) * plane->format_count)) {
1870 ret = -EFAULT;
1871 goto out;
1872 }
1873 }
1874 plane_resp->count_format_types = plane->format_count;
1875
1876out:
Daniel Vetter84849902012-12-02 00:28:11 +01001877 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001878 return ret;
1879}
1880
1881/**
1882 * drm_mode_setplane - set up or tear down an plane
1883 * @dev: DRM device
1884 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001885 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001886 *
1887 * Set plane info, including placement, fb, scaling, and other factors.
1888 * Or pass a NULL fb to disable.
1889 */
1890int drm_mode_setplane(struct drm_device *dev, void *data,
1891 struct drm_file *file_priv)
1892{
1893 struct drm_mode_set_plane *plane_req = data;
1894 struct drm_mode_object *obj;
1895 struct drm_plane *plane;
1896 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001897 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001898 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001899 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001900 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001901
1902 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1903 return -EINVAL;
1904
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001905 /*
1906 * First, find the plane, crtc, and fb objects. If not available,
1907 * we don't bother to call the driver.
1908 */
1909 obj = drm_mode_object_find(dev, plane_req->plane_id,
1910 DRM_MODE_OBJECT_PLANE);
1911 if (!obj) {
1912 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1913 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001914 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001915 }
1916 plane = obj_to_plane(obj);
1917
1918 /* No fb means shut it down */
1919 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001920 drm_modeset_lock_all(dev);
1921 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001922 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001923 plane->crtc = NULL;
1924 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001925 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001926 goto out;
1927 }
1928
1929 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1930 DRM_MODE_OBJECT_CRTC);
1931 if (!obj) {
1932 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1933 plane_req->crtc_id);
1934 ret = -ENOENT;
1935 goto out;
1936 }
1937 crtc = obj_to_crtc(obj);
1938
Daniel Vetter786b99e2012-12-02 21:53:40 +01001939 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1940 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001941 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1942 plane_req->fb_id);
1943 ret = -ENOENT;
1944 goto out;
1945 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001946
Ville Syrjälä62443be2011-12-20 00:06:47 +02001947 /* Check whether this plane supports the fb pixel format. */
1948 for (i = 0; i < plane->format_count; i++)
1949 if (fb->pixel_format == plane->format_types[i])
1950 break;
1951 if (i == plane->format_count) {
1952 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1953 ret = -EINVAL;
1954 goto out;
1955 }
1956
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001957 fb_width = fb->width << 16;
1958 fb_height = fb->height << 16;
1959
1960 /* Make sure source coordinates are inside the fb. */
1961 if (plane_req->src_w > fb_width ||
1962 plane_req->src_x > fb_width - plane_req->src_w ||
1963 plane_req->src_h > fb_height ||
1964 plane_req->src_y > fb_height - plane_req->src_h) {
1965 DRM_DEBUG_KMS("Invalid source coordinates "
1966 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1967 plane_req->src_w >> 16,
1968 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1969 plane_req->src_h >> 16,
1970 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1971 plane_req->src_x >> 16,
1972 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1973 plane_req->src_y >> 16,
1974 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1975 ret = -ENOSPC;
1976 goto out;
1977 }
1978
Ville Syrjälä687a0402011-12-20 00:06:45 +02001979 /* Give drivers some help against integer overflows */
1980 if (plane_req->crtc_w > INT_MAX ||
1981 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1982 plane_req->crtc_h > INT_MAX ||
1983 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1984 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1985 plane_req->crtc_w, plane_req->crtc_h,
1986 plane_req->crtc_x, plane_req->crtc_y);
1987 ret = -ERANGE;
1988 goto out;
1989 }
1990
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001991 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001992 ret = plane->funcs->update_plane(plane, crtc, fb,
1993 plane_req->crtc_x, plane_req->crtc_y,
1994 plane_req->crtc_w, plane_req->crtc_h,
1995 plane_req->src_x, plane_req->src_y,
1996 plane_req->src_w, plane_req->src_h);
1997 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001998 old_fb = plane->fb;
1999 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002000 plane->crtc = crtc;
2001 plane->fb = fb;
2002 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002003 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002004
2005out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002006 if (fb)
2007 drm_framebuffer_unreference(fb);
2008 if (old_fb)
2009 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002010
2011 return ret;
2012}
2013
2014/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002015 * drm_mode_set_config_internal - helper to call ->set_config
2016 * @set: modeset config to set
2017 *
2018 * This is a little helper to wrap internal calls to the ->set_config driver
2019 * interface. The only thing it adds is correct refcounting dance.
2020 */
2021int drm_mode_set_config_internal(struct drm_mode_set *set)
2022{
2023 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002024 struct drm_framebuffer *fb, *old_fb;
2025 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002026
Daniel Vetterb0d12322012-12-11 01:07:12 +01002027 old_fb = crtc->fb;
2028 fb = set->fb;
2029
2030 ret = crtc->funcs->set_config(set);
2031 if (ret == 0) {
2032 if (old_fb)
2033 drm_framebuffer_unreference(old_fb);
2034 if (fb)
2035 drm_framebuffer_reference(fb);
2036 }
2037
2038 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002039}
2040EXPORT_SYMBOL(drm_mode_set_config_internal);
2041
2042/**
Dave Airlief453ba02008-11-07 14:05:41 -08002043 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002044 * @dev: drm device for the ioctl
2045 * @data: data pointer for the ioctl
2046 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002047 *
Dave Airlief453ba02008-11-07 14:05:41 -08002048 * Build a new CRTC configuration based on user request.
2049 *
2050 * Called by the user via ioctl.
2051 *
2052 * RETURNS:
2053 * Zero on success, errno on failure.
2054 */
2055int drm_mode_setcrtc(struct drm_device *dev, void *data,
2056 struct drm_file *file_priv)
2057{
2058 struct drm_mode_config *config = &dev->mode_config;
2059 struct drm_mode_crtc *crtc_req = data;
2060 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002061 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002062 struct drm_connector **connector_set = NULL, *connector;
2063 struct drm_framebuffer *fb = NULL;
2064 struct drm_display_mode *mode = NULL;
2065 struct drm_mode_set set;
2066 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002067 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002068 int i;
2069
Dave Airliefb3b06c2011-02-08 13:55:21 +10002070 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2071 return -EINVAL;
2072
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002073 /* For some reason crtc x/y offsets are signed internally. */
2074 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2075 return -ERANGE;
2076
Daniel Vetter84849902012-12-02 00:28:11 +01002077 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002078 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2079 DRM_MODE_OBJECT_CRTC);
2080 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002081 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002082 ret = -EINVAL;
2083 goto out;
2084 }
2085 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002086 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002087
2088 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002089 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002090 /* If we have a mode we need a framebuffer. */
2091 /* If we pass -1, set the mode with the currently bound fb */
2092 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002093 if (!crtc->fb) {
2094 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2095 ret = -EINVAL;
2096 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002097 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002098 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002099 /* Make refcounting symmetric with the lookup path. */
2100 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002101 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002102 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2103 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002104 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2105 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002106 ret = -EINVAL;
2107 goto out;
2108 }
Dave Airlief453ba02008-11-07 14:05:41 -08002109 }
2110
2111 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002112 if (!mode) {
2113 ret = -ENOMEM;
2114 goto out;
2115 }
2116
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002117 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2118 if (ret) {
2119 DRM_DEBUG_KMS("Invalid mode\n");
2120 goto out;
2121 }
2122
Dave Airlief453ba02008-11-07 14:05:41 -08002123 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002124
Rob Clark7c80e122012-09-04 16:35:56 +00002125 hdisplay = mode->hdisplay;
2126 vdisplay = mode->vdisplay;
2127
2128 if (crtc->invert_dimensions)
2129 swap(hdisplay, vdisplay);
2130
2131 if (hdisplay > fb->width ||
2132 vdisplay > fb->height ||
2133 crtc_req->x > fb->width - hdisplay ||
2134 crtc_req->y > fb->height - vdisplay) {
2135 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2136 fb->width, fb->height,
2137 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2138 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002139 ret = -ENOSPC;
2140 goto out;
2141 }
Dave Airlief453ba02008-11-07 14:05:41 -08002142 }
2143
2144 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002145 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002146 ret = -EINVAL;
2147 goto out;
2148 }
2149
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002150 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002151 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002152 crtc_req->count_connectors);
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 if (crtc_req->count_connectors > 0) {
2158 u32 out_id;
2159
2160 /* Avoid unbounded kernel memory allocation */
2161 if (crtc_req->count_connectors > config->num_connector) {
2162 ret = -EINVAL;
2163 goto out;
2164 }
2165
2166 connector_set = kmalloc(crtc_req->count_connectors *
2167 sizeof(struct drm_connector *),
2168 GFP_KERNEL);
2169 if (!connector_set) {
2170 ret = -ENOMEM;
2171 goto out;
2172 }
2173
2174 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002175 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002176 if (get_user(out_id, &set_connectors_ptr[i])) {
2177 ret = -EFAULT;
2178 goto out;
2179 }
2180
2181 obj = drm_mode_object_find(dev, out_id,
2182 DRM_MODE_OBJECT_CONNECTOR);
2183 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002184 DRM_DEBUG_KMS("Connector id %d unknown\n",
2185 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002186 ret = -EINVAL;
2187 goto out;
2188 }
2189 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002190 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2191 connector->base.id,
2192 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002193
2194 connector_set[i] = connector;
2195 }
2196 }
2197
2198 set.crtc = crtc;
2199 set.x = crtc_req->x;
2200 set.y = crtc_req->y;
2201 set.mode = mode;
2202 set.connectors = connector_set;
2203 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002204 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002205 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002206
2207out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002208 if (fb)
2209 drm_framebuffer_unreference(fb);
2210
Dave Airlief453ba02008-11-07 14:05:41 -08002211 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002212 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002213 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002214 return ret;
2215}
2216
2217int drm_mode_cursor_ioctl(struct drm_device *dev,
2218 void *data, struct drm_file *file_priv)
2219{
2220 struct drm_mode_cursor *req = data;
2221 struct drm_mode_object *obj;
2222 struct drm_crtc *crtc;
2223 int ret = 0;
2224
Dave Airliefb3b06c2011-02-08 13:55:21 +10002225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2226 return -EINVAL;
2227
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002228 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002229 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002230
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002231 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002232 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002233 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002234 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002235 }
2236 crtc = obj_to_crtc(obj);
2237
Daniel Vetterdac35662012-12-02 15:24:10 +01002238 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002239 if (req->flags & DRM_MODE_CURSOR_BO) {
2240 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002241 ret = -ENXIO;
2242 goto out;
2243 }
2244 /* Turns off the cursor if handle is 0 */
2245 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2246 req->width, req->height);
2247 }
2248
2249 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2250 if (crtc->funcs->cursor_move) {
2251 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2252 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002253 ret = -EFAULT;
2254 goto out;
2255 }
2256 }
2257out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002258 mutex_unlock(&crtc->mutex);
2259
Dave Airlief453ba02008-11-07 14:05:41 -08002260 return ret;
2261}
2262
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002263/* Original addfb only supported RGB formats, so figure out which one */
2264uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2265{
2266 uint32_t fmt;
2267
2268 switch (bpp) {
2269 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002270 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002271 break;
2272 case 16:
2273 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002274 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002275 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002276 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002277 break;
2278 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002279 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002280 break;
2281 case 32:
2282 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002283 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002284 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002285 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002286 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002287 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002288 break;
2289 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002290 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2291 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002292 break;
2293 }
2294
2295 return fmt;
2296}
2297EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2298
Dave Airlief453ba02008-11-07 14:05:41 -08002299/**
2300 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002301 * @dev: drm device for the ioctl
2302 * @data: data pointer for the ioctl
2303 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002304 *
Dave Airlief453ba02008-11-07 14:05:41 -08002305 * Add a new FB to the specified CRTC, given a user request.
2306 *
2307 * Called by the user via ioctl.
2308 *
2309 * RETURNS:
2310 * Zero on success, errno on failure.
2311 */
2312int drm_mode_addfb(struct drm_device *dev,
2313 void *data, struct drm_file *file_priv)
2314{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002315 struct drm_mode_fb_cmd *or = data;
2316 struct drm_mode_fb_cmd2 r = {};
2317 struct drm_mode_config *config = &dev->mode_config;
2318 struct drm_framebuffer *fb;
2319 int ret = 0;
2320
2321 /* Use new struct with format internally */
2322 r.fb_id = or->fb_id;
2323 r.width = or->width;
2324 r.height = or->height;
2325 r.pitches[0] = or->pitch;
2326 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2327 r.handles[0] = or->handle;
2328
2329 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2330 return -EINVAL;
2331
Jesse Barnesacb4b992011-11-07 12:03:23 -08002332 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002333 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002334
2335 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002336 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002337
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002338 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2339 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002340 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002341 drm_modeset_unlock_all(dev);
2342 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002343 }
2344
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002345 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002346 or->fb_id = fb->base.id;
2347 list_add(&fb->filp_head, &file_priv->fbs);
2348 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002349 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002350
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002351 return ret;
2352}
2353
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002354static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002355{
2356 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2357
2358 switch (format) {
2359 case DRM_FORMAT_C8:
2360 case DRM_FORMAT_RGB332:
2361 case DRM_FORMAT_BGR233:
2362 case DRM_FORMAT_XRGB4444:
2363 case DRM_FORMAT_XBGR4444:
2364 case DRM_FORMAT_RGBX4444:
2365 case DRM_FORMAT_BGRX4444:
2366 case DRM_FORMAT_ARGB4444:
2367 case DRM_FORMAT_ABGR4444:
2368 case DRM_FORMAT_RGBA4444:
2369 case DRM_FORMAT_BGRA4444:
2370 case DRM_FORMAT_XRGB1555:
2371 case DRM_FORMAT_XBGR1555:
2372 case DRM_FORMAT_RGBX5551:
2373 case DRM_FORMAT_BGRX5551:
2374 case DRM_FORMAT_ARGB1555:
2375 case DRM_FORMAT_ABGR1555:
2376 case DRM_FORMAT_RGBA5551:
2377 case DRM_FORMAT_BGRA5551:
2378 case DRM_FORMAT_RGB565:
2379 case DRM_FORMAT_BGR565:
2380 case DRM_FORMAT_RGB888:
2381 case DRM_FORMAT_BGR888:
2382 case DRM_FORMAT_XRGB8888:
2383 case DRM_FORMAT_XBGR8888:
2384 case DRM_FORMAT_RGBX8888:
2385 case DRM_FORMAT_BGRX8888:
2386 case DRM_FORMAT_ARGB8888:
2387 case DRM_FORMAT_ABGR8888:
2388 case DRM_FORMAT_RGBA8888:
2389 case DRM_FORMAT_BGRA8888:
2390 case DRM_FORMAT_XRGB2101010:
2391 case DRM_FORMAT_XBGR2101010:
2392 case DRM_FORMAT_RGBX1010102:
2393 case DRM_FORMAT_BGRX1010102:
2394 case DRM_FORMAT_ARGB2101010:
2395 case DRM_FORMAT_ABGR2101010:
2396 case DRM_FORMAT_RGBA1010102:
2397 case DRM_FORMAT_BGRA1010102:
2398 case DRM_FORMAT_YUYV:
2399 case DRM_FORMAT_YVYU:
2400 case DRM_FORMAT_UYVY:
2401 case DRM_FORMAT_VYUY:
2402 case DRM_FORMAT_AYUV:
2403 case DRM_FORMAT_NV12:
2404 case DRM_FORMAT_NV21:
2405 case DRM_FORMAT_NV16:
2406 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002407 case DRM_FORMAT_NV24:
2408 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002409 case DRM_FORMAT_YUV410:
2410 case DRM_FORMAT_YVU410:
2411 case DRM_FORMAT_YUV411:
2412 case DRM_FORMAT_YVU411:
2413 case DRM_FORMAT_YUV420:
2414 case DRM_FORMAT_YVU420:
2415 case DRM_FORMAT_YUV422:
2416 case DRM_FORMAT_YVU422:
2417 case DRM_FORMAT_YUV444:
2418 case DRM_FORMAT_YVU444:
2419 return 0;
2420 default:
2421 return -EINVAL;
2422 }
2423}
2424
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002425static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002426{
2427 int ret, hsub, vsub, num_planes, i;
2428
2429 ret = format_check(r);
2430 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002431 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002432 return ret;
2433 }
2434
2435 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2436 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2437 num_planes = drm_format_num_planes(r->pixel_format);
2438
2439 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002440 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002441 return -EINVAL;
2442 }
2443
2444 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002445 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002446 return -EINVAL;
2447 }
2448
2449 for (i = 0; i < num_planes; i++) {
2450 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002451 unsigned int height = r->height / (i != 0 ? vsub : 1);
2452 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002453
2454 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002455 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002456 return -EINVAL;
2457 }
2458
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002459 if ((uint64_t) width * cpp > UINT_MAX)
2460 return -ERANGE;
2461
2462 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2463 return -ERANGE;
2464
2465 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002466 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002467 return -EINVAL;
2468 }
2469 }
2470
2471 return 0;
2472}
2473
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002474/**
2475 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002476 * @dev: drm device for the ioctl
2477 * @data: data pointer for the ioctl
2478 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002479 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002480 * Add a new FB to the specified CRTC, given a user request with format.
2481 *
2482 * Called by the user via ioctl.
2483 *
2484 * RETURNS:
2485 * Zero on success, errno on failure.
2486 */
2487int drm_mode_addfb2(struct drm_device *dev,
2488 void *data, struct drm_file *file_priv)
2489{
2490 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002491 struct drm_mode_config *config = &dev->mode_config;
2492 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002493 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002494
Dave Airliefb3b06c2011-02-08 13:55:21 +10002495 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2496 return -EINVAL;
2497
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002498 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2499 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2500 return -EINVAL;
2501 }
2502
Dave Airlief453ba02008-11-07 14:05:41 -08002503 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002504 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002505 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002506 return -EINVAL;
2507 }
2508 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002509 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002510 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002511 return -EINVAL;
2512 }
2513
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002514 ret = framebuffer_check(r);
2515 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002516 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002517
Dave Airlief453ba02008-11-07 14:05:41 -08002518 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002519 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002520 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002521 drm_modeset_unlock_all(dev);
2522 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002523 }
2524
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002525 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002526 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002527 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002528 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002529 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002530
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002531
Dave Airlief453ba02008-11-07 14:05:41 -08002532 return ret;
2533}
2534
2535/**
2536 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002537 * @dev: drm device for the ioctl
2538 * @data: data pointer for the ioctl
2539 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002540 *
Dave Airlief453ba02008-11-07 14:05:41 -08002541 * Remove the FB specified by the user.
2542 *
2543 * Called by the user via ioctl.
2544 *
2545 * RETURNS:
2546 * Zero on success, errno on failure.
2547 */
2548int drm_mode_rmfb(struct drm_device *dev,
2549 void *data, struct drm_file *file_priv)
2550{
Dave Airlief453ba02008-11-07 14:05:41 -08002551 struct drm_framebuffer *fb = NULL;
2552 struct drm_framebuffer *fbl = NULL;
2553 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002554 int found = 0;
2555
Dave Airliefb3b06c2011-02-08 13:55:21 +10002556 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2557 return -EINVAL;
2558
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002559 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002560 mutex_lock(&dev->mode_config.fb_lock);
2561 fb = __drm_framebuffer_lookup(dev, *id);
2562 if (!fb)
2563 goto fail_lookup;
2564
Dave Airlief453ba02008-11-07 14:05:41 -08002565 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2566 if (fb == fbl)
2567 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002568 if (!found)
2569 goto fail_lookup;
2570
2571 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2572 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002573
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002574 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002575 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002576 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002577
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002578 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002579
Daniel Vetter2b677e82012-12-10 21:16:05 +01002580 return 0;
2581
2582fail_lookup:
2583 mutex_unlock(&dev->mode_config.fb_lock);
2584 mutex_unlock(&file_priv->fbs_lock);
2585
2586 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002587}
2588
2589/**
2590 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002591 * @dev: drm device for the ioctl
2592 * @data: data pointer for the ioctl
2593 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002594 *
Dave Airlief453ba02008-11-07 14:05:41 -08002595 * Lookup the FB given its ID and return info about it.
2596 *
2597 * Called by the user via ioctl.
2598 *
2599 * RETURNS:
2600 * Zero on success, errno on failure.
2601 */
2602int drm_mode_getfb(struct drm_device *dev,
2603 void *data, struct drm_file *file_priv)
2604{
2605 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002606 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002607 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002608
Dave Airliefb3b06c2011-02-08 13:55:21 +10002609 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2610 return -EINVAL;
2611
Daniel Vetter786b99e2012-12-02 21:53:40 +01002612 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002613 if (!fb)
2614 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002615
2616 r->height = fb->height;
2617 r->width = fb->width;
2618 r->depth = fb->depth;
2619 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002620 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002621 if (fb->funcs->create_handle)
2622 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2623 else
2624 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002625
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002626 drm_framebuffer_unreference(fb);
2627
Dave Airlief453ba02008-11-07 14:05:41 -08002628 return ret;
2629}
2630
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002631int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2632 void *data, struct drm_file *file_priv)
2633{
2634 struct drm_clip_rect __user *clips_ptr;
2635 struct drm_clip_rect *clips = NULL;
2636 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002637 struct drm_framebuffer *fb;
2638 unsigned flags;
2639 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002640 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002641
Dave Airliefb3b06c2011-02-08 13:55:21 +10002642 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2643 return -EINVAL;
2644
Daniel Vetter786b99e2012-12-02 21:53:40 +01002645 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002646 if (!fb)
2647 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002648
2649 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002650 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002651
2652 if (!num_clips != !clips_ptr) {
2653 ret = -EINVAL;
2654 goto out_err1;
2655 }
2656
2657 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2658
2659 /* If userspace annotates copy, clips must come in pairs */
2660 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2661 ret = -EINVAL;
2662 goto out_err1;
2663 }
2664
2665 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002666 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2667 ret = -EINVAL;
2668 goto out_err1;
2669 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002670 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2671 if (!clips) {
2672 ret = -ENOMEM;
2673 goto out_err1;
2674 }
2675
2676 ret = copy_from_user(clips, clips_ptr,
2677 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002678 if (ret) {
2679 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002680 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002681 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002682 }
2683
2684 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002685 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002686 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2687 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002688 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002689 } else {
2690 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002691 }
2692
2693out_err2:
2694 kfree(clips);
2695out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002696 drm_framebuffer_unreference(fb);
2697
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002698 return ret;
2699}
2700
2701
Dave Airlief453ba02008-11-07 14:05:41 -08002702/**
2703 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002704 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002705 *
Dave Airlief453ba02008-11-07 14:05:41 -08002706 * Destroy all the FBs associated with @filp.
2707 *
2708 * Called by the user via ioctl.
2709 *
2710 * RETURNS:
2711 * Zero on success, errno on failure.
2712 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002713void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002714{
Dave Airlief453ba02008-11-07 14:05:41 -08002715 struct drm_device *dev = priv->minor->dev;
2716 struct drm_framebuffer *fb, *tfb;
2717
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002718 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002719 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002720
2721 mutex_lock(&dev->mode_config.fb_lock);
2722 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2723 __drm_framebuffer_unregister(dev, fb);
2724 mutex_unlock(&dev->mode_config.fb_lock);
2725
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002726 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002727
2728 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002729 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002730 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002731 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002732}
2733
2734/**
2735 * drm_mode_attachmode - add a mode to the user mode list
2736 * @dev: DRM device
2737 * @connector: connector to add the mode to
2738 * @mode: mode to add
2739 *
2740 * Add @mode to @connector's user mode list.
2741 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002742static void drm_mode_attachmode(struct drm_device *dev,
2743 struct drm_connector *connector,
2744 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002745{
Dave Airlief453ba02008-11-07 14:05:41 -08002746 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002747}
2748
2749int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002750 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002751{
2752 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002753 int ret = 0;
2754 struct drm_display_mode *dup_mode, *next;
2755 LIST_HEAD(list);
2756
Dave Airlief453ba02008-11-07 14:05:41 -08002757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2758 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002759 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002760 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002761 dup_mode = drm_mode_duplicate(dev, mode);
2762 if (!dup_mode) {
2763 ret = -ENOMEM;
2764 goto out;
2765 }
2766 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002767 }
2768 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002769
2770 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2771 if (!connector->encoder)
2772 continue;
2773 if (connector->encoder->crtc == crtc)
2774 list_move_tail(list.next, &connector->user_modes);
2775 }
2776
2777 WARN_ON(!list_empty(&list));
2778
2779 out:
2780 list_for_each_entry_safe(dup_mode, next, &list, head)
2781 drm_mode_destroy(dev, dup_mode);
2782
2783 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002784}
2785EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2786
2787static int drm_mode_detachmode(struct drm_device *dev,
2788 struct drm_connector *connector,
2789 struct drm_display_mode *mode)
2790{
2791 int found = 0;
2792 int ret = 0;
2793 struct drm_display_mode *match_mode, *t;
2794
2795 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2796 if (drm_mode_equal(match_mode, mode)) {
2797 list_del(&match_mode->head);
2798 drm_mode_destroy(dev, match_mode);
2799 found = 1;
2800 break;
2801 }
2802 }
2803
2804 if (!found)
2805 ret = -EINVAL;
2806
2807 return ret;
2808}
2809
2810int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2811{
2812 struct drm_connector *connector;
2813
2814 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2815 drm_mode_detachmode(dev, connector, mode);
2816 }
2817 return 0;
2818}
2819EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2820
2821/**
2822 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002823 * @dev: drm device for the ioctl
2824 * @data: data pointer for the ioctl
2825 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002826 *
2827 * This attaches a user specified mode to an connector.
2828 * Called by the user via ioctl.
2829 *
2830 * RETURNS:
2831 * Zero on success, errno on failure.
2832 */
2833int drm_mode_attachmode_ioctl(struct drm_device *dev,
2834 void *data, struct drm_file *file_priv)
2835{
2836 struct drm_mode_mode_cmd *mode_cmd = data;
2837 struct drm_connector *connector;
2838 struct drm_display_mode *mode;
2839 struct drm_mode_object *obj;
2840 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002841 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002842
Dave Airliefb3b06c2011-02-08 13:55:21 +10002843 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2844 return -EINVAL;
2845
Daniel Vetter84849902012-12-02 00:28:11 +01002846 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002847
2848 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2849 if (!obj) {
2850 ret = -EINVAL;
2851 goto out;
2852 }
2853 connector = obj_to_connector(obj);
2854
2855 mode = drm_mode_create(dev);
2856 if (!mode) {
2857 ret = -ENOMEM;
2858 goto out;
2859 }
2860
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002861 ret = drm_crtc_convert_umode(mode, umode);
2862 if (ret) {
2863 DRM_DEBUG_KMS("Invalid mode\n");
2864 drm_mode_destroy(dev, mode);
2865 goto out;
2866 }
Dave Airlief453ba02008-11-07 14:05:41 -08002867
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002868 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002869out:
Daniel Vetter84849902012-12-02 00:28:11 +01002870 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002871 return ret;
2872}
2873
2874
2875/**
2876 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002877 * @dev: drm device for the ioctl
2878 * @data: data pointer for the ioctl
2879 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002880 *
2881 * Called by the user via ioctl.
2882 *
2883 * RETURNS:
2884 * Zero on success, errno on failure.
2885 */
2886int drm_mode_detachmode_ioctl(struct drm_device *dev,
2887 void *data, struct drm_file *file_priv)
2888{
2889 struct drm_mode_object *obj;
2890 struct drm_mode_mode_cmd *mode_cmd = data;
2891 struct drm_connector *connector;
2892 struct drm_display_mode mode;
2893 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002894 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002895
Dave Airliefb3b06c2011-02-08 13:55:21 +10002896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2897 return -EINVAL;
2898
Daniel Vetter84849902012-12-02 00:28:11 +01002899 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002900
2901 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2902 if (!obj) {
2903 ret = -EINVAL;
2904 goto out;
2905 }
2906 connector = obj_to_connector(obj);
2907
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002908 ret = drm_crtc_convert_umode(&mode, umode);
2909 if (ret) {
2910 DRM_DEBUG_KMS("Invalid mode\n");
2911 goto out;
2912 }
2913
Dave Airlief453ba02008-11-07 14:05:41 -08002914 ret = drm_mode_detachmode(dev, connector, &mode);
2915out:
Daniel Vetter84849902012-12-02 00:28:11 +01002916 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002917 return ret;
2918}
2919
2920struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2921 const char *name, int num_values)
2922{
2923 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002924 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002925
2926 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2927 if (!property)
2928 return NULL;
2929
2930 if (num_values) {
2931 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2932 if (!property->values)
2933 goto fail;
2934 }
2935
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002936 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2937 if (ret)
2938 goto fail;
2939
Dave Airlief453ba02008-11-07 14:05:41 -08002940 property->flags = flags;
2941 property->num_values = num_values;
2942 INIT_LIST_HEAD(&property->enum_blob_list);
2943
Vinson Lee471dd2e2011-11-10 11:55:40 -08002944 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002945 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002946 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2947 }
Dave Airlief453ba02008-11-07 14:05:41 -08002948
2949 list_add_tail(&property->head, &dev->mode_config.property_list);
2950 return property;
2951fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002952 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002953 kfree(property);
2954 return NULL;
2955}
2956EXPORT_SYMBOL(drm_property_create);
2957
Sascha Hauer4a67d392012-02-06 10:58:17 +01002958struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2959 const char *name,
2960 const struct drm_prop_enum_list *props,
2961 int num_values)
2962{
2963 struct drm_property *property;
2964 int i, ret;
2965
2966 flags |= DRM_MODE_PROP_ENUM;
2967
2968 property = drm_property_create(dev, flags, name, num_values);
2969 if (!property)
2970 return NULL;
2971
2972 for (i = 0; i < num_values; i++) {
2973 ret = drm_property_add_enum(property, i,
2974 props[i].type,
2975 props[i].name);
2976 if (ret) {
2977 drm_property_destroy(dev, property);
2978 return NULL;
2979 }
2980 }
2981
2982 return property;
2983}
2984EXPORT_SYMBOL(drm_property_create_enum);
2985
Rob Clark49e27542012-05-17 02:23:26 -06002986struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2987 int flags, const char *name,
2988 const struct drm_prop_enum_list *props,
2989 int num_values)
2990{
2991 struct drm_property *property;
2992 int i, ret;
2993
2994 flags |= DRM_MODE_PROP_BITMASK;
2995
2996 property = drm_property_create(dev, flags, name, num_values);
2997 if (!property)
2998 return NULL;
2999
3000 for (i = 0; i < num_values; i++) {
3001 ret = drm_property_add_enum(property, i,
3002 props[i].type,
3003 props[i].name);
3004 if (ret) {
3005 drm_property_destroy(dev, property);
3006 return NULL;
3007 }
3008 }
3009
3010 return property;
3011}
3012EXPORT_SYMBOL(drm_property_create_bitmask);
3013
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003014struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3015 const char *name,
3016 uint64_t min, uint64_t max)
3017{
3018 struct drm_property *property;
3019
3020 flags |= DRM_MODE_PROP_RANGE;
3021
3022 property = drm_property_create(dev, flags, name, 2);
3023 if (!property)
3024 return NULL;
3025
3026 property->values[0] = min;
3027 property->values[1] = max;
3028
3029 return property;
3030}
3031EXPORT_SYMBOL(drm_property_create_range);
3032
Dave Airlief453ba02008-11-07 14:05:41 -08003033int drm_property_add_enum(struct drm_property *property, int index,
3034 uint64_t value, const char *name)
3035{
3036 struct drm_property_enum *prop_enum;
3037
Rob Clark49e27542012-05-17 02:23:26 -06003038 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3039 return -EINVAL;
3040
3041 /*
3042 * Bitmask enum properties have the additional constraint of values
3043 * from 0 to 63
3044 */
3045 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003046 return -EINVAL;
3047
3048 if (!list_empty(&property->enum_blob_list)) {
3049 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3050 if (prop_enum->value == value) {
3051 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3052 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3053 return 0;
3054 }
3055 }
3056 }
3057
3058 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3059 if (!prop_enum)
3060 return -ENOMEM;
3061
3062 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3063 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3064 prop_enum->value = value;
3065
3066 property->values[index] = value;
3067 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3068 return 0;
3069}
3070EXPORT_SYMBOL(drm_property_add_enum);
3071
3072void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3073{
3074 struct drm_property_enum *prop_enum, *pt;
3075
3076 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3077 list_del(&prop_enum->head);
3078 kfree(prop_enum);
3079 }
3080
3081 if (property->num_values)
3082 kfree(property->values);
3083 drm_mode_object_put(dev, &property->base);
3084 list_del(&property->head);
3085 kfree(property);
3086}
3087EXPORT_SYMBOL(drm_property_destroy);
3088
Paulo Zanonic5431882012-05-15 18:09:02 -03003089void drm_object_attach_property(struct drm_mode_object *obj,
3090 struct drm_property *property,
3091 uint64_t init_val)
3092{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003093 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003094
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003095 if (count == DRM_OBJECT_MAX_PROPERTY) {
3096 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3097 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3098 "you see this message on the same object type.\n",
3099 obj->type);
3100 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003101 }
3102
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003103 obj->properties->ids[count] = property->base.id;
3104 obj->properties->values[count] = init_val;
3105 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003106}
3107EXPORT_SYMBOL(drm_object_attach_property);
3108
3109int drm_object_property_set_value(struct drm_mode_object *obj,
3110 struct drm_property *property, uint64_t val)
3111{
3112 int i;
3113
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003114 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003115 if (obj->properties->ids[i] == property->base.id) {
3116 obj->properties->values[i] = val;
3117 return 0;
3118 }
3119 }
3120
3121 return -EINVAL;
3122}
3123EXPORT_SYMBOL(drm_object_property_set_value);
3124
3125int drm_object_property_get_value(struct drm_mode_object *obj,
3126 struct drm_property *property, uint64_t *val)
3127{
3128 int i;
3129
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003130 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003131 if (obj->properties->ids[i] == property->base.id) {
3132 *val = obj->properties->values[i];
3133 return 0;
3134 }
3135 }
3136
3137 return -EINVAL;
3138}
3139EXPORT_SYMBOL(drm_object_property_get_value);
3140
Dave Airlief453ba02008-11-07 14:05:41 -08003141int drm_mode_getproperty_ioctl(struct drm_device *dev,
3142 void *data, struct drm_file *file_priv)
3143{
3144 struct drm_mode_object *obj;
3145 struct drm_mode_get_property *out_resp = data;
3146 struct drm_property *property;
3147 int enum_count = 0;
3148 int blob_count = 0;
3149 int value_count = 0;
3150 int ret = 0, i;
3151 int copied;
3152 struct drm_property_enum *prop_enum;
3153 struct drm_mode_property_enum __user *enum_ptr;
3154 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003155 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003156 uint64_t __user *values_ptr;
3157 uint32_t __user *blob_length_ptr;
3158
Dave Airliefb3b06c2011-02-08 13:55:21 +10003159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3160 return -EINVAL;
3161
Daniel Vetter84849902012-12-02 00:28:11 +01003162 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003163 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3164 if (!obj) {
3165 ret = -EINVAL;
3166 goto done;
3167 }
3168 property = obj_to_property(obj);
3169
Rob Clark49e27542012-05-17 02:23:26 -06003170 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003171 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3172 enum_count++;
3173 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3174 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3175 blob_count++;
3176 }
3177
3178 value_count = property->num_values;
3179
3180 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3181 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3182 out_resp->flags = property->flags;
3183
3184 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003185 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003186 for (i = 0; i < value_count; i++) {
3187 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3188 ret = -EFAULT;
3189 goto done;
3190 }
3191 }
3192 }
3193 out_resp->count_values = value_count;
3194
Rob Clark49e27542012-05-17 02:23:26 -06003195 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003196 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3197 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003198 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003199 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3200
3201 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3202 ret = -EFAULT;
3203 goto done;
3204 }
3205
3206 if (copy_to_user(&enum_ptr[copied].name,
3207 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3208 ret = -EFAULT;
3209 goto done;
3210 }
3211 copied++;
3212 }
3213 }
3214 out_resp->count_enum_blobs = enum_count;
3215 }
3216
3217 if (property->flags & DRM_MODE_PROP_BLOB) {
3218 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3219 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003220 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3221 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003222
3223 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3224 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3225 ret = -EFAULT;
3226 goto done;
3227 }
3228
3229 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3230 ret = -EFAULT;
3231 goto done;
3232 }
3233
3234 copied++;
3235 }
3236 }
3237 out_resp->count_enum_blobs = blob_count;
3238 }
3239done:
Daniel Vetter84849902012-12-02 00:28:11 +01003240 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003241 return ret;
3242}
3243
3244static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3245 void *data)
3246{
3247 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003248 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003249
3250 if (!length || !data)
3251 return NULL;
3252
3253 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3254 if (!blob)
3255 return NULL;
3256
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003257 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3258 if (ret) {
3259 kfree(blob);
3260 return NULL;
3261 }
3262
Dave Airlief453ba02008-11-07 14:05:41 -08003263 blob->length = length;
3264
3265 memcpy(blob->data, data, length);
3266
Dave Airlief453ba02008-11-07 14:05:41 -08003267 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3268 return blob;
3269}
3270
3271static void drm_property_destroy_blob(struct drm_device *dev,
3272 struct drm_property_blob *blob)
3273{
3274 drm_mode_object_put(dev, &blob->base);
3275 list_del(&blob->head);
3276 kfree(blob);
3277}
3278
3279int drm_mode_getblob_ioctl(struct drm_device *dev,
3280 void *data, struct drm_file *file_priv)
3281{
3282 struct drm_mode_object *obj;
3283 struct drm_mode_get_blob *out_resp = data;
3284 struct drm_property_blob *blob;
3285 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003286 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003287
Dave Airliefb3b06c2011-02-08 13:55:21 +10003288 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3289 return -EINVAL;
3290
Daniel Vetter84849902012-12-02 00:28:11 +01003291 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003292 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3293 if (!obj) {
3294 ret = -EINVAL;
3295 goto done;
3296 }
3297 blob = obj_to_blob(obj);
3298
3299 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003300 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003301 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3302 ret = -EFAULT;
3303 goto done;
3304 }
3305 }
3306 out_resp->length = blob->length;
3307
3308done:
Daniel Vetter84849902012-12-02 00:28:11 +01003309 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003310 return ret;
3311}
3312
3313int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3314 struct edid *edid)
3315{
3316 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003317 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003318
3319 if (connector->edid_blob_ptr)
3320 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3321
3322 /* Delete edid, when there is none. */
3323 if (!edid) {
3324 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003325 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003326 return ret;
3327 }
3328
Adam Jackson7466f4c2010-03-29 21:43:23 +00003329 size = EDID_LENGTH * (1 + edid->extensions);
3330 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3331 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003332 if (!connector->edid_blob_ptr)
3333 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003334
Rob Clark58495562012-10-11 20:50:56 -05003335 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003336 dev->mode_config.edid_property,
3337 connector->edid_blob_ptr->base.id);
3338
3339 return ret;
3340}
3341EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3342
Paulo Zanoni26a34812012-05-15 18:08:59 -03003343static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003344 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003345{
3346 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3347 return false;
3348 if (property->flags & DRM_MODE_PROP_RANGE) {
3349 if (value < property->values[0] || value > property->values[1])
3350 return false;
3351 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003352 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3353 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003354 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003355 for (i = 0; i < property->num_values; i++)
3356 valid_mask |= (1ULL << property->values[i]);
3357 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003358 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3359 /* Only the driver knows */
3360 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003361 } else {
3362 int i;
3363 for (i = 0; i < property->num_values; i++)
3364 if (property->values[i] == value)
3365 return true;
3366 return false;
3367 }
3368}
3369
Dave Airlief453ba02008-11-07 14:05:41 -08003370int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3371 void *data, struct drm_file *file_priv)
3372{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003373 struct drm_mode_connector_set_property *conn_set_prop = data;
3374 struct drm_mode_obj_set_property obj_set_prop = {
3375 .value = conn_set_prop->value,
3376 .prop_id = conn_set_prop->prop_id,
3377 .obj_id = conn_set_prop->connector_id,
3378 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3379 };
Dave Airlief453ba02008-11-07 14:05:41 -08003380
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003381 /* It does all the locking and checking we need */
3382 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003383}
3384
Paulo Zanonic5431882012-05-15 18:09:02 -03003385static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3386 struct drm_property *property,
3387 uint64_t value)
3388{
3389 int ret = -EINVAL;
3390 struct drm_connector *connector = obj_to_connector(obj);
3391
3392 /* Do DPMS ourselves */
3393 if (property == connector->dev->mode_config.dpms_property) {
3394 if (connector->funcs->dpms)
3395 (*connector->funcs->dpms)(connector, (int)value);
3396 ret = 0;
3397 } else if (connector->funcs->set_property)
3398 ret = connector->funcs->set_property(connector, property, value);
3399
3400 /* store the property value if successful */
3401 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003402 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003403 return ret;
3404}
3405
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003406static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3407 struct drm_property *property,
3408 uint64_t value)
3409{
3410 int ret = -EINVAL;
3411 struct drm_crtc *crtc = obj_to_crtc(obj);
3412
3413 if (crtc->funcs->set_property)
3414 ret = crtc->funcs->set_property(crtc, property, value);
3415 if (!ret)
3416 drm_object_property_set_value(obj, property, value);
3417
3418 return ret;
3419}
3420
Rob Clark4d939142012-05-17 02:23:27 -06003421static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3422 struct drm_property *property,
3423 uint64_t value)
3424{
3425 int ret = -EINVAL;
3426 struct drm_plane *plane = obj_to_plane(obj);
3427
3428 if (plane->funcs->set_property)
3429 ret = plane->funcs->set_property(plane, property, value);
3430 if (!ret)
3431 drm_object_property_set_value(obj, property, value);
3432
3433 return ret;
3434}
3435
Paulo Zanonic5431882012-05-15 18:09:02 -03003436int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3437 struct drm_file *file_priv)
3438{
3439 struct drm_mode_obj_get_properties *arg = data;
3440 struct drm_mode_object *obj;
3441 int ret = 0;
3442 int i;
3443 int copied = 0;
3444 int props_count = 0;
3445 uint32_t __user *props_ptr;
3446 uint64_t __user *prop_values_ptr;
3447
3448 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3449 return -EINVAL;
3450
Daniel Vetter84849902012-12-02 00:28:11 +01003451 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003452
3453 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3454 if (!obj) {
3455 ret = -EINVAL;
3456 goto out;
3457 }
3458 if (!obj->properties) {
3459 ret = -EINVAL;
3460 goto out;
3461 }
3462
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003463 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003464
3465 /* This ioctl is called twice, once to determine how much space is
3466 * needed, and the 2nd time to fill it. */
3467 if ((arg->count_props >= props_count) && props_count) {
3468 copied = 0;
3469 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3470 prop_values_ptr = (uint64_t __user *)(unsigned long)
3471 (arg->prop_values_ptr);
3472 for (i = 0; i < props_count; i++) {
3473 if (put_user(obj->properties->ids[i],
3474 props_ptr + copied)) {
3475 ret = -EFAULT;
3476 goto out;
3477 }
3478 if (put_user(obj->properties->values[i],
3479 prop_values_ptr + copied)) {
3480 ret = -EFAULT;
3481 goto out;
3482 }
3483 copied++;
3484 }
3485 }
3486 arg->count_props = props_count;
3487out:
Daniel Vetter84849902012-12-02 00:28:11 +01003488 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003489 return ret;
3490}
3491
3492int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3493 struct drm_file *file_priv)
3494{
3495 struct drm_mode_obj_set_property *arg = data;
3496 struct drm_mode_object *arg_obj;
3497 struct drm_mode_object *prop_obj;
3498 struct drm_property *property;
3499 int ret = -EINVAL;
3500 int i;
3501
3502 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3503 return -EINVAL;
3504
Daniel Vetter84849902012-12-02 00:28:11 +01003505 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003506
3507 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3508 if (!arg_obj)
3509 goto out;
3510 if (!arg_obj->properties)
3511 goto out;
3512
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003513 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003514 if (arg_obj->properties->ids[i] == arg->prop_id)
3515 break;
3516
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003517 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003518 goto out;
3519
3520 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3521 DRM_MODE_OBJECT_PROPERTY);
3522 if (!prop_obj)
3523 goto out;
3524 property = obj_to_property(prop_obj);
3525
3526 if (!drm_property_change_is_valid(property, arg->value))
3527 goto out;
3528
3529 switch (arg_obj->type) {
3530 case DRM_MODE_OBJECT_CONNECTOR:
3531 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3532 arg->value);
3533 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003534 case DRM_MODE_OBJECT_CRTC:
3535 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3536 break;
Rob Clark4d939142012-05-17 02:23:27 -06003537 case DRM_MODE_OBJECT_PLANE:
3538 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3539 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003540 }
3541
3542out:
Daniel Vetter84849902012-12-02 00:28:11 +01003543 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003544 return ret;
3545}
3546
Dave Airlief453ba02008-11-07 14:05:41 -08003547int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3548 struct drm_encoder *encoder)
3549{
3550 int i;
3551
3552 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3553 if (connector->encoder_ids[i] == 0) {
3554 connector->encoder_ids[i] = encoder->base.id;
3555 return 0;
3556 }
3557 }
3558 return -ENOMEM;
3559}
3560EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3561
3562void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3563 struct drm_encoder *encoder)
3564{
3565 int i;
3566 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3567 if (connector->encoder_ids[i] == encoder->base.id) {
3568 connector->encoder_ids[i] = 0;
3569 if (connector->encoder == encoder)
3570 connector->encoder = NULL;
3571 break;
3572 }
3573 }
3574}
3575EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3576
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003577int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003578 int gamma_size)
3579{
3580 crtc->gamma_size = gamma_size;
3581
3582 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3583 if (!crtc->gamma_store) {
3584 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003585 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003586 }
3587
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003588 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003589}
3590EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3591
3592int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3593 void *data, struct drm_file *file_priv)
3594{
3595 struct drm_mode_crtc_lut *crtc_lut = data;
3596 struct drm_mode_object *obj;
3597 struct drm_crtc *crtc;
3598 void *r_base, *g_base, *b_base;
3599 int size;
3600 int ret = 0;
3601
Dave Airliefb3b06c2011-02-08 13:55:21 +10003602 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3603 return -EINVAL;
3604
Daniel Vetter84849902012-12-02 00:28:11 +01003605 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003606 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3607 if (!obj) {
3608 ret = -EINVAL;
3609 goto out;
3610 }
3611 crtc = obj_to_crtc(obj);
3612
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003613 if (crtc->funcs->gamma_set == NULL) {
3614 ret = -ENOSYS;
3615 goto out;
3616 }
3617
Dave Airlief453ba02008-11-07 14:05:41 -08003618 /* memcpy into gamma store */
3619 if (crtc_lut->gamma_size != crtc->gamma_size) {
3620 ret = -EINVAL;
3621 goto out;
3622 }
3623
3624 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3625 r_base = crtc->gamma_store;
3626 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3627 ret = -EFAULT;
3628 goto out;
3629 }
3630
3631 g_base = r_base + size;
3632 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3633 ret = -EFAULT;
3634 goto out;
3635 }
3636
3637 b_base = g_base + size;
3638 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3639 ret = -EFAULT;
3640 goto out;
3641 }
3642
James Simmons72034252010-08-03 01:33:19 +01003643 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003644
3645out:
Daniel Vetter84849902012-12-02 00:28:11 +01003646 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003647 return ret;
3648
3649}
3650
3651int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3652 void *data, struct drm_file *file_priv)
3653{
3654 struct drm_mode_crtc_lut *crtc_lut = data;
3655 struct drm_mode_object *obj;
3656 struct drm_crtc *crtc;
3657 void *r_base, *g_base, *b_base;
3658 int size;
3659 int ret = 0;
3660
Dave Airliefb3b06c2011-02-08 13:55:21 +10003661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3662 return -EINVAL;
3663
Daniel Vetter84849902012-12-02 00:28:11 +01003664 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003665 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3666 if (!obj) {
3667 ret = -EINVAL;
3668 goto out;
3669 }
3670 crtc = obj_to_crtc(obj);
3671
3672 /* memcpy into gamma store */
3673 if (crtc_lut->gamma_size != crtc->gamma_size) {
3674 ret = -EINVAL;
3675 goto out;
3676 }
3677
3678 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3679 r_base = crtc->gamma_store;
3680 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3681 ret = -EFAULT;
3682 goto out;
3683 }
3684
3685 g_base = r_base + size;
3686 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3687 ret = -EFAULT;
3688 goto out;
3689 }
3690
3691 b_base = g_base + size;
3692 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3693 ret = -EFAULT;
3694 goto out;
3695 }
3696out:
Daniel Vetter84849902012-12-02 00:28:11 +01003697 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003698 return ret;
3699}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003700
3701int drm_mode_page_flip_ioctl(struct drm_device *dev,
3702 void *data, struct drm_file *file_priv)
3703{
3704 struct drm_mode_crtc_page_flip *page_flip = data;
3705 struct drm_mode_object *obj;
3706 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003707 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003708 struct drm_pending_vblank_event *e = NULL;
3709 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003710 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003711 int ret = -EINVAL;
3712
3713 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3714 page_flip->reserved != 0)
3715 return -EINVAL;
3716
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003717 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3718 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003719 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003720 crtc = obj_to_crtc(obj);
3721
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003722 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003723 if (crtc->fb == NULL) {
3724 /* The framebuffer is currently unbound, presumably
3725 * due to a hotplug event, that userspace has not
3726 * yet discovered.
3727 */
3728 ret = -EBUSY;
3729 goto out;
3730 }
3731
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003732 if (crtc->funcs->page_flip == NULL)
3733 goto out;
3734
Daniel Vetter786b99e2012-12-02 21:53:40 +01003735 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3736 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003737 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003738
Rob Clark7c80e122012-09-04 16:35:56 +00003739 hdisplay = crtc->mode.hdisplay;
3740 vdisplay = crtc->mode.vdisplay;
3741
3742 if (crtc->invert_dimensions)
3743 swap(hdisplay, vdisplay);
3744
3745 if (hdisplay > fb->width ||
3746 vdisplay > fb->height ||
3747 crtc->x > fb->width - hdisplay ||
3748 crtc->y > fb->height - vdisplay) {
3749 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3750 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3751 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003752 ret = -ENOSPC;
3753 goto out;
3754 }
3755
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003756 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3757 ret = -ENOMEM;
3758 spin_lock_irqsave(&dev->event_lock, flags);
3759 if (file_priv->event_space < sizeof e->event) {
3760 spin_unlock_irqrestore(&dev->event_lock, flags);
3761 goto out;
3762 }
3763 file_priv->event_space -= sizeof e->event;
3764 spin_unlock_irqrestore(&dev->event_lock, flags);
3765
3766 e = kzalloc(sizeof *e, GFP_KERNEL);
3767 if (e == NULL) {
3768 spin_lock_irqsave(&dev->event_lock, flags);
3769 file_priv->event_space += sizeof e->event;
3770 spin_unlock_irqrestore(&dev->event_lock, flags);
3771 goto out;
3772 }
3773
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003774 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003775 e->event.base.length = sizeof e->event;
3776 e->event.user_data = page_flip->user_data;
3777 e->base.event = &e->event.base;
3778 e->base.file_priv = file_priv;
3779 e->base.destroy =
3780 (void (*) (struct drm_pending_event *)) kfree;
3781 }
3782
Daniel Vetterb0d12322012-12-11 01:07:12 +01003783 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003784 ret = crtc->funcs->page_flip(crtc, fb, e);
3785 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003786 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3787 spin_lock_irqsave(&dev->event_lock, flags);
3788 file_priv->event_space += sizeof e->event;
3789 spin_unlock_irqrestore(&dev->event_lock, flags);
3790 kfree(e);
3791 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003792 /* Keep the old fb, don't unref it. */
3793 old_fb = NULL;
3794 } else {
3795 /* Unref only the old framebuffer. */
3796 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003797 }
3798
3799out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003800 if (fb)
3801 drm_framebuffer_unreference(fb);
3802 if (old_fb)
3803 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003804 mutex_unlock(&crtc->mutex);
3805
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003806 return ret;
3807}
Chris Wilsoneb033552011-01-24 15:11:08 +00003808
3809void drm_mode_config_reset(struct drm_device *dev)
3810{
3811 struct drm_crtc *crtc;
3812 struct drm_encoder *encoder;
3813 struct drm_connector *connector;
3814
3815 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3816 if (crtc->funcs->reset)
3817 crtc->funcs->reset(crtc);
3818
3819 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3820 if (encoder->funcs->reset)
3821 encoder->funcs->reset(encoder);
3822
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003823 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3824 connector->status = connector_status_unknown;
3825
Chris Wilsoneb033552011-01-24 15:11:08 +00003826 if (connector->funcs->reset)
3827 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003828 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003829}
3830EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003831
3832int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3833 void *data, struct drm_file *file_priv)
3834{
3835 struct drm_mode_create_dumb *args = data;
3836
3837 if (!dev->driver->dumb_create)
3838 return -ENOSYS;
3839 return dev->driver->dumb_create(file_priv, dev, args);
3840}
3841
3842int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3843 void *data, struct drm_file *file_priv)
3844{
3845 struct drm_mode_map_dumb *args = data;
3846
3847 /* call driver ioctl to get mmap offset */
3848 if (!dev->driver->dumb_map_offset)
3849 return -ENOSYS;
3850
3851 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3852}
3853
3854int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3855 void *data, struct drm_file *file_priv)
3856{
3857 struct drm_mode_destroy_dumb *args = data;
3858
3859 if (!dev->driver->dumb_destroy)
3860 return -ENOSYS;
3861
3862 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3863}
Dave Airlie248dbc22011-11-29 20:02:54 +00003864
3865/*
3866 * Just need to support RGB formats here for compat with code that doesn't
3867 * use pixel formats directly yet.
3868 */
3869void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3870 int *bpp)
3871{
3872 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003873 case DRM_FORMAT_RGB332:
3874 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003875 *depth = 8;
3876 *bpp = 8;
3877 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003878 case DRM_FORMAT_XRGB1555:
3879 case DRM_FORMAT_XBGR1555:
3880 case DRM_FORMAT_RGBX5551:
3881 case DRM_FORMAT_BGRX5551:
3882 case DRM_FORMAT_ARGB1555:
3883 case DRM_FORMAT_ABGR1555:
3884 case DRM_FORMAT_RGBA5551:
3885 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003886 *depth = 15;
3887 *bpp = 16;
3888 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003889 case DRM_FORMAT_RGB565:
3890 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003891 *depth = 16;
3892 *bpp = 16;
3893 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003894 case DRM_FORMAT_RGB888:
3895 case DRM_FORMAT_BGR888:
3896 *depth = 24;
3897 *bpp = 24;
3898 break;
3899 case DRM_FORMAT_XRGB8888:
3900 case DRM_FORMAT_XBGR8888:
3901 case DRM_FORMAT_RGBX8888:
3902 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003903 *depth = 24;
3904 *bpp = 32;
3905 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003906 case DRM_FORMAT_XRGB2101010:
3907 case DRM_FORMAT_XBGR2101010:
3908 case DRM_FORMAT_RGBX1010102:
3909 case DRM_FORMAT_BGRX1010102:
3910 case DRM_FORMAT_ARGB2101010:
3911 case DRM_FORMAT_ABGR2101010:
3912 case DRM_FORMAT_RGBA1010102:
3913 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003914 *depth = 30;
3915 *bpp = 32;
3916 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003917 case DRM_FORMAT_ARGB8888:
3918 case DRM_FORMAT_ABGR8888:
3919 case DRM_FORMAT_RGBA8888:
3920 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003921 *depth = 32;
3922 *bpp = 32;
3923 break;
3924 default:
3925 DRM_DEBUG_KMS("unsupported pixel format\n");
3926 *depth = 0;
3927 *bpp = 0;
3928 break;
3929 }
3930}
3931EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003932
3933/**
3934 * drm_format_num_planes - get the number of planes for format
3935 * @format: pixel format (DRM_FORMAT_*)
3936 *
3937 * RETURNS:
3938 * The number of planes used by the specified pixel format.
3939 */
3940int drm_format_num_planes(uint32_t format)
3941{
3942 switch (format) {
3943 case DRM_FORMAT_YUV410:
3944 case DRM_FORMAT_YVU410:
3945 case DRM_FORMAT_YUV411:
3946 case DRM_FORMAT_YVU411:
3947 case DRM_FORMAT_YUV420:
3948 case DRM_FORMAT_YVU420:
3949 case DRM_FORMAT_YUV422:
3950 case DRM_FORMAT_YVU422:
3951 case DRM_FORMAT_YUV444:
3952 case DRM_FORMAT_YVU444:
3953 return 3;
3954 case DRM_FORMAT_NV12:
3955 case DRM_FORMAT_NV21:
3956 case DRM_FORMAT_NV16:
3957 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003958 case DRM_FORMAT_NV24:
3959 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003960 return 2;
3961 default:
3962 return 1;
3963 }
3964}
3965EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003966
3967/**
3968 * drm_format_plane_cpp - determine the bytes per pixel value
3969 * @format: pixel format (DRM_FORMAT_*)
3970 * @plane: plane index
3971 *
3972 * RETURNS:
3973 * The bytes per pixel value for the specified plane.
3974 */
3975int drm_format_plane_cpp(uint32_t format, int plane)
3976{
3977 unsigned int depth;
3978 int bpp;
3979
3980 if (plane >= drm_format_num_planes(format))
3981 return 0;
3982
3983 switch (format) {
3984 case DRM_FORMAT_YUYV:
3985 case DRM_FORMAT_YVYU:
3986 case DRM_FORMAT_UYVY:
3987 case DRM_FORMAT_VYUY:
3988 return 2;
3989 case DRM_FORMAT_NV12:
3990 case DRM_FORMAT_NV21:
3991 case DRM_FORMAT_NV16:
3992 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003993 case DRM_FORMAT_NV24:
3994 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003995 return plane ? 2 : 1;
3996 case DRM_FORMAT_YUV410:
3997 case DRM_FORMAT_YVU410:
3998 case DRM_FORMAT_YUV411:
3999 case DRM_FORMAT_YVU411:
4000 case DRM_FORMAT_YUV420:
4001 case DRM_FORMAT_YVU420:
4002 case DRM_FORMAT_YUV422:
4003 case DRM_FORMAT_YVU422:
4004 case DRM_FORMAT_YUV444:
4005 case DRM_FORMAT_YVU444:
4006 return 1;
4007 default:
4008 drm_fb_get_bpp_depth(format, &depth, &bpp);
4009 return bpp >> 3;
4010 }
4011}
4012EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004013
4014/**
4015 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4016 * @format: pixel format (DRM_FORMAT_*)
4017 *
4018 * RETURNS:
4019 * The horizontal chroma subsampling factor for the
4020 * specified pixel format.
4021 */
4022int drm_format_horz_chroma_subsampling(uint32_t format)
4023{
4024 switch (format) {
4025 case DRM_FORMAT_YUV411:
4026 case DRM_FORMAT_YVU411:
4027 case DRM_FORMAT_YUV410:
4028 case DRM_FORMAT_YVU410:
4029 return 4;
4030 case DRM_FORMAT_YUYV:
4031 case DRM_FORMAT_YVYU:
4032 case DRM_FORMAT_UYVY:
4033 case DRM_FORMAT_VYUY:
4034 case DRM_FORMAT_NV12:
4035 case DRM_FORMAT_NV21:
4036 case DRM_FORMAT_NV16:
4037 case DRM_FORMAT_NV61:
4038 case DRM_FORMAT_YUV422:
4039 case DRM_FORMAT_YVU422:
4040 case DRM_FORMAT_YUV420:
4041 case DRM_FORMAT_YVU420:
4042 return 2;
4043 default:
4044 return 1;
4045 }
4046}
4047EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4048
4049/**
4050 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4051 * @format: pixel format (DRM_FORMAT_*)
4052 *
4053 * RETURNS:
4054 * The vertical chroma subsampling factor for the
4055 * specified pixel format.
4056 */
4057int drm_format_vert_chroma_subsampling(uint32_t format)
4058{
4059 switch (format) {
4060 case DRM_FORMAT_YUV410:
4061 case DRM_FORMAT_YVU410:
4062 return 4;
4063 case DRM_FORMAT_YUV420:
4064 case DRM_FORMAT_YVU420:
4065 case DRM_FORMAT_NV12:
4066 case DRM_FORMAT_NV21:
4067 return 2;
4068 default:
4069 return 1;
4070 }
4071}
4072EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);