blob: 3be0802c6797d9ceb761a8696a194e3562f7bf33 [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
Daniel Vettera9b054e2013-05-02 09:43:05 +020081 /* Locking is currently fubar in the panic handler. */
82 if (oops_in_progress)
83 return;
84
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010085 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
86 WARN_ON(!mutex_is_locked(&crtc->mutex));
87
88 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
89}
90EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
91
Dave Airlief453ba02008-11-07 14:05:41 -080092/* Avoid boilerplate. I'm tired of typing. */
93#define DRM_ENUM_NAME_FN(fnname, list) \
94 char *fnname(int val) \
95 { \
96 int i; \
97 for (i = 0; i < ARRAY_SIZE(list); i++) { \
98 if (list[i].type == val) \
99 return list[i].name; \
100 } \
101 return "(unknown)"; \
102 }
103
104/*
105 * Global properties
106 */
107static struct drm_prop_enum_list drm_dpms_enum_list[] =
108{ { DRM_MODE_DPMS_ON, "On" },
109 { DRM_MODE_DPMS_STANDBY, "Standby" },
110 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
111 { DRM_MODE_DPMS_OFF, "Off" }
112};
113
114DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
115
116/*
117 * Optional properties
118 */
119static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
120{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700121 { DRM_MODE_SCALE_NONE, "None" },
122 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
123 { DRM_MODE_SCALE_CENTER, "Center" },
124 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800125};
126
127static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
128{
129 { DRM_MODE_DITHERING_OFF, "Off" },
130 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000131 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800132};
133
134/*
135 * Non-global properties, but "required" for certain connectors.
136 */
137static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
138{
139 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
140 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
141 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
142};
143
144DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
145
146static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
147{
148 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
149 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
150 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
151};
152
153DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
154 drm_dvi_i_subconnector_enum_list)
155
156static struct drm_prop_enum_list drm_tv_select_enum_list[] =
157{
158 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
159 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
160 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200162 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
165DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
166
167static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
168{
169 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
170 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
171 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200173 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800174};
175
176DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
177 drm_tv_subconnector_enum_list)
178
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000179static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
180 { DRM_MODE_DIRTY_OFF, "Off" },
181 { DRM_MODE_DIRTY_ON, "On" },
182 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
183};
184
185DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
186 drm_dirty_info_enum_list)
187
Dave Airlief453ba02008-11-07 14:05:41 -0800188struct drm_conn_prop_enum_list {
189 int type;
190 char *name;
191 int count;
192};
193
194/*
195 * Connector and encoder types.
196 */
197static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
198{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
199 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
200 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
201 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
202 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
203 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
204 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
205 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
206 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500207 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
208 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
209 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
210 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200211 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500212 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200213 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800214};
215
216static struct drm_prop_enum_list drm_encoder_enum_list[] =
217{ { DRM_MODE_ENCODER_NONE, "None" },
218 { DRM_MODE_ENCODER_DAC, "DAC" },
219 { DRM_MODE_ENCODER_TMDS, "TMDS" },
220 { DRM_MODE_ENCODER_LVDS, "LVDS" },
221 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200222 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800223};
224
225char *drm_get_encoder_name(struct drm_encoder *encoder)
226{
227 static char buf[32];
228
229 snprintf(buf, 32, "%s-%d",
230 drm_encoder_enum_list[encoder->encoder_type].name,
231 encoder->base.id);
232 return buf;
233}
Dave Airlie13a81952009-09-07 15:45:33 +1000234EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800235
236char *drm_get_connector_name(struct drm_connector *connector)
237{
238 static char buf[32];
239
240 snprintf(buf, 32, "%s-%d",
241 drm_connector_enum_list[connector->connector_type].name,
242 connector->connector_type_id);
243 return buf;
244}
245EXPORT_SYMBOL(drm_get_connector_name);
246
247char *drm_get_connector_status_name(enum drm_connector_status status)
248{
249 if (status == connector_status_connected)
250 return "connected";
251 else if (status == connector_status_disconnected)
252 return "disconnected";
253 else
254 return "unknown";
255}
256
257/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100258 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800259 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100260 * @obj: object pointer, used to generate unique ID
261 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800262 *
Dave Airlief453ba02008-11-07 14:05:41 -0800263 * Create a unique identifier based on @ptr in @dev's identifier space. Used
264 * for tracking modes, CRTCs and connectors.
265 *
266 * RETURNS:
267 * New unique (relative to other objects in @dev) integer identifier for the
268 * object.
269 */
270static int drm_mode_object_get(struct drm_device *dev,
271 struct drm_mode_object *obj, uint32_t obj_type)
272{
Dave Airlief453ba02008-11-07 14:05:41 -0800273 int ret;
274
Jesse Barnesad2563c2009-01-19 17:21:45 +1000275 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800276 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
277 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100278 /*
279 * Set up the object linking under the protection of the idr
280 * lock so that other users can't see inconsistent state.
281 */
Tejun Heo2e928812013-02-27 17:04:08 -0800282 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100283 obj->type = obj_type;
284 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000285 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100286
Tejun Heo2e928812013-02-27 17:04:08 -0800287 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800288}
289
290/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100291 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800292 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100293 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800294 *
Dave Airlief453ba02008-11-07 14:05:41 -0800295 * Free @id from @dev's unique identifier pool.
296 */
297static void drm_mode_object_put(struct drm_device *dev,
298 struct drm_mode_object *object)
299{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000300 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800301 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000302 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800303}
304
Daniel Vetter786b99e2012-12-02 21:53:40 +0100305/**
306 * drm_mode_object_find - look up a drm object with static lifetime
307 * @dev: drm device
308 * @id: id of the mode object
309 * @type: type of the mode object
310 *
311 * Note that framebuffers cannot be looked up with this functions - since those
312 * are reference counted, they need special treatment.
313 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200314struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
315 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800316{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000317 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800318
Daniel Vetter786b99e2012-12-02 21:53:40 +0100319 /* Framebuffers are reference counted and need their own lookup
320 * function.*/
321 WARN_ON(type == DRM_MODE_OBJECT_FB);
322
Jesse Barnesad2563c2009-01-19 17:21:45 +1000323 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800324 obj = idr_find(&dev->mode_config.crtc_idr, id);
325 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000326 obj = NULL;
327 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800328
329 return obj;
330}
331EXPORT_SYMBOL(drm_mode_object_find);
332
333/**
Dave Airlief453ba02008-11-07 14:05:41 -0800334 * drm_framebuffer_init - initialize a framebuffer
335 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100336 * @fb: framebuffer to be initialized
337 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800338 *
Dave Airlief453ba02008-11-07 14:05:41 -0800339 * Allocates an ID for the framebuffer's parent mode object, sets its mode
340 * functions & device file and adds it to the master fd list.
341 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100342 * IMPORTANT:
343 * This functions publishes the fb and makes it available for concurrent access
344 * by other users. Which means by this point the fb _must_ be fully set up -
345 * since all the fb attributes are invariant over its lifetime, no further
346 * locking but only correct reference counting is required.
347 *
Dave Airlief453ba02008-11-07 14:05:41 -0800348 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200349 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800350 */
351int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
352 const struct drm_framebuffer_funcs *funcs)
353{
354 int ret;
355
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100356 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000357 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100358 INIT_LIST_HEAD(&fb->filp_head);
359 fb->dev = dev;
360 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000361
Dave Airlief453ba02008-11-07 14:05:41 -0800362 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200363 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100364 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800365
Daniel Vetter2b677e82012-12-10 21:16:05 +0100366 /* Grab the idr reference. */
367 drm_framebuffer_reference(fb);
368
Dave Airlief453ba02008-11-07 14:05:41 -0800369 dev->mode_config.num_fb++;
370 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100371out:
372 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800373
374 return 0;
375}
376EXPORT_SYMBOL(drm_framebuffer_init);
377
Rob Clarkf7eff602012-09-05 21:48:38 +0000378static void drm_framebuffer_free(struct kref *kref)
379{
380 struct drm_framebuffer *fb =
381 container_of(kref, struct drm_framebuffer, refcount);
382 fb->funcs->destroy(fb);
383}
384
Daniel Vetter2b677e82012-12-10 21:16:05 +0100385static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
386 uint32_t id)
387{
388 struct drm_mode_object *obj = NULL;
389 struct drm_framebuffer *fb;
390
391 mutex_lock(&dev->mode_config.idr_mutex);
392 obj = idr_find(&dev->mode_config.crtc_idr, id);
393 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
394 fb = NULL;
395 else
396 fb = obj_to_fb(obj);
397 mutex_unlock(&dev->mode_config.idr_mutex);
398
399 return fb;
400}
401
Rob Clarkf7eff602012-09-05 21:48:38 +0000402/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100403 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
404 * @dev: drm device
405 * @id: id of the fb object
406 *
407 * If successful, this grabs an additional reference to the framebuffer -
408 * callers need to make sure to eventually unreference the returned framebuffer
409 * again.
410 */
411struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
412 uint32_t id)
413{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100414 struct drm_framebuffer *fb;
415
416 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100417 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100418 if (fb)
419 kref_get(&fb->refcount);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100420 mutex_unlock(&dev->mode_config.fb_lock);
421
422 return fb;
423}
424EXPORT_SYMBOL(drm_framebuffer_lookup);
425
426/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000427 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100428 * @fb: framebuffer to unref
429 *
430 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000431 */
432void drm_framebuffer_unreference(struct drm_framebuffer *fb)
433{
Rob Clarkf7eff602012-09-05 21:48:38 +0000434 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000435 kref_put(&fb->refcount, drm_framebuffer_free);
436}
437EXPORT_SYMBOL(drm_framebuffer_unreference);
438
439/**
440 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100441 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000442 */
443void drm_framebuffer_reference(struct drm_framebuffer *fb)
444{
445 DRM_DEBUG("FB ID: %d\n", fb->base.id);
446 kref_get(&fb->refcount);
447}
448EXPORT_SYMBOL(drm_framebuffer_reference);
449
Daniel Vetter2b677e82012-12-10 21:16:05 +0100450static void drm_framebuffer_free_bug(struct kref *kref)
451{
452 BUG();
453}
454
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100455static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
456{
457 DRM_DEBUG("FB ID: %d\n", fb->base.id);
458 kref_put(&fb->refcount, drm_framebuffer_free_bug);
459}
460
Daniel Vetter2b677e82012-12-10 21:16:05 +0100461/* dev->mode_config.fb_lock must be held! */
462static void __drm_framebuffer_unregister(struct drm_device *dev,
463 struct drm_framebuffer *fb)
464{
465 mutex_lock(&dev->mode_config.idr_mutex);
466 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
467 mutex_unlock(&dev->mode_config.idr_mutex);
468
469 fb->base.id = 0;
470
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100471 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100472}
473
Dave Airlief453ba02008-11-07 14:05:41 -0800474/**
Daniel Vetter36206362012-12-10 20:42:17 +0100475 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
476 * @fb: fb to unregister
477 *
478 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
479 * those used for fbdev. Note that the caller must hold a reference of it's own,
480 * i.e. the object may not be destroyed through this call (since it'll lead to a
481 * locking inversion).
482 */
483void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
484{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100485 struct drm_device *dev = fb->dev;
486
487 mutex_lock(&dev->mode_config.fb_lock);
488 /* Mark fb as reaped and drop idr ref. */
489 __drm_framebuffer_unregister(dev, fb);
490 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100491}
492EXPORT_SYMBOL(drm_framebuffer_unregister_private);
493
494/**
Dave Airlief453ba02008-11-07 14:05:41 -0800495 * drm_framebuffer_cleanup - remove a framebuffer object
496 * @fb: framebuffer to remove
497 *
Daniel Vetter36206362012-12-10 20:42:17 +0100498 * Cleanup references to a user-created framebuffer. This function is intended
499 * to be used from the drivers ->destroy callback.
500 *
501 * Note that this function does not remove the fb from active usuage - if it is
502 * still used anywhere, hilarity can ensue since userspace could call getfb on
503 * the id and get back -EINVAL. Obviously no concern at driver unload time.
504 *
505 * Also, the framebuffer will not be removed from the lookup idr - for
506 * user-created framebuffers this will happen in in the rmfb ioctl. For
507 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
508 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800509 */
510void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
511{
512 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100513
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100514 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000515 list_del(&fb->head);
516 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100517 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000518}
519EXPORT_SYMBOL(drm_framebuffer_cleanup);
520
521/**
522 * drm_framebuffer_remove - remove and unreference a framebuffer object
523 * @fb: framebuffer to remove
524 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000525 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100526 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100527 * passed-in framebuffer. Might take the modeset locks.
528 *
529 * Note that this function optimizes the cleanup away if the caller holds the
530 * last reference to the framebuffer. It is also guaranteed to not take the
531 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000532 */
533void drm_framebuffer_remove(struct drm_framebuffer *fb)
534{
535 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800536 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800537 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000538 struct drm_mode_set set;
539 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800540
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100541 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100542
Daniel Vetterb62584e2012-12-11 16:51:35 +0100543 /*
544 * drm ABI mandates that we remove any deleted framebuffers from active
545 * useage. But since most sane clients only remove framebuffers they no
546 * longer need, try to optimize this away.
547 *
548 * Since we're holding a reference ourselves, observing a refcount of 1
549 * means that we're the last holder and can skip it. Also, the refcount
550 * can never increase from 1 again, so we don't need any barriers or
551 * locks.
552 *
553 * Note that userspace could try to race with use and instate a new
554 * usage _after_ we've cleared all current ones. End result will be an
555 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
556 * in this manner.
557 */
558 if (atomic_read(&fb->refcount.refcount) > 1) {
559 drm_modeset_lock_all(dev);
560 /* remove from any CRTC */
561 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
562 if (crtc->fb == fb) {
563 /* should turn off the crtc */
564 memset(&set, 0, sizeof(struct drm_mode_set));
565 set.crtc = crtc;
566 set.fb = NULL;
567 ret = drm_mode_set_config_internal(&set);
568 if (ret)
569 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
570 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000571 }
Dave Airlief453ba02008-11-07 14:05:41 -0800572
Daniel Vetterb62584e2012-12-11 16:51:35 +0100573 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
574 if (plane->fb == fb) {
575 /* should turn off the crtc */
576 ret = plane->funcs->disable_plane(plane);
577 if (ret)
578 DRM_ERROR("failed to disable plane with busy fb\n");
579 /* disconnect the plane from the fb and crtc: */
580 __drm_framebuffer_unreference(plane->fb);
581 plane->fb = NULL;
582 plane->crtc = NULL;
583 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800584 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100585 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800586 }
587
Rob Clarkf7eff602012-09-05 21:48:38 +0000588 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800589}
Rob Clarkf7eff602012-09-05 21:48:38 +0000590EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800591
592/**
593 * drm_crtc_init - Initialise a new CRTC object
594 * @dev: DRM device
595 * @crtc: CRTC object to init
596 * @funcs: callbacks for the new CRTC
597 *
Dave Airlief453ba02008-11-07 14:05:41 -0800598 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200599 *
600 * RETURNS:
601 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800602 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200603int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800604 const struct drm_crtc_funcs *funcs)
605{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200606 int ret;
607
Dave Airlief453ba02008-11-07 14:05:41 -0800608 crtc->dev = dev;
609 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000610 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800611
Daniel Vetter84849902012-12-02 00:28:11 +0100612 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100613 mutex_init(&crtc->mutex);
614 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200615
616 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
617 if (ret)
618 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800619
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300620 crtc->base.properties = &crtc->properties;
621
Dave Airlief453ba02008-11-07 14:05:41 -0800622 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
623 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200624
625 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100626 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200627
628 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800629}
630EXPORT_SYMBOL(drm_crtc_init);
631
632/**
633 * drm_crtc_cleanup - Cleans up the core crtc usage.
634 * @crtc: CRTC to cleanup
635 *
Dave Airlief453ba02008-11-07 14:05:41 -0800636 * Cleanup @crtc. Removes from drm modesetting space
637 * does NOT free object, caller does that.
638 */
639void drm_crtc_cleanup(struct drm_crtc *crtc)
640{
641 struct drm_device *dev = crtc->dev;
642
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000643 kfree(crtc->gamma_store);
644 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800645
646 drm_mode_object_put(dev, &crtc->base);
647 list_del(&crtc->head);
648 dev->mode_config.num_crtc--;
649}
650EXPORT_SYMBOL(drm_crtc_cleanup);
651
652/**
653 * drm_mode_probed_add - add a mode to a connector's probed mode list
654 * @connector: connector the new mode
655 * @mode: mode data
656 *
Dave Airlief453ba02008-11-07 14:05:41 -0800657 * Add @mode to @connector's mode list for later use.
658 */
659void drm_mode_probed_add(struct drm_connector *connector,
660 struct drm_display_mode *mode)
661{
662 list_add(&mode->head, &connector->probed_modes);
663}
664EXPORT_SYMBOL(drm_mode_probed_add);
665
666/**
667 * drm_mode_remove - remove and free a mode
668 * @connector: connector list to modify
669 * @mode: mode to remove
670 *
Dave Airlief453ba02008-11-07 14:05:41 -0800671 * Remove @mode from @connector's mode list, then free it.
672 */
673void drm_mode_remove(struct drm_connector *connector,
674 struct drm_display_mode *mode)
675{
676 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100677 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800678}
679EXPORT_SYMBOL(drm_mode_remove);
680
681/**
682 * drm_connector_init - Init a preallocated connector
683 * @dev: DRM device
684 * @connector: the connector to init
685 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100686 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800687 *
Dave Airlief453ba02008-11-07 14:05:41 -0800688 * Initialises a preallocated connector. Connectors should be
689 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200690 *
691 * RETURNS:
692 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800693 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200694int drm_connector_init(struct drm_device *dev,
695 struct drm_connector *connector,
696 const struct drm_connector_funcs *funcs,
697 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800698{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200699 int ret;
700
Daniel Vetter84849902012-12-02 00:28:11 +0100701 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800702
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
704 if (ret)
705 goto out;
706
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300707 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800708 connector->dev = dev;
709 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800710 connector->connector_type = connector_type;
711 connector->connector_type_id =
712 ++drm_connector_enum_list[connector_type].count; /* TODO */
713 INIT_LIST_HEAD(&connector->user_modes);
714 INIT_LIST_HEAD(&connector->probed_modes);
715 INIT_LIST_HEAD(&connector->modes);
716 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000717 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800718
719 list_add_tail(&connector->head, &dev->mode_config.connector_list);
720 dev->mode_config.num_connector++;
721
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200722 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500723 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200724 dev->mode_config.edid_property,
725 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800726
Rob Clark58495562012-10-11 20:50:56 -0500727 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800728 dev->mode_config.dpms_property, 0);
729
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200730 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100731 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200732
733 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800734}
735EXPORT_SYMBOL(drm_connector_init);
736
737/**
738 * drm_connector_cleanup - cleans up an initialised connector
739 * @connector: connector to cleanup
740 *
Dave Airlief453ba02008-11-07 14:05:41 -0800741 * Cleans up the connector but doesn't free the object.
742 */
743void drm_connector_cleanup(struct drm_connector *connector)
744{
745 struct drm_device *dev = connector->dev;
746 struct drm_display_mode *mode, *t;
747
748 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
749 drm_mode_remove(connector, mode);
750
751 list_for_each_entry_safe(mode, t, &connector->modes, head)
752 drm_mode_remove(connector, mode);
753
754 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
755 drm_mode_remove(connector, mode);
756
Dave Airlief453ba02008-11-07 14:05:41 -0800757 drm_mode_object_put(dev, &connector->base);
758 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000759 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800760}
761EXPORT_SYMBOL(drm_connector_cleanup);
762
Dave Airliecbc7e222012-02-20 14:16:40 +0000763void drm_connector_unplug_all(struct drm_device *dev)
764{
765 struct drm_connector *connector;
766
767 /* taking the mode config mutex ends up in a clash with sysfs */
768 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
769 drm_sysfs_connector_remove(connector);
770
771}
772EXPORT_SYMBOL(drm_connector_unplug_all);
773
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200774int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000775 struct drm_encoder *encoder,
776 const struct drm_encoder_funcs *funcs,
777 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800778{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200779 int ret;
780
Daniel Vetter84849902012-12-02 00:28:11 +0100781 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800782
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200783 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
784 if (ret)
785 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800786
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200787 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800788 encoder->encoder_type = encoder_type;
789 encoder->funcs = funcs;
790
791 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
792 dev->mode_config.num_encoder++;
793
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100795 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200796
797 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800798}
799EXPORT_SYMBOL(drm_encoder_init);
800
801void drm_encoder_cleanup(struct drm_encoder *encoder)
802{
803 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100804 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800805 drm_mode_object_put(dev, &encoder->base);
806 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000807 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100808 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800809}
810EXPORT_SYMBOL(drm_encoder_cleanup);
811
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800812int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
813 unsigned long possible_crtcs,
814 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600815 const uint32_t *formats, uint32_t format_count,
816 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800817{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200818 int ret;
819
Daniel Vetter84849902012-12-02 00:28:11 +0100820 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800821
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200822 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
823 if (ret)
824 goto out;
825
Rob Clark4d939142012-05-17 02:23:27 -0600826 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800827 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800828 plane->funcs = funcs;
829 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
830 GFP_KERNEL);
831 if (!plane->format_types) {
832 DRM_DEBUG_KMS("out of memory when allocating plane\n");
833 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200834 ret = -ENOMEM;
835 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800836 }
837
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800838 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800839 plane->format_count = format_count;
840 plane->possible_crtcs = possible_crtcs;
841
Rob Clark0a7eb242011-12-13 20:19:36 -0600842 /* private planes are not exposed to userspace, but depending on
843 * display hardware, might be convenient to allow sharing programming
844 * for the scanout engine with the crtc implementation.
845 */
846 if (!priv) {
847 list_add_tail(&plane->head, &dev->mode_config.plane_list);
848 dev->mode_config.num_plane++;
849 } else {
850 INIT_LIST_HEAD(&plane->head);
851 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800852
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200853 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100854 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800855
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200856 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800857}
858EXPORT_SYMBOL(drm_plane_init);
859
860void drm_plane_cleanup(struct drm_plane *plane)
861{
862 struct drm_device *dev = plane->dev;
863
Daniel Vetter84849902012-12-02 00:28:11 +0100864 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800865 kfree(plane->format_types);
866 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600867 /* if not added to a list, it must be a private plane */
868 if (!list_empty(&plane->head)) {
869 list_del(&plane->head);
870 dev->mode_config.num_plane--;
871 }
Daniel Vetter84849902012-12-02 00:28:11 +0100872 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800873}
874EXPORT_SYMBOL(drm_plane_cleanup);
875
Dave Airlief453ba02008-11-07 14:05:41 -0800876/**
877 * drm_mode_create - create a new display mode
878 * @dev: DRM device
879 *
Dave Airlief453ba02008-11-07 14:05:41 -0800880 * Create a new drm_display_mode, give it an ID, and return it.
881 *
882 * RETURNS:
883 * Pointer to new mode on success, NULL on error.
884 */
885struct drm_display_mode *drm_mode_create(struct drm_device *dev)
886{
887 struct drm_display_mode *nmode;
888
889 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
890 if (!nmode)
891 return NULL;
892
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200893 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
894 kfree(nmode);
895 return NULL;
896 }
897
Dave Airlief453ba02008-11-07 14:05:41 -0800898 return nmode;
899}
900EXPORT_SYMBOL(drm_mode_create);
901
902/**
903 * drm_mode_destroy - remove a mode
904 * @dev: DRM device
905 * @mode: mode to remove
906 *
Dave Airlief453ba02008-11-07 14:05:41 -0800907 * Free @mode's unique identifier, then free it.
908 */
909void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
910{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200911 if (!mode)
912 return;
913
Dave Airlief453ba02008-11-07 14:05:41 -0800914 drm_mode_object_put(dev, &mode->base);
915
916 kfree(mode);
917}
918EXPORT_SYMBOL(drm_mode_destroy);
919
920static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
921{
922 struct drm_property *edid;
923 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800924
925 /*
926 * Standard properties (apply to all connectors)
927 */
928 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
929 DRM_MODE_PROP_IMMUTABLE,
930 "EDID", 0);
931 dev->mode_config.edid_property = edid;
932
Sascha Hauer4a67d392012-02-06 10:58:17 +0100933 dpms = drm_property_create_enum(dev, 0,
934 "DPMS", drm_dpms_enum_list,
935 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800936 dev->mode_config.dpms_property = dpms;
937
938 return 0;
939}
940
941/**
942 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
943 * @dev: DRM device
944 *
945 * Called by a driver the first time a DVI-I connector is made.
946 */
947int drm_mode_create_dvi_i_properties(struct drm_device *dev)
948{
949 struct drm_property *dvi_i_selector;
950 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800951
952 if (dev->mode_config.dvi_i_select_subconnector_property)
953 return 0;
954
955 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100956 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800957 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100958 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800959 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800960 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
961
Sascha Hauer4a67d392012-02-06 10:58:17 +0100962 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800963 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100964 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800965 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800966 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
967
968 return 0;
969}
970EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
971
972/**
973 * drm_create_tv_properties - create TV specific connector properties
974 * @dev: DRM device
975 * @num_modes: number of different TV formats (modes) supported
976 * @modes: array of pointers to strings containing name of each format
977 *
978 * Called by a driver's TV initialization routine, this function creates
979 * the TV specific connector properties for a given device. Caller is
980 * responsible for allocating a list of format names and passing them to
981 * this routine.
982 */
983int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
984 char *modes[])
985{
986 struct drm_property *tv_selector;
987 struct drm_property *tv_subconnector;
988 int i;
989
990 if (dev->mode_config.tv_select_subconnector_property)
991 return 0;
992
993 /*
994 * Basic connector properties
995 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100996 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800997 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100998 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800999 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001000 dev->mode_config.tv_select_subconnector_property = tv_selector;
1001
1002 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001003 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1004 "subconnector",
1005 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001006 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001007 dev->mode_config.tv_subconnector_property = tv_subconnector;
1008
1009 /*
1010 * Other, TV specific properties: margins & TV modes.
1011 */
1012 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001013 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001014
1015 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001016 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001017
1018 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001019 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001020
1021 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001022 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001023
1024 dev->mode_config.tv_mode_property =
1025 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1026 "mode", num_modes);
1027 for (i = 0; i < num_modes; i++)
1028 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1029 i, modes[i]);
1030
Francisco Jerezb6b79022009-08-02 04:19:20 +02001031 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001032 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001033
1034 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001035 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001036
1037 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001038 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001039
Francisco Jereza75f0232009-08-12 02:30:10 +02001040 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001041 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001042
1043 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001044 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001045
1046 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001047 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001048
Dave Airlief453ba02008-11-07 14:05:41 -08001049 return 0;
1050}
1051EXPORT_SYMBOL(drm_mode_create_tv_properties);
1052
1053/**
1054 * drm_mode_create_scaling_mode_property - create scaling mode property
1055 * @dev: DRM device
1056 *
1057 * Called by a driver the first time it's needed, must be attached to desired
1058 * connectors.
1059 */
1060int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1061{
1062 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001063
1064 if (dev->mode_config.scaling_mode_property)
1065 return 0;
1066
1067 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001068 drm_property_create_enum(dev, 0, "scaling mode",
1069 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001070 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001071
1072 dev->mode_config.scaling_mode_property = scaling_mode;
1073
1074 return 0;
1075}
1076EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1077
1078/**
1079 * drm_mode_create_dithering_property - create dithering property
1080 * @dev: DRM device
1081 *
1082 * Called by a driver the first time it's needed, must be attached to desired
1083 * connectors.
1084 */
1085int drm_mode_create_dithering_property(struct drm_device *dev)
1086{
1087 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001088
1089 if (dev->mode_config.dithering_mode_property)
1090 return 0;
1091
1092 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001093 drm_property_create_enum(dev, 0, "dithering",
1094 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001095 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001096 dev->mode_config.dithering_mode_property = dithering_mode;
1097
1098 return 0;
1099}
1100EXPORT_SYMBOL(drm_mode_create_dithering_property);
1101
1102/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001103 * drm_mode_create_dirty_property - create dirty property
1104 * @dev: DRM device
1105 *
1106 * Called by a driver the first time it's needed, must be attached to desired
1107 * connectors.
1108 */
1109int drm_mode_create_dirty_info_property(struct drm_device *dev)
1110{
1111 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001112
1113 if (dev->mode_config.dirty_info_property)
1114 return 0;
1115
1116 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001117 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001118 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001119 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001120 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001121 dev->mode_config.dirty_info_property = dirty_info;
1122
1123 return 0;
1124}
1125EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1126
1127/**
Dave Airlief453ba02008-11-07 14:05:41 -08001128 * drm_mode_config_init - initialize DRM mode_configuration structure
1129 * @dev: DRM device
1130 *
Dave Airlief453ba02008-11-07 14:05:41 -08001131 * Initialize @dev's mode_config structure, used for tracking the graphics
1132 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001133 *
1134 * Since this initializes the modeset locks, no locking is possible. Which is no
1135 * problem, since this should happen single threaded at init time. It is the
1136 * driver's problem to ensure this guarantee.
1137 *
Dave Airlief453ba02008-11-07 14:05:41 -08001138 */
1139void drm_mode_config_init(struct drm_device *dev)
1140{
1141 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001142 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001143 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001144 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001145 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1146 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1147 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1148 INIT_LIST_HEAD(&dev->mode_config.property_list);
1149 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001150 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001151 idr_init(&dev->mode_config.crtc_idr);
1152
Daniel Vetter84849902012-12-02 00:28:11 +01001153 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001154 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001155 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001156
1157 /* Just to be sure */
1158 dev->mode_config.num_fb = 0;
1159 dev->mode_config.num_connector = 0;
1160 dev->mode_config.num_crtc = 0;
1161 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001162}
1163EXPORT_SYMBOL(drm_mode_config_init);
1164
1165int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1166{
1167 uint32_t total_objects = 0;
1168
1169 total_objects += dev->mode_config.num_crtc;
1170 total_objects += dev->mode_config.num_connector;
1171 total_objects += dev->mode_config.num_encoder;
1172
Dave Airlief453ba02008-11-07 14:05:41 -08001173 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1174 if (!group->id_list)
1175 return -ENOMEM;
1176
1177 group->num_crtcs = 0;
1178 group->num_connectors = 0;
1179 group->num_encoders = 0;
1180 return 0;
1181}
1182
1183int drm_mode_group_init_legacy_group(struct drm_device *dev,
1184 struct drm_mode_group *group)
1185{
1186 struct drm_crtc *crtc;
1187 struct drm_encoder *encoder;
1188 struct drm_connector *connector;
1189 int ret;
1190
1191 if ((ret = drm_mode_group_init(dev, group)))
1192 return ret;
1193
1194 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1195 group->id_list[group->num_crtcs++] = crtc->base.id;
1196
1197 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1198 group->id_list[group->num_crtcs + group->num_encoders++] =
1199 encoder->base.id;
1200
1201 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1202 group->id_list[group->num_crtcs + group->num_encoders +
1203 group->num_connectors++] = connector->base.id;
1204
1205 return 0;
1206}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001207EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001208
1209/**
1210 * drm_mode_config_cleanup - free up DRM mode_config info
1211 * @dev: DRM device
1212 *
Dave Airlief453ba02008-11-07 14:05:41 -08001213 * Free up all the connectors and CRTCs associated with this DRM device, then
1214 * free up the framebuffers and associated buffer objects.
1215 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001216 * Note that since this /should/ happen single-threaded at driver/device
1217 * teardown time, no locking is required. It's the driver's job to ensure that
1218 * this guarantee actually holds true.
1219 *
Dave Airlief453ba02008-11-07 14:05:41 -08001220 * FIXME: cleanup any dangling user buffer objects too
1221 */
1222void drm_mode_config_cleanup(struct drm_device *dev)
1223{
1224 struct drm_connector *connector, *ot;
1225 struct drm_crtc *crtc, *ct;
1226 struct drm_encoder *encoder, *enct;
1227 struct drm_framebuffer *fb, *fbt;
1228 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001229 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001230
1231 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1232 head) {
1233 encoder->funcs->destroy(encoder);
1234 }
1235
1236 list_for_each_entry_safe(connector, ot,
1237 &dev->mode_config.connector_list, head) {
1238 connector->funcs->destroy(connector);
1239 }
1240
1241 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1242 head) {
1243 drm_property_destroy(dev, property);
1244 }
1245
Daniel Vetter2b677e82012-12-10 21:16:05 +01001246 /*
1247 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001248 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001249 * would actually deadlock with the drm_framebuffer_cleanup function.
1250 *
1251 * Also, if there are any framebuffers left, that's a driver leak now,
1252 * so politely WARN about this.
1253 */
1254 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001255 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001256 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001257 }
1258
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001259 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1260 head) {
1261 plane->funcs->destroy(plane);
1262 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001263
Chris Wilson31840092012-09-17 09:38:03 +00001264 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1265 crtc->funcs->destroy(crtc);
1266 }
1267
Sascha Hauer59ce0622012-02-01 11:38:20 +01001268 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001269}
1270EXPORT_SYMBOL(drm_mode_config_cleanup);
1271
Dave Airlief453ba02008-11-07 14:05:41 -08001272/**
1273 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1274 * @out: drm_mode_modeinfo struct to return to the user
1275 * @in: drm_display_mode to use
1276 *
Dave Airlief453ba02008-11-07 14:05:41 -08001277 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1278 * the user.
1279 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001280static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1281 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001282{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001283 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1284 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1285 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1286 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1287 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1288 "timing values too large for mode info\n");
1289
Dave Airlief453ba02008-11-07 14:05:41 -08001290 out->clock = in->clock;
1291 out->hdisplay = in->hdisplay;
1292 out->hsync_start = in->hsync_start;
1293 out->hsync_end = in->hsync_end;
1294 out->htotal = in->htotal;
1295 out->hskew = in->hskew;
1296 out->vdisplay = in->vdisplay;
1297 out->vsync_start = in->vsync_start;
1298 out->vsync_end = in->vsync_end;
1299 out->vtotal = in->vtotal;
1300 out->vscan = in->vscan;
1301 out->vrefresh = in->vrefresh;
1302 out->flags = in->flags;
1303 out->type = in->type;
1304 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1305 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1306}
1307
1308/**
1309 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1310 * @out: drm_display_mode to return to the user
1311 * @in: drm_mode_modeinfo to use
1312 *
Dave Airlief453ba02008-11-07 14:05:41 -08001313 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1314 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001315 *
1316 * RETURNS:
1317 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001318 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001319static int drm_crtc_convert_umode(struct drm_display_mode *out,
1320 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001321{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001322 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1323 return -ERANGE;
1324
Dave Airlief453ba02008-11-07 14:05:41 -08001325 out->clock = in->clock;
1326 out->hdisplay = in->hdisplay;
1327 out->hsync_start = in->hsync_start;
1328 out->hsync_end = in->hsync_end;
1329 out->htotal = in->htotal;
1330 out->hskew = in->hskew;
1331 out->vdisplay = in->vdisplay;
1332 out->vsync_start = in->vsync_start;
1333 out->vsync_end = in->vsync_end;
1334 out->vtotal = in->vtotal;
1335 out->vscan = in->vscan;
1336 out->vrefresh = in->vrefresh;
1337 out->flags = in->flags;
1338 out->type = in->type;
1339 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1340 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001341
1342 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001343}
1344
1345/**
1346 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001347 * @dev: drm device for the ioctl
1348 * @data: data pointer for the ioctl
1349 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001350 *
Dave Airlief453ba02008-11-07 14:05:41 -08001351 * Construct a set of configuration description structures and return
1352 * them to the user, including CRTC, connector and framebuffer configuration.
1353 *
1354 * Called by the user via ioctl.
1355 *
1356 * RETURNS:
1357 * Zero on success, errno on failure.
1358 */
1359int drm_mode_getresources(struct drm_device *dev, void *data,
1360 struct drm_file *file_priv)
1361{
1362 struct drm_mode_card_res *card_res = data;
1363 struct list_head *lh;
1364 struct drm_framebuffer *fb;
1365 struct drm_connector *connector;
1366 struct drm_crtc *crtc;
1367 struct drm_encoder *encoder;
1368 int ret = 0;
1369 int connector_count = 0;
1370 int crtc_count = 0;
1371 int fb_count = 0;
1372 int encoder_count = 0;
1373 int copied = 0, i;
1374 uint32_t __user *fb_id;
1375 uint32_t __user *crtc_id;
1376 uint32_t __user *connector_id;
1377 uint32_t __user *encoder_id;
1378 struct drm_mode_group *mode_group;
1379
Dave Airliefb3b06c2011-02-08 13:55:21 +10001380 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1381 return -EINVAL;
1382
Dave Airlief453ba02008-11-07 14:05:41 -08001383
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001384 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001385 /*
1386 * For the non-control nodes we need to limit the list of resources
1387 * by IDs in the group list for this node
1388 */
1389 list_for_each(lh, &file_priv->fbs)
1390 fb_count++;
1391
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001392 /* handle this in 4 parts */
1393 /* FBs */
1394 if (card_res->count_fbs >= fb_count) {
1395 copied = 0;
1396 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1397 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1398 if (put_user(fb->base.id, fb_id + copied)) {
1399 mutex_unlock(&file_priv->fbs_lock);
1400 return -EFAULT;
1401 }
1402 copied++;
1403 }
1404 }
1405 card_res->count_fbs = fb_count;
1406 mutex_unlock(&file_priv->fbs_lock);
1407
1408 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001409 mode_group = &file_priv->master->minor->mode_group;
1410 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1411
1412 list_for_each(lh, &dev->mode_config.crtc_list)
1413 crtc_count++;
1414
1415 list_for_each(lh, &dev->mode_config.connector_list)
1416 connector_count++;
1417
1418 list_for_each(lh, &dev->mode_config.encoder_list)
1419 encoder_count++;
1420 } else {
1421
1422 crtc_count = mode_group->num_crtcs;
1423 connector_count = mode_group->num_connectors;
1424 encoder_count = mode_group->num_encoders;
1425 }
1426
1427 card_res->max_height = dev->mode_config.max_height;
1428 card_res->min_height = dev->mode_config.min_height;
1429 card_res->max_width = dev->mode_config.max_width;
1430 card_res->min_width = dev->mode_config.min_width;
1431
Dave Airlief453ba02008-11-07 14:05:41 -08001432 /* CRTCs */
1433 if (card_res->count_crtcs >= crtc_count) {
1434 copied = 0;
1435 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1436 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1437 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1438 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001439 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001440 if (put_user(crtc->base.id, crtc_id + copied)) {
1441 ret = -EFAULT;
1442 goto out;
1443 }
1444 copied++;
1445 }
1446 } else {
1447 for (i = 0; i < mode_group->num_crtcs; i++) {
1448 if (put_user(mode_group->id_list[i],
1449 crtc_id + copied)) {
1450 ret = -EFAULT;
1451 goto out;
1452 }
1453 copied++;
1454 }
1455 }
1456 }
1457 card_res->count_crtcs = crtc_count;
1458
1459 /* Encoders */
1460 if (card_res->count_encoders >= encoder_count) {
1461 copied = 0;
1462 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1463 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1464 list_for_each_entry(encoder,
1465 &dev->mode_config.encoder_list,
1466 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001467 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1468 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001469 if (put_user(encoder->base.id, encoder_id +
1470 copied)) {
1471 ret = -EFAULT;
1472 goto out;
1473 }
1474 copied++;
1475 }
1476 } else {
1477 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1478 if (put_user(mode_group->id_list[i],
1479 encoder_id + copied)) {
1480 ret = -EFAULT;
1481 goto out;
1482 }
1483 copied++;
1484 }
1485
1486 }
1487 }
1488 card_res->count_encoders = encoder_count;
1489
1490 /* Connectors */
1491 if (card_res->count_connectors >= connector_count) {
1492 copied = 0;
1493 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1494 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1495 list_for_each_entry(connector,
1496 &dev->mode_config.connector_list,
1497 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001498 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1499 connector->base.id,
1500 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001501 if (put_user(connector->base.id,
1502 connector_id + copied)) {
1503 ret = -EFAULT;
1504 goto out;
1505 }
1506 copied++;
1507 }
1508 } else {
1509 int start = mode_group->num_crtcs +
1510 mode_group->num_encoders;
1511 for (i = start; i < start + mode_group->num_connectors; i++) {
1512 if (put_user(mode_group->id_list[i],
1513 connector_id + copied)) {
1514 ret = -EFAULT;
1515 goto out;
1516 }
1517 copied++;
1518 }
1519 }
1520 }
1521 card_res->count_connectors = connector_count;
1522
Jerome Glisse94401062010-07-15 15:43:25 -04001523 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001524 card_res->count_connectors, card_res->count_encoders);
1525
1526out:
Daniel Vetter84849902012-12-02 00:28:11 +01001527 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001528 return ret;
1529}
1530
1531/**
1532 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001533 * @dev: drm device for the ioctl
1534 * @data: data pointer for the ioctl
1535 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001536 *
Dave Airlief453ba02008-11-07 14:05:41 -08001537 * Construct a CRTC configuration structure to return to the user.
1538 *
1539 * Called by the user via ioctl.
1540 *
1541 * RETURNS:
1542 * Zero on success, errno on failure.
1543 */
1544int drm_mode_getcrtc(struct drm_device *dev,
1545 void *data, struct drm_file *file_priv)
1546{
1547 struct drm_mode_crtc *crtc_resp = data;
1548 struct drm_crtc *crtc;
1549 struct drm_mode_object *obj;
1550 int ret = 0;
1551
Dave Airliefb3b06c2011-02-08 13:55:21 +10001552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1553 return -EINVAL;
1554
Daniel Vetter84849902012-12-02 00:28:11 +01001555 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001556
1557 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1558 DRM_MODE_OBJECT_CRTC);
1559 if (!obj) {
1560 ret = -EINVAL;
1561 goto out;
1562 }
1563 crtc = obj_to_crtc(obj);
1564
1565 crtc_resp->x = crtc->x;
1566 crtc_resp->y = crtc->y;
1567 crtc_resp->gamma_size = crtc->gamma_size;
1568 if (crtc->fb)
1569 crtc_resp->fb_id = crtc->fb->base.id;
1570 else
1571 crtc_resp->fb_id = 0;
1572
1573 if (crtc->enabled) {
1574
1575 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1576 crtc_resp->mode_valid = 1;
1577
1578 } else {
1579 crtc_resp->mode_valid = 0;
1580 }
1581
1582out:
Daniel Vetter84849902012-12-02 00:28:11 +01001583 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001584 return ret;
1585}
1586
1587/**
1588 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001589 * @dev: drm device for the ioctl
1590 * @data: data pointer for the ioctl
1591 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001592 *
Dave Airlief453ba02008-11-07 14:05:41 -08001593 * Construct a connector configuration structure to return to the user.
1594 *
1595 * Called by the user via ioctl.
1596 *
1597 * RETURNS:
1598 * Zero on success, errno on failure.
1599 */
1600int drm_mode_getconnector(struct drm_device *dev, void *data,
1601 struct drm_file *file_priv)
1602{
1603 struct drm_mode_get_connector *out_resp = data;
1604 struct drm_mode_object *obj;
1605 struct drm_connector *connector;
1606 struct drm_display_mode *mode;
1607 int mode_count = 0;
1608 int props_count = 0;
1609 int encoders_count = 0;
1610 int ret = 0;
1611 int copied = 0;
1612 int i;
1613 struct drm_mode_modeinfo u_mode;
1614 struct drm_mode_modeinfo __user *mode_ptr;
1615 uint32_t __user *prop_ptr;
1616 uint64_t __user *prop_values;
1617 uint32_t __user *encoder_ptr;
1618
Dave Airliefb3b06c2011-02-08 13:55:21 +10001619 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1620 return -EINVAL;
1621
Dave Airlief453ba02008-11-07 14:05:41 -08001622 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1623
Jerome Glisse94401062010-07-15 15:43:25 -04001624 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001625
Daniel Vetter7b240562012-12-12 00:35:33 +01001626 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001627
1628 obj = drm_mode_object_find(dev, out_resp->connector_id,
1629 DRM_MODE_OBJECT_CONNECTOR);
1630 if (!obj) {
1631 ret = -EINVAL;
1632 goto out;
1633 }
1634 connector = obj_to_connector(obj);
1635
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001636 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001637
1638 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1639 if (connector->encoder_ids[i] != 0) {
1640 encoders_count++;
1641 }
1642 }
1643
1644 if (out_resp->count_modes == 0) {
1645 connector->funcs->fill_modes(connector,
1646 dev->mode_config.max_width,
1647 dev->mode_config.max_height);
1648 }
1649
1650 /* delayed so we get modes regardless of pre-fill_modes state */
1651 list_for_each_entry(mode, &connector->modes, head)
1652 mode_count++;
1653
1654 out_resp->connector_id = connector->base.id;
1655 out_resp->connector_type = connector->connector_type;
1656 out_resp->connector_type_id = connector->connector_type_id;
1657 out_resp->mm_width = connector->display_info.width_mm;
1658 out_resp->mm_height = connector->display_info.height_mm;
1659 out_resp->subpixel = connector->display_info.subpixel_order;
1660 out_resp->connection = connector->status;
1661 if (connector->encoder)
1662 out_resp->encoder_id = connector->encoder->base.id;
1663 else
1664 out_resp->encoder_id = 0;
1665
1666 /*
1667 * This ioctl is called twice, once to determine how much space is
1668 * needed, and the 2nd time to fill it.
1669 */
1670 if ((out_resp->count_modes >= mode_count) && mode_count) {
1671 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001672 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001673 list_for_each_entry(mode, &connector->modes, head) {
1674 drm_crtc_convert_to_umode(&u_mode, mode);
1675 if (copy_to_user(mode_ptr + copied,
1676 &u_mode, sizeof(u_mode))) {
1677 ret = -EFAULT;
1678 goto out;
1679 }
1680 copied++;
1681 }
1682 }
1683 out_resp->count_modes = mode_count;
1684
1685 if ((out_resp->count_props >= props_count) && props_count) {
1686 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001687 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1688 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001689 for (i = 0; i < connector->properties.count; i++) {
1690 if (put_user(connector->properties.ids[i],
1691 prop_ptr + copied)) {
1692 ret = -EFAULT;
1693 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001694 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001695
1696 if (put_user(connector->properties.values[i],
1697 prop_values + copied)) {
1698 ret = -EFAULT;
1699 goto out;
1700 }
1701 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001702 }
1703 }
1704 out_resp->count_props = props_count;
1705
1706 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1707 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001708 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001709 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1710 if (connector->encoder_ids[i] != 0) {
1711 if (put_user(connector->encoder_ids[i],
1712 encoder_ptr + copied)) {
1713 ret = -EFAULT;
1714 goto out;
1715 }
1716 copied++;
1717 }
1718 }
1719 }
1720 out_resp->count_encoders = encoders_count;
1721
1722out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001723 mutex_unlock(&dev->mode_config.mutex);
1724
Dave Airlief453ba02008-11-07 14:05:41 -08001725 return ret;
1726}
1727
1728int drm_mode_getencoder(struct drm_device *dev, void *data,
1729 struct drm_file *file_priv)
1730{
1731 struct drm_mode_get_encoder *enc_resp = data;
1732 struct drm_mode_object *obj;
1733 struct drm_encoder *encoder;
1734 int ret = 0;
1735
Dave Airliefb3b06c2011-02-08 13:55:21 +10001736 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1737 return -EINVAL;
1738
Daniel Vetter84849902012-12-02 00:28:11 +01001739 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001740 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1741 DRM_MODE_OBJECT_ENCODER);
1742 if (!obj) {
1743 ret = -EINVAL;
1744 goto out;
1745 }
1746 encoder = obj_to_encoder(obj);
1747
1748 if (encoder->crtc)
1749 enc_resp->crtc_id = encoder->crtc->base.id;
1750 else
1751 enc_resp->crtc_id = 0;
1752 enc_resp->encoder_type = encoder->encoder_type;
1753 enc_resp->encoder_id = encoder->base.id;
1754 enc_resp->possible_crtcs = encoder->possible_crtcs;
1755 enc_resp->possible_clones = encoder->possible_clones;
1756
1757out:
Daniel Vetter84849902012-12-02 00:28:11 +01001758 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001759 return ret;
1760}
1761
1762/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001763 * drm_mode_getplane_res - get plane info
1764 * @dev: DRM device
1765 * @data: ioctl data
1766 * @file_priv: DRM file info
1767 *
1768 * Return an plane count and set of IDs.
1769 */
1770int drm_mode_getplane_res(struct drm_device *dev, void *data,
1771 struct drm_file *file_priv)
1772{
1773 struct drm_mode_get_plane_res *plane_resp = data;
1774 struct drm_mode_config *config;
1775 struct drm_plane *plane;
1776 uint32_t __user *plane_ptr;
1777 int copied = 0, ret = 0;
1778
1779 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1780 return -EINVAL;
1781
Daniel Vetter84849902012-12-02 00:28:11 +01001782 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001783 config = &dev->mode_config;
1784
1785 /*
1786 * This ioctl is called twice, once to determine how much space is
1787 * needed, and the 2nd time to fill it.
1788 */
1789 if (config->num_plane &&
1790 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001791 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001792
1793 list_for_each_entry(plane, &config->plane_list, head) {
1794 if (put_user(plane->base.id, plane_ptr + copied)) {
1795 ret = -EFAULT;
1796 goto out;
1797 }
1798 copied++;
1799 }
1800 }
1801 plane_resp->count_planes = config->num_plane;
1802
1803out:
Daniel Vetter84849902012-12-02 00:28:11 +01001804 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001805 return ret;
1806}
1807
1808/**
1809 * drm_mode_getplane - get plane info
1810 * @dev: DRM device
1811 * @data: ioctl data
1812 * @file_priv: DRM file info
1813 *
1814 * Return plane info, including formats supported, gamma size, any
1815 * current fb, etc.
1816 */
1817int drm_mode_getplane(struct drm_device *dev, void *data,
1818 struct drm_file *file_priv)
1819{
1820 struct drm_mode_get_plane *plane_resp = data;
1821 struct drm_mode_object *obj;
1822 struct drm_plane *plane;
1823 uint32_t __user *format_ptr;
1824 int ret = 0;
1825
1826 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1827 return -EINVAL;
1828
Daniel Vetter84849902012-12-02 00:28:11 +01001829 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1831 DRM_MODE_OBJECT_PLANE);
1832 if (!obj) {
1833 ret = -ENOENT;
1834 goto out;
1835 }
1836 plane = obj_to_plane(obj);
1837
1838 if (plane->crtc)
1839 plane_resp->crtc_id = plane->crtc->base.id;
1840 else
1841 plane_resp->crtc_id = 0;
1842
1843 if (plane->fb)
1844 plane_resp->fb_id = plane->fb->base.id;
1845 else
1846 plane_resp->fb_id = 0;
1847
1848 plane_resp->plane_id = plane->base.id;
1849 plane_resp->possible_crtcs = plane->possible_crtcs;
1850 plane_resp->gamma_size = plane->gamma_size;
1851
1852 /*
1853 * This ioctl is called twice, once to determine how much space is
1854 * needed, and the 2nd time to fill it.
1855 */
1856 if (plane->format_count &&
1857 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001858 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001859 if (copy_to_user(format_ptr,
1860 plane->format_types,
1861 sizeof(uint32_t) * plane->format_count)) {
1862 ret = -EFAULT;
1863 goto out;
1864 }
1865 }
1866 plane_resp->count_format_types = plane->format_count;
1867
1868out:
Daniel Vetter84849902012-12-02 00:28:11 +01001869 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001870 return ret;
1871}
1872
1873/**
1874 * drm_mode_setplane - set up or tear down an plane
1875 * @dev: DRM device
1876 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001877 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001878 *
1879 * Set plane info, including placement, fb, scaling, and other factors.
1880 * Or pass a NULL fb to disable.
1881 */
1882int drm_mode_setplane(struct drm_device *dev, void *data,
1883 struct drm_file *file_priv)
1884{
1885 struct drm_mode_set_plane *plane_req = data;
1886 struct drm_mode_object *obj;
1887 struct drm_plane *plane;
1888 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001889 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001890 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001891 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001892 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001893
1894 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1895 return -EINVAL;
1896
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001897 /*
1898 * First, find the plane, crtc, and fb objects. If not available,
1899 * we don't bother to call the driver.
1900 */
1901 obj = drm_mode_object_find(dev, plane_req->plane_id,
1902 DRM_MODE_OBJECT_PLANE);
1903 if (!obj) {
1904 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1905 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001906 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001907 }
1908 plane = obj_to_plane(obj);
1909
1910 /* No fb means shut it down */
1911 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001912 drm_modeset_lock_all(dev);
1913 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001914 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001915 plane->crtc = NULL;
1916 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001917 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001918 goto out;
1919 }
1920
1921 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1922 DRM_MODE_OBJECT_CRTC);
1923 if (!obj) {
1924 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1925 plane_req->crtc_id);
1926 ret = -ENOENT;
1927 goto out;
1928 }
1929 crtc = obj_to_crtc(obj);
1930
Daniel Vetter786b99e2012-12-02 21:53:40 +01001931 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1932 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001933 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1934 plane_req->fb_id);
1935 ret = -ENOENT;
1936 goto out;
1937 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001938
Ville Syrjälä62443be2011-12-20 00:06:47 +02001939 /* Check whether this plane supports the fb pixel format. */
1940 for (i = 0; i < plane->format_count; i++)
1941 if (fb->pixel_format == plane->format_types[i])
1942 break;
1943 if (i == plane->format_count) {
1944 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1945 ret = -EINVAL;
1946 goto out;
1947 }
1948
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001949 fb_width = fb->width << 16;
1950 fb_height = fb->height << 16;
1951
1952 /* Make sure source coordinates are inside the fb. */
1953 if (plane_req->src_w > fb_width ||
1954 plane_req->src_x > fb_width - plane_req->src_w ||
1955 plane_req->src_h > fb_height ||
1956 plane_req->src_y > fb_height - plane_req->src_h) {
1957 DRM_DEBUG_KMS("Invalid source coordinates "
1958 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1959 plane_req->src_w >> 16,
1960 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1961 plane_req->src_h >> 16,
1962 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1963 plane_req->src_x >> 16,
1964 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1965 plane_req->src_y >> 16,
1966 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1967 ret = -ENOSPC;
1968 goto out;
1969 }
1970
Ville Syrjälä687a0402011-12-20 00:06:45 +02001971 /* Give drivers some help against integer overflows */
1972 if (plane_req->crtc_w > INT_MAX ||
1973 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1974 plane_req->crtc_h > INT_MAX ||
1975 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1976 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1977 plane_req->crtc_w, plane_req->crtc_h,
1978 plane_req->crtc_x, plane_req->crtc_y);
1979 ret = -ERANGE;
1980 goto out;
1981 }
1982
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001983 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001984 ret = plane->funcs->update_plane(plane, crtc, fb,
1985 plane_req->crtc_x, plane_req->crtc_y,
1986 plane_req->crtc_w, plane_req->crtc_h,
1987 plane_req->src_x, plane_req->src_y,
1988 plane_req->src_w, plane_req->src_h);
1989 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001990 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001991 plane->crtc = crtc;
1992 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001993 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001994 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001995 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001996
1997out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001998 if (fb)
1999 drm_framebuffer_unreference(fb);
2000 if (old_fb)
2001 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002002
2003 return ret;
2004}
2005
2006/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002007 * drm_mode_set_config_internal - helper to call ->set_config
2008 * @set: modeset config to set
2009 *
2010 * This is a little helper to wrap internal calls to the ->set_config driver
2011 * interface. The only thing it adds is correct refcounting dance.
2012 */
2013int drm_mode_set_config_internal(struct drm_mode_set *set)
2014{
2015 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002016 struct drm_framebuffer *fb, *old_fb;
2017 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002018
Daniel Vetterb0d12322012-12-11 01:07:12 +01002019 old_fb = crtc->fb;
2020 fb = set->fb;
2021
2022 ret = crtc->funcs->set_config(set);
2023 if (ret == 0) {
2024 if (old_fb)
2025 drm_framebuffer_unreference(old_fb);
2026 if (fb)
2027 drm_framebuffer_reference(fb);
2028 }
2029
2030 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002031}
2032EXPORT_SYMBOL(drm_mode_set_config_internal);
2033
2034/**
Dave Airlief453ba02008-11-07 14:05:41 -08002035 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002036 * @dev: drm device for the ioctl
2037 * @data: data pointer for the ioctl
2038 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002039 *
Dave Airlief453ba02008-11-07 14:05:41 -08002040 * Build a new CRTC configuration based on user request.
2041 *
2042 * Called by the user via ioctl.
2043 *
2044 * RETURNS:
2045 * Zero on success, errno on failure.
2046 */
2047int drm_mode_setcrtc(struct drm_device *dev, void *data,
2048 struct drm_file *file_priv)
2049{
2050 struct drm_mode_config *config = &dev->mode_config;
2051 struct drm_mode_crtc *crtc_req = data;
2052 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002053 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002054 struct drm_connector **connector_set = NULL, *connector;
2055 struct drm_framebuffer *fb = NULL;
2056 struct drm_display_mode *mode = NULL;
2057 struct drm_mode_set set;
2058 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002059 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002060 int i;
2061
Dave Airliefb3b06c2011-02-08 13:55:21 +10002062 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2063 return -EINVAL;
2064
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002065 /* For some reason crtc x/y offsets are signed internally. */
2066 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2067 return -ERANGE;
2068
Daniel Vetter84849902012-12-02 00:28:11 +01002069 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002070 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2071 DRM_MODE_OBJECT_CRTC);
2072 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002073 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002074 ret = -EINVAL;
2075 goto out;
2076 }
2077 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002078 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002079
2080 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002081 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002082 /* If we have a mode we need a framebuffer. */
2083 /* If we pass -1, set the mode with the currently bound fb */
2084 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002085 if (!crtc->fb) {
2086 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2087 ret = -EINVAL;
2088 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002089 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002090 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002091 /* Make refcounting symmetric with the lookup path. */
2092 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002093 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002094 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2095 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002096 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2097 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002098 ret = -EINVAL;
2099 goto out;
2100 }
Dave Airlief453ba02008-11-07 14:05:41 -08002101 }
2102
2103 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002104 if (!mode) {
2105 ret = -ENOMEM;
2106 goto out;
2107 }
2108
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002109 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2110 if (ret) {
2111 DRM_DEBUG_KMS("Invalid mode\n");
2112 goto out;
2113 }
2114
Dave Airlief453ba02008-11-07 14:05:41 -08002115 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002116
Rob Clark7c80e122012-09-04 16:35:56 +00002117 hdisplay = mode->hdisplay;
2118 vdisplay = mode->vdisplay;
2119
2120 if (crtc->invert_dimensions)
2121 swap(hdisplay, vdisplay);
2122
2123 if (hdisplay > fb->width ||
2124 vdisplay > fb->height ||
2125 crtc_req->x > fb->width - hdisplay ||
2126 crtc_req->y > fb->height - vdisplay) {
2127 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2128 fb->width, fb->height,
2129 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2130 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002131 ret = -ENOSPC;
2132 goto out;
2133 }
Dave Airlief453ba02008-11-07 14:05:41 -08002134 }
2135
2136 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002137 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002138 ret = -EINVAL;
2139 goto out;
2140 }
2141
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002142 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002143 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002144 crtc_req->count_connectors);
2145 ret = -EINVAL;
2146 goto out;
2147 }
2148
2149 if (crtc_req->count_connectors > 0) {
2150 u32 out_id;
2151
2152 /* Avoid unbounded kernel memory allocation */
2153 if (crtc_req->count_connectors > config->num_connector) {
2154 ret = -EINVAL;
2155 goto out;
2156 }
2157
2158 connector_set = kmalloc(crtc_req->count_connectors *
2159 sizeof(struct drm_connector *),
2160 GFP_KERNEL);
2161 if (!connector_set) {
2162 ret = -ENOMEM;
2163 goto out;
2164 }
2165
2166 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002167 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002168 if (get_user(out_id, &set_connectors_ptr[i])) {
2169 ret = -EFAULT;
2170 goto out;
2171 }
2172
2173 obj = drm_mode_object_find(dev, out_id,
2174 DRM_MODE_OBJECT_CONNECTOR);
2175 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002176 DRM_DEBUG_KMS("Connector id %d unknown\n",
2177 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002178 ret = -EINVAL;
2179 goto out;
2180 }
2181 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002182 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2183 connector->base.id,
2184 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002185
2186 connector_set[i] = connector;
2187 }
2188 }
2189
2190 set.crtc = crtc;
2191 set.x = crtc_req->x;
2192 set.y = crtc_req->y;
2193 set.mode = mode;
2194 set.connectors = connector_set;
2195 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002196 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002197 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002198
2199out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002200 if (fb)
2201 drm_framebuffer_unreference(fb);
2202
Dave Airlief453ba02008-11-07 14:05:41 -08002203 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002204 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002205 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002206 return ret;
2207}
2208
2209int drm_mode_cursor_ioctl(struct drm_device *dev,
2210 void *data, struct drm_file *file_priv)
2211{
2212 struct drm_mode_cursor *req = data;
2213 struct drm_mode_object *obj;
2214 struct drm_crtc *crtc;
2215 int ret = 0;
2216
Dave Airliefb3b06c2011-02-08 13:55:21 +10002217 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2218 return -EINVAL;
2219
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002220 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002221 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002222
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002223 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002224 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002225 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002226 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002227 }
2228 crtc = obj_to_crtc(obj);
2229
Daniel Vetterdac35662012-12-02 15:24:10 +01002230 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002231 if (req->flags & DRM_MODE_CURSOR_BO) {
2232 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002233 ret = -ENXIO;
2234 goto out;
2235 }
2236 /* Turns off the cursor if handle is 0 */
2237 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2238 req->width, req->height);
2239 }
2240
2241 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2242 if (crtc->funcs->cursor_move) {
2243 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2244 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002245 ret = -EFAULT;
2246 goto out;
2247 }
2248 }
2249out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002250 mutex_unlock(&crtc->mutex);
2251
Dave Airlief453ba02008-11-07 14:05:41 -08002252 return ret;
2253}
2254
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002255/* Original addfb only supported RGB formats, so figure out which one */
2256uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2257{
2258 uint32_t fmt;
2259
2260 switch (bpp) {
2261 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002262 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002263 break;
2264 case 16:
2265 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002266 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002267 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002268 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002269 break;
2270 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002271 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002272 break;
2273 case 32:
2274 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002275 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002276 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002277 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002278 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002279 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002280 break;
2281 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002282 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2283 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002284 break;
2285 }
2286
2287 return fmt;
2288}
2289EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2290
Dave Airlief453ba02008-11-07 14:05:41 -08002291/**
2292 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002293 * @dev: drm device for the ioctl
2294 * @data: data pointer for the ioctl
2295 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002296 *
Dave Airlief453ba02008-11-07 14:05:41 -08002297 * Add a new FB to the specified CRTC, given a user request.
2298 *
2299 * Called by the user via ioctl.
2300 *
2301 * RETURNS:
2302 * Zero on success, errno on failure.
2303 */
2304int drm_mode_addfb(struct drm_device *dev,
2305 void *data, struct drm_file *file_priv)
2306{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002307 struct drm_mode_fb_cmd *or = data;
2308 struct drm_mode_fb_cmd2 r = {};
2309 struct drm_mode_config *config = &dev->mode_config;
2310 struct drm_framebuffer *fb;
2311 int ret = 0;
2312
2313 /* Use new struct with format internally */
2314 r.fb_id = or->fb_id;
2315 r.width = or->width;
2316 r.height = or->height;
2317 r.pitches[0] = or->pitch;
2318 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2319 r.handles[0] = or->handle;
2320
2321 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2322 return -EINVAL;
2323
Jesse Barnesacb4b992011-11-07 12:03:23 -08002324 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002325 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002326
2327 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002328 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002329
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002330 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2331 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002332 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002333 drm_modeset_unlock_all(dev);
2334 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002335 }
2336
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002337 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002338 or->fb_id = fb->base.id;
2339 list_add(&fb->filp_head, &file_priv->fbs);
2340 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002341 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002342
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002343 return ret;
2344}
2345
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002346static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002347{
2348 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2349
2350 switch (format) {
2351 case DRM_FORMAT_C8:
2352 case DRM_FORMAT_RGB332:
2353 case DRM_FORMAT_BGR233:
2354 case DRM_FORMAT_XRGB4444:
2355 case DRM_FORMAT_XBGR4444:
2356 case DRM_FORMAT_RGBX4444:
2357 case DRM_FORMAT_BGRX4444:
2358 case DRM_FORMAT_ARGB4444:
2359 case DRM_FORMAT_ABGR4444:
2360 case DRM_FORMAT_RGBA4444:
2361 case DRM_FORMAT_BGRA4444:
2362 case DRM_FORMAT_XRGB1555:
2363 case DRM_FORMAT_XBGR1555:
2364 case DRM_FORMAT_RGBX5551:
2365 case DRM_FORMAT_BGRX5551:
2366 case DRM_FORMAT_ARGB1555:
2367 case DRM_FORMAT_ABGR1555:
2368 case DRM_FORMAT_RGBA5551:
2369 case DRM_FORMAT_BGRA5551:
2370 case DRM_FORMAT_RGB565:
2371 case DRM_FORMAT_BGR565:
2372 case DRM_FORMAT_RGB888:
2373 case DRM_FORMAT_BGR888:
2374 case DRM_FORMAT_XRGB8888:
2375 case DRM_FORMAT_XBGR8888:
2376 case DRM_FORMAT_RGBX8888:
2377 case DRM_FORMAT_BGRX8888:
2378 case DRM_FORMAT_ARGB8888:
2379 case DRM_FORMAT_ABGR8888:
2380 case DRM_FORMAT_RGBA8888:
2381 case DRM_FORMAT_BGRA8888:
2382 case DRM_FORMAT_XRGB2101010:
2383 case DRM_FORMAT_XBGR2101010:
2384 case DRM_FORMAT_RGBX1010102:
2385 case DRM_FORMAT_BGRX1010102:
2386 case DRM_FORMAT_ARGB2101010:
2387 case DRM_FORMAT_ABGR2101010:
2388 case DRM_FORMAT_RGBA1010102:
2389 case DRM_FORMAT_BGRA1010102:
2390 case DRM_FORMAT_YUYV:
2391 case DRM_FORMAT_YVYU:
2392 case DRM_FORMAT_UYVY:
2393 case DRM_FORMAT_VYUY:
2394 case DRM_FORMAT_AYUV:
2395 case DRM_FORMAT_NV12:
2396 case DRM_FORMAT_NV21:
2397 case DRM_FORMAT_NV16:
2398 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002399 case DRM_FORMAT_NV24:
2400 case DRM_FORMAT_NV42:
Ville Syrjälä935b59772011-12-20 00:06:48 +02002401 case DRM_FORMAT_YUV410:
2402 case DRM_FORMAT_YVU410:
2403 case DRM_FORMAT_YUV411:
2404 case DRM_FORMAT_YVU411:
2405 case DRM_FORMAT_YUV420:
2406 case DRM_FORMAT_YVU420:
2407 case DRM_FORMAT_YUV422:
2408 case DRM_FORMAT_YVU422:
2409 case DRM_FORMAT_YUV444:
2410 case DRM_FORMAT_YVU444:
2411 return 0;
2412 default:
2413 return -EINVAL;
2414 }
2415}
2416
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002417static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002418{
2419 int ret, hsub, vsub, num_planes, i;
2420
2421 ret = format_check(r);
2422 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002423 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002424 return ret;
2425 }
2426
2427 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2428 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2429 num_planes = drm_format_num_planes(r->pixel_format);
2430
2431 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002432 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002433 return -EINVAL;
2434 }
2435
2436 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002437 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002438 return -EINVAL;
2439 }
2440
2441 for (i = 0; i < num_planes; i++) {
2442 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002443 unsigned int height = r->height / (i != 0 ? vsub : 1);
2444 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002445
2446 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002447 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002448 return -EINVAL;
2449 }
2450
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002451 if ((uint64_t) width * cpp > UINT_MAX)
2452 return -ERANGE;
2453
2454 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2455 return -ERANGE;
2456
2457 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002458 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002459 return -EINVAL;
2460 }
2461 }
2462
2463 return 0;
2464}
2465
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002466/**
2467 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002468 * @dev: drm device for the ioctl
2469 * @data: data pointer for the ioctl
2470 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002471 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002472 * Add a new FB to the specified CRTC, given a user request with format.
2473 *
2474 * Called by the user via ioctl.
2475 *
2476 * RETURNS:
2477 * Zero on success, errno on failure.
2478 */
2479int drm_mode_addfb2(struct drm_device *dev,
2480 void *data, struct drm_file *file_priv)
2481{
2482 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002483 struct drm_mode_config *config = &dev->mode_config;
2484 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002485 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002486
Dave Airliefb3b06c2011-02-08 13:55:21 +10002487 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2488 return -EINVAL;
2489
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002490 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2491 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2492 return -EINVAL;
2493 }
2494
Dave Airlief453ba02008-11-07 14:05:41 -08002495 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002496 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002497 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002498 return -EINVAL;
2499 }
2500 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002501 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002503 return -EINVAL;
2504 }
2505
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002506 ret = framebuffer_check(r);
2507 if (ret)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002508 return ret;
Ville Syrjälä935b59772011-12-20 00:06:48 +02002509
Dave Airlief453ba02008-11-07 14:05:41 -08002510 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002511 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002512 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002513 drm_modeset_unlock_all(dev);
2514 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002515 }
2516
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002517 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002518 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002519 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002520 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002521 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002522
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002523
Dave Airlief453ba02008-11-07 14:05:41 -08002524 return ret;
2525}
2526
2527/**
2528 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002529 * @dev: drm device for the ioctl
2530 * @data: data pointer for the ioctl
2531 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002532 *
Dave Airlief453ba02008-11-07 14:05:41 -08002533 * Remove the FB specified by the user.
2534 *
2535 * Called by the user via ioctl.
2536 *
2537 * RETURNS:
2538 * Zero on success, errno on failure.
2539 */
2540int drm_mode_rmfb(struct drm_device *dev,
2541 void *data, struct drm_file *file_priv)
2542{
Dave Airlief453ba02008-11-07 14:05:41 -08002543 struct drm_framebuffer *fb = NULL;
2544 struct drm_framebuffer *fbl = NULL;
2545 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002546 int found = 0;
2547
Dave Airliefb3b06c2011-02-08 13:55:21 +10002548 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2549 return -EINVAL;
2550
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002551 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002552 mutex_lock(&dev->mode_config.fb_lock);
2553 fb = __drm_framebuffer_lookup(dev, *id);
2554 if (!fb)
2555 goto fail_lookup;
2556
Dave Airlief453ba02008-11-07 14:05:41 -08002557 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2558 if (fb == fbl)
2559 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002560 if (!found)
2561 goto fail_lookup;
2562
2563 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2564 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002565
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002566 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002567 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002568 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002569
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002570 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002571
Daniel Vetter2b677e82012-12-10 21:16:05 +01002572 return 0;
2573
2574fail_lookup:
2575 mutex_unlock(&dev->mode_config.fb_lock);
2576 mutex_unlock(&file_priv->fbs_lock);
2577
2578 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002579}
2580
2581/**
2582 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002583 * @dev: drm device for the ioctl
2584 * @data: data pointer for the ioctl
2585 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002586 *
Dave Airlief453ba02008-11-07 14:05:41 -08002587 * Lookup the FB given its ID and return info about it.
2588 *
2589 * Called by the user via ioctl.
2590 *
2591 * RETURNS:
2592 * Zero on success, errno on failure.
2593 */
2594int drm_mode_getfb(struct drm_device *dev,
2595 void *data, struct drm_file *file_priv)
2596{
2597 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002598 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002599 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002600
Dave Airliefb3b06c2011-02-08 13:55:21 +10002601 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2602 return -EINVAL;
2603
Daniel Vetter786b99e2012-12-02 21:53:40 +01002604 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002605 if (!fb)
2606 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002607
2608 r->height = fb->height;
2609 r->width = fb->width;
2610 r->depth = fb->depth;
2611 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002612 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002613 if (fb->funcs->create_handle)
2614 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2615 else
2616 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002617
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002618 drm_framebuffer_unreference(fb);
2619
Dave Airlief453ba02008-11-07 14:05:41 -08002620 return ret;
2621}
2622
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002623int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2624 void *data, struct drm_file *file_priv)
2625{
2626 struct drm_clip_rect __user *clips_ptr;
2627 struct drm_clip_rect *clips = NULL;
2628 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002629 struct drm_framebuffer *fb;
2630 unsigned flags;
2631 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002632 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002633
Dave Airliefb3b06c2011-02-08 13:55:21 +10002634 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2635 return -EINVAL;
2636
Daniel Vetter786b99e2012-12-02 21:53:40 +01002637 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002638 if (!fb)
2639 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002640
2641 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002642 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002643
2644 if (!num_clips != !clips_ptr) {
2645 ret = -EINVAL;
2646 goto out_err1;
2647 }
2648
2649 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2650
2651 /* If userspace annotates copy, clips must come in pairs */
2652 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2653 ret = -EINVAL;
2654 goto out_err1;
2655 }
2656
2657 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002658 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2659 ret = -EINVAL;
2660 goto out_err1;
2661 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002662 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2663 if (!clips) {
2664 ret = -ENOMEM;
2665 goto out_err1;
2666 }
2667
2668 ret = copy_from_user(clips, clips_ptr,
2669 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002670 if (ret) {
2671 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002672 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002673 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002674 }
2675
2676 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002677 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002678 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2679 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002680 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002681 } else {
2682 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002683 }
2684
2685out_err2:
2686 kfree(clips);
2687out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002688 drm_framebuffer_unreference(fb);
2689
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002690 return ret;
2691}
2692
2693
Dave Airlief453ba02008-11-07 14:05:41 -08002694/**
2695 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002696 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002697 *
Dave Airlief453ba02008-11-07 14:05:41 -08002698 * Destroy all the FBs associated with @filp.
2699 *
2700 * Called by the user via ioctl.
2701 *
2702 * RETURNS:
2703 * Zero on success, errno on failure.
2704 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002705void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002706{
Dave Airlief453ba02008-11-07 14:05:41 -08002707 struct drm_device *dev = priv->minor->dev;
2708 struct drm_framebuffer *fb, *tfb;
2709
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002710 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002711 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002712
2713 mutex_lock(&dev->mode_config.fb_lock);
2714 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2715 __drm_framebuffer_unregister(dev, fb);
2716 mutex_unlock(&dev->mode_config.fb_lock);
2717
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002718 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002719
2720 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002721 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002722 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002723 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002724}
2725
2726/**
2727 * drm_mode_attachmode - add a mode to the user mode list
2728 * @dev: DRM device
2729 * @connector: connector to add the mode to
2730 * @mode: mode to add
2731 *
2732 * Add @mode to @connector's user mode list.
2733 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002734static void drm_mode_attachmode(struct drm_device *dev,
2735 struct drm_connector *connector,
2736 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002737{
Dave Airlief453ba02008-11-07 14:05:41 -08002738 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002739}
2740
2741int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002742 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002743{
2744 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002745 int ret = 0;
2746 struct drm_display_mode *dup_mode, *next;
2747 LIST_HEAD(list);
2748
Dave Airlief453ba02008-11-07 14:05:41 -08002749 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2750 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002751 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002752 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002753 dup_mode = drm_mode_duplicate(dev, mode);
2754 if (!dup_mode) {
2755 ret = -ENOMEM;
2756 goto out;
2757 }
2758 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002759 }
2760 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002761
2762 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2763 if (!connector->encoder)
2764 continue;
2765 if (connector->encoder->crtc == crtc)
2766 list_move_tail(list.next, &connector->user_modes);
2767 }
2768
2769 WARN_ON(!list_empty(&list));
2770
2771 out:
2772 list_for_each_entry_safe(dup_mode, next, &list, head)
2773 drm_mode_destroy(dev, dup_mode);
2774
2775 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002776}
2777EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2778
2779static int drm_mode_detachmode(struct drm_device *dev,
2780 struct drm_connector *connector,
2781 struct drm_display_mode *mode)
2782{
2783 int found = 0;
2784 int ret = 0;
2785 struct drm_display_mode *match_mode, *t;
2786
2787 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2788 if (drm_mode_equal(match_mode, mode)) {
2789 list_del(&match_mode->head);
2790 drm_mode_destroy(dev, match_mode);
2791 found = 1;
2792 break;
2793 }
2794 }
2795
2796 if (!found)
2797 ret = -EINVAL;
2798
2799 return ret;
2800}
2801
2802int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2803{
2804 struct drm_connector *connector;
2805
2806 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2807 drm_mode_detachmode(dev, connector, mode);
2808 }
2809 return 0;
2810}
2811EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2812
2813/**
2814 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002815 * @dev: drm device for the ioctl
2816 * @data: data pointer for the ioctl
2817 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002818 *
2819 * This attaches a user specified mode to an connector.
2820 * Called by the user via ioctl.
2821 *
2822 * RETURNS:
2823 * Zero on success, errno on failure.
2824 */
2825int drm_mode_attachmode_ioctl(struct drm_device *dev,
2826 void *data, struct drm_file *file_priv)
2827{
2828 struct drm_mode_mode_cmd *mode_cmd = data;
2829 struct drm_connector *connector;
2830 struct drm_display_mode *mode;
2831 struct drm_mode_object *obj;
2832 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002833 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002834
Dave Airliefb3b06c2011-02-08 13:55:21 +10002835 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2836 return -EINVAL;
2837
Daniel Vetter84849902012-12-02 00:28:11 +01002838 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002839
2840 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2841 if (!obj) {
2842 ret = -EINVAL;
2843 goto out;
2844 }
2845 connector = obj_to_connector(obj);
2846
2847 mode = drm_mode_create(dev);
2848 if (!mode) {
2849 ret = -ENOMEM;
2850 goto out;
2851 }
2852
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002853 ret = drm_crtc_convert_umode(mode, umode);
2854 if (ret) {
2855 DRM_DEBUG_KMS("Invalid mode\n");
2856 drm_mode_destroy(dev, mode);
2857 goto out;
2858 }
Dave Airlief453ba02008-11-07 14:05:41 -08002859
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002860 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002861out:
Daniel Vetter84849902012-12-02 00:28:11 +01002862 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002863 return ret;
2864}
2865
2866
2867/**
2868 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002869 * @dev: drm device for the ioctl
2870 * @data: data pointer for the ioctl
2871 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002872 *
2873 * Called by the user via ioctl.
2874 *
2875 * RETURNS:
2876 * Zero on success, errno on failure.
2877 */
2878int drm_mode_detachmode_ioctl(struct drm_device *dev,
2879 void *data, struct drm_file *file_priv)
2880{
2881 struct drm_mode_object *obj;
2882 struct drm_mode_mode_cmd *mode_cmd = data;
2883 struct drm_connector *connector;
2884 struct drm_display_mode mode;
2885 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002886 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002887
Dave Airliefb3b06c2011-02-08 13:55:21 +10002888 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2889 return -EINVAL;
2890
Daniel Vetter84849902012-12-02 00:28:11 +01002891 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002892
2893 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2894 if (!obj) {
2895 ret = -EINVAL;
2896 goto out;
2897 }
2898 connector = obj_to_connector(obj);
2899
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002900 ret = drm_crtc_convert_umode(&mode, umode);
2901 if (ret) {
2902 DRM_DEBUG_KMS("Invalid mode\n");
2903 goto out;
2904 }
2905
Dave Airlief453ba02008-11-07 14:05:41 -08002906 ret = drm_mode_detachmode(dev, connector, &mode);
2907out:
Daniel Vetter84849902012-12-02 00:28:11 +01002908 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002909 return ret;
2910}
2911
2912struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2913 const char *name, int num_values)
2914{
2915 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002916 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002917
2918 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2919 if (!property)
2920 return NULL;
2921
2922 if (num_values) {
2923 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2924 if (!property->values)
2925 goto fail;
2926 }
2927
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002928 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2929 if (ret)
2930 goto fail;
2931
Dave Airlief453ba02008-11-07 14:05:41 -08002932 property->flags = flags;
2933 property->num_values = num_values;
2934 INIT_LIST_HEAD(&property->enum_blob_list);
2935
Vinson Lee471dd2e2011-11-10 11:55:40 -08002936 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002937 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002938 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2939 }
Dave Airlief453ba02008-11-07 14:05:41 -08002940
2941 list_add_tail(&property->head, &dev->mode_config.property_list);
2942 return property;
2943fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002944 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002945 kfree(property);
2946 return NULL;
2947}
2948EXPORT_SYMBOL(drm_property_create);
2949
Sascha Hauer4a67d392012-02-06 10:58:17 +01002950struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2951 const char *name,
2952 const struct drm_prop_enum_list *props,
2953 int num_values)
2954{
2955 struct drm_property *property;
2956 int i, ret;
2957
2958 flags |= DRM_MODE_PROP_ENUM;
2959
2960 property = drm_property_create(dev, flags, name, num_values);
2961 if (!property)
2962 return NULL;
2963
2964 for (i = 0; i < num_values; i++) {
2965 ret = drm_property_add_enum(property, i,
2966 props[i].type,
2967 props[i].name);
2968 if (ret) {
2969 drm_property_destroy(dev, property);
2970 return NULL;
2971 }
2972 }
2973
2974 return property;
2975}
2976EXPORT_SYMBOL(drm_property_create_enum);
2977
Rob Clark49e27542012-05-17 02:23:26 -06002978struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2979 int flags, const char *name,
2980 const struct drm_prop_enum_list *props,
2981 int num_values)
2982{
2983 struct drm_property *property;
2984 int i, ret;
2985
2986 flags |= DRM_MODE_PROP_BITMASK;
2987
2988 property = drm_property_create(dev, flags, name, num_values);
2989 if (!property)
2990 return NULL;
2991
2992 for (i = 0; i < num_values; i++) {
2993 ret = drm_property_add_enum(property, i,
2994 props[i].type,
2995 props[i].name);
2996 if (ret) {
2997 drm_property_destroy(dev, property);
2998 return NULL;
2999 }
3000 }
3001
3002 return property;
3003}
3004EXPORT_SYMBOL(drm_property_create_bitmask);
3005
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003006struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3007 const char *name,
3008 uint64_t min, uint64_t max)
3009{
3010 struct drm_property *property;
3011
3012 flags |= DRM_MODE_PROP_RANGE;
3013
3014 property = drm_property_create(dev, flags, name, 2);
3015 if (!property)
3016 return NULL;
3017
3018 property->values[0] = min;
3019 property->values[1] = max;
3020
3021 return property;
3022}
3023EXPORT_SYMBOL(drm_property_create_range);
3024
Dave Airlief453ba02008-11-07 14:05:41 -08003025int drm_property_add_enum(struct drm_property *property, int index,
3026 uint64_t value, const char *name)
3027{
3028 struct drm_property_enum *prop_enum;
3029
Rob Clark49e27542012-05-17 02:23:26 -06003030 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3031 return -EINVAL;
3032
3033 /*
3034 * Bitmask enum properties have the additional constraint of values
3035 * from 0 to 63
3036 */
3037 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003038 return -EINVAL;
3039
3040 if (!list_empty(&property->enum_blob_list)) {
3041 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3042 if (prop_enum->value == value) {
3043 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3044 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3045 return 0;
3046 }
3047 }
3048 }
3049
3050 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3051 if (!prop_enum)
3052 return -ENOMEM;
3053
3054 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3055 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3056 prop_enum->value = value;
3057
3058 property->values[index] = value;
3059 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3060 return 0;
3061}
3062EXPORT_SYMBOL(drm_property_add_enum);
3063
3064void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3065{
3066 struct drm_property_enum *prop_enum, *pt;
3067
3068 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3069 list_del(&prop_enum->head);
3070 kfree(prop_enum);
3071 }
3072
3073 if (property->num_values)
3074 kfree(property->values);
3075 drm_mode_object_put(dev, &property->base);
3076 list_del(&property->head);
3077 kfree(property);
3078}
3079EXPORT_SYMBOL(drm_property_destroy);
3080
Paulo Zanonic5431882012-05-15 18:09:02 -03003081void drm_object_attach_property(struct drm_mode_object *obj,
3082 struct drm_property *property,
3083 uint64_t init_val)
3084{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003085 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003086
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003087 if (count == DRM_OBJECT_MAX_PROPERTY) {
3088 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3089 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3090 "you see this message on the same object type.\n",
3091 obj->type);
3092 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003093 }
3094
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003095 obj->properties->ids[count] = property->base.id;
3096 obj->properties->values[count] = init_val;
3097 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003098}
3099EXPORT_SYMBOL(drm_object_attach_property);
3100
3101int drm_object_property_set_value(struct drm_mode_object *obj,
3102 struct drm_property *property, uint64_t val)
3103{
3104 int i;
3105
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003106 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003107 if (obj->properties->ids[i] == property->base.id) {
3108 obj->properties->values[i] = val;
3109 return 0;
3110 }
3111 }
3112
3113 return -EINVAL;
3114}
3115EXPORT_SYMBOL(drm_object_property_set_value);
3116
3117int drm_object_property_get_value(struct drm_mode_object *obj,
3118 struct drm_property *property, uint64_t *val)
3119{
3120 int i;
3121
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003122 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003123 if (obj->properties->ids[i] == property->base.id) {
3124 *val = obj->properties->values[i];
3125 return 0;
3126 }
3127 }
3128
3129 return -EINVAL;
3130}
3131EXPORT_SYMBOL(drm_object_property_get_value);
3132
Dave Airlief453ba02008-11-07 14:05:41 -08003133int drm_mode_getproperty_ioctl(struct drm_device *dev,
3134 void *data, struct drm_file *file_priv)
3135{
3136 struct drm_mode_object *obj;
3137 struct drm_mode_get_property *out_resp = data;
3138 struct drm_property *property;
3139 int enum_count = 0;
3140 int blob_count = 0;
3141 int value_count = 0;
3142 int ret = 0, i;
3143 int copied;
3144 struct drm_property_enum *prop_enum;
3145 struct drm_mode_property_enum __user *enum_ptr;
3146 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003147 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003148 uint64_t __user *values_ptr;
3149 uint32_t __user *blob_length_ptr;
3150
Dave Airliefb3b06c2011-02-08 13:55:21 +10003151 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3152 return -EINVAL;
3153
Daniel Vetter84849902012-12-02 00:28:11 +01003154 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003155 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3156 if (!obj) {
3157 ret = -EINVAL;
3158 goto done;
3159 }
3160 property = obj_to_property(obj);
3161
Rob Clark49e27542012-05-17 02:23:26 -06003162 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003163 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3164 enum_count++;
3165 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3166 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3167 blob_count++;
3168 }
3169
3170 value_count = property->num_values;
3171
3172 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3173 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3174 out_resp->flags = property->flags;
3175
3176 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003177 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003178 for (i = 0; i < value_count; i++) {
3179 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3180 ret = -EFAULT;
3181 goto done;
3182 }
3183 }
3184 }
3185 out_resp->count_values = value_count;
3186
Rob Clark49e27542012-05-17 02:23:26 -06003187 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003188 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3189 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003190 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003191 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3192
3193 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3194 ret = -EFAULT;
3195 goto done;
3196 }
3197
3198 if (copy_to_user(&enum_ptr[copied].name,
3199 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3200 ret = -EFAULT;
3201 goto done;
3202 }
3203 copied++;
3204 }
3205 }
3206 out_resp->count_enum_blobs = enum_count;
3207 }
3208
3209 if (property->flags & DRM_MODE_PROP_BLOB) {
3210 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3211 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003212 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3213 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003214
3215 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3216 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3217 ret = -EFAULT;
3218 goto done;
3219 }
3220
3221 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3222 ret = -EFAULT;
3223 goto done;
3224 }
3225
3226 copied++;
3227 }
3228 }
3229 out_resp->count_enum_blobs = blob_count;
3230 }
3231done:
Daniel Vetter84849902012-12-02 00:28:11 +01003232 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003233 return ret;
3234}
3235
3236static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3237 void *data)
3238{
3239 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003240 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003241
3242 if (!length || !data)
3243 return NULL;
3244
3245 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3246 if (!blob)
3247 return NULL;
3248
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003249 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3250 if (ret) {
3251 kfree(blob);
3252 return NULL;
3253 }
3254
Dave Airlief453ba02008-11-07 14:05:41 -08003255 blob->length = length;
3256
3257 memcpy(blob->data, data, length);
3258
Dave Airlief453ba02008-11-07 14:05:41 -08003259 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3260 return blob;
3261}
3262
3263static void drm_property_destroy_blob(struct drm_device *dev,
3264 struct drm_property_blob *blob)
3265{
3266 drm_mode_object_put(dev, &blob->base);
3267 list_del(&blob->head);
3268 kfree(blob);
3269}
3270
3271int drm_mode_getblob_ioctl(struct drm_device *dev,
3272 void *data, struct drm_file *file_priv)
3273{
3274 struct drm_mode_object *obj;
3275 struct drm_mode_get_blob *out_resp = data;
3276 struct drm_property_blob *blob;
3277 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003278 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003279
Dave Airliefb3b06c2011-02-08 13:55:21 +10003280 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3281 return -EINVAL;
3282
Daniel Vetter84849902012-12-02 00:28:11 +01003283 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003284 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3285 if (!obj) {
3286 ret = -EINVAL;
3287 goto done;
3288 }
3289 blob = obj_to_blob(obj);
3290
3291 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003292 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003293 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3294 ret = -EFAULT;
3295 goto done;
3296 }
3297 }
3298 out_resp->length = blob->length;
3299
3300done:
Daniel Vetter84849902012-12-02 00:28:11 +01003301 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003302 return ret;
3303}
3304
3305int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3306 struct edid *edid)
3307{
3308 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003309 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003310
3311 if (connector->edid_blob_ptr)
3312 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3313
3314 /* Delete edid, when there is none. */
3315 if (!edid) {
3316 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003317 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003318 return ret;
3319 }
3320
Adam Jackson7466f4c2010-03-29 21:43:23 +00003321 size = EDID_LENGTH * (1 + edid->extensions);
3322 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3323 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003324 if (!connector->edid_blob_ptr)
3325 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003326
Rob Clark58495562012-10-11 20:50:56 -05003327 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003328 dev->mode_config.edid_property,
3329 connector->edid_blob_ptr->base.id);
3330
3331 return ret;
3332}
3333EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3334
Paulo Zanoni26a34812012-05-15 18:08:59 -03003335static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003336 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003337{
3338 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3339 return false;
3340 if (property->flags & DRM_MODE_PROP_RANGE) {
3341 if (value < property->values[0] || value > property->values[1])
3342 return false;
3343 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003344 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3345 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003346 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003347 for (i = 0; i < property->num_values; i++)
3348 valid_mask |= (1ULL << property->values[i]);
3349 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003350 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3351 /* Only the driver knows */
3352 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003353 } else {
3354 int i;
3355 for (i = 0; i < property->num_values; i++)
3356 if (property->values[i] == value)
3357 return true;
3358 return false;
3359 }
3360}
3361
Dave Airlief453ba02008-11-07 14:05:41 -08003362int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3363 void *data, struct drm_file *file_priv)
3364{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003365 struct drm_mode_connector_set_property *conn_set_prop = data;
3366 struct drm_mode_obj_set_property obj_set_prop = {
3367 .value = conn_set_prop->value,
3368 .prop_id = conn_set_prop->prop_id,
3369 .obj_id = conn_set_prop->connector_id,
3370 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3371 };
Dave Airlief453ba02008-11-07 14:05:41 -08003372
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003373 /* It does all the locking and checking we need */
3374 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003375}
3376
Paulo Zanonic5431882012-05-15 18:09:02 -03003377static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3378 struct drm_property *property,
3379 uint64_t value)
3380{
3381 int ret = -EINVAL;
3382 struct drm_connector *connector = obj_to_connector(obj);
3383
3384 /* Do DPMS ourselves */
3385 if (property == connector->dev->mode_config.dpms_property) {
3386 if (connector->funcs->dpms)
3387 (*connector->funcs->dpms)(connector, (int)value);
3388 ret = 0;
3389 } else if (connector->funcs->set_property)
3390 ret = connector->funcs->set_property(connector, property, value);
3391
3392 /* store the property value if successful */
3393 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003394 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003395 return ret;
3396}
3397
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003398static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3399 struct drm_property *property,
3400 uint64_t value)
3401{
3402 int ret = -EINVAL;
3403 struct drm_crtc *crtc = obj_to_crtc(obj);
3404
3405 if (crtc->funcs->set_property)
3406 ret = crtc->funcs->set_property(crtc, property, value);
3407 if (!ret)
3408 drm_object_property_set_value(obj, property, value);
3409
3410 return ret;
3411}
3412
Rob Clark4d939142012-05-17 02:23:27 -06003413static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3414 struct drm_property *property,
3415 uint64_t value)
3416{
3417 int ret = -EINVAL;
3418 struct drm_plane *plane = obj_to_plane(obj);
3419
3420 if (plane->funcs->set_property)
3421 ret = plane->funcs->set_property(plane, property, value);
3422 if (!ret)
3423 drm_object_property_set_value(obj, property, value);
3424
3425 return ret;
3426}
3427
Paulo Zanonic5431882012-05-15 18:09:02 -03003428int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3429 struct drm_file *file_priv)
3430{
3431 struct drm_mode_obj_get_properties *arg = data;
3432 struct drm_mode_object *obj;
3433 int ret = 0;
3434 int i;
3435 int copied = 0;
3436 int props_count = 0;
3437 uint32_t __user *props_ptr;
3438 uint64_t __user *prop_values_ptr;
3439
3440 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3441 return -EINVAL;
3442
Daniel Vetter84849902012-12-02 00:28:11 +01003443 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003444
3445 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3446 if (!obj) {
3447 ret = -EINVAL;
3448 goto out;
3449 }
3450 if (!obj->properties) {
3451 ret = -EINVAL;
3452 goto out;
3453 }
3454
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003455 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003456
3457 /* This ioctl is called twice, once to determine how much space is
3458 * needed, and the 2nd time to fill it. */
3459 if ((arg->count_props >= props_count) && props_count) {
3460 copied = 0;
3461 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3462 prop_values_ptr = (uint64_t __user *)(unsigned long)
3463 (arg->prop_values_ptr);
3464 for (i = 0; i < props_count; i++) {
3465 if (put_user(obj->properties->ids[i],
3466 props_ptr + copied)) {
3467 ret = -EFAULT;
3468 goto out;
3469 }
3470 if (put_user(obj->properties->values[i],
3471 prop_values_ptr + copied)) {
3472 ret = -EFAULT;
3473 goto out;
3474 }
3475 copied++;
3476 }
3477 }
3478 arg->count_props = props_count;
3479out:
Daniel Vetter84849902012-12-02 00:28:11 +01003480 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003481 return ret;
3482}
3483
3484int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3485 struct drm_file *file_priv)
3486{
3487 struct drm_mode_obj_set_property *arg = data;
3488 struct drm_mode_object *arg_obj;
3489 struct drm_mode_object *prop_obj;
3490 struct drm_property *property;
3491 int ret = -EINVAL;
3492 int i;
3493
3494 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3495 return -EINVAL;
3496
Daniel Vetter84849902012-12-02 00:28:11 +01003497 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003498
3499 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3500 if (!arg_obj)
3501 goto out;
3502 if (!arg_obj->properties)
3503 goto out;
3504
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003505 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003506 if (arg_obj->properties->ids[i] == arg->prop_id)
3507 break;
3508
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003509 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003510 goto out;
3511
3512 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3513 DRM_MODE_OBJECT_PROPERTY);
3514 if (!prop_obj)
3515 goto out;
3516 property = obj_to_property(prop_obj);
3517
3518 if (!drm_property_change_is_valid(property, arg->value))
3519 goto out;
3520
3521 switch (arg_obj->type) {
3522 case DRM_MODE_OBJECT_CONNECTOR:
3523 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3524 arg->value);
3525 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003526 case DRM_MODE_OBJECT_CRTC:
3527 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3528 break;
Rob Clark4d939142012-05-17 02:23:27 -06003529 case DRM_MODE_OBJECT_PLANE:
3530 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3531 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003532 }
3533
3534out:
Daniel Vetter84849902012-12-02 00:28:11 +01003535 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003536 return ret;
3537}
3538
Dave Airlief453ba02008-11-07 14:05:41 -08003539int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3540 struct drm_encoder *encoder)
3541{
3542 int i;
3543
3544 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3545 if (connector->encoder_ids[i] == 0) {
3546 connector->encoder_ids[i] = encoder->base.id;
3547 return 0;
3548 }
3549 }
3550 return -ENOMEM;
3551}
3552EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3553
3554void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3555 struct drm_encoder *encoder)
3556{
3557 int i;
3558 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3559 if (connector->encoder_ids[i] == encoder->base.id) {
3560 connector->encoder_ids[i] = 0;
3561 if (connector->encoder == encoder)
3562 connector->encoder = NULL;
3563 break;
3564 }
3565 }
3566}
3567EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3568
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003569int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003570 int gamma_size)
3571{
3572 crtc->gamma_size = gamma_size;
3573
3574 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3575 if (!crtc->gamma_store) {
3576 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003577 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003578 }
3579
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003580 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003581}
3582EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3583
3584int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3585 void *data, struct drm_file *file_priv)
3586{
3587 struct drm_mode_crtc_lut *crtc_lut = data;
3588 struct drm_mode_object *obj;
3589 struct drm_crtc *crtc;
3590 void *r_base, *g_base, *b_base;
3591 int size;
3592 int ret = 0;
3593
Dave Airliefb3b06c2011-02-08 13:55:21 +10003594 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3595 return -EINVAL;
3596
Daniel Vetter84849902012-12-02 00:28:11 +01003597 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003598 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3599 if (!obj) {
3600 ret = -EINVAL;
3601 goto out;
3602 }
3603 crtc = obj_to_crtc(obj);
3604
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003605 if (crtc->funcs->gamma_set == NULL) {
3606 ret = -ENOSYS;
3607 goto out;
3608 }
3609
Dave Airlief453ba02008-11-07 14:05:41 -08003610 /* memcpy into gamma store */
3611 if (crtc_lut->gamma_size != crtc->gamma_size) {
3612 ret = -EINVAL;
3613 goto out;
3614 }
3615
3616 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3617 r_base = crtc->gamma_store;
3618 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3619 ret = -EFAULT;
3620 goto out;
3621 }
3622
3623 g_base = r_base + size;
3624 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3625 ret = -EFAULT;
3626 goto out;
3627 }
3628
3629 b_base = g_base + size;
3630 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3631 ret = -EFAULT;
3632 goto out;
3633 }
3634
James Simmons72034252010-08-03 01:33:19 +01003635 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003636
3637out:
Daniel Vetter84849902012-12-02 00:28:11 +01003638 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003639 return ret;
3640
3641}
3642
3643int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3644 void *data, struct drm_file *file_priv)
3645{
3646 struct drm_mode_crtc_lut *crtc_lut = data;
3647 struct drm_mode_object *obj;
3648 struct drm_crtc *crtc;
3649 void *r_base, *g_base, *b_base;
3650 int size;
3651 int ret = 0;
3652
Dave Airliefb3b06c2011-02-08 13:55:21 +10003653 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3654 return -EINVAL;
3655
Daniel Vetter84849902012-12-02 00:28:11 +01003656 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003657 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3658 if (!obj) {
3659 ret = -EINVAL;
3660 goto out;
3661 }
3662 crtc = obj_to_crtc(obj);
3663
3664 /* memcpy into gamma store */
3665 if (crtc_lut->gamma_size != crtc->gamma_size) {
3666 ret = -EINVAL;
3667 goto out;
3668 }
3669
3670 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3671 r_base = crtc->gamma_store;
3672 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3673 ret = -EFAULT;
3674 goto out;
3675 }
3676
3677 g_base = r_base + size;
3678 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3679 ret = -EFAULT;
3680 goto out;
3681 }
3682
3683 b_base = g_base + size;
3684 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3685 ret = -EFAULT;
3686 goto out;
3687 }
3688out:
Daniel Vetter84849902012-12-02 00:28:11 +01003689 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003690 return ret;
3691}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003692
3693int drm_mode_page_flip_ioctl(struct drm_device *dev,
3694 void *data, struct drm_file *file_priv)
3695{
3696 struct drm_mode_crtc_page_flip *page_flip = data;
3697 struct drm_mode_object *obj;
3698 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003699 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003700 struct drm_pending_vblank_event *e = NULL;
3701 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003702 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003703 int ret = -EINVAL;
3704
3705 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3706 page_flip->reserved != 0)
3707 return -EINVAL;
3708
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003709 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3710 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003711 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003712 crtc = obj_to_crtc(obj);
3713
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003714 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003715 if (crtc->fb == NULL) {
3716 /* The framebuffer is currently unbound, presumably
3717 * due to a hotplug event, that userspace has not
3718 * yet discovered.
3719 */
3720 ret = -EBUSY;
3721 goto out;
3722 }
3723
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003724 if (crtc->funcs->page_flip == NULL)
3725 goto out;
3726
Daniel Vetter786b99e2012-12-02 21:53:40 +01003727 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3728 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003729 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003730
Rob Clark7c80e122012-09-04 16:35:56 +00003731 hdisplay = crtc->mode.hdisplay;
3732 vdisplay = crtc->mode.vdisplay;
3733
3734 if (crtc->invert_dimensions)
3735 swap(hdisplay, vdisplay);
3736
3737 if (hdisplay > fb->width ||
3738 vdisplay > fb->height ||
3739 crtc->x > fb->width - hdisplay ||
3740 crtc->y > fb->height - vdisplay) {
3741 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3742 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3743 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003744 ret = -ENOSPC;
3745 goto out;
3746 }
3747
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003748 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3749 ret = -ENOMEM;
3750 spin_lock_irqsave(&dev->event_lock, flags);
3751 if (file_priv->event_space < sizeof e->event) {
3752 spin_unlock_irqrestore(&dev->event_lock, flags);
3753 goto out;
3754 }
3755 file_priv->event_space -= sizeof e->event;
3756 spin_unlock_irqrestore(&dev->event_lock, flags);
3757
3758 e = kzalloc(sizeof *e, GFP_KERNEL);
3759 if (e == NULL) {
3760 spin_lock_irqsave(&dev->event_lock, flags);
3761 file_priv->event_space += sizeof e->event;
3762 spin_unlock_irqrestore(&dev->event_lock, flags);
3763 goto out;
3764 }
3765
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003766 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003767 e->event.base.length = sizeof e->event;
3768 e->event.user_data = page_flip->user_data;
3769 e->base.event = &e->event.base;
3770 e->base.file_priv = file_priv;
3771 e->base.destroy =
3772 (void (*) (struct drm_pending_event *)) kfree;
3773 }
3774
Daniel Vetterb0d12322012-12-11 01:07:12 +01003775 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003776 ret = crtc->funcs->page_flip(crtc, fb, e);
3777 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003778 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3779 spin_lock_irqsave(&dev->event_lock, flags);
3780 file_priv->event_space += sizeof e->event;
3781 spin_unlock_irqrestore(&dev->event_lock, flags);
3782 kfree(e);
3783 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003784 /* Keep the old fb, don't unref it. */
3785 old_fb = NULL;
3786 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003787 /*
3788 * Warn if the driver hasn't properly updated the crtc->fb
3789 * field to reflect that the new framebuffer is now used.
3790 * Failing to do so will screw with the reference counting
3791 * on framebuffers.
3792 */
3793 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003794 /* Unref only the old framebuffer. */
3795 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003796 }
3797
3798out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003799 if (fb)
3800 drm_framebuffer_unreference(fb);
3801 if (old_fb)
3802 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003803 mutex_unlock(&crtc->mutex);
3804
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003805 return ret;
3806}
Chris Wilsoneb033552011-01-24 15:11:08 +00003807
3808void drm_mode_config_reset(struct drm_device *dev)
3809{
3810 struct drm_crtc *crtc;
3811 struct drm_encoder *encoder;
3812 struct drm_connector *connector;
3813
3814 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3815 if (crtc->funcs->reset)
3816 crtc->funcs->reset(crtc);
3817
3818 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3819 if (encoder->funcs->reset)
3820 encoder->funcs->reset(encoder);
3821
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003822 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3823 connector->status = connector_status_unknown;
3824
Chris Wilsoneb033552011-01-24 15:11:08 +00003825 if (connector->funcs->reset)
3826 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003827 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003828}
3829EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003830
3831int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3832 void *data, struct drm_file *file_priv)
3833{
3834 struct drm_mode_create_dumb *args = data;
3835
3836 if (!dev->driver->dumb_create)
3837 return -ENOSYS;
3838 return dev->driver->dumb_create(file_priv, dev, args);
3839}
3840
3841int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3842 void *data, struct drm_file *file_priv)
3843{
3844 struct drm_mode_map_dumb *args = data;
3845
3846 /* call driver ioctl to get mmap offset */
3847 if (!dev->driver->dumb_map_offset)
3848 return -ENOSYS;
3849
3850 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3851}
3852
3853int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3854 void *data, struct drm_file *file_priv)
3855{
3856 struct drm_mode_destroy_dumb *args = data;
3857
3858 if (!dev->driver->dumb_destroy)
3859 return -ENOSYS;
3860
3861 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3862}
Dave Airlie248dbc22011-11-29 20:02:54 +00003863
3864/*
3865 * Just need to support RGB formats here for compat with code that doesn't
3866 * use pixel formats directly yet.
3867 */
3868void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3869 int *bpp)
3870{
3871 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003872 case DRM_FORMAT_C8:
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);